Hikyuu
Parameter.h
浏览该文件的文档.
1 /*
2  * Parameter.h
3  *
4  * Created on: 2013-2-28
5  * Author: fasiondog
6  */
7 
8 #ifndef PARAMETER_H_
9 #define PARAMETER_H_
10 
11 #include <iostream>
12 #include <stdexcept>
13 #include <sstream>
14 #include <string>
15 #include <vector>
16 #include <map>
17 #include <boost/any.hpp>
18 
19 #include "../config.h"
20 
21 #if HKU_SUPPORT_SERIALIZATION
22 #include <boost/lexical_cast.hpp>
23 #include <boost/serialization/string.hpp>
24 #include <boost/serialization/split_member.hpp>
25 #include <boost/serialization/nvp.hpp>
26 #endif
27 
28 
29 #ifndef DATATYPE_H_
30  #ifndef HKU_API
31  #define HKU_API
32  #endif
33 #endif
34 
35 namespace hku {
36 
37 #ifndef DATATYPE_H_
38  using std::string;
39  using std::map;
40  using std::vector;
41  typedef vector<string> StringList;
42 #endif
43 
44 
102  HKU_API friend std::ostream& operator <<(std::ostream &os,
103  const Parameter& param);
104 
105 public:
106  Parameter();
107  Parameter(const Parameter&);
108  virtual ~Parameter();
109 
110  Parameter& operator=(const Parameter&);
111 
112  static bool support(const boost::any&);
113 
115  StringList getNameList() const;
116 
118  string getValueList() const;
119 
121  string getNameValueList() const;
122 
124  bool have(const string& name) const {
125  return m_params.count(name) == 0 ? false : true;
126  }
127 
134  template <typename ValueType>
135  void set(const string& name, const ValueType& value);
136 
142  template <typename ValueType>
143  ValueType get(const string& name) const;
144 
145 private:
146  typedef map<string, boost::any> param_map_t;
147  param_map_t m_params;
148 
149 //================================
150 // 序列化支持
151 //================================
152 #if HKU_SUPPORT_SERIALIZATION
153 private:
154  friend class boost::serialization::access;
155 
156  struct ItemRecord {
157  friend class boost::serialization::access;
158 
159  ItemRecord() {}
160  ItemRecord(const string& name, const boost::any& arg): name(name) {
161  if (arg.type() == typeid(bool)) {
162  type = "bool";
163  bool x = boost::any_cast<bool>(arg);
164  value = boost::lexical_cast<string>(x);
165  } else if (arg.type() == typeid(int)) {
166  type = "int";
167  int x = boost::any_cast<int>(arg);
168  value = boost::lexical_cast<string>(x);
169  } else if (arg.type() == typeid(double)) {
170  type = "double";
171  double x = boost::any_cast<double>(arg);
172  value = boost::lexical_cast<string>(x);
173  } else if (arg.type() == typeid(string)) {
174  type = "string";
175  value = boost::any_cast<string>(arg);
176  } else {
177  type = "Unknown";
178  value = "Unknown";
179  }
180  }
181 
182  string name;
183  string type;
184  string value;
185 
186  template<class Archive>
187  void serialize(Archive & ar, const unsigned int version) {
188  ar & BOOST_SERIALIZATION_NVP(name);
189  ar & BOOST_SERIALIZATION_NVP(type);
190  ar & BOOST_SERIALIZATION_NVP(value);
191  }
192  };
193 
194  template<class Archive>
195  void save(Archive & ar, const unsigned int version) const{
196  namespace bs = boost::serialization;
197  size_t total = m_params.size();
198  ar & bs::make_nvp<size_t>("count", total);
199  param_map_t::const_iterator iter = m_params.begin();
200  for (; iter != m_params.end(); ++iter) {
201  ItemRecord record(iter->first, iter->second);
202  ar & bs::make_nvp<ItemRecord>("Item", record);
203  }
204  }
205 
206  template<class Archive>
207  void load(Archive & ar, const unsigned int version) {
208  namespace bs = boost::serialization;
209  size_t total = 0;
210  ar & bs::make_nvp<size_t>("count", total);
211  ItemRecord record;
212  for (size_t i = 0; i < total; i++) {
213  ar & bs::make_nvp<ItemRecord>("Item", record);
214 
215  if (record.type == "bool") {
216  m_params[record.name] = boost::lexical_cast<bool>(record.value);
217  } else if (record.type == "int") {
218  m_params[record.name] = boost::lexical_cast<int>(record.value);
219  } else if (record.type == "double") {
220  m_params[record.name] = boost::lexical_cast<double>(record.value);
221  } else if (record.type == "string") {
222  m_params[record.name] = record.value;
223  } else {
224  std::cout << "Unknown type! [Parameter::load]" << std::endl;
225  }
226  }
227  }
228  BOOST_SERIALIZATION_SPLIT_MEMBER()
229 #endif /* HKU_SUPPORT_SERIALIZATION */
230 };
231 
232 
233 #define PARAMETER_SUPPORT protected: \
234  Parameter m_params; \
235  public: \
236  const Parameter& getParameter() const { \
237  return m_params; \
238  } \
239  \
240  void setParameter(const Parameter& param) { \
241  m_params = param; \
242  } \
243  \
244  bool haveParam(const string& name) const { \
245  return m_params.have(name); \
246  } \
247  \
248  template <typename ValueType> \
249  void setParam(const string& name, const ValueType& value) { \
250  m_params.set<ValueType>(name, value); \
251  } \
252  \
253  template <typename ValueType> \
254  ValueType getParam(const string& name) const { \
255  return m_params.get<ValueType>(name); \
256  }
257 
258 
259 template <typename ValueType>
260 ValueType Parameter::get(const string& name) const {
261  param_map_t::const_iterator iter;
262  iter = m_params.find(name);
263  if (iter == m_params.end()) {
264  throw std::out_of_range("out_of_range in Parameter::get : " + name);
265  }
266  return boost::any_cast<ValueType>(iter->second);
267 }
268 
269 
270 template <typename ValueType>
271 void Parameter::set(const string& name, const ValueType& value) {
272  if( !have(name)){
273  if (!support(value)){
274  throw std::logic_error("Unsuport Type! " + name);
275  return;
276  }
277  m_params[name] = value;
278  return;
279  }
280 
281  if (m_params[name].type() != typeid(ValueType)) {
282  throw std::logic_error("Mismatching type! " + name);
283  return;
284  }
285 
286  m_params[name] = value;
287 }
288 
289 
290 HKU_API bool operator==(const Parameter&, const Parameter&);
291 HKU_API bool operator!=(const Parameter&, const Parameter&);
292 HKU_API bool operator<(const Parameter&, const Parameter&);
293 
294 } /* namespace hku */
295 #endif /* PARAMETER_H_ */
供需要命名参数设定的类使用
Definition: Parameter.h:101
std::string string
Definition: DataType.h:55
bool have(const string &name) const
是否存在指定名称的参数
Definition: Parameter.h:124
void set(const string &name, const ValueType &value)
设定指定的参数值
Definition: Parameter.h:271
ValueType get(const string &name) const
获取指定的参数值
Definition: Parameter.h:260
void load(Archive &ar, hku::Block &blk, unsigned int version)
Definition: Block_serialization.h:34
Definition: Block_serialization.h:18
bool operator!=(const Datetime &, const Datetime &)
Definition: Datetime.h:205
void save(Archive &ar, const hku::Block &blk, unsigned int version)
Definition: Block_serialization.h:20
HKU_API std::ostream & operator<<(std::ostream &os, const Block &blk)
Definition: Block.cpp:13
bool operator==(const Datetime &, const Datetime &)
Definition: Datetime.h:201
vector< string > StringList
Definition: DataType.h:67
bool operator<(const Datetime &, const Datetime &)
Definition: Datetime.h:213
#define HKU_API
Definition: Parameter.h:31
Hikyuu核心命名空间,包含股票数据的管理、指标实现、交易系统框架等
Definition: Block.cpp:11