Object with attributes of any type with easy storage and iteration
I have an "object" with different attributes stored as key / value. The key is a string and the value can be any basic type. My first idea was to use a template class:
template <class T>
class Attribute {
public:
Attribute<T>(const std::string& key, T value) :
m_key(key),
m_value(value)
{
}
~Attribute(){}
T getValue() const
{
return m_value;
}
std::string getKey() const
{
return m_key;
}
private:
std::string m_key;
T m_value;
};
But now the problem is, in my object class, I have to declare fields and overloading functions for every possible attribute type:
class MyObject {
public:
MyObject(int value) :
m_value(value)
{
}
~MyObject()
{
}
int getValue() const
{
return m_value;
}
void addAttribute(Attribute<int> attribute)
{
m_intAttributes.push_back(attribute);
}
void addAttribute(Attribute<double> attribute)
{
m_doubleAttributes.push_back(attribute);
}
const std::list<Attribute<int> >& getIntAttributes() const
{
return m_intAttributes;
}
const std::list<Attribute<double> >& getDoubleAttributes() const
{
return m_doubleAttributes;
}
private:
int m_value;
std::list<Attribute<int> > m_intAttributes;
std::list<Attribute<double> > m_doubleAttributes;
};
Also, iterating through attributes is not very convenient and finding an attribute of a given name is very difficult:
void showMyObject(const MyObject& myObject)
{
std::list<Attribute<int> > intAttributes;
std::list<Attribute<int> >::const_iterator itInt;
std::list<Attribute<double> > doubleAttributes;
std::list<Attribute<double> >::const_iterator itDouble;
std::cout << "Value in myObject " << myObject.getValue() << std::endl;
intAttributes = myObject.getIntAttributes();
for(itInt = intAttributes.begin() ; itInt != intAttributes.end() ; itInt++)
{
std::cout << itInt->getKey() << " = " << itInt->getValue() << std::endl;
}
doubleAttributes = myObject.getDoubleAttributes();
for(itDouble = doubleAttributes.begin() ; itDouble != doubleAttributes.end() ; itDouble++)
{
std::cout << itDouble->getKey() << " = " << itDouble->getValue() << std::endl;
}
}
FYI, my main function looks like this:
int main(int argc, char* argv[])
{
MyObject object(123);
object.addAttribute(Attribute<double>("testDouble", 3.23));
object.addAttribute(Attribute<double>("testDouble2", 99.654));
object.addAttribute(Attribute<int>("testInt", 3));
object.addAttribute(Attribute<int>("testInt2", 99));
showMyObject(object);
return 0;
}
My guess is that if we want to guarantee type safety, there must be a list of functions with the correct return type in the signature (getTYPEAttributes in my example).
However, I was wondering if there is a more elegant solution, and if a design pattern that I am not aware of can help me deal with this problem correctly.
Sounds like a job for Boost.TypeErasure . Do you want to store different things that have common features (are streaming, have a key), but can be explicitly accessible and do not need a common base? Store your attributes this way:
namespace mpl = boost::mpl
using namespace boost::type_erasure;
BOOST_TYPE_ERASURE_MEMBER((has_getKey), getKey, 0)
using AnyAttribute = any<mpl::vector<
copy_constructible<>,
typeid_<>,
ostreamable<>, // add a stream operator for Attribute
has_getKey<std::string(), const _self>
> >;
std::vector<AnyAttribute> attributes;
Adding an attribute will look like this:
template <typename T>
void addAttribute(const std::string& key, const T& value) {
// since Attribute<T> is copy-constructible, streamable,
// and has a member function with the signature std::string getKey() const
// we can construct an AnyAttribute with it.
attributes.push_back(Attribute<T>(key, value));
}
Printing all attributes:
void showMe() {
for (const auto& attr : attributes) {
std::cout << attr << ' '; // since we specified ostreamable<>,
// everything we put into this any<> is streamable
// so the any<> is too
}
std::cout << '\n';
}
Searching for an attribute by name and specified type returns nullptr
if not found or of wrong type:
template <typename T>
const Attribute<T>* lookupAttribute(const std::string& key) {
// can use getKey() even if they're all different types
// because we added has_getKey<> as a concept
auto it = std::find_if(attributes.begin(), attributes.end(),
[=](const AnyAttribute& a) {
return a.getKey() == key;
});
if (it != attributes.end()) {
// this will return a valid Attribute<T>* you specified the
// correct type, nullptr if you specified the incorrect type
// it is not possible to query the type.
return any_cast<Attribute<T>*>(&*it);
}
else {
return nullptr;
}
}
There is a simpler object with a type, which is simple Boost.Any
, but there you cannot have any general functionality - to make it difficult to implement either search or print operations that I illustrated above.