Skip to content

Commit

Permalink
Add document for how to implement MetaEnum
Browse files Browse the repository at this point in the history
  • Loading branch information
wqking committed Dec 11, 2023
1 parent 369e46d commit cb46e65
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
40 changes: 40 additions & 0 deletions doc/interfaces/metaenum.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [Overview](#mdtoc_e7c3d1bb)
- [Header](#mdtoc_6e72a8c1)
- [Get MetaEnum interface](#mdtoc_42ccb38f)
- [Implement MetaEnum](#mdtoc_247b079c)
- [Implemented built-in meta types](#mdtoc_ed7f0e2e)
- [MetaEnum constructor](#mdtoc_348cb714)
- [MetaEnum member functions for registering meta data](#mdtoc_31633ac1)
Expand Down Expand Up @@ -38,6 +39,45 @@ const metapp::MetaType * metaType = metapp::getMetaType<MyEnum>();
const metapp::MetaEnum * metaEnum = metaType->getMetaEnum();
```

<a id="mdtoc_247b079c"></a>
## Implement MetaEnum

```c++
// This is the enum we are going to reflect for.
enum class EnumAnimal {
dog = 1,
cat = 2,
panda = 3
};

// We use metapp::DeclareMetaType to declare a type for EnumAnimal.
template <>
struct metapp::DeclareMetaType <EnumAnimal> : metapp::DeclareMetaTypeBase <EnumAnimal>
{
// The static function getMetaEnum is where we implement MetaEnum.
static const metapp::MetaEnum * getMetaEnum() {
// Define a static metapp::MetaEnum object. Note it must be static.
static const metapp::MetaEnum metaEnum([](metapp::MetaEnum & me) {
// Register the values into the passed in MetaEnum
me.registerValue("dog", EnumAnimal::dog);
me.registerValue("cat", EnumAnimal::cat);
me.registerValue("panda", EnumAnimal::panda);
});
// Return the MetaEnum object.
return &metaEnum;
}
};
```

```c++
// Let's use the MetaEnum
const auto metaType = metapp::getMetaType<EnumAnimal>();
const auto metaEnum = metaType->getMetaEnum();
ASSERT(metaEnum != nullptr);
ASSERT(metaEnum->getByName("dog").asEnumValue().get<EnumAnimal>() == EnumAnimal::dog);
ASSERT(metaEnum->getByValue(EnumAnimal::cat).getName() == "cat");
```
<a id="mdtoc_ed7f0e2e"></a>
## Implemented built-in meta types
Expand Down
44 changes: 44 additions & 0 deletions tests/docsrc/interfaces/doc_metaenum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,50 @@ const metapp::MetaType * metaType = metapp::getMetaType<MyEnum>();
const metapp::MetaEnum * metaEnum = metaType->getMetaEnum();
```
## Implement MetaEnum
desc*/

//code
// This is the enum we are going to reflect for.
enum class EnumAnimal {
dog = 1,
cat = 2,
panda = 3
};

// We use metapp::DeclareMetaType to declare a type for EnumAnimal.
template <>
struct metapp::DeclareMetaType <EnumAnimal> : metapp::DeclareMetaTypeBase <EnumAnimal>
{
// The static function getMetaEnum is where we implement MetaEnum.
static const metapp::MetaEnum * getMetaEnum() {
// Define a static metapp::MetaEnum object. Note it must be static.
static const metapp::MetaEnum metaEnum([](metapp::MetaEnum & me) {
// Register the values into the passed in MetaEnum
me.registerValue("dog", EnumAnimal::dog);
me.registerValue("cat", EnumAnimal::cat);
me.registerValue("panda", EnumAnimal::panda);
});
// Return the MetaEnum object.
return &metaEnum;
}
};

//code

ExampleFunc
{
//code
// Let's use the MetaEnum
const auto metaType = metapp::getMetaType<EnumAnimal>();
const auto metaEnum = metaType->getMetaEnum();
ASSERT(metaEnum != nullptr);
ASSERT(metaEnum->getByName("dog").asEnumValue().get<EnumAnimal>() == EnumAnimal::dog);
ASSERT(metaEnum->getByValue(EnumAnimal::cat).getName() == "cat");
//code
}

/*desc
## Implemented built-in meta types
None
Expand Down

0 comments on commit cb46e65

Please sign in to comment.