17 #include <boost/any.hpp> 19 #include "../config.h" 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> 112 static bool support(
const boost::any&);
115 StringList getNameList()
const;
118 string getValueList()
const;
121 string getNameValueList()
const;
124 bool have(
const string& name)
const {
125 return m_params.count(name) == 0 ?
false :
true;
134 template <
typename ValueType>
135 void set(
const string& name,
const ValueType& value);
142 template <
typename ValueType>
143 ValueType
get(
const string& name)
const;
146 typedef map<string, boost::any> param_map_t;
147 param_map_t m_params;
152 #if HKU_SUPPORT_SERIALIZATION 154 friend class boost::serialization::access;
157 friend class boost::serialization::access;
160 ItemRecord(
const string& name,
const boost::any& arg): name(name) {
161 if (arg.type() ==
typeid(bool)) {
163 bool x = boost::any_cast<
bool>(arg);
164 value = boost::lexical_cast<
string>(x);
165 }
else if (arg.type() ==
typeid(int)) {
167 int x = boost::any_cast<
int>(arg);
168 value = boost::lexical_cast<
string>(x);
169 }
else if (arg.type() ==
typeid(double)) {
171 double x = boost::any_cast<
double>(arg);
172 value = boost::lexical_cast<
string>(x);
173 }
else if (arg.type() ==
typeid(
string)) {
175 value = boost::any_cast<
string>(arg);
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);
194 template<
class Archive>
195 void save(Archive & ar,
const unsigned int version)
const{
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);
206 template<
class Archive>
207 void load(Archive & ar,
const unsigned int version) {
210 ar & bs::make_nvp<size_t>(
"count", total);
212 for (
size_t i = 0; i < total; i++) {
213 ar & bs::make_nvp<ItemRecord>(
"Item", record);
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;
224 std::cout <<
"Unknown type! [Parameter::load]" << std::endl;
228 BOOST_SERIALIZATION_SPLIT_MEMBER()
233 #define PARAMETER_SUPPORT protected: \ 234 Parameter m_params; \ 236 const Parameter& getParameter() const { \ 240 void setParameter(const Parameter& param) { \ 244 bool haveParam(const string& name) const { \ 245 return m_params.have(name); \ 248 template <typename ValueType> \ 249 void setParam(const string& name, const ValueType& value) { \ 250 m_params.set<ValueType>(name, value); \ 253 template <typename ValueType> \ 254 ValueType getParam(const string& name) const { \ 255 return m_params.get<ValueType>(name); \ 259 template <
typename ValueType>
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);
266 return boost::any_cast<ValueType>(iter->second);
270 template <
typename ValueType>
273 if (!support(value)){
274 throw std::logic_error(
"Unsuport Type! " + name);
277 m_params[name] = value;
281 if (m_params[name].type() !=
typeid(ValueType)) {
282 throw std::logic_error(
"Mismatching type! " + name);
286 m_params[name] = value;
供需要命名参数设定的类使用
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