27 #ifndef DMRG_UTILS_PARAMETER_PROXY_H
28 #define DMRG_UTILS_PARAMETER_PROXY_H
30 #include <boost/tokenizer.hpp>
31 #include <boost/lexical_cast.hpp>
32 #include <boost/algorithm/string.hpp>
34 namespace parameters {
41 template<
class T>
struct get_
46 return boost::lexical_cast<T>(val);
47 }
catch (std::exception &e) {
48 maquis::cerr <<
"Exception raised casting " << val <<
" to type " <<
typeid(T).name() << std::endl;
55 template <>
struct get_<std::string>
59 std::string ret = val;
60 boost::trim_if(ret, boost::is_any_of(
"\"'"));
65 template<
class T>
struct get_<std::vector<T> >
69 std::string raw = val;
70 boost::trim_if(raw, boost::is_any_of(
"\"'"));
73 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
74 boost::char_separator<char> sep(
",");
75 tokenizer tokens(raw, sep);
76 BOOST_FOREACH(std::string t, tokens) {
77 ret.push_back(boost::lexical_cast<T, std::string>(t));
85 #define FOREACH_PROXY_NUMERIC_TYPE(CALLBACK) \
92 CALLBACK(unsigned long long)
94 #define FOREACH_PROXY_STRING_TYPE(CALLBACK) \
99 proxy(std::string & val) : val_(val) { }
101 template <
typename T>
108 template <
typename T>
116 return ( val_ == rhs.val_ );
121 return ( val_ == std::string(rhs) );
126 return ( val_ != std::string(rhs) );
129 template <
typename T>
132 val_ = boost::lexical_cast<std::string>(new_val);
136 std::string
const&
str()
const
152 #define PROXY_BINARY_OP_DECL(U,OP,T) \
154 U operator OP (T lhs, proxy const& rhs) \
156 return lhs OP rhs.as<T>(); \
159 U operator OP (proxy const& rhs, T lhs) \
161 return rhs.as<T>() OP lhs; \
164 #define PROXY_NUM_OPERATORS_DECL(T) \
165 PROXY_BINARY_OP_DECL(T,+,T) \
166 PROXY_BINARY_OP_DECL(T,*,T) \
167 PROXY_BINARY_OP_DECL(T,-,T) \
168 PROXY_BINARY_OP_DECL(T,/,T) \
169 PROXY_BINARY_OP_DECL(bool,>,T) \
170 PROXY_BINARY_OP_DECL(bool,>=,T) \
171 PROXY_BINARY_OP_DECL(bool,<,T) \
172 PROXY_BINARY_OP_DECL(bool,<=,T)
174 #define PROXY_COMP_OPERATORS_DECL(T) \
175 PROXY_BINARY_OP_DECL(bool,==,T) \
176 PROXY_BINARY_OP_DECL(bool,!=,T)
182 #undef PROXY_BINARY_OP_DECL
183 #undef PROXY_NUM_OPERATORS_DECL
184 #undef PROXY_COMP_OPERATORS_DECL
185 #undef FOREACH_PROXY_NUMERIC_TYPE
186 #undef FOREACH_PROXY_STRING_TYPE
#define FOREACH_PROXY_STRING_TYPE(CALLBACK)
T operator()(std::string const &val)
std::vector< T > operator()(std::string const &val)
#define PROXY_NUM_OPERATORS_DECL(T)
std::string const & str() const
std::string operator()(std::string const &val)
proxy & operator=(T const &new_val)
const char * c_str() const
#define PROXY_COMP_OPERATORS_DECL(T)
bool operator==(proxy const &rhs) const
#define FOREACH_PROXY_NUMERIC_TYPE(CALLBACK)
friend std::ostream & operator<<(std::ostream &os, proxy const &p)
bool operator!=(const char *rhs) const