Skip to content

Commit 6f2bb8c

Browse files
Update examples
1 parent 714adc3 commit 6f2bb8c

38 files changed

+705
-99
lines changed

Examples/Arrays.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "TestReflectionModule.h"
2+
3+
#include "TestTypes.h"
4+
#include "TestProperties.h"
5+
6+
using namespace ursine;
7+
using namespace meta;
8+
9+
int main(void)
10+
{
11+
MetaInitialize( UsingModule( TestModule ) );
12+
13+
Array<int> intArray { 1, 2, 3, 4, 5 };
14+
15+
Variant intArrayVariant = intArray;
16+
17+
ArrayWrapper arrayWrapper = intArrayVariant.GetArray( );
18+
19+
// updating a value
20+
arrayWrapper.SetValue( 0, 1000 );
21+
22+
// inserting a value
23+
arrayWrapper.Insert( 0, -100 );
24+
25+
// removing a value
26+
arrayWrapper.Remove( 5 );
27+
28+
size_t size = arrayWrapper.Size( );
29+
30+
std::cout << "values: ";
31+
32+
for (size_t i = 0; i < size; ++i)
33+
{
34+
// could also use .GetValue<int>( )
35+
std::cout << arrayWrapper.GetValue( i ).ToInt( ) << " ";
36+
}
37+
38+
return 0;
39+
}

Examples/CMakeLists.txt

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
cmake_minimum_required(VERSION 2.8.3)
2+
3+
project(MetaTests CXX)
4+
5+
add_subdirectory(../Source/Runtime MetaRuntime)
6+
add_subdirectory(../Source/Parser MetaParser)
7+
8+
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/../Source/CMake")
9+
10+
include(MetaParser)
11+
12+
include_directories(${META_RUNTIME_INCLUDE_DIRS})
13+
14+
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
15+
set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "CMake")
16+
17+
set(TEST_META_SOURCE_ROOT "${CMAKE_CURRENT_LIST_DIR}")
18+
set(TEST_META_GENERATED_DIR "${CMAKE_CURRENT_BINARY_DIR}/Generated")
19+
20+
set(TEST_META_GENERATED_HEADERS "")
21+
set(TEST_META_GENERATED_SOURCES "")
22+
23+
set(TEST_REFLECTION_HEADER TestReflection.h)
24+
set(TEST_MODULE_HEADER TestReflectionModule.h)
25+
set(TEST_MODULE_TARGET TestModule)
26+
27+
set(TEST_HEADERS TestProperties.h TestTypes.h)
28+
29+
meta_parser_prebuild(
30+
TARGET ${TEST_MODULE_TARGET}
31+
GENERATED_DIR ${TEST_META_GENERATED_DIR}
32+
SOURCE_ROOT ${TEST_META_SOURCE_ROOT}
33+
HEADER_FILES ${TEST_HEADERS}
34+
MODULE_HEADER ${TEST_MODULE_HEADER}
35+
OUT_MODULE_SOURCE META_MODULE_SOURCE
36+
OUT_GENERATED_FILES META_GENERATED_FILES
37+
OUT_INC TEST_META_GENERATED_HEADERS
38+
OUT_SRC TEST_META_GENERATED_SOURCES
39+
)
40+
41+
add_library(${TEST_MODULE_TARGET} ${META_GENERATED_FILES})
42+
43+
macro (add_meta_test TARGET SOURCES)
44+
add_executable(${TARGET}
45+
${TEST_HEADERS}
46+
${TEST_META_GENERATED_HEADERS}
47+
${TEST_META_GENERATED_SOURCES}
48+
${SOURCES}
49+
)
50+
51+
target_link_libraries(${TARGET} MetaRuntime ${TEST_MODULE_TARGET})
52+
set_property(TARGET ${TARGET} PROPERTY FOLDER Tests)
53+
endmacro ()
54+
55+
add_meta_test(MetaProperties MetaProperties.cpp)
56+
add_meta_test(Enums Enums.cpp)
57+
add_meta_test(FunctionsAndMethods FunctionsAndMethods.cpp)
58+
add_meta_test(Arrays Arrays.cpp)
59+
add_meta_test(Serialization Serialization.cpp)
60+
61+
meta_parser_build(
62+
TARGET ${TEST_MODULE_TARGET}
63+
SOURCE_ROOT ${TEST_META_SOURCE_ROOT}
64+
GENERATED_DIR ${TEST_META_GENERATED_DIR}
65+
GENERATED_FILES ${META_GENERATED_FILES}
66+
SOURCE_FILE ${TEST_REFLECTION_HEADER}
67+
MODULE_HEADER ${TEST_MODULE_HEADER}
68+
MODULE_SOURCE_FILE ${META_MODULE_SOURCE}
69+
HEADER_FILES ${TEST_HEADERS}
70+
PARSER_EXECUTABLE "$<TARGET_FILE:MetaParser>"
71+
)

