I have been strugling the past weeks designing a system to add ".net style"
properties to my library. After some experiments like wrapping the class
members inside member<type> m_member stuff I realized that I was wrong about
what exactly a property is. So I rewrote everything again with the following
idea in mind:
A property is nothing more that a pack of some accessor functions and maybe
some data.
My objective was to support properties while keeping the class size the same
and without any speed impact or weird requeriment in the get/set functions.
The code is working right now but I think it can be more compact for the
user, but unfortunately my c++ skills are insuficient to polish some things.
Here is an example:
This block shows how the system actually works:
{
rectangle_i r(1,2,3,4);
size_i sz = get_property(r, L"size");
get_property(sz, r, L"size");
set_property(r, L"size", size_i(10,20));
bool IsEmpty; get_property(IsEmpty, r, L"IsEmpty");
rectangle_i r2(10,1,1,1); get_property(r2, r, L"empty");
}
And this section of a common rectangle class (ripped from .net api) shows
how to register this "properties":
template <class T_> class rectangle
{
// ...
const point& get_location() const {return m_location;}
void set_location(const point& value)
{m_location = value;}
static void initialize_property_pool(property_pool& out)
{
out.add, point>(L"location",
&rectangle::get_location, &rectangle::set_location);
out.add, size>(L"size",
&rectangle::get_size, &rectangle::set_size);
out.add, T_>(L"height",
&rectangle::get_height, &rectangle::set_height);
out.add, T_>(L"width", &rectangle::get_width,
&rectangle::set_width);
out.add, T_>(L"bottom",
&rectangle::get_bottom, &rectangle::set_bottom);
out.add, T_>(L"left", &rectangle::get_left,
&rectangle::set_left);
out.add, T_>(L"right", &rectangle::get_right,
&rectangle::set_right);
out.add, T_>(L"top", &rectangle::get_top,
&rectangle::set_top);
out.add_read_only, bool>(L"IsEmpty",
&rectangle::get_is_empty);
out.add_static_read_only, rectangle>(L"empty",
&rectangle::get_empty);
}
// ...
};
I want to know if there is a way of deducing the template parameters to
reduce this verborreic initialize_property_pool function and to simplify
things to the user and to replace "add_read_only" and "add_static_read_only"
with just another pair of plain "add" functions.
Any observation will be apreciated,
thanks,
Isaac Lascasas.