|
Boost Users : |
From: Markus Werle (numerical.simulation_at_[hidden])
Date: 2006-11-23 12:38:27
Parag Gadkari <parag.gadkari <at> gmail.com> writes:
>
>
> It could be the way you are trying to pass parameters. Can you post a snippet
of what you are trying to do?
Well, I tried to obtain something smaller, but I failed.
Due to copyright restrictions I post only excerpts to give you an idea.
Most of the code passed gcc-4.0.3 (cygwin), so I blame the compiler until
proven wrong (looking forward to Service Pack 1).
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/key_extractors.hpp>
[...]
#include <string>
#include <iostream>
[...]
struct int_string_pair
{
int i_;
std::string s_;
int_string_pair(int i, std::string const & s)
: i_(i)
, s_(s)
{}
};
struct as_int {};
struct as_string {};
// shortcut for typedef
struct property_storage
{
typedef int_string_pair value_type;
typedef boost::multi_index::multi_index_container<
value_type,
boost::multi_index::indexed_by
<boost::multi_index::sequenced<>,
boost::multi_index::
ordered_unique<boost::multi_index::tag<as_int>,
boost::multi_index::member<value_type,
int,
&value_type::i_> >,
boost::multi_index::
ordered_unique<boost::multi_index::tag<as_string>,
boost::multi_index::member<value_type,
std::string,
&value_type::s_> >
> > value_storage_t;
};
template <typename T>
class CConfigurationProperty
{
public:
typedef property_storage::value_type value_type;
typedef property_storage::value_storage_t value_storage_t;
private:
value_type m_Value;
value_type GetCheckedValue(int i)
{
value_storage_t const & possible_values = T::PossibleValues();
value_storage_t::index_iterator<as_int>::type it =
boost::multi_index::get<as_int>(possible_values).find(i);
ENFORCE(it != boost::multi_index::get<as_int>(possible_values).end())
("Trying to assign invalid value '")(i)
("' to configuration property.");
return value_type(*it);
}
value_type GetCheckedValue(std::string const & s)
{
value_storage_t const & possible_values = T::PossibleValues();
value_storage_t::index_iterator<as_string>::type it =
boost::multi_index::get<as_string>(possible_values).find(s);
ENFORCE(it != boost::multi_index::get<as_string>(possible_values).end())
("Trying to assign invalid value '")(s)
("' to configuration property.");
return value_type(*it);
}
public:
CConfigurationProperty()
: m_Value(GetCheckedValue(T::DefaultValue()))
{}
CConfigurationProperty(int i)
: m_Value(GetCheckedValue(i))
{}
CConfigurationProperty(std::string const & s)
: m_Value(GetCheckedValue(s))
{}
CConfigurationProperty(char const * s)
: m_Value(GetCheckedValue(std::string(s)))
{}
operator int() const
{
return m_Value.i_;
}
operator std::string() const
{
return m_Value.s_;
}
CConfigurationProperty<T> & operator=(int i)
{
m_Value = GetCheckedValue(i);
return *this;
}
CConfigurationProperty<T> & operator=(std::string const & s)
{
m_Value = GetCheckedValue(s);
return *this;
}
CConfigurationProperty<T> & operator=(char const * s)
{
m_Value = GetCheckedValue(std::string(s));
return *this;
}
};
[...]
struct example_type
: public property_storage
{
static
value_storage_t const &
PossibleValues()
{
static value_storage_t const possible_values =
boost::assign::list_of<int_string_pair>
(-1, "undefined")
(0, "foo")
(1, "bar");
return possible_values;
}
static int DefaultValue() { return -1; }
};
typedef CConfigurationProperty<example_type> example_type_t;
example_type_t test;
test = "foo";
assert(test != "bar");
assert(test == 0);
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net