diff --git a/libraries/chain/betting_market_group_object.cpp b/libraries/chain/betting_market_group_object.cpp index 393a9f3f..14c65626 100644 --- a/libraries/chain/betting_market_group_object.cpp +++ b/libraries/chain/betting_market_group_object.cpp @@ -30,6 +30,8 @@ #include #include +#include +#include #include namespace graphene { namespace chain { @@ -458,6 +460,18 @@ betting_market_group_status betting_market_group_object::get_status() const }; } +void betting_market_group_object::pack_impl(std::ostream& stream) const +{ + boost::archive::binary_oarchive oa(stream, boost::archive::no_header|boost::archive::no_codecvt|boost::archive::no_xml_tag_checking); + oa << my->state_machine; +} + +void betting_market_group_object::unpack_impl(std::istream& stream) +{ + boost::archive::binary_iarchive ia(stream, boost::archive::no_header|boost::archive::no_codecvt|boost::archive::no_xml_tag_checking); + ia >> my->state_machine; +} + void betting_market_group_object::on_upcoming_event(database& db) { my->state_machine.process_event(upcoming_event(db)); @@ -527,3 +541,38 @@ void betting_market_group_object::dispatch_new_status(database& db, betting_mark } } // graphene::chain +namespace fc { +// Manually reflect betting_market_group_object to variant to properly reflect "state" +void to_variant(const graphene::chain::betting_market_group_object& betting_market_group_obj, fc::variant& v, uint32_t max_depth) +{ + fc::mutable_variant_object o; + o("id", fc::variant(betting_market_group_obj.id, max_depth)) + ("description", fc::variant(betting_market_group_obj.description, max_depth)) + ("event_id", fc::variant(betting_market_group_obj.event_id, max_depth)) + ("rules_id", fc::variant(betting_market_group_obj.rules_id, max_depth)) + ("asset_id", fc::variant(betting_market_group_obj.asset_id, max_depth)) + ("total_matched_bets_amount", fc::variant(betting_market_group_obj.total_matched_bets_amount, max_depth)) + ("never_in_play", fc::variant(betting_market_group_obj.never_in_play, max_depth)) + ("delay_before_settling", fc::variant(betting_market_group_obj.delay_before_settling, max_depth)) + ("settling_time", fc::variant(betting_market_group_obj.settling_time, max_depth)) + ("status", fc::variant(betting_market_group_obj.get_status(), max_depth)); + + v = o; +} + +// Manually reflect betting_market_group_object to variant to properly reflect "state" +void from_variant(const fc::variant& v, graphene::chain::betting_market_group_object& betting_market_group_obj, uint32_t max_depth) +{ + betting_market_group_obj.id = v["id"].as( max_depth ); + betting_market_group_obj.description = v["description"].as( max_depth ); + betting_market_group_obj.event_id = v["event_id"].as( max_depth ); + betting_market_group_obj.asset_id = v["asset_id"].as( max_depth ); + betting_market_group_obj.total_matched_bets_amount = v["total_matched_bets_amount"].as( max_depth ); + betting_market_group_obj.never_in_play = v["never_in_play"].as( max_depth ); + betting_market_group_obj.delay_before_settling = v["delay_before_settling"].as( max_depth ); + betting_market_group_obj.settling_time = v["settling_time"].as>( max_depth ); + graphene::chain::betting_market_group_status status = v["status"].as( max_depth ); + const_cast(betting_market_group_obj.my->state_machine.current_state())[0] = (int)status; +} +} //end namespace fc + diff --git a/libraries/chain/betting_market_object.cpp b/libraries/chain/betting_market_object.cpp index 8f706458..62ed883a 100644 --- a/libraries/chain/betting_market_object.cpp +++ b/libraries/chain/betting_market_object.cpp @@ -29,6 +29,8 @@ #include #include +#include +#include #include namespace graphene { namespace chain { @@ -420,6 +422,18 @@ void betting_market_object::cancel_all_bets(database& db) const } } +void betting_market_object::pack_impl(std::ostream& stream) const +{ + boost::archive::binary_oarchive oa(stream, boost::archive::no_header|boost::archive::no_codecvt|boost::archive::no_xml_tag_checking); + oa << my->state_machine; +} + +void betting_market_object::unpack_impl(std::istream& stream) +{ + boost::archive::binary_iarchive ia(stream, boost::archive::no_header|boost::archive::no_codecvt|boost::archive::no_xml_tag_checking); + ia >> my->state_machine; +} + void betting_market_object::on_unresolved_event(database& db) { my->state_machine.process_event(unresolved_event(db)); @@ -452,3 +466,30 @@ void betting_market_object::on_canceled_event(database& db) } } // graphene::chain +namespace fc { +// Manually reflect betting_market_object to variant to properly reflect "state" +void to_variant(const graphene::chain::betting_market_object& event_obj, fc::variant& v, uint32_t max_depth) +{ + fc::mutable_variant_object o; + o("id", fc::variant(event_obj.id, max_depth) ) + ("group_id", fc::variant(event_obj.group_id, max_depth)) + ("description", fc::variant(event_obj.description, max_depth)) + ("payout_condition", fc::variant(event_obj.payout_condition, max_depth)) + ("resolution", fc::variant(event_obj.resolution, max_depth)) + ("status", fc::variant(event_obj.get_status(), max_depth)); + + v = o; +} + +// Manually reflect betting_market_object to variant to properly reflect "state" +void from_variant(const fc::variant& v, graphene::chain::betting_market_object& event_obj, uint32_t max_depth) +{ + event_obj.id = v["id"].as( max_depth ); + event_obj.group_id = v["name"].as( max_depth ); + event_obj.description = v["description"].as( max_depth ); + event_obj.payout_condition = v["payout_condition"].as( max_depth ); + event_obj.resolution = v["resolution"].as>( max_depth ); + graphene::chain::betting_market_status status = v["status"].as( max_depth ); + const_cast(event_obj.my->state_machine.current_state())[0] = (int)status; +} +} //end namespace fc \ No newline at end of file diff --git a/libraries/chain/include/graphene/chain/betting_market_object.hpp b/libraries/chain/include/graphene/chain/betting_market_object.hpp index 34946344..e2d2d6e1 100644 --- a/libraries/chain/include/graphene/chain/betting_market_object.hpp +++ b/libraries/chain/include/graphene/chain/betting_market_object.hpp @@ -27,8 +27,20 @@ #include #include #include - #include +#include + +namespace graphene { namespace chain { +class betting_market_object; +class betting_market_group_object; +} } + +namespace fc { +void to_variant(const graphene::chain::betting_market_object& betting_market_obj, fc::variant& v, uint32_t max_depth = 1); +void from_variant(const fc::variant& v, graphene::chain::betting_market_object& betting_market_obj, uint32_t max_depth = 1); +void to_variant(const graphene::chain::betting_market_group_object& betting_market_group_obj, fc::variant& v, uint32_t max_depth = 1); +void from_variant(const fc::variant& v, graphene::chain::betting_market_group_object& betting_market_group_obj, uint32_t max_depth = 1); +} //end namespace fc namespace graphene { namespace chain { @@ -88,6 +100,21 @@ class betting_market_group_object : public graphene::db::abstract_object< bettin betting_market_group_status get_status() const; + // serialization functions: + // for serializing to raw, go through a temporary sstream object to avoid + // having to implement serialization in the header file + template + friend Stream& operator<<( Stream& s, const betting_market_group_object& betting_market_group_obj ); + + template + friend Stream& operator>>( Stream& s, betting_market_group_object& betting_market_group_obj ); + + friend void ::fc::to_variant(const graphene::chain::betting_market_group_object& betting_market_group_obj, fc::variant& v, uint32_t max_depth); + friend void ::fc::from_variant(const fc::variant& v, graphene::chain::betting_market_group_object& betting_market_group_obj, uint32_t max_depth); + + void pack_impl(std::ostream& stream) const; + void unpack_impl(std::istream& stream); + void on_upcoming_event(database& db); void on_in_play_event(database& db); void on_frozen_event(database& db); @@ -129,6 +156,21 @@ class betting_market_object : public graphene::db::abstract_object< betting_mark void cancel_all_unmatched_bets(database& db) const; void cancel_all_bets(database& db) const; + // serialization functions: + // for serializing to raw, go through a temporary sstream object to avoid + // having to implement serialization in the header file + template + friend Stream& operator<<( Stream& s, const betting_market_object& betting_market_obj ); + + template + friend Stream& operator>>( Stream& s, betting_market_object& betting_market_obj ); + + friend void ::fc::to_variant(const graphene::chain::betting_market_object& betting_market_obj, fc::variant& v, uint32_t max_depth); + friend void ::fc::from_variant(const fc::variant& v, graphene::chain::betting_market_object& betting_market_obj, uint32_t max_depth); + + void pack_impl(std::ostream& stream) const; + void unpack_impl(std::istream& stream); + void on_unresolved_event(database& db); void on_frozen_event(database& db); void on_closed_event(database& db); @@ -582,16 +624,198 @@ typedef multi_index_container< > > betting_market_position_multi_index_type; typedef generic_index betting_market_position_index; + +template +inline Stream& operator<<( Stream& s, const betting_market_object& betting_market_obj ) +{ + // pack all fields exposed in the header in the usual way + // instead of calling the derived pack, just serialize the one field in the base class + // fc::raw::pack >(s, betting_market_obj); + fc::raw::pack(s, betting_market_obj.id); + fc::raw::pack(s, betting_market_obj.group_id); + fc::raw::pack(s, betting_market_obj.description); + fc::raw::pack(s, betting_market_obj.payout_condition); + fc::raw::pack(s, betting_market_obj.resolution); + + // fc::raw::pack the contents hidden in the impl class + std::ostringstream stream; + betting_market_obj.pack_impl(stream); + std::string stringified_stream(stream.str()); + fc::raw::pack(s, stream.str()); + + return s; +} +template +inline Stream& operator>>( Stream& s, betting_market_object& betting_market_obj ) +{ + // unpack all fields exposed in the header in the usual way + //fc::raw::unpack >(s, betting_market_obj); + fc::raw::unpack(s, betting_market_obj.id); + fc::raw::unpack(s, betting_market_obj.group_id); + fc::raw::unpack(s, betting_market_obj.description); + fc::raw::unpack(s, betting_market_obj.payout_condition); + fc::raw::unpack(s, betting_market_obj.resolution); + + // fc::raw::unpack the contents hidden in the impl class + std::string stringified_stream; + fc::raw::unpack(s, stringified_stream); + std::istringstream stream(stringified_stream); + betting_market_obj.unpack_impl(stream); + + return s; +} + + +template +inline Stream& operator<<( Stream& s, const betting_market_group_object& betting_market_group_obj ) +{ + // pack all fields exposed in the header in the usual way + // instead of calling the derived pack, just serialize the one field in the base class + // fc::raw::pack >(s, betting_market_group_obj); + fc::raw::pack(s, betting_market_group_obj.id); + fc::raw::pack(s, betting_market_group_obj.description); + fc::raw::pack(s, betting_market_group_obj.event_id); + fc::raw::pack(s, betting_market_group_obj.rules_id); + fc::raw::pack(s, betting_market_group_obj.asset_id); + fc::raw::pack(s, betting_market_group_obj.total_matched_bets_amount); + fc::raw::pack(s, betting_market_group_obj.never_in_play); + fc::raw::pack(s, betting_market_group_obj.delay_before_settling); + fc::raw::pack(s, betting_market_group_obj.settling_time); + // fc::raw::pack the contents hidden in the impl class + std::ostringstream stream; + betting_market_group_obj.pack_impl(stream); + std::string stringified_stream(stream.str()); + fc::raw::pack(s, stream.str()); + + return s; +} +template +inline Stream& operator>>( Stream& s, betting_market_group_object& betting_market_group_obj ) +{ + // unpack all fields exposed in the header in the usual way + //fc::raw::unpack >(s, betting_market_group_obj); + fc::raw::unpack(s, betting_market_group_obj.id); + fc::raw::unpack(s, betting_market_group_obj.description); + fc::raw::unpack(s, betting_market_group_obj.event_id); + fc::raw::unpack(s, betting_market_group_obj.rules_id); + fc::raw::unpack(s, betting_market_group_obj.asset_id); + fc::raw::unpack(s, betting_market_group_obj.total_matched_bets_amount); + fc::raw::unpack(s, betting_market_group_obj.never_in_play); + fc::raw::unpack(s, betting_market_group_obj.delay_before_settling); + fc::raw::unpack(s, betting_market_group_obj.settling_time); + + // fc::raw::unpack the contents hidden in the impl class + std::string stringified_stream; + fc::raw::unpack(s, stringified_stream); + std::istringstream stream(stringified_stream); + betting_market_group_obj.unpack_impl(stream); + + return s; +} } } // graphene::chain FC_REFLECT_DERIVED( graphene::chain::betting_market_rules_object, (graphene::db::object), (name)(description) ) -FC_REFLECT_DERIVED( graphene::chain::betting_market_group_object, (graphene::db::object), (description)(event_id)(rules_id)(asset_id)(total_matched_bets_amount)(never_in_play)(delay_before_settling)(settling_time) ) -GRAPHENE_EXTERNAL_SERIALIZATION( extern, graphene::chain::betting_market_group_object ) - -FC_REFLECT_DERIVED( graphene::chain::betting_market_object, (graphene::db::object), (group_id)(description)(payout_condition)(resolution) ) -GRAPHENE_EXTERNAL_SERIALIZATION( extern, graphene::chain::betting_market_object ) - FC_REFLECT_DERIVED( graphene::chain::bet_object, (graphene::db::object), (bettor_id)(betting_market_id)(amount_to_bet)(backer_multiplier)(back_or_lay)(end_of_delay) ) FC_REFLECT_DERIVED( graphene::chain::betting_market_position_object, (graphene::db::object), (bettor_id)(betting_market_id)(pay_if_payout_condition)(pay_if_not_payout_condition)(pay_if_canceled)(pay_if_not_canceled)(fees_collected) ) + +namespace fc { + +template<> +template<> +inline void if_enum::from_variant(const variant &vo, graphene::chain::betting_market_object &v, uint32_t max_depth) { + from_variant(vo, v, max_depth); +} + +template<> +template<> +inline void if_enum::to_variant(const graphene::chain::betting_market_object &v, variant &vo, uint32_t max_depth) { + to_variant(v, vo, max_depth); +} + +namespace raw { namespace detail { + +template<> +template<> +inline void if_enum::pack(fc::datastream &s, const graphene::chain::betting_market_object &v, uint32_t) { + s << v; +} + +template<> +template<> +inline void if_enum::pack(fc::datastream &s, const graphene::chain::betting_market_object &v, uint32_t) { + s << v; +} + +template<> +template<> +inline void if_enum::unpack(fc::datastream &s, graphene::chain::betting_market_object &v, uint32_t) { + s >> v; +} + +} } + +template <> +struct get_typename { + static const char *name() { + return "graphene::chain::betting_market_object"; + } +}; +template <> +struct reflector { + typedef graphene::chain::betting_market_object type; + typedef fc::true_type is_defined; + typedef fc::false_type is_enum; +}; +} // namespace fc + +namespace fc { + +template<> +template<> +inline void if_enum::from_variant(const variant &vo, graphene::chain::betting_market_group_object &v, uint32_t max_depth) { + from_variant(vo, v, max_depth); +} + +template<> +template<> +inline void if_enum::to_variant(const graphene::chain::betting_market_group_object &v, variant &vo, uint32_t max_depth) { + to_variant(v, vo, max_depth); +} + +namespace raw { namespace detail { + +template<> +template<> +inline void if_enum::pack(fc::datastream &s, const graphene::chain::betting_market_group_object &v, uint32_t) { + s << v; +} + +template<> +template<> +inline void if_enum::pack(fc::datastream &s, const graphene::chain::betting_market_group_object &v, uint32_t) { + s << v; +} + +template<> +template<> +inline void if_enum::unpack(fc::datastream &s, graphene::chain::betting_market_group_object &v, uint32_t) { + s >> v; +} + +} } + +template <> +struct get_typename { + static const char *name() { + return "graphene::chain::betting_market_group_object"; + } +}; +template <> +struct reflector { + typedef graphene::chain::betting_market_group_object type; + typedef fc::true_type is_defined; + typedef fc::false_type is_enum; +}; +} // namespace fc