ALPS MPS Codes
Reference documentation.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
parameter_proxy.h
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * ALPS MPS DMRG Project
4  *
5  * Copyright (C) 2013 Institute for Theoretical Physics, ETH Zurich
6  * 2013-2013 by Michele Dolfi <dolfim@phys.ethz.ch>
7  *
8  * This software is part of the ALPS Applications, published under the ALPS
9  * Application License; you can use, redistribute it and/or modify it under
10  * the terms of the license, either version 1 or (at your option) any later
11  * version.
12  *
13  * You should have received a copy of the ALPS Application License along with
14  * the ALPS Applications; see the file LICENSE.txt. If not, the license is also
15  * available from http://alps.comp-phys.org/.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
20  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
21  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  *****************************************************************************/
26 
27 #ifndef DMRG_UTILS_PARAMETER_PROXY_H
28 #define DMRG_UTILS_PARAMETER_PROXY_H
29 
30 #include <boost/tokenizer.hpp>
31 #include <boost/lexical_cast.hpp>
32 #include <boost/algorithm/string.hpp>
33 
34 namespace parameters {
35 
36  namespace conversion
37  {
38  // this can be specialized to provide conversion for types that cannot be read
39  // with the boost::any_cast, or whatever program options uses for the as<>
40  // method
41  template<class T> struct get_
42  {
43  T operator()(std::string const & val)
44  {
45  try {
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;
49  throw e;
50  }
51  }
52  };
53 
54  // eliminating quatation marks around strings
55  template <> struct get_<std::string>
56  {
57  std::string operator()(std::string const & val)
58  {
59  std::string ret = val;
60  boost::trim_if(ret, boost::is_any_of("\"'"));
61  return ret;
62  }
63  };
64 
65  template<class T> struct get_<std::vector<T> >
66  {
67  std::vector<T> operator()(std::string const & val)
68  {
69  std::string raw = val;
70  boost::trim_if(raw, boost::is_any_of("\"'"));
71  std::vector<T> ret;
72 
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));
78  }
79  return ret;
80  }
81  };
82  }
83 
84 
85  #define FOREACH_PROXY_NUMERIC_TYPE(CALLBACK) \
86  CALLBACK(double) \
87  CALLBACK(int) \
88  CALLBACK(bool) \
89  CALLBACK(float) \
90  CALLBACK(long) \
91  CALLBACK(unsigned) \
92  CALLBACK(unsigned long long)
93 
94  #define FOREACH_PROXY_STRING_TYPE(CALLBACK) \
95  CALLBACK(std::string)
96 
97  class proxy {
98  public:
99  proxy(std::string & val) : val_(val) { }
100 
101  template <typename T>
102  T as() const
103  {
105  return g(val_);
106  }
107 
108  template <typename T>
109  operator T () const
110  {
111  return as<T>();
112  }
113 
114  bool operator == (proxy const& rhs) const
115  {
116  return ( val_ == rhs.val_ );
117  }
118 
119  bool operator == (const char* rhs) const
120  {
121  return ( val_ == std::string(rhs) );
122  }
123 
124  bool operator != (const char* rhs) const
125  {
126  return ( val_ != std::string(rhs) );
127  }
128 
129  template <typename T>
130  proxy& operator= (T const& new_val)
131  {
132  val_ = boost::lexical_cast<std::string>(new_val);
133  return *this;
134  }
135 
136  std::string const& str() const
137  {
138  return val_;
139  }
140 
141  const char* c_str() const
142  {
143  return val_.c_str();
144  }
145 
146  bool empty() const
147  {
148  return val_.empty();
149  }
150 
151 
152  #define PROXY_BINARY_OP_DECL(U,OP,T) \
153  friend \
154  U operator OP (T lhs, proxy const& rhs) \
155  { \
156  return lhs OP rhs.as<T>(); \
157  } \
158  friend \
159  U operator OP (proxy const& rhs, T lhs) \
160  { \
161  return rhs.as<T>() OP lhs; \
162  }
163 
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)
173 
174  #define PROXY_COMP_OPERATORS_DECL(T) \
175  PROXY_BINARY_OP_DECL(bool,==,T) \
176  PROXY_BINARY_OP_DECL(bool,!=,T)
177 
181 
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
187 
188  friend std::ostream& operator<< (std::ostream& os, proxy const& p)
189  {
190  os << p.val_;
191  return os;
192  }
193 
194  private:
195  std::string & val_;
196  };
197 }
198 
199 #endif
#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)
proxy(std::string &val)
std::string const & str() const
std::string operator()(std::string const &val)
bool empty() const
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