Boost logo

Boost :

From: Andy Little (andy_at_[hidden])
Date: 2006-04-30 11:19:19


"Marcin Kalicinski" <kalita_at_[hidden]> wrote in message
news:e327rh$9tu$1_at_sea.gmane.org...

> There are more operations one can perform on a node than just getting. So I
> would rather replace operator () overload with explicitly named functions:
> get, put, set, del.
>
> The use will look like that:
>
> int n = pt["a.very"]["long"]["path"].get<int>();
> pt["a.very"]["long"]["path"].del();
> pt["a.very"]["long"]["path"].put(17);

I would rather expect something like the following. This makes a path a simple
object.
Other required path functionality might be serialising to/from a file as has
been wished for, but that is trivial and output specific IMO

    using namespace boost::assign;
    typedef std::string path_element1;
    std::vector<path_element1> path1;

    path1 += "a","very","long","path";
    boost::property_tree<path_element1> pt1;

    int n1 =pt1.get(path1,n1);
    pt1[path1].del();
    pt1[path1].put(17);

    typedef int path_element2;
    std::vector<path_element2> path2;

    path2 += 1,2,3,4;
    boost::property_tree<path_element2> pt2;

    int n2 = pt2.get(pt2,n2);
    pt2[path2].del();
    pt2[path2].put(17);

}
regards
Andy Little

(FWIW a 5 minute impl of such tree with root and branch functionality
follows --->
-----------------------------------

#include <cassert>

namespace boost{

    template <typename Key>
    struct node;
    /*
        ABC class for elements of member nodes of a property_tree
    */
    template <typename Key>
    struct abc_node_element{
    private:
        friend struct node<Key>;
    protected:
        const Key m_key;
        abc_node_element(Key const & key) : m_key(key){}
        virtual ~abc_node_element(){};
        virtual abc_node_element* clone() const =0;
        abc_node_element():m_key(Key()){};
    };
    /*
        node_element implementation for member nodes of property_tree
        root or branch depends on Data parameter
        Data is a container (list/vector/property_tree etc) for branch or just
data for data
    */
    template <typename Key, typename Data>
    struct node_element : abc_node_element <Key>{
        node_element(Key const & k, Data const & d)
        : abc_node_element<Key>(k), m_data (d){}
        Data m_data;
        void put(Data const & d){m_data = d;}
        abc_node_element<Key>* clone() const
        {
            return new node_element(this->m_key,this->m_data);
        }
    };

    /*
        a member property_tree node holds a private polymorphic node_element
    */
    template <typename Key>
    struct node{
        node():m_element(0){}
        node& operator = (node const & in);
        node(node const &);
        template <typename Data>
        node( Key const & k, Data const & d)
        {
            m_element = new node_element<Key,Data>(k,d);
        }
        ~node(){ delete m_element;}

        template <typename AltData>
        void put(AltData const & d)
        {
            assert(this->m_element);
            Key k = this->m_element->m_key;
            delete m_element ;
            m_element = 0;
            m_element = new node_element<Key,AltData>(k,d);
        }
        void del(){delete m_element; m_element = 0;}

        template <typename T>
        T & get()
        {
            node_element<T>* pe = dynamic_cast<node_element<T>*> m_element;
            assert (pe);
            return pe->get();
        }
        template <typename T>
        T const & get() const
        {
            const node_element<T>* pe = dynamic_cast<const node_element<T>*>
m_element;
            assert (pe);
            return pe->get();
        }
    private:
     abc_node_element<Key>* m_element;
    };

    template <typename KeyType>
    struct property_tree{
        node<KeyType> m_node;
        template <typename Path>
        node<KeyType> & operator[]( Path const & p);
        template <typename Path>
        node<KeyType> const & operator[]( Path const & p)const;
        template <typename Path, typename Data>
        Data& get(Path const & p, Data & d);
    };

}//boost

//----------------------------

#include <boost/assign.hpp>
#include <vector>
#include <string>

int main()
{
    using namespace boost::assign;
    typedef std::string path_element1;
    std::vector<path_element1> path1;

    path1 += "a","very","long","path";
    boost::property_tree<path_element1> pt1;

    int n1 =pt1.get(path1,n1);
    pt1[path1].del();
    pt1[path1].put(17);

    typedef int path_element2;
    std::vector<path_element2> path2;

    path2 += 1,2,3,4;
    boost::property_tree<path_element2> pt2;

    int n2 = pt2.get(pt2,n2);
    pt2[path2].del();
    pt2[path2].put(17);

}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk