Sometimes it is necessary to adjust the default registration behaviour of RTTR. Therefore policies were introduced. There are policies for constructors, methods and properties available.
Constructors:
- as_object
- as_std_shared_ptr
- as_raw_ptr
Properties:
- bind_as_ptr
- as_reference_wrapper
Methods:
- discard_return
- return_ref_as_ptr
For easier usage, the policies are grouped to its corresponding items; e.g. all policies for methods can be found under policy::meth
; all policies for properties under policy::prop
etc.
Constructors
as_object
The as_object policy will create an instance of a class with automatic storage.
Objects with automatic storage duration are automatically destroyed when the block in which they are created exits. Which is in our case the variant. However, that means also you don't have to deal with pointers or wrappers. In order to use this creation policy, the object must be copy constructible.
See following example code:
struct Foo
{
};
{
registration::class_<Foo>("Foo")
.constructor<>()
(
);
}
int main()
{
variant var = type::get<Foo>().create();
std::cout << var.is_type<Foo>();
variant var2 = var;
return 0;
}
Definition: access_levels.h:34
#define RTTR_REGISTRATION
Use this macro to automatically register your reflection information to RTTR before main is called.
Definition: registration.h:745
static const detail::as_object as_object
The as_object policy will create an instance of a class with automatic storage.
Definition: policy.h:306
as_std_shared_ptr
The as_std_shared_ptr policy will create an instance of a class through std::make_shared<T>
.
That means the object is wrapped into a std::shared_ptr<T>
. The wrapped object is destroyed and its memory deallocated when either of the following happens:
- the last remaining variant object (which contains the
shared_ptr
owning the object is destroyed.
- the last remaining variant owning the
shared_ptr
is assigned another object.
The object is destroyed using the default deleter of std::shared_ptr
.
See following example code:
struct Foo
{
};
{
registration::class_<Foo>("Foo")
.constructor<>()
(
);
}
int main()
{
variant var = type::get<Foo>().create();
std::cout << var.is_type<std::shared_ptr<Foo>>();
return 0;
}
static const detail::as_std_shared_ptr as_std_shared_ptr
The as_std_shared_ptr policy will create an instance of a class through std::make_shared<T>.
Definition: policy.h:271
as_raw_ptr
The as_raw_ptr policy will create an instance of a class as raw pointer.
That means the object is created with a new
-expression and its lifetime lasts until it is destroyed using a delete
-expression. In order to invoke the delete expression use the corresponding destructor.
See following example code:
struct Foo
{
};
{
registration::class_<Foo>("Foo")
.constructor<>()
(
);
}
int main()
{
variant var = type::get<Foo>().create();
std::cout << var.is_type<Foo*>();
var.get_type().destroy(var);
std::cout << var.is_valid();
return 0;
}
static const detail::as_raw_pointer as_raw_ptr
The as_raw_ptr policy will create an instance of a class as raw pointer.
Definition: policy.h:235
Properties
bind_as_ptr
The motivation for bind_as_ptr policy is to avoid expensive copies when returning a property. The default registration behaviour of RTTR is to return all values by copying the content in a variant. When you have primitive data types like integer or doubles you are good to go with the standard binding behaviour. But when you have big arrays, it would be a waste to always copy the content when retrieving or setting the value, therefore this policy was introduced.
Example:
struct Foo
{
std::vector<int> vec;
};
{
registration::class_<Foo>("Foo")
(
)
}
int main()
{
Foo obj;
property prop = type::get<Foo>().get_property("vec");
std::cout << var.
is_type<std::vector<int>*>();
prop.set_value(obj, var);
}
The property class provides several meta information about a property and gives read/write access to ...
Definition: property.h:117
variant get_value(instance object) const
Returns the current property value of the given instance object.
bool is_type() const
Returns true if the containing variant data is of the given template type T.
static const detail::bind_as_ptr bind_as_ptr
The bind_as_ptr policy will bind a member object as pointer type.
Definition: policy.h:158
as_reference_wrapper
The motivation for the as_reference_wrapper policy is the same like the prop::bind_as_ptr policy, avoid copy of data. The value will be wrapped inside a std::reference_wrapper<T>
Example:
struct Foo
{
std::vector<int> vec;
};
{
registration::class_<Foo>("Foo")
(
)
}
int main()
{
Foo obj;
property prop = type::get<Foo>().get_property("vec");
variant var = prop.get_value(obj);
std::cout << var.is_type< std::reference_wrapper<std::vector<int>> >();
prop.set_value(obj, var);
}
static const detail::as_reference_wrapper as_reference_wrapper
The as_reference_wrapper policy will bind a member object as std::reference_wrapper type.
Definition: policy.h:193
Methods
discard_return
Sometimes it is necessary that the client caller should ignore the return value of a method call. Therefore discard_return policy was introduced.
Example:
struct Foo
{
int calculate_func() { return 42; }
};
{
registration::class_<Foo>("Foo")
.
method(
"get_value", &Foo::calculate_func)
(
);
}
int main()
{
Foo obj;
method meth = type::get<Foo>().get_method("calculate_func");
variant var = meth.
invoke(obj);
}
The method class provides several meta information about a method and can be invoked.
Definition: method.h:121
variant invoke(instance object) const
Invokes the method represented by the current instance object.
static const detail::discard_return discard_return
This policy should be used when the return value of a method should not be forwarded to the caller.
Definition: policy.h:116
return_ref_as_ptr
The motivation for the return_ref_as_ptr policy is the same like the prop::bind_as_ptr policy. When you really need to get a reference to the return value of a method call you have to use this policy, otherwise the returned reference will be copied into the variant.
Example:
struct Foo
{
std::string& get_text() { static std::string text; return text; }
};
{
registration::class_<Foo>("Foo")
.
method(
"get_text", &Foo::get_text)
(
);
}
int main()
{
Foo obj;
method meth = type::get<Foo>().get_method("get_text");
variant var = meth.
invoke(obj);
std::cout << var.
is_type<std::string*>();
}
static const detail::return_as_ptr return_ref_as_ptr
This policy can be used when a method return a reference to an object and the caller should be able t...
Definition: policy.h:86
A closing hint: you can of course build your own policies in that way, that you build wrappers around your properties or methods and then bind the wrapper instead of the original accessor.