HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Small C++ Boost extension based on boost::property_tree

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
extensionsmallbasedboostproperty_tree

Problem

This is a small extension based on a boost::property_tree, which supports arbitrary values for the properties and appropriate serialization. It can be used as an alternative to QSettings in Qt and provides very convenient way to pass properties inside your applications.

I would like to have a code review from somebody, just to point out what I might be doing wrong, because I'm not that good at C++ coding.

```
#include
#include
#include
#include
#include
#include
#include
#include
#include

#include "core_defines.h"
#include "core_extensions_lexical_cast.h"

namespace boost {
namespace property_tree {

// Generic value holder, which will attempt to cast objects rather than
// indicate error if wrong type was supplied. This behavior is likely to
// be more error-prone, but is more 'user-friendly'. See 'strict_value_holder'
// class if you need strict type support.

// Supports trivial integral, floating point and string types in 'strict'
// mode and also allows the storage to hold arbitrary values using
// 'boost::any' storage. Note that due to implementation details,
// values of specific (non-default) types can't be serialized
// (we skip them / replace with dummy objects).
class value_holder {
// Main value holder type, which has support for common types
// and can use the internal visitor for casting.
typedef boost::variant Holder;
public:
// Models 'DefaultConstructible', default constructor
// leaves the storage uninitialized.
value_holder() : using_any_holder_(false) { }

template value_holder(const T& value, typename boost::enable_if >::type* = 0)
: holder_(value), using_any_holder_(false) { }

// Custom constructing routine for string-like types.
explicit value_holder(const char* value)
: holder_(std::string(value)), using_any_holder_(false) { }

// Custom constructing routine for string-like types.
explicit value_holder(const wchar_t* value)
: holder_(std::wstring(value)), using_any_holder_(f

Solution

I think use boost::serialize is not are good idea. I read about this library (not all Boost) and they have problem compatibility with previous versions.

I wrote other property container and property manager some times ago. And we use template class, without Boost, like this:

//Contains one property any complexity you want
template 
class Property : public PropertyBase
{
protected:
    Type m_property;
public:
    /**
        for deep copy you must define copy c-tor
        @param Type const& prop - any property
    **/
    Property(Type const& prop)
    :m_property(prop)
    {
    }
    virtual ~Property() { }
public:
    // @return Type const& - contained property
    Type const& GetProperty() const { return m_property; }

    /**
        for deep copy you must define operator=
        @param Type const& prop - any property
    **/
    void SetProperty(Type const& prop)
    {
        m_property = prop;
    }
};

typedef PointerContainer TProperties; //list of properties for group
typedef PointerContainer  TPropertyGroupList; //list of properties groups

//Class for manipulation with properies
class BasePropertyProvider
{
protected:
    TPropertyGroupList  m_propertyGroupList; // container of all properties
.................
public:
    /**
        @param int propertyGroup - group of properties for which we search 
        @param int propertyType - type of property which we search
        @return Type const* - pointer on value or 0 if searching failed
    **/
    template 
    Type const* GetProperty(int propertyGroup, int propertyType) const;

    /**
        Add property in inner storage
        @param int propertyGroup - group of properties for add
        @param int propertyType - type of property for add
        @param Type const & - reference on value for add
    **/
    template 
    void AddProperty(int propertyGroup, int propertyType, Type const &prop);
};


It's not full realization, but I think you understand the direction.

Also I know Qt solution property. You can find it here.

But they're really heavy tools, and if you want to add some original property you must write 3 or 4 classes.

Code Snippets

//Contains one property any complexity you want
template <typename Type>
class Property : public PropertyBase
{
protected:
    Type m_property;
public:
    /**
        for deep copy you must define copy c-tor
        @param Type const& prop - any property
    **/
    Property(Type const& prop)
    :m_property(prop)
    {
    }
    virtual ~Property() { }
public:
    // @return Type const& - contained property
    Type const& GetProperty() const { return m_property; }

    /**
        for deep copy you must define operator=
        @param Type const& prop - any property
    **/
    void SetProperty(Type const& prop)
    {
        m_property = prop;
    }
};

typedef PointerContainer<int, PropertyBase> TProperties; //list of properties for group
typedef PointerContainer<int, TProperties>  TPropertyGroupList; //list of properties groups

//Class for manipulation with properies
class BasePropertyProvider
{
protected:
    TPropertyGroupList  m_propertyGroupList; // container of all properties
.................
public:
    /**
        @param int propertyGroup - group of properties for which we search 
        @param int propertyType - type of property which we search
        @return Type const* - pointer on value or 0 if searching failed
    **/
    template <typename Type>
    Type const* GetProperty(int propertyGroup, int propertyType) const;

    /**
        Add property in inner storage
        @param int propertyGroup - group of properties for add
        @param int propertyType - type of property for add
        @param Type const & - reference on value for add
    **/
    template <typename Type>
    void AddProperty(int propertyGroup, int propertyType, Type const &prop);
};

Context

StackExchange Code Review Q#1818, answer score: 4

Revisions (0)

No revisions yet.