|
| 1 | +# C++ Reflection |
| 2 | + |
| 3 | +## Preface |
| 4 | +I set out this summer (*2015*) to implement a flexible reflection system for the game project I'm working on. This repository contains a skeleton for parts of the system that I prototyped throughout the summer. With the proper dependencies and build system setup, you should have enough to integrate into your engine / application without much fuss. |
| 5 | + |
| 6 | +## Quick Intro |
| 7 | +As a statically typed language, C++ wasn't designed to facilitate runtime type information. Instead, it's crazy fast and optimization friendly. As performance critical applications, it is for this reason that C++ is basically the standard backend for games. |
| 8 | + |
| 9 | +Type introspection is crucial for complex / large code bases that need to interface with tools (*i.e. a game editor*). Unless you're a team of all programmers (*I'm sorry if that's the case*) it is effectively impossible to iterate upon a larger game without some sort of tools to abstract away code (*especially in 3D*). Without type introspection, you can expect to copy and paste a lot of boilerplate code. This is **absurdly** tedious and undesirable. |
| 10 | + |
| 11 | +The good news is that there are *tons and tons* of great resources out there for "extending" C++ to include meta information within your code base. The most common approaches you'll find are as follows: |
| 12 | + |
| 13 | ++ Using macros and templates to simplify the craziness that is writing the aforementioned boilerplate code. |
| 14 | ++ Parsing your code to **generate** the crazy boilerplate code. |
| 15 | + |
| 16 | +The latter technique isn't adopted nearly as much as the former, but feels like it's becoming much more common. With that said, I chose to use the generation technique. I'm pretty glad I did. |
| 17 | + |
| 18 | +The purpose of this repository is to be a simple jumpstart reference for those interested in implementing the generation method in their own code base. |
| 19 | + |
| 20 | +Here are a few links that cover more specifics on the concept (*specifically in the realm of C/C++*) |
| 21 | + |
| 22 | ++ [GDC: Physics for Game Programmers - Debugging](http://www.gdcvault.com/play/1020065/Physics-for-Game-Programmers-Debugging) (*it's relevant, I swear!*) |
| 23 | ++ [GDC: Robustification Through Introspection and Analysis Tools (Avoiding Developer Taxes)](http://www.gdcvault.com/play/1015586/) |
| 24 | ++ [Game Engine Metadata Creation with Clang](http://www.myopictopics.com/?p=368) |
| 25 | ++ [Parsing C++ in Python with Clang](http://eli.thegreenplace.net/2011/07/03/parsing-c-in-python-with-clang/) |
| 26 | + |
| 27 | +## Goals |
| 28 | + |
| 29 | +##### Make the pipeline as hands off as possible. |
| 30 | +Specifically, you shouldn't have to jump through a bunch of hoops just to expose your code to the reflection runtime library. Make changes to your code, recompile, and the changes are reflected immediately (*yep, it was intended*). |
| 31 | + |
| 32 | +No extra button clicks or steps to synchronize the reflection data. |
| 33 | + |
| 34 | +##### Provide rich functionality in the runtime library. |
| 35 | +If we're going through all this trouble in the first place, might as well make it worth while! |
| 36 | + |
| 37 | +##### Avoid huge build times. |
| 38 | +We're effectively compiling our code twice. First to parse our code base and understand it as intricately as a compiler does, then to *actually* compile it as per usual (*with the addition of our generated source*). |
| 39 | + |
| 40 | +This is one of the downsides to the generation approach. Instead of manually writing these macros inline with our source, we're using the brains of a compiler. However, we would much rather sacrifice a little bit shorter build times for the luxury of cleaner, less cluttered code. |
| 41 | + |
| 42 | +Unfortunately, this also implies creating a much less intuitive build pipeline. Don't worry though! I have some nifty diagrams for you. |
| 43 | + |
| 44 | +## Pipeline |
| 45 | + |
| 46 | +In our engine (_we call it **Ursine Engine**, because we're dangerously clever and played on the fact that our team name is Bear King_), we use [CMake](http://www.cmake.org/) for managing most aspects of the the overall build pipeline. CMake is a horribly wonderful tool that I've come to love despite hating it at the same time. It allows us to generate solutions for most IDEs that anyone on the team likes to use, although currently, everyone is using Visual Studio 2015 (_finally **some** C++14, baby!_). |
| 47 | + |
| 48 | +CMake makes this pipeline surprisingly simple which was a relief. I won't go into much specific detail, but I'll provide relevant snippets of the integration into our engine a little later when I describe the code in this repository. |
| 49 | + |
| 50 | +Here's a diagram of the entire pipeline from writing the source, to building your game / application. |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +## Code |
| 55 | + |
| 56 | +The respository has two parts - [Parser](https://github.com/AustinBrunkhorst/CPP-Reflection/tree/master/Source/Parser) and [Runtime](https://github.com/AustinBrunkhorst/CPP-Reflection/tree/master/Source/Runtime). |
| 57 | + |
| 58 | ++ **Parser** is for the command line source parsing tool. (_requires [Boost 1.59.0](http://www.boost.org/users/history/version_1_59_0.html) and [libclang 3.7.0](http://llvm.org/releases/download.html)_) |
| 59 | ++ **Runtime** is for the reflection runtime library. |
| 60 | + |
| 61 | +### CMake Prebuild Example |
| 62 | + |
| 63 | +This is basic example of adding the prebuild step to an existing target in CMake. |
| 64 | + |
| 65 | +**Note:** this assumes you have created empty files for `${META_GENERATED_HEADER}` and `${META_GENERATED_SOURCE}` and added it to the list of sources when calling `add_executable( )`. Otherwise, CMake will complain about the files not existing initially. |
| 66 | + |
| 67 | +```CMake |
| 68 | +# assume a target has been created, with add_executable( ... ) |
| 69 | +set(PROJECT_NAME "Example") |
| 70 | +
|
| 71 | +# path to the reflection parser executable |
| 72 | +set(PARSE_TOOL_EXE "ReflectionParser.exe") |
| 73 | +
|
| 74 | +# input source file to pass to the reflection parser compiler |
| 75 | +# in this file, include any files that you want exposed to the parser |
| 76 | +set(PROJECT_META_HEADER "Reflection.h") |
| 77 | +
|
| 78 | +# generated file names |
| 79 | +set(META_GENERATED_HEADER "Meta.Generated.h") |
| 80 | +set(META_GENERATED_SOURCE "Meta.Generated.cpp") |
| 81 | +
|
| 82 | +# fetch all include directories for the project target |
| 83 | +get_property(DIRECTORIES TARGET ${PROJECT_NAME} PROPERTY INCLUDE_DIRECTORIES) |
| 84 | +
|
| 85 | +# flags that will eventually be based to the reflection parser compiler |
| 86 | +set(META_FLAGS "") |
| 87 | +
|
| 88 | +# build the include directory flags |
| 89 | +foreach (DIRECTORY ${DIRECTORIES}) |
| 90 | + set(META_FLAGS ${meta_flags} "\\-I${DIRECTORY}") |
| 91 | +endforeach () |
| 92 | +
|
| 93 | +# include the system directories |
| 94 | +if (MSVC) |
| 95 | + # assume ${VS_VERSION} is the version of Visual Studio being used to compile |
| 96 | + set(META_FLAGS ${META_FLAGS} |
| 97 | + "\\-IC:/Program Files (x86)/Microsoft Visual Studio ${VS_VERSION}/VC/include" |
| 98 | + ) |
| 99 | +else () |
| 100 | + # you can figure it out for other compilers :) |
| 101 | + message(FATAL_ERROR "System include directories not implemented for this compiler.") |
| 102 | +endif () |
| 103 | +
|
| 104 | +# add the prebuild command that invokes the reflection parser executable |
| 105 | +add_custom_command( |
| 106 | + TARGET ${PROJECT_NAME} |
| 107 | + PRE_BUILD |
| 108 | + COMMAND call "${PARSE_TOOL_EXE}" |
| 109 | + --target-name "${PROJECT_NAME}" |
| 110 | + --in-source "${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_SOURCE_DIR}/${PROJECT_META_HEADER}" |
| 111 | + --out-header "${META_GENERATED_HEADER}" |
| 112 | + --out-source "${META_GENERATED_SOURCE}" |
| 113 | + --flags ${META_FLAGS} |
| 114 | +) |
| 115 | +``` |
| 116 | + |
| 117 | +### String Templates |
| 118 | +Generating code is usually a pretty ugly process. |
| 119 | + |
| 120 | +Instead of writing the characters manually (i.e. `output += "REGISTER_FUNCTION(" + name + ")"`), I wanted to use *"String Templates"*. That is why I chose [Mustache](https://mustache.github.io/). I found a simple [header only implemenation](https://github.com/kainjow/Mustache), which is included in the [Parser](https://github.com/AustinBrunkhorst/CPP-Reflection/blob/master/Source/Parser/Mustache.h) section. |
| 121 | + |
| 122 | +In the **Generate Source Files** section of the pipeline diagram, you'll notice two steps. *"Compile"* and *"Render"*. Compile simply takes all of the types that we've extracted and [compiles the data to be referenced in Mustache](https://github.com/AustinBrunkhorst/CPP-Reflection/blob/master/Source/Parser/ReflectionParser.cpp#L217-L220). Render actually renders the templates and writes them to the configured output files. |
| 123 | + |
| 124 | +In the [Templates](https://github.com/AustinBrunkhorst/CPP-Reflection/tree/master/Templates) folder of the repository, you'll find the mustache template files referenced in the reflection parser. |
| 125 | + |
| 126 | +### Type Meta Data |
| 127 | +One of the biggest features that I wanted to implement in the runtime library is being able to add meta data to types at compile time. |
| 128 | + |
| 129 | +If you've ever used C#, you know they have a pretty groovy reflection system built into the language. I really like their syntax for [Attributes](http://www.tutorialspoint.com/csharp/csharp_attributes.htm), which is a way to add meta data to language types / constructs. |
| 130 | + |
| 131 | +The closest I could get to this style, was with the use of Macros. C++11 introduced [Attribute Specifiers](http://en.cppreference.com/w/cpp/language/attributes) as a way to hint compilers on intended behavior or add language extensions. Unfortunately, compiler support varies widely, and as mentioned it's only managed at compile time. |
| 132 | + |
| 133 | +Luckily for us, Clang supports the attribute `annotate( )`. You can extract the contents of an annotation with [libclang](https://github.com/AustinBrunkhorst/CPP-Reflection/blob/master/Source/Parser/MetaDataManager.cpp#L10-L17). |
| 134 | + |
| 135 | +The syntax for this attribute look something like this. |
| 136 | + |
| 137 | + __attribute__(( annotate( "Hey look at this annotation!" ) )) |
| 138 | + |
| 139 | +You might be thinking, *"But it's only for Clang.. how will this work in MSVC?"* |
| 140 | + |
| 141 | +More good news! libclang preprocesses source files, so we can use preprocessor directives. In the source parsing tool, I define `__REFLECTION_PARSER__` [before compiling](https://github.com/AustinBrunkhorst/CPP-Reflection/blob/master/Source/Parser/Main.cpp#L126). We can use this to make a nice solution for all compilers. |
| 142 | + |
| 143 | +```C++ |
| 144 | +#if defined(__REFLECTION_PARSER__) |
| 145 | +# define Meta(...) __attribute__((annotate(#__VA_ARGS__))) |
| 146 | +#else |
| 147 | +# define Meta(...) |
| 148 | +#endif |
| 149 | +``` |
| 150 | + |
| 151 | +We would use it like so. |
| 152 | + |
| 153 | +```C++ |
| 154 | +Meta(Mashed) |
| 155 | +int potatoes; |
| 156 | +``` |
| 157 | + |
| 158 | +Now that I could annotate code, I needed to define how I would interact with it. Initially I assumed key value pairs separated by commas, like so. |
| 159 | +
|
| 160 | +```C++ |
| 161 | +Meta(Key = Value, Key2, Key = "Yep!") |
| 162 | +``` |
| 163 | + |
| 164 | +But after reviewing this approach with my teammate [Jordan](http://www.jordanellis.me), he came up with the brilliant idea of doing exactly what C# does, and that is using user defined types as annotations, queryable at runtime. So I came up with this. |
| 165 | + |
| 166 | +```C++ |
| 167 | +struct Mashed : public MetaProperty { }; |
| 168 | + |
| 169 | +Meta(Mashed) |
| 170 | +int potatoes; |
| 171 | +``` |
| 172 | +
|
| 173 | +Heres how it works - I treat all values delimited by commas as constructors. If a value doesn't have parenthases, it's assumed to be a default constructor. |
| 174 | +
|
| 175 | +For each constructor, I then extract the arguments provided. When I generate the source, I simply paste the extracted arguments as a constructor call of the provided type. The value is converted to a `Variant` and accessible at runtime. This allows us to do some really awesome things. |
| 176 | +
|
| 177 | +```C++ |
| 178 | +enum class SliderType |
| 179 | +{ |
| 180 | + Horizontal, |
| 181 | + Vertical |
| 182 | +}; |
| 183 | +
|
| 184 | +struct Slider : public MetaProperty |
| 185 | +{ |
| 186 | + SliderType type; |
| 187 | + |
| 188 | + Slider(SliderType type) |
| 189 | + : type( type ) { } |
| 190 | +}; |
| 191 | +
|
| 192 | +struct Range : public MetaProperty |
| 193 | +{ |
| 194 | + float min, max; |
| 195 | + |
| 196 | + Range(float min, float max) |
| 197 | + : min( min ) |
| 198 | + , max( max ) { } |
| 199 | +}; |
| 200 | +
|
| 201 | +Meta(Range(0.0f, 1.0f), Slider(SliderType::Horizontal)) |
| 202 | +float someIntensityField; |
| 203 | +``` |
| 204 | + |
| 205 | +One of the coolest things about this aside from type safety, is that Visual Studio correctly syntax highlights the contents of the macro and also provides intellisense! It's a beautiful thing. Here's a more complete example of interfacing with it at runtime using the runtime library. |
| 206 | + |
| 207 | +```C++ |
| 208 | +struct SoundEffect |
| 209 | +{ |
| 210 | + Meta(Range(0.0f, 100.0f)) |
| 211 | + float volume; |
| 212 | +}; |
| 213 | + |
| 214 | +int main(void) |
| 215 | +{ |
| 216 | + // you can also use type meta::Type::Get( "SoundEffect" ) based on a string name |
| 217 | + Field volumeField = typeof( SoundEffect ).GetField( "volume" ); |
| 218 | + |
| 219 | + // meta data for the volume field |
| 220 | + MetaManager &volumeMeta = soundEffectType.GetMeta( ); |
| 221 | + |
| 222 | + // getting the "Range" property, then casting the variant as a range |
| 223 | + Variant volumeRange = volumeMeta.GetProperty( typeof( Range ) ).GetValue<Range>( ); |
| 224 | + |
| 225 | + // 0.0f |
| 226 | + float min = volumeRange.min; |
| 227 | + |
| 228 | + // 100.0f |
| 229 | + float max = volumeRange.max; |
| 230 | + |
| 231 | + return 0; |
| 232 | +} |
| 233 | +``` |
| 234 | +
|
| 235 | +### Function Binding |
| 236 | +
|
| 237 | +Another notoriously difficult or convoluted process in managing reflection info in C++ is dynamically invoking functions / methods. |
| 238 | +
|
| 239 | +The most common approach is to store raw function / method pointers and calculate the offsets of arguments. The result is a ton of templates and difficult to follow operations. Not to mention, I can't imagine it's fun to debug! |
| 240 | +
|
| 241 | +In libclang, you're able to easily extract signatures from functions. With this in mind, I came up with the most simple approach that I could think of. **Wrapping function calls in generated lambdas**. |
| 242 | +
|
| 243 | +Here's a simple demonstration of the concept. |
| 244 | +
|
| 245 | +```C++ |
| 246 | +int foo(int a, float b, double c) |
| 247 | +{ |
| 248 | + return 0; |
| 249 | +} |
| 250 | +
|
| 251 | +auto fooWrapper = [](int a, float b, double c) |
| 252 | +{ |
| 253 | + return foo( a, b, c ); |
| 254 | +}; |
| 255 | +
|
| 256 | +// same behavior as foo( 0, 1.0f, 2.0 ); |
| 257 | +fooWrapper( 0, 1.0f, 2.0 ); |
| 258 | +``` |
| 259 | + |
| 260 | +In the context of our runtime library, here's an example of something that might be generated for a class method. |
| 261 | + |
| 262 | +```C++ |
| 263 | +class DemoClass |
| 264 | +{ |
| 265 | +public: |
| 266 | + int someMethod(int a) |
| 267 | + { |
| 268 | + return a; |
| 269 | + } |
| 270 | +}; |
| 271 | + |
| 272 | +auto someMethodWrapper = [](Variant &obj, ArgumentList &args) |
| 273 | +{ |
| 274 | + auto &instance = obj.GetValue<DemoClass>( ); |
| 275 | + |
| 276 | + return Variant { |
| 277 | + instance.someMethod( |
| 278 | + args[ 0 ].GetValue<int>( ) |
| 279 | + ) |
| 280 | + }; |
| 281 | +}; |
| 282 | +``` |
| 283 | + |
| 284 | +That's it! It's much less compilicated than the previously mentioned approach. This concept is also applied to fields / globals - getters and setters are simply generated lambdas. |
| 285 | + |
| 286 | +There are some downsides though: |
| 287 | ++ Larger code size. For each generated lambda, the compiler has to generate a bunch of symbols behind the scenes. |
| 288 | ++ Larger compile times. |
| 289 | ++ Decent amount of indirection just for one function call. |
| 290 | + |
| 291 | +You shouldn't have to worry **too** much however. If like most people, you use reflection for editor functionality, not a physics simulation. Performance in most cases is not critical. |
| 292 | + |
| 293 | +Here's a more complete example of dynamically calling functions with the runtime library. |
| 294 | + |
| 295 | +```C++ |
| 296 | +struct SoundEffect |
| 297 | +{ |
| 298 | + float volume; |
| 299 | + |
| 300 | + void Load(const char *filename); |
| 301 | +}; |
| 302 | + |
| 303 | +int main(void) |
| 304 | +{ |
| 305 | + Type soundEffectType = typeof( SoundEffect ); |
| 306 | + auto volumeField = soundEffectType.GetField( "volume" ); |
| 307 | + |
| 308 | + // the runtime supports overloading, but by default returns the first overload |
| 309 | + Method loadMethod = soundEffectType.GetMethod( "Load" ); |
| 310 | + |
| 311 | + // creates an instance of a sound effect |
| 312 | + Variant effect = soundEffectType.Create( ); |
| 313 | + |
| 314 | + // effect.volume is now 85 |
| 315 | + volumeField.SetValue( effect, 85.0f ); |
| 316 | + |
| 317 | + // 85 |
| 318 | + volumeField.GetValue( effect ); |
| 319 | + |
| 320 | + // effect.Load is called |
| 321 | + loadMethod.Invoke( effect, { "Explosion.wav" } ); |
| 322 | + |
| 323 | + return 0; |
| 324 | +} |
| 325 | +``` |
0 commit comments