Boost logo

Boost :

From: Aleksey Gurtovoy (agurtovoy_at_[hidden])
Date: 2003-01-17 15:48:26


hartmutkaiser_at_[hidden] wrote:
> I do not expect from the cla framework to do the parsing for me, it
> should return strings associated with keys (which are strings too)
> from different locations (cla, cfg file, registry etc.).

... through something like lexical property map:

    template<
          typename KeyType
        , typename T
        , typename Compare = std::less<KeyType>
>
    class lexical_property_map
        : private std::map< KeyType,T,Compare >
    {
        typedef lexical_property_map self_t;
        typedef std::map< KeyType,T,Compare > base_t;
     
     public:
        typedef typename base_t::key_type key_type;

        using base_t::begin;
        using base_t::end;
        using base_t::empty;
        using base_t::size;
        using base_t::clear;
        using base_t::find;

        bool has_key(key_type const& a_key) const
        {
            return this->find(a_key) != this->end();
        }
            
        template< typename U >
        self_t& put(key_type const& a_key, U const& a_value)
        {
            (*this)[a_key] = boost::lexical_cast<T>(a_value);
            return *this;
        }

        template< typename U >
        U get(key_type const& a_key, U const& a_default = U()) const
        {
            return this->has_key(a_key)
                ? boost::lexical_cast<U>( this->find(a_key)->second )
                : a_default
                ;
        }

        T get(key_type const& a_key, T const& a_default = T()) const
        {
            return get<T>(a_key, a_default);
        }
        
    };

    int main()
    {
        lexical_property_map<std::string,std::string> options;
        options.put("delay", 50);
        std::cout << options.get<int>("delay");
    }

Aleksey


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