Examples/Enums.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "TestReflectionModule.h"
2+
3+
#include "TestTypes.h"
4+
#include "TestProperties.h"
5+
6+
using namespace ursine;
7+
using namespace meta;
8+
9+
int main(void)
10+
{
11+
MetaInitialize( UsingModule( TestModule ) );
12+
13+
return 0;
14+
}

Examples/FunctionsAndMethods.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "TestReflectionModule.h"
2+
3+
#include "TestTypes.h"
4+
#include "TestProperties.h"
5+
6+
using namespace ursine::meta;
7+
8+
int main(void)
9+
{
10+
MetaInitialize( UsingModule( TestModule ) );
11+
12+
Type soundEffectType = typeof( SoundEffect );
13+
Field volumeField = soundEffectType.GetField( "volume" );
14+
15+
// the runtime supports overloading, but by default returns the first overload
16+
Method loadMethod = soundEffectType.GetMethod( "Load" );
17+
18+
// creates an instance of a sound effect
19+
Variant effect = soundEffectType.Create( );
20+
21+
// effect.volume is now 85
22+
volumeField.SetValue( effect, 85.0f );
23+
24+
// 85 -- can also use GetValue<float>( )
25+
float volumeValue = volumeField.GetValue( effect ).ToFloat( );
26+
27+
std::cout << "SoundEffect.volume: " << volumeValue << std::endl;
28+
29+
// effect.Load is called
30+
loadMethod.Invoke( effect, std::string { "Explosion.wav" } );
31+
32+
return 0;
33+
}

Examples/MetaProperties.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "TestReflectionModule.h"
2+
3+
#include "TestTypes.h"
4+
#include "TestProperties.h"
5+
6+
using namespace ursine::meta;
7+
8+
int main(void)
9+
{
10+
MetaInitialize( UsingModule( TestModule ) );
11+
12+
// you can also use type meta::Type::Get( "SoundEffect" ) based on a string name
13+
Type soundEffectType = typeof( SoundEffect );
14+
15+
// the volume field in the SoundEffect struct
16+
Field volumeField = soundEffectType.GetField( "volume" );
17+
18+
// meta data for the volume field
19+
const MetaManager &volumeMeta = volumeField.GetMeta( );
20+
21+
// getting the "Range" property, then casting the variant as a Range
22+
Range &volumeRange = volumeMeta.GetProperty( typeof( Range ) ).GetValue<Range>( );
23+
24+
// 0.0f
25+
std::cout << "SoundEffect::volume [Range.min]: " << volumeRange.min << std::endl;
26+
27+
// 100.0f
28+
std::cout << "SoundEffect::volume [Range.max]: " << volumeRange.max << std::endl;
29+
30+
// getting the "Slider" property, then casting the variant as a Slider
31+
Slider &volumeSlider = volumeMeta.GetProperty( typeof( Slider ) ).GetValue<Slider>( );
32+
33+
// type representing the SlideType enum
34+
Type sliderTypeEnumType = typeof( SliderType );
35+
36+
// enum representing the SliderType
37+
const Enum &sliderTypeEnum = sliderTypeEnumType.GetEnum( );
38+
39+
// get the associative value of the enum
40+
std::cout << "SoundEffect::volume [Slider.type]: " << sliderTypeEnum.GetKey( volumeSlider.type ) << std::endl;
41+
42+
return 0;
43+
}

