jeudi 30 avril 2015

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 using 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 that in my object class, I have to declare fields and overload functions for each 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;
};

Moreover, iterating through the attributes is not very comfortable and looking for 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;
}

I guess if we want to guarantee type safety, there must be somewhere list of functions with the right return type in the signature (getTYPEAttributes in my example).

However, I was wondering if a more elegant solution exists and if a design pattern I'm not aware of could help me to handle this problem correctly.

Aucun commentaire:

Enregistrer un commentaire