$include_dir="/home/hyper-archives/boost/include"; include("$include_dir/msg-header.inc") ?>
Subject: [boost] [tuple] Type composition
From: Amir Ansari (amir.ansari80_at_[hidden])
Date: 2012-07-20 00:52:36
I like to reuse tuple<> to quickly create new types as:
class my_data : public boost::tuple<int, int, bool, int> {
// The following could go into a macro:
enum { Id, Price, Sellable, Shares, Gain }
// Ensure copy constructor
my_data(const my_data & d) : boost::tuple<int, int, bool, int>(d) {}
};
The enum and copy constructor could be defined using a macro whose signature is:
HANDLE_TUPLE4(Class, Type1, Name1, Type2, Name2, Type3, Name3, Type4, Name4)
I find this to be a lot less clutter than:
class my_data
{
int Id;
int Price;
bool Sellable;
int Shares;
int Gain;
// Boiler plate code...
};
Users of my_data can now access members as obj.get<my_data::Id>() The only thing in the above is that I can't declare the data to be protected so that users of my_data won't be able to do get<my_data::Price> = 0; for example. Ideally there should be a way so that some of the data members could be private to my_data and not exposed to even derived classes but I don't see a way for that. But protected could be handled through a slight modification in tuple:
enum AccessSpecifier { Protected, Public }
template< enum AccessSpecifier, typename... Args> class typle...
template<typename.. Args> class tuple<Protected, Args...> // Make sure Args are declared protected
With some specialization magic, we can let users define which arguments should be public (that is, the get<> method) and which should be protected. Does this make sense? I think we could add more magic so that users can only get and NOT set the values. Thoughts?