From c4ad9000266d0cf2dd3c998ccdfab13543c08783 Mon Sep 17 00:00:00 2001 From: Eric Frias Date: Tue, 18 Oct 2016 15:56:10 -0400 Subject: [PATCH] Fix build error in member_enumerator when an operation includes an enum. Implement the commit/reveal timeouts in RPS games, and generate automatic ("insurance") moves. Make the CLI wallet watch for new games in tournaments you're registered for. --- libraries/chain/db_management.cpp | 3 +- libraries/chain/db_update.cpp | 26 +++ libraries/chain/game_object.cpp | 195 +++++++++++++++--- .../chain/include/graphene/chain/database.hpp | 5 + .../include/graphene/chain/game_object.hpp | 15 +- .../graphene/chain/tournament_object.hpp | 2 +- libraries/chain/match_object.cpp | 2 +- libraries/chain/tournament_object.cpp | 51 +++-- libraries/wallet/wallet.cpp | 3 +- programs/build_helpers/member_enumerator.cpp | 43 +++- programs/js_operation_serializer/main.cpp | 55 +++-- 11 files changed, 323 insertions(+), 77 deletions(-) diff --git a/libraries/chain/db_management.cpp b/libraries/chain/db_management.cpp index e27d03c4..6371c61f 100644 --- a/libraries/chain/db_management.cpp +++ b/libraries/chain/db_management.cpp @@ -35,7 +35,8 @@ namespace graphene { namespace chain { -database::database() +database::database() : + _random_number_generator(fc::ripemd160().data()) { initialize_indexes(); initialize_evaluators(); diff --git a/libraries/chain/db_update.cpp b/libraries/chain/db_update.cpp index 4dbf531f..fbbecb4a 100644 --- a/libraries/chain/db_update.cpp +++ b/libraries/chain/db_update.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include @@ -70,6 +71,8 @@ void database::update_global_dynamic_data( const signed_block& b ) fc::raw::pack( enc, b.previous_secret ); dgp.random = enc.result(); + _random_number_generator = fc::hash_ctr_rng(dgp.random.data()); + if( BOOST_UNLIKELY( b.block_num() == 1 ) ) dgp.recently_missed_count = 0; else if( _checkpoints.size() && _checkpoints.rbegin()->first >= b.block_num() ) @@ -476,6 +479,11 @@ void database::update_withdraw_permissions() remove(*permit_index.begin()); } +uint64_t database::get_random_bits( uint64_t bound ) +{ + return _random_number_generator(bound); +} + void process_finished_games(database& db) { //auto& games_index = db.get_index_type().indices().get(); @@ -546,6 +554,24 @@ void initiate_next_round_of_matches(database& db) void initiate_next_games(database& db) { + // Next, trigger timeouts on any games which have been waiting too long for commit or + // reveal moves + auto& next_timeout_index = db.get_index_type().indices().get(); + while (1) + { + // empty time_points are sorted to the beginning, so upper_bound takes us to the first + // non-empty time_point + auto start_iter = next_timeout_index.upper_bound(boost::make_tuple(optional())); + if (start_iter != next_timeout_index.end() && + *start_iter->next_timeout <= db.head_block_time()) + { + db.modify(*start_iter, [&](game_object& game) { + game.on_timeout(db); + }); + } + else + break; + } } void database::update_tournaments() diff --git a/libraries/chain/game_object.cpp b/libraries/chain/game_object.cpp index 4031890f..bc205f20 100644 --- a/libraries/chain/game_object.cpp +++ b/libraries/chain/game_object.cpp @@ -33,6 +33,8 @@ #include #include +#include + namespace graphene { namespace chain { namespace msm = boost::msm; @@ -59,6 +61,14 @@ namespace graphene { namespace chain { {} }; + struct timeout + { + database& db; + timeout(database& db) : + db(db) + {} + }; + struct game_state_machine_ : public msm::front::state_machine_def { // disable a few state machine features we don't use for performance @@ -69,6 +79,13 @@ namespace graphene { namespace chain { struct waiting_for_game_to_start : public msm::front::state<> {}; struct expecting_commit_moves : public msm::front::state<> { + void set_next_timeout(database& db, game_object& game) + { + const match_object& match_obj = game.match_id(db); + const tournament_object& tournament_obj = match_obj.tournament_id(db); + const rock_paper_scissors_game_options& game_options = tournament_obj.options.game_options.get(); + game.next_timeout = db.head_block_time() + game_options.time_per_commit_move; + } void on_entry(const initiate_game& event, game_state_machine_& fsm) { game_object& game = *fsm.game_obj; @@ -80,6 +97,7 @@ namespace graphene { namespace chain { "game ${id} is associtated with match ${match_id}", ("id", game.id) ("match_id", game.match_id)); + set_next_timeout(event.db, game); } void on_entry(const game_move& event, game_state_machine_& fsm) { @@ -88,10 +106,26 @@ namespace graphene { namespace chain { fc_ilog(fc::logger::get("tournament"), "game ${id} received a commit move, still expecting another commit move", ("id", game.id)); + set_next_timeout(event.db, game); } }; struct expecting_reveal_moves : public msm::front::state<> { + void set_next_timeout(database& db, game_object& game) + { + const match_object& match_obj = game.match_id(db); + const tournament_object& tournament_obj = match_obj.tournament_id(db); + const rock_paper_scissors_game_options& game_options = tournament_obj.options.game_options.get(); + game.next_timeout = db.head_block_time() + game_options.time_per_reveal_move; + } + void on_entry(const timeout& event, game_state_machine_& fsm) + { + game_object& game = *fsm.game_obj; + fc_ilog(fc::logger::get("tournament"), + "game ${id} timed out waiting for commit moves, now expecting reveal move", + ("id", game.id)); + set_next_timeout(event.db, game); + } void on_entry(const game_move& event, game_state_machine_& fsm) { game_object& game = *fsm.game_obj; @@ -104,10 +138,31 @@ namespace graphene { namespace chain { fc_ilog(fc::logger::get("tournament"), "game ${id} received a reveal move, still expecting reveal moves", ("id", game.id)); + set_next_timeout(event.db, game); } }; + struct game_complete : public msm::front::state<> { + void clear_next_timeout(database& db, game_object& game) + { + const match_object& match_obj = game.match_id(db); + const tournament_object& tournament_obj = match_obj.tournament_id(db); + const rock_paper_scissors_game_options& game_options = tournament_obj.options.game_options.get(); + game.next_timeout = fc::optional(); + } + void on_entry(const timeout& event, game_state_machine_& fsm) + { + game_object& game = *fsm.game_obj; + fc_ilog(fc::logger::get("tournament"), + "timed out waiting for commits or reveals, game ${id} is complete", + ("id", game.id)); + + game.make_automatic_moves(event.db); + game.determine_winner(event.db); + clear_next_timeout(event.db, game); + } + void on_entry(const game_move& event, game_state_machine_& fsm) { game_object& game = *fsm.game_obj; @@ -115,31 +170,10 @@ namespace graphene { namespace chain { "received a reveal move, game ${id} is complete", ("id", fsm.game_obj->id)); - // we now know who played what, figure out if we have a winner - const rock_paper_scissors_game_details& game_details = game.game_details.get(); - if (game_details.reveal_moves[0]->gesture == game_details.reveal_moves[1]->gesture) - ilog("The game was a tie, both players threw ${gesture}", ("gesture", game_details.reveal_moves[0]->gesture)); - else - { - const match_object& match_obj = game.match_id(event.db); - const tournament_object& tournament_obj = match_obj.tournament_id(event.db); - const rock_paper_scissors_game_options& game_options = tournament_obj.options.game_options.get(); - - unsigned winner = ((((int)game_details.reveal_moves[0]->gesture - - (int)game_details.reveal_moves[1]->gesture + - game_options.number_of_gestures) % game_options.number_of_gestures) + 1) % 2; - ilog("${gesture1} vs ${gesture2}, ${winner} wins", - ("gesture1", game_details.reveal_moves[1]->gesture) - ("gesture2", game_details.reveal_moves[0]->gesture) - ("winner", game_details.reveal_moves[winner]->gesture)); - game.winners.insert(game.players[winner]); - } - - - const match_object& match_obj = game.match_id(event.db); - event.db.modify(match_obj, [&](match_object& match) { - match.on_game_complete(event.db, game); - }); + // if one player didn't commit a move we might need to make their "insurance" move now + game.make_automatic_moves(event.db); + game.determine_winner(event.db); + clear_next_timeout(event.db, game); } }; typedef waiting_for_game_to_start initial_state; @@ -158,15 +192,23 @@ namespace graphene { namespace chain { return game_details.commit_moves.at(other_player_index).valid(); } - bool already_have_other_reveal(const game_move& event) + bool now_have_reveals_for_all_commits(const game_move& event) { auto iter = std::find(game_obj->players.begin(), game_obj->players.end(), event.move.player_account_id); - unsigned player_index = std::distance(game_obj->players.begin(), iter); - // hard-coded here for two-player games - unsigned other_player_index = player_index == 0 ? 1 : 0; + unsigned this_reveal_index = std::distance(game_obj->players.begin(), iter); + const rock_paper_scissors_game_details& game_details = game_obj->game_details.get(); - return game_details.reveal_moves.at(other_player_index).valid(); + for (unsigned i = 0; i < game_details.commit_moves.size(); ++i) + if (!game_details.reveal_moves[i] && i != this_reveal_index) + return false; + return true; + } + + bool have_at_least_one_commit_move(const timeout& event) + { + const rock_paper_scissors_game_details& game_details = game_obj->game_details.get(); + return game_details.commit_moves[0] || game_details.commit_moves[1]; } void apply_commit_move(const game_move& event) @@ -203,9 +245,12 @@ namespace graphene { namespace chain { // +-------------------------------+-------------------------+----------------------------+---------------------+----------------------+ a_row < expecting_commit_moves, game_move, expecting_commit_moves, &x::apply_commit_move >, row < expecting_commit_moves, game_move, expecting_reveal_moves, &x::apply_commit_move, &x::already_have_other_commit >, + _row < expecting_commit_moves, timeout, game_complete >, + g_row < expecting_commit_moves, timeout, expecting_reveal_moves, &x::have_at_least_one_commit_move >, // +-------------------------------+-------------------------+----------------------------+---------------------+----------------------+ + _row < expecting_reveal_moves, timeout, game_complete >, a_row < expecting_reveal_moves, game_move, expecting_reveal_moves, &x::apply_reveal_move >, - row < expecting_reveal_moves, game_move, game_complete, &x::apply_reveal_move, &x::already_have_other_reveal > + row < expecting_reveal_moves, game_move, game_complete, &x::apply_reveal_move, &x::now_have_reveals_for_all_commits > // +-------------------------------+-------------------------+----------------------------+---------------------+----------------------+ //a_row < game_in_progress, game_complete, game_in_progress, &x::start_next_game >, //g_row < game_in_progress, game_complete, game_complete, &x::was_final_game > @@ -237,6 +282,7 @@ namespace graphene { namespace chain { players(rhs.players), winners(rhs.winners), game_details(rhs.game_details), + next_timeout(rhs.next_timeout), my(new impl(this)) { my->state_machine = rhs.my->state_machine; @@ -251,6 +297,7 @@ namespace graphene { namespace chain { players = rhs.players; winners = rhs.winners; game_details = rhs.game_details; + next_timeout = rhs.next_timeout; my->state_machine = rhs.my->state_machine; my->state_machine.game_obj = this; @@ -384,11 +431,95 @@ namespace graphene { namespace chain { FC_THROW("Game of type ${type} not supported", ("type", game_details.which())); } + void game_object::make_automatic_moves(database& db) + { + rock_paper_scissors_game_details& rps_game_details = game_details.get(); + + unsigned players_without_commit_moves = 0; + bool no_player_has_reveal_move = true; + for (unsigned i = 0; i < 2; ++i) + { + if (!rps_game_details.commit_moves[i]) + ++players_without_commit_moves; + if (rps_game_details.reveal_moves[i]) + no_player_has_reveal_move = false; + } + + if (players_without_commit_moves || no_player_has_reveal_move) + { + const match_object& match_obj = match_id(db); + const tournament_object& tournament_obj = match_obj.tournament_id(db); + const rock_paper_scissors_game_options& game_options = tournament_obj.options.game_options.get(); + for (unsigned i = 0; i < 2; ++i) + { + if (!rps_game_details.commit_moves[i] || + no_player_has_reveal_move) + { + struct rock_paper_scissors_throw_reveal reveal; + reveal.nonce2 = 0; + reveal.gesture = (rock_paper_scissors_gesture)db.get_random_bits(game_options.number_of_gestures); + rps_game_details.reveal_moves[i] = reveal; + ilog("Player ${player} failed to commit a move, generating a random move for them: ${gesture}", + ("player", i)("gesture", reveal.gesture)); + } + } + } + } + + void game_object::determine_winner(database& db) + { + // we now know who played what, figure out if we have a winner + const rock_paper_scissors_game_details& rps_game_details = game_details.get(); + if (rps_game_details.reveal_moves[0]->gesture == rps_game_details.reveal_moves[1]->gesture) + ilog("The game was a tie, both players threw ${gesture}", ("gesture", rps_game_details.reveal_moves[0]->gesture)); + else + { + const match_object& match_obj = match_id(db); + const tournament_object& tournament_obj = match_obj.tournament_id(db); + const rock_paper_scissors_game_options& game_options = tournament_obj.options.game_options.get(); + + if (rps_game_details.reveal_moves[0] && rps_game_details.reveal_moves[1]) + { + unsigned winner = ((((int)rps_game_details.reveal_moves[0]->gesture - + (int)rps_game_details.reveal_moves[1]->gesture + + game_options.number_of_gestures) % game_options.number_of_gestures) + 1) % 2; + ilog("${gesture1} vs ${gesture2}, ${winner} wins", + ("gesture1", rps_game_details.reveal_moves[1]->gesture) + ("gesture2", rps_game_details.reveal_moves[0]->gesture) + ("winner", rps_game_details.reveal_moves[winner]->gesture)); + winners.insert(players[winner]); + } + else if (rps_game_details.reveal_moves[0]) + { + ilog("Player 1 didn't commit or reveal their move, player 0 wins"); + winners.insert(players[0]); + } + else if (rps_game_details.reveal_moves[1]) + { + ilog("Player 0 didn't commit or reveal their move, player 1 wins"); + winners.insert(players[1]); + } + else if (rps_game_details.reveal_moves[1]) + ilog("Neither player made a move, both players lose"); + } + + + const match_object& match_obj = match_id(db); + db.modify(match_obj, [&](match_object& match) { + match.on_game_complete(db, *this); + }); + } + void game_object::on_move(database& db, const game_move_operation& op) { my->state_machine.process_event(game_move(db, op)); } + void game_object::on_timeout(database& db) + { + my->state_machine.process_event(timeout(db)); + } + void game_object::start_game(database& db, const std::vector& players) { my->state_machine.process_event(initiate_game(db, players)); @@ -420,6 +551,7 @@ namespace fc { ("players", game_obj.players) ("winners", game_obj.winners) ("game_details", game_obj.game_details) + ("next_timeout", game_obj.next_timeout) ("state", game_obj.get_state()); v = o; @@ -434,6 +566,7 @@ namespace fc { game_obj.players = v["players"].as >(); game_obj.winners = v["winners"].as >(); game_obj.game_details = v["game_details"].as(); + game_obj.next_timeout = v["next_timeout"].as >(); graphene::chain::game_state state = v["state"].as(); const_cast(game_obj.my->state_machine.current_state())[0] = (int)state; } diff --git a/libraries/chain/include/graphene/chain/database.hpp b/libraries/chain/include/graphene/chain/database.hpp index 36f7f622..0f8102a0 100644 --- a/libraries/chain/include/graphene/chain/database.hpp +++ b/libraries/chain/include/graphene/chain/database.hpp @@ -36,6 +36,8 @@ #include #include +#include + #include #include @@ -250,6 +252,8 @@ namespace graphene { namespace chain { const node_property_object& get_node_properties()const; const fee_schedule& current_fee_schedule()const; + uint64_t get_random_bits( uint64_t bound ); + time_point_sec head_block_time()const; uint32_t head_block_num()const; block_id_type head_block_id()const; @@ -488,6 +492,7 @@ namespace graphene { namespace chain { flat_map _checkpoints; node_property_object _node_property_object; + fc::hash_ctr_rng _random_number_generator; }; namespace detail diff --git a/libraries/chain/include/graphene/chain/game_object.hpp b/libraries/chain/include/graphene/chain/game_object.hpp index 6f8ba82f..a6c3232f 100644 --- a/libraries/chain/include/graphene/chain/game_object.hpp +++ b/libraries/chain/include/graphene/chain/game_object.hpp @@ -41,6 +41,8 @@ namespace graphene { namespace chain { flat_set winners; game_specific_details game_details; + + fc::optional next_timeout; game_state get_state() const; @@ -50,7 +52,11 @@ namespace graphene { namespace chain { game_object& operator=(const game_object& rhs); void evaluate_move_operation(const database& db, const game_move_operation& op) const; + void make_automatic_moves(database& db); + void determine_winner(database& db); + void on_move(database& db, const game_move_operation& op); + void on_timeout(database& db); void start_game(database& db, const std::vector& players); // serialization functions: @@ -72,10 +78,15 @@ namespace graphene { namespace chain { std::unique_ptr my; }; + struct by_next_timeout {}; typedef multi_index_container< game_object, indexed_by< - ordered_unique< tag, member< object, object_id_type, &object::id > > > + ordered_unique< tag, member< object, object_id_type, &object::id > >, + ordered_unique< tag, + composite_key, &game_object::next_timeout>, + member > > > > game_object_multi_index_type; typedef generic_index game_index; @@ -90,6 +101,7 @@ namespace graphene { namespace chain { fc::raw::pack(s, game_obj.players); fc::raw::pack(s, game_obj.winners); fc::raw::pack(s, game_obj.game_details); + fc::raw::pack(s, game_obj.next_timeout); // fc::raw::pack the contents hidden in the impl class std::ostringstream stream; @@ -110,6 +122,7 @@ namespace graphene { namespace chain { fc::raw::unpack(s, game_obj.players); fc::raw::unpack(s, game_obj.winners); fc::raw::unpack(s, game_obj.game_details); + fc::raw::unpack(s, game_obj.next_timeout); // fc::raw::unpack the contents hidden in the impl class std::string stringified_stream; diff --git a/libraries/chain/include/graphene/chain/tournament_object.hpp b/libraries/chain/include/graphene/chain/tournament_object.hpp index f8a04a97..df144f7d 100644 --- a/libraries/chain/include/graphene/chain/tournament_object.hpp +++ b/libraries/chain/include/graphene/chain/tournament_object.hpp @@ -111,7 +111,7 @@ namespace graphene { namespace chain { void on_registration_deadline_passed(database& db); void on_player_registered(database& db, account_id_type payer_id, account_id_type player_id); void on_start_time_arrived(database& db); - void on_final_game_completed(); + void on_match_completed(database& db, const match_object& match); void check_for_new_matches_to_start(database& db) const; private: diff --git a/libraries/chain/match_object.cpp b/libraries/chain/match_object.cpp index 5627965c..8c39c1b3 100644 --- a/libraries/chain/match_object.cpp +++ b/libraries/chain/match_object.cpp @@ -98,7 +98,7 @@ namespace graphene { namespace chain { const tournament_object& tournament_obj = match.tournament_id(event.db); event.db.modify(tournament_obj, [&](tournament_object& tournament) { - tournament.on_final_game_completed(); + tournament.on_match_completed(event.db, match); }); } diff --git a/libraries/chain/tournament_object.cpp b/libraries/chain/tournament_object.cpp index 0ee5be92..afd965a8 100644 --- a/libraries/chain/tournament_object.cpp +++ b/libraries/chain/tournament_object.cpp @@ -31,8 +31,6 @@ #include #include -#include - namespace graphene { namespace chain { namespace msm = boost::msm; @@ -60,7 +58,13 @@ namespace graphene { namespace chain { database& db; start_time_arrived(database& db) : db(db) {}; }; - struct final_game_completed {}; + + struct match_completed + { + database& db; + const match_object& match; + match_completed(database& db, const match_object& match) : db(db), match(match) {} + }; struct tournament_state_machine_ : public msm::front::state_machine_def { @@ -120,9 +124,6 @@ namespace graphene { namespace chain { ("id", fsm.tournament_obj->id)); const tournament_details_object& tournament_details_obj = fsm.tournament_obj->tournament_details_id(event.db); - // TODO hoist the rng to reset once per block? - fc::hash_ctr_rng rng(event.db.get_dynamic_global_properties().random.data()); - // Create the "seeding" order for the tournament as a random shuffle of the players. // // If this were a game of skill where players were ranked, this algorithm expects the @@ -131,7 +132,7 @@ namespace graphene { namespace chain { tournament_details_obj.registered_players.end()); for (unsigned i = seeded_players.size() - 1; i >= 1; --i) { - unsigned j = (unsigned)rng(i + 1); + unsigned j = (unsigned)event.db.get_random_bits(i + 1); std::swap(seeded_players[i], seeded_players[j]); } @@ -178,6 +179,12 @@ namespace graphene { namespace chain { tournament_details_obj.matches = matches; }); } + void on_entry(const match_completed& event, tournament_state_machine_& fsm) + { + fc_ilog(fc::logger::get("tournament"), + "Tournament ${id} is still in progress, maybe should start a new match here", + ("id", fsm.tournament_obj->id)); + } }; struct registration_period_expired : public msm::front::state<> { @@ -199,7 +206,17 @@ namespace graphene { namespace chain { } } }; - struct concluded : public msm::front::state<>{}; + + struct concluded : public msm::front::state<> + { + void on_entry(const match_completed& event, tournament_state_machine_& fsm) + { + fc_ilog(fc::logger::get("tournament"), + "Tournament ${id} is complete", + ("id", fsm.tournament_obj->id)); + } + }; + typedef accepting_registrations initial_state; @@ -214,6 +231,15 @@ namespace graphene { namespace chain { return tournament_obj->registered_players == tournament_obj->options.number_of_players - 1; } + bool was_final_match(const match_completed& event) + { + const tournament_details_object& tournament_details_obj = tournament_obj->tournament_details_id(event.db); + fc_ilog(fc::logger::get("tournament"), + "In was_final_match guard, returning ${value}", + ("value", event.match.id == tournament_details_obj.matches[tournament_details_obj.matches.size()])); + return event.match.id == tournament_details_obj.matches[tournament_details_obj.matches.size() - 1]; + } + void register_player(const player_registered& event) { fc_ilog(fc::logger::get("tournament"), @@ -235,12 +261,13 @@ namespace graphene { namespace chain { // Start Event Next Action Guard // +---------------------------+-----------------------------+----------------------------+---------------------+----------------------+ a_row < accepting_registrations, player_registered, accepting_registrations, &x::register_player >, - row < accepting_registrations, player_registered, awaiting_start, &x::register_player, &x::will_be_fully_registered >, + row < accepting_registrations, player_registered, awaiting_start, &x::register_player, &x::will_be_fully_registered >, _row < accepting_registrations, registration_deadline_passed, registration_period_expired >, // +---------------------------+-----------------------------+----------------------------+---------------------+----------------------+ _row < awaiting_start, start_time_arrived, in_progress >, // +---------------------------+-----------------------------+----------------------------+---------------------+----------------------+ - _row < in_progress, final_game_completed, concluded > + _row < in_progress, match_completed, in_progress >, + g_row < in_progress, match_completed, concluded, &x::was_final_match > // +---------------------------+-----------------------------+----------------------------+---------------------+----------------------+ > {}; @@ -366,9 +393,9 @@ namespace graphene { namespace chain { my->state_machine.process_event(start_time_arrived(db)); } - void tournament_object::on_final_game_completed() + void tournament_object::on_match_completed(database& db, const match_object& match) { - my->state_machine.process_event(final_game_completed()); + my->state_machine.process_event(match_completed(db, match)); } void tournament_object::check_for_new_matches_to_start(database& db) const diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index 604a99f7..0afe527b 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -406,7 +406,8 @@ private: if (match_cache_iter != match_cache.end()) { const match_object& cached_match_obj = *match_cache_iter; - if (cached_match_obj.get_state() != current_match_obj.get_state()) + if (cached_match_obj.get_state() != current_match_obj.get_state() || + cached_match_obj.games.size() != current_match_obj.games.size()) { ilog("match ${id} changed state from ${old} to ${new}", ("id", id) diff --git a/programs/build_helpers/member_enumerator.cpp b/programs/build_helpers/member_enumerator.cpp index 001b47bd..8ad26633 100644 --- a/programs/build_helpers/member_enumerator.cpp +++ b/programs/build_helpers/member_enumerator.cpp @@ -110,6 +110,37 @@ void class_processor::process_class( const static_variant< T... >* dummy ) } } +template +struct if_enum +{ + template< typename T > + static void process_class( class_processor* proc, const T* dummy ) + { + std::string tname = fc::get_typename::name(); + if( proc->result.find( tname ) != proc->result.end() ) + return; + ilog( "processing class ${c}", ("c", tname) ); + // need this to keep from recursing on same class + proc->result.emplace( tname, std::vector< std::string >() ); + + member_visitor vtor( proc ); + fc::reflector::visit( vtor ); + ilog( "members of class ${c} are ${m}", ("c", tname)("m", vtor.members) ); + proc->result[tname] = vtor.members; + } +}; + +template<> +struct if_enum +{ + template< typename T > + static void process_class( class_processor* proc, const T* dummy ) + { + std::string tname = fc::get_typename::name(); + std::cerr << "skipping reflected enum " << tname << std::endl; + } +}; + template struct if_reflected { @@ -127,17 +158,7 @@ struct if_reflected template< typename T > static void process_class( class_processor* proc, const T* dummy ) { - std::string tname = fc::get_typename::name(); - if( proc->result.find( tname ) != proc->result.end() ) - return; - ilog( "processing class ${c}", ("c", tname) ); - // need this to keep from recursing on same class - proc->result.emplace( tname, std::vector< std::string >() ); - - member_visitor vtor( proc ); - fc::reflector::visit( vtor ); - ilog( "members of class ${c} are ${m}", ("c", tname)("m", vtor.members) ); - proc->result[tname] = vtor.members; + if_enum< typename fc::reflector::is_enum >::process_class(proc, dummy); } }; diff --git a/programs/js_operation_serializer/main.cpp b/programs/js_operation_serializer/main.cpp index 24c18c93..6c60d943 100644 --- a/programs/js_operation_serializer/main.cpp +++ b/programs/js_operation_serializer/main.cpp @@ -37,6 +37,8 @@ #include #include #include +#include +#include #include #include @@ -344,32 +346,49 @@ class register_member_visitor } }; +template +struct serializer_init_helper { + static void init() + { + auto name = js_name::name(); + if( st.find(name) == st.end() ) + { + fc::reflector::visit( register_member_visitor() ); + register_serializer( name, [=](){ generate(); } ); + } + } + static void generate() + { + auto name = remove_namespace( js_name::name() ); + if( name == "int64" ) return; + std::cout << "" << name + << " = new Serializer( \n" + << " \"" + name + "\"\n"; + + fc::reflector::visit( serialize_member_visitor() ); + + std::cout <<")\n\n"; + } +}; + +template +struct serializer_init_helper { + static void init() + { + } +}; + + template struct serializer { static_assert( fc::reflector::is_defined::value == reflected, "invalid template arguments" ); + static void init() { - auto name = js_name::name(); - if( st.find(name) == st.end() ) - { - fc::reflector::visit( register_member_visitor() ); - register_serializer( name, [=](){ generate(); } ); - } + serializer_init_helper< T, typename fc::reflector::is_enum >::init(); } - static void generate() - { - auto name = remove_namespace( js_name::name() ); - if( name == "int64" ) return; - std::cout << "" << name - << " = new Serializer( \n" - << " \"" + name + "\"\n"; - - fc::reflector::visit( serialize_member_visitor() ); - - std::cout <<")\n\n"; - } }; } // namespace detail_ns