Printing user-defined types via reflection¶
A key feature of Hippo is the ease of printing user-defined types.
Classes¶
Hippo provides utilities for printing user-defined types. Consider the following types:
struct Foo {
int a;
float b;
};
struct Bar {
std::vector<Foo> foos;
};
To print these types, we reflect them using HIPPO_CLASS_BEGIN, HIPPO_MEMBER, and HIPPO_CLASS_END:
#include "hippo/hippo.h" // reflection macros
#include "hippo/std/vector.h" // std::vector printer
HIPPO_CLASS_BEGIN(Foo)
HIPPO_MEMBER(a)
HIPPO_MEMBER(b)
HIPPO_CLASS_END()
HIPPO_CLASS_BEGIN(Bar)
HIPPO_MEMBER(foos)
HIPPO_CLASS_END()
The printers for int, float, and std::vector are all provided by Hippo.
Once we’ve provided the printer for Foo, we are able to use it to print Bar.
A printed instance of Bar might look something like this:
Bar {
foos: std::vector [
Foo { a: 1, b: 0.5 },
Foo { a: 2, b: -3.1 }
]
}
Enums¶
Like classes, enums can be reflected wish HIPPO_ENUM_BEGIN, HIPPO_ENUM_VALUE, and HIPPO_ENUM_END:
enum Foo {
Bar,
Baz
};
HIPPO_ENUM_BEGIN(Foo)
HIPPO_ENUM_VALUE(Bar)
HIPPO_ENUM_VALUE(Baz)
HIPPO_ENUM_END()
Base classes¶
Hippo can also reflect base classes with HIPPO_BASE:
struct Foo : Bar {
/* members */
};
HIPPO_CLASS_BEGIN(Foo)
HIPPO_BASE(Bar)
/* members */
HIPPO_CLASS_END()
Custom member access expressions¶
In some cases, it’s useful to use another expression to access a member.
This is accomplished by using the HIPPO_MEMBER_EXPR macro, which allows a custom expression to be provided, operating on the input object:
class Foo {
int bar;
public:
Foo(int bar) : bar(bar) {}
int get_bar() const { return bar; }
};
HIPPO_CLASS_BEGIN(Foo)
HIPPO_MEMBER_EXPR(bar, object.get_bar())
HIPPO_CLASS_END()
Interface¶
Class reflection¶
-
HIPPO_CLASS_BEGIN(Type)¶ Begin the definition of a printer specialization for a class
Type
-
HIPPO_CLASS_END()¶ End the definition of a printer specialization for a class.
-
HIPPO_BASE(Type)¶ Register
Typeas a base class in a class printer specialization.
-
HIPPO_MEMBER(Name)¶ Register
Nameas a member in a class printer specialization.
-
HIPPO_MEMBER_EXPR(Name, Expression)¶ Register
Nameas a member, printed asExpression, in a class printer specialization.