From 224eedee394f2c7300c4cc405a3b8ac943bbf87e Mon Sep 17 00:00:00 2001 From: Vlad Dobromyslov Date: Thu, 24 Feb 2022 09:39:51 +0300 Subject: [PATCH] #235 Add custom funtions for match_object --- .../include/graphene/chain/match_object.hpp | 137 ++++++++++++++++-- libraries/chain/match_object.cpp | 55 +++++++ 2 files changed, 180 insertions(+), 12 deletions(-) diff --git a/libraries/chain/include/graphene/chain/match_object.hpp b/libraries/chain/include/graphene/chain/match_object.hpp index 2f5152cc..e1194e1e 100644 --- a/libraries/chain/include/graphene/chain/match_object.hpp +++ b/libraries/chain/include/graphene/chain/match_object.hpp @@ -1,6 +1,18 @@ #pragma once #include #include +#include +#include + +namespace graphene { namespace chain { +class match_object; +} } + +namespace fc { +void to_variant(const graphene::chain::match_object& match_obj, fc::variant& v, uint32_t max_depth = 1); +void from_variant(const fc::variant& v, graphene::chain::match_object& match_obj, uint32_t max_depth = 1); +} //end namespace fc + namespace graphene { namespace chain { class database; @@ -61,6 +73,21 @@ namespace graphene { namespace chain { match_state get_state() 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 match_object& match_obj ); + + template + friend Stream& operator>>( Stream& s, match_object& match_obj ); + + friend void ::fc::to_variant(const graphene::chain::match_object& match_obj, fc::variant& v, uint32_t max_depth); + friend void ::fc::from_variant(const fc::variant& v, graphene::chain::match_object& match_obj, uint32_t max_depth); + + void pack_impl(std::ostream& stream) const; + void unpack_impl(std::istream& stream); + void on_initiate_match(database& db); void on_game_complete(database& db, const game_object& game); game_id_type start_next_game(database& db, match_id_type match_id); @@ -76,6 +103,56 @@ namespace graphene { namespace chain { > match_object_multi_index_type; typedef generic_index match_index; + template + inline Stream& operator<<( Stream& s, const match_object& match_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, match_obj); + fc::raw::pack(s, match_obj.id); + fc::raw::pack(s, match_obj.tournament_id); + fc::raw::pack(s, match_obj.players); + fc::raw::pack(s, match_obj.games); + fc::raw::pack(s, match_obj.game_winners); + fc::raw::pack(s, match_obj.number_of_wins); + fc::raw::pack(s, match_obj.number_of_ties); + fc::raw::pack(s, match_obj.match_winners); + fc::raw::pack(s, match_obj.start_time); + fc::raw::pack(s, match_obj.end_time); + + // fc::raw::pack the contents hidden in the impl class + std::ostringstream stream; + match_obj.pack_impl(stream); + std::string stringified_stream(stream.str()); + fc::raw::pack(s, stream.str()); + + return s; + } + + template + inline Stream& operator>>( Stream& s, match_object& match_obj ) + { + // unpack all fields exposed in the header in the usual way + //fc::raw::unpack >(s, match_obj); + fc::raw::unpack(s, match_obj.id); + fc::raw::unpack(s, match_obj.tournament_id); + fc::raw::unpack(s, match_obj.players); + fc::raw::unpack(s, match_obj.games); + fc::raw::unpack(s, match_obj.game_winners); + fc::raw::unpack(s, match_obj.number_of_wins); + fc::raw::unpack(s, match_obj.number_of_ties); + fc::raw::unpack(s, match_obj.match_winners); + fc::raw::unpack(s, match_obj.start_time); + fc::raw::unpack(s, match_obj.end_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); + match_obj.unpack_impl(stream); + + return s; + } } } FC_REFLECT_ENUM(graphene::chain::match_state, @@ -83,16 +160,52 @@ FC_REFLECT_ENUM(graphene::chain::match_state, (match_in_progress) (match_complete)) -//FC_REFLECT_TYPENAME(graphene::chain::match_object) // manually serialized -FC_REFLECT_DERIVED(graphene::chain::match_object, (graphene::db::object), - (tournament_id) - (players) - (games) - (game_winners) - (number_of_wins) - (number_of_ties) - (match_winners) - (start_time) - (end_time)) +namespace fc { -GRAPHENE_EXTERNAL_SERIALIZATION( extern, graphene::chain::match_object ) \ No newline at end of file +template<> +template<> +inline void if_enum::from_variant(const variant &vo, graphene::chain::match_object &v, uint32_t max_depth) { + from_variant(vo, v, max_depth); +} + +template<> +template<> +inline void if_enum::to_variant(const graphene::chain::match_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::match_object &v, uint32_t) { + s << v; +} + +template<> +template<> +inline void if_enum::pack(fc::datastream &s, const graphene::chain::match_object &v, uint32_t) { + s << v; +} + +template<> +template<> +inline void if_enum::unpack(fc::datastream &s, graphene::chain::match_object &v, uint32_t) { + s >> v; +} + +} } + +template <> +struct get_typename { + static const char *name() { + return "graphene::chain::match_object"; + } +}; +template <> +struct reflector { + typedef graphene::chain::match_object type; + typedef fc::true_type is_defined; + typedef fc::false_type is_enum; +}; +} // namespace fc \ No newline at end of file diff --git a/libraries/chain/match_object.cpp b/libraries/chain/match_object.cpp index c0b75755..46313bff 100644 --- a/libraries/chain/match_object.cpp +++ b/libraries/chain/match_object.cpp @@ -28,6 +28,8 @@ #include #include +#include +#include #include #include @@ -325,6 +327,18 @@ namespace graphene { namespace chain { return state; } + void match_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 match_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 match_object::on_initiate_match(database& db) { my->state_machine.process_event(initiate_match(db)); @@ -348,4 +362,45 @@ namespace graphene { namespace chain { } } // graphene::chain +namespace fc { +// Manually reflect match_object to variant to properly reflect "state" +void to_variant(const graphene::chain::match_object& match_obj, fc::variant& v, uint32_t max_depth) +{ try { + fc_elog(fc::logger::get("tournament"), "In match_obj to_variant"); + elog("In match_obj to_variant"); + fc::mutable_variant_object o; + o("id", fc::variant(match_obj.id, max_depth)) + ("tournament_id", fc::variant(match_obj.tournament_id, max_depth)) + ("players", fc::variant(match_obj.players, max_depth)) + ("games", fc::variant(match_obj.games, max_depth)) + ("game_winners", fc::variant(match_obj.game_winners, max_depth)) + ("number_of_wins", fc::variant(match_obj.number_of_wins, max_depth)) + ("number_of_ties", fc::variant(match_obj.number_of_ties, max_depth)) + ("match_winners", fc::variant(match_obj.match_winners, max_depth)) + ("start_time", fc::variant(match_obj.start_time, max_depth)) + ("end_time", fc::variant(match_obj.end_time, max_depth)) + ("state", fc::variant(match_obj.get_state(), max_depth)); + + v = o; + } FC_RETHROW_EXCEPTIONS(warn, "") } + +// Manually reflect match_object to variant to properly reflect "state" +void from_variant(const fc::variant& v, graphene::chain::match_object& match_obj, uint32_t max_depth) +{ try { + fc_elog(fc::logger::get("tournament"), "In match_obj from_variant"); + match_obj.id = v["id"].as( max_depth ); + match_obj.tournament_id = v["tournament_id"].as( max_depth ); + match_obj.players = v["players"].as >( max_depth ); + match_obj.games = v["games"].as >( max_depth ); + match_obj.game_winners = v["game_winners"].as > >( max_depth ); + match_obj.number_of_wins = v["number_of_wins"].as >( max_depth ); + match_obj.number_of_ties = v["number_of_ties"].as( max_depth ); + match_obj.match_winners = v["match_winners"].as >( max_depth ); + match_obj.start_time = v["start_time"].as( max_depth ); + match_obj.end_time = v["end_time"].as >( max_depth ); + graphene::chain::match_state state = v["state"].as( max_depth ); + const_cast(match_obj.my->state_machine.current_state())[0] = (int)state; + } FC_RETHROW_EXCEPTIONS(warn, "") } +} //end namespace fc +