Examples/Serialization.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include "TestReflectionModule.h"
2+
3+
#include "TestTypes.h"
4+
#include "TestProperties.h"
5+
6+
using namespace ursine;
7+
using namespace meta;
8+
9+
int main(void)
10+
{
11+
MetaInitialize( UsingModule( TestModule ) );
12+
13+
///////////////////////////////////////////////////////////////////////////
14+
// Serialization
15+
///////////////////////////////////////////////////////////////////////////
16+
17+
ComplexType complexType;
18+
19+
complexType.stringValue = "Testing";
20+
complexType.intValue = 23;
21+
complexType.floatValue = 77.0f;
22+
complexType.doubleValue = 86.35f;
23+
24+
complexType.soundEffect.volume = 50.0f;
25+
26+
complexType.arrayValue = { 1, 2, 3, 4, 5 };
27+
28+
complexType.enumValue = Eighty;
29+
30+
std::cout << "Serialized: "
31+
<< Variant( complexType ).SerializeJson( ).dump( )
32+
<< std::endl;
33+
34+
///////////////////////////////////////////////////////////////////////////
35+
// Deserialization
36+
///////////////////////////////////////////////////////////////////////////
37+
38+
std::string complexJson = R"(
39+
{
40+
"arrayValue": [ 5, 6, 7, 8 ],
41+
"doubleValue": 100.0,
42+
"enumValue": "Two",
43+
"floatValue": 98.5,
44+
"intValue": -25,
45+
"soundEffect":{
46+
"volume": 100.0
47+
},
48+
"stringValue": "Deserialization!"
49+
}
50+
)";
51+
52+
std::string jsonError;
53+
54+
// This is a static helper method. It is essentially doing this -
55+
// typeof( ComplexType ).DeserializeJson( ).GetValue<ComplexType>( )
56+
ComplexType deserializedComplexType = Type::DeserializeJson<ComplexType>(
57+
Json::parse( complexJson, jsonError )
58+
);
59+
60+
std::cout << "Deserialized: " << std::endl
61+
<< Variant( deserializedComplexType ).SerializeJson( ).dump( )
62+
<< std::endl;
63+
64+
return 0;
65+
}

Examples/TestProperties.h

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
3+
#include <Meta.h>
4+
5+
enum class SliderType
6+
{
7+
Horizontal,
8+
Vertical
9+
} Meta(Enable);
10+
11+
struct Slider : ursine::meta::MetaProperty
12+
{
13+
META_OBJECT;
14+
15+
SliderType type;
16+
17+
Slider(SliderType type)
18+
: type(type) { }
19+
} Meta(Enable);
20+
21+
struct Range : ursine::meta::MetaProperty
22+
{
23+
META_OBJECT;
24+
25+
float min, max;
26+
27+
Range(float min, float max)
28+
: min(min)
29+
, max(max) { }
30+
} Meta(Enable);

Examples/TestReflection.h

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#pragma once
2+
3+
#include "TestProperties.h"
4+
#include "TestTypes.h"

Examples/TestReflectionModule.h

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#pragma once
2+
3+
#include <ReflectionModule.h>
4+
5+
DECLARE_REFLECTION_MODULE( TestModule );

Examples/TestTypes.h

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#pragma once
2+
3+
#include <Meta.h>
4+
5+
#include "TestProperties.h"
6+
7+
#include <string>
8+
#include <vector>
9+
10+
#include <Array.h>
11+
12+
enum TestEnum
13+
{
14+
One,
15+
Two,
16+
Three,
17+
Four,
18+
Five,
19+
Eighty = 80
20+
} Meta(Enable);
21+
22+
struct SoundEffect
23+
{
24+
Meta(Range(0.0f, 100.0f), Slider(SliderType::Horizontal))
25+
float volume;
26+
27+
void Load(const std::string &filename)
28+
{
29+
std::cout << "Loaded sound effect \"" << filename << "\"." << std::endl;
30+
}
31+
} Meta(Enable);
32+
33+
struct ComplexType
34+
{
35+
std::string stringValue;
36+
int intValue;
37+
float floatValue;
38+
double doubleValue;
39+
40+
SoundEffect soundEffect;
41+
42+
ursine::Array<int> arrayValue;
43+
44+
TestEnum enumValue;
45+
46+
ComplexType(void) = default;
47+
} Meta(Enable);

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ int main(void)
233233
Field volumeField = soundEffectType.GetField( "volume" );
234234

235235
// meta data for the volume field
236-
MetaManager &volumeMeta = soundEffectType.GetMeta( );
236+
MetaManager &volumeMeta = volumeField.GetMeta( );
237237

238238
// getting the "Range" property, then casting the variant as a range
239239
Range volumeRange = volumeMeta.GetProperty( typeof( Range ) ).GetValue<Range>( );
@@ -313,7 +313,7 @@ struct SoundEffect
313313
{
314314
float volume;
315315

316-
void Load(const char *filename);
316+
void Load(const std::string &filename);
317317
};
318318

319319
int main(void)
@@ -334,7 +334,7 @@ int main(void)
334334
volumeField.GetValue( effect );
335335

336336
// effect.Load is called
337-
loadMethod.Invoke( effect, { "Explosion.wav" } );
337+
loadMethod.Invoke( effect, std::string { "Explosion.wav" } );
338338

339339
return 0;
340340
}

0 commit comments

Comments
 (0)