/* * _save_load.cpp * * Created on: 2013-4-26 * Author: fasiondog */ #include #include #include #include #include #include #include #include #include #include #include #if HKU_SUPPORT_SERIALIZATION using namespace boost::python; using namespace hku; static map g_support_class_dict; void registerSupportClass() { g_support_class_dict[typeid(PriceList).hash_code()] = "PriceList"; g_support_class_dict[typeid(Datetime).hash_code()] = "Datetime"; g_support_class_dict[typeid(DatetimeList).hash_code()] = "DatetimeList"; g_support_class_dict[typeid(KData).hash_code()] = "KData"; g_support_class_dict[typeid(KQuery).hash_code()] = "KQuery"; g_support_class_dict[typeid(KRecord).hash_code()] = "KRecord"; g_support_class_dict[typeid(KRecordList).hash_code()] = "KRecordList"; g_support_class_dict[typeid(TimeLineRecord).hash_code()] = "TimeLineRecord"; g_support_class_dict[typeid(TimeLineList).hash_code()] = "TimeLineList"; g_support_class_dict[typeid(TransRecord).hash_code()] = "TransRecord"; g_support_class_dict[typeid(TransList).hash_code()] = "TransList"; g_support_class_dict[typeid(MarketInfo).hash_code()] = "MarketInfo"; g_support_class_dict[typeid(Stock).hash_code()] = "Stock"; g_support_class_dict[typeid(Block).hash_code()] = "Block"; g_support_class_dict[typeid(StockTypeInfo).hash_code()] = "StockTypeInfo"; g_support_class_dict[typeid(StockWeight).hash_code()] = "StockWeight"; g_support_class_dict[typeid(StockWeightList).hash_code()] = "StockWeightList"; g_support_class_dict[typeid(Parameter).hash_code()] = "Parameter"; g_support_class_dict[typeid(Indicator).hash_code()] = "Indicator"; g_support_class_dict[typeid(BorrowRecord).hash_code()] = "BorrowRecord"; g_support_class_dict[typeid(CostRecord).hash_code()] = "CostRecord"; g_support_class_dict[typeid(FundsRecord).hash_code()] = "FundsRecord"; g_support_class_dict[typeid(PositionRecord).hash_code()] = "PositionRecord"; g_support_class_dict[typeid(PositionRecordList).hash_code()] = "PositionRecordList"; g_support_class_dict[typeid(TradeCostPtr).hash_code()] = "TradeCostPtr"; g_support_class_dict[typeid(TradeRecord).hash_code()] = "TradeRecord"; g_support_class_dict[typeid(TradeRecordList).hash_code()] = "TradeRecordList"; g_support_class_dict[typeid(TradeManager).hash_code()] = "TradeManager"; g_support_class_dict[typeid(TradeRequest).hash_code()] = "TradeRequest"; g_support_class_dict[typeid(SystemPtr).hash_code()] = "SystemPtr"; g_support_class_dict[typeid(SignalPtr).hash_code()] = "SignalBase"; g_support_class_dict[typeid(StoplossPtr).hash_code()] = "StoplossBase"; g_support_class_dict[typeid(MoneyManagerPtr).hash_code()] = "MoneyManagerBase"; g_support_class_dict[typeid(ProfitGoalPtr).hash_code()] = "ProfitGoalBase"; g_support_class_dict[typeid(SlippagePtr).hash_code()] = "SlippageBase"; g_support_class_dict[typeid(ConditionPtr).hash_code()] = "ConditionBase"; g_support_class_dict[typeid(EnvironmentPtr).hash_code()] = "EnvironmentBase"; } string supportClassName(const boost::any& arg) { map::const_iterator iter; iter = g_support_class_dict.find(arg.type().hash_code()); if (iter != g_support_class_dict.end()) { return iter->second; } return "Unknown"; } template void xml_save(const T& arg, const string& filename) { try { std::ofstream ofs(filename); if (!ofs) { std::cout << "Can't open file(" << filename << ")!" << std::endl; } boost::archive::xml_oarchive oa(ofs); boost::any obj = arg; string name(supportClassName(obj)); oa << boost::serialization::make_nvp("type", name); oa << BOOST_SERIALIZATION_NVP(arg); } catch (std::exception &e) { std::cout << e.what() << std::endl; } catch(...) { std::cout << "Unknow error! [xml_save]" << std::endl; } } template void xml_load(T& arg, const string& filename) { try { std::ifstream ifs(filename); if (!ifs) { std::cout << "Can't open file(" << filename << ")!" << std::endl; } boost::archive::xml_iarchive ia(ifs); string name; ia >> boost::serialization::make_nvp("type", name); boost::any obj = arg; if (name == supportClassName(obj)) { ia >> BOOST_SERIALIZATION_NVP(arg); } else { std::cout << "Unsupport type! [xml_load]" << std::endl; } } catch (std::exception &e) { std::cout << e.what() << std::endl; } catch(...) { std::cout << "Unknow error! [xml_load]" << std::endl; } } void export_save_load() { //初始化注册支持的类型 registerSupportClass(); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); def("hku_save", xml_save); def("hku_load", xml_load); } #else /* HKU_SUPPORT_SERIALIZATION */ void export_save_load() { } #endif /* HKU_SUPPORT_SERIALIZATION */