diff --git a/libraries/db/CMakeLists.txt b/libraries/db/CMakeLists.txt index 82ab91b8..986fe9cb 100644 --- a/libraries/db/CMakeLists.txt +++ b/libraries/db/CMakeLists.txt @@ -1,5 +1,5 @@ file(GLOB HEADERS "include/graphene/db/*.hpp") -add_library( graphene_db undo_database.cpp index.cpp object_database.cpp type_serializer.cpp ${HEADERS} ) +add_library( graphene_db undo_database.cpp index.cpp object_database.cpp ${HEADERS} ) target_link_libraries( graphene_db fc ) target_include_directories( graphene_db PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" ) diff --git a/libraries/db/include/graphene/db/index.hpp b/libraries/db/include/graphene/db/index.hpp index 06c9fe61..6c1c975e 100644 --- a/libraries/db/include/graphene/db/index.hpp +++ b/libraries/db/include/graphene/db/index.hpp @@ -23,7 +23,6 @@ */ #pragma once #include -#include #include #include #include diff --git a/libraries/db/include/graphene/db/type_serializer.hpp b/libraries/db/include/graphene/db/type_serializer.hpp deleted file mode 100644 index 87dbb968..00000000 --- a/libraries/db/include/graphene/db/type_serializer.hpp +++ /dev/null @@ -1,306 +0,0 @@ -/* - * Copyright (c) 2015 Cryptonomex, Inc., and contributors. - * - * The MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -#include -#include -#include -#include -#include - -namespace graphene { namespace db { -using std::set; -using std::map; -using fc::time_point_sec; -using fc::time_point; -using std::string; - -std::set& processed_types(); -std::stringstream& current_stream( std::unique_ptr&& s = std::unique_ptr() ); -string remove_tail_if( const string& str, char c, const string& match ); -string remove_namespace_if( const string& str, const string& match ); -string remove_namespace( string str ); - -template -void generate_serializer(); -template -void register_serializer(); - -extern map st; -extern vector> serializers; - -bool register_serializer( const string& name, std::function sr ); - -template struct js_name { static std::string name(){ return remove_namespace(fc::get_typename::name()); }; }; - -template -struct js_name> -{ - static std::string name(){ return "fixed_array "+ fc::to_string(N) + ", " + remove_namespace(fc::get_typename::name()); }; -}; -template struct js_name> { static std::string name(){ return "bytes "+ fc::to_string(N); }; }; -template struct js_name> { static std::string name(){ return "bytes "+ fc::to_string(N); }; }; -template struct js_name< fc::optional > { static std::string name(){ return "optional " + js_name::name(); } }; -template<> struct js_name< object_id_type > { static std::string name(){ return "object_id_type"; } }; -template struct js_name< fc::flat_set > { static std::string name(){ return "set " + js_name::name(); } }; -template struct js_name< std::vector > { static std::string name(){ return "array " + js_name::name(); } }; -template struct js_name< fc::safe > { static std::string name(){ return js_name::name(); } }; - - -template<> struct js_name< std::vector > { static std::string name(){ return "bytes()"; } }; -//template<> struct js_name< op_wrapper > { static std::string name(){ return "operation "; } }; -template<> struct js_name { static std::string name(){ return "bytes 20"; } }; -template<> struct js_name { static std::string name(){ return "bytes 28"; } }; -template<> struct js_name { static std::string name(){ return "varuint32"; } }; -template<> struct js_name { static std::string name(){ return "varint32"; } }; -template<> struct js_name< time_point_sec > { static std::string name(){ return "time_point_sec"; } }; - -template -struct js_name > -{ - static std::string name(){ - return "protocol_id_type \"" + remove_namespace(fc::get_typename::name()) + "\""; - }; -}; - - -template struct js_name< std::set > { static std::string name(){ return "set " + js_name::name(); } }; - -template -struct js_name< std::map > { static std::string name(){ return "map (" + js_name::name() + "), (" + js_name::name() +")"; } }; - -template -struct js_name< fc::flat_map > { static std::string name(){ return "map (" + js_name::name() + "), (" + js_name::name() +")"; } }; - - -template struct js_sv_name; - -template struct js_sv_name -{ static std::string name(){ return "\n " + js_name::name(); } }; - -template -struct js_sv_name { static std::string name(){ return "\n " + js_name::name() +" " + js_sv_name::name(); } }; - - -template -struct js_name< fc::static_variant > -{ - static std::string name( std::string n = ""){ - static const std::string name = n; - if( name == "" ) - return "static_variant [" + js_sv_name::name() + "\n]"; - else return name; - } -}; - - -template::is_defined::value> -struct serializer; - - -struct register_type_visitor -{ - typedef void result_type; - - template - result_type operator()( const Type& op )const { serializer::init(); } -}; - -class register_member_visitor; - -struct serialize_type_visitor -{ - typedef void result_type; - - fc::mutable_variant_object& obj; - int t = 0; - serialize_type_visitor( fc::mutable_variant_object& o, int _t ):obj(o),t(_t){} - - template - result_type operator()( const Type& op )const - { - obj(remove_namespace( fc::get_typename::name()),t); - } -}; - - -class serialize_member_visitor -{ - public: - template - void operator()( const char* name )const - { - current_stream() << " " << name << " : " << fc::get_typename::name() <<"\n"; - } -}; - -template -struct serializer -{ - static_assert( fc::reflector::is_defined::value == false, "invalid template arguments" ); - static void init() - {} - - static void generate() - {} -}; - -template -struct serializer,false> -{ - static void init() { serializer::init(); } - static void generate() {} -}; -template -struct serializer,false> -{ - static void init() { serializer::init(); } - static void generate() {} -}; -/* -template<> -struct serializer,false> -{ - static void init() { } - static void generate() {} -}; -*/ - -template<> -struct serializer -{ - static void init() {} - - static void generate() {} -}; -template<> -struct serializer -{ - static void init() {} - static void generate() {} -}; -#ifdef __APPLE__ -// on mac, size_t is a distinct type from uint64_t or uint32_t and needs a separate specialization -template<> struct serializer { static void init() {} static void generate() {} }; -#endif -template<> struct serializer { static void init() {} static void generate() {} }; -template<> struct serializer { static void init() {} static void generate() {} }; - -template -struct serializer,false> -{ - static void init() { serializer::init(); } - static void generate(){} -}; - -template -struct serializer< graphene::db::object_id ,true> -{ - static void init() {} - static void generate() {} -}; - -template -struct serializer< fc::static_variant, false > -{ - static void init() - { - auto name = js_name>::name(); - if( processed_types().find( name ) == processed_types().end() ) - { - processed_types().insert( name ); - fc::static_variant var; - for( int i = 0; i < var.count(); ++i ) - { - var.set_which(i); - var.visit( register_type_visitor() ); - } - register_serializer( js_name>::name(), [=](){ generate(); } ); - } - } - - static void generate() - { - current_stream() << js_name>::name() << " = static_variant [" + js_sv_name::name() + "\n]\n\n"; - } -}; - -class register_member_visitor -{ - public: - template - void operator()( const char* name )const - { - serializer::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(); } ); - } - } - - static void generate() - { - auto name = remove_namespace( js_name::name() ); - if( name == "int64" ) return; - current_stream() << "" << name - << " = new Serializer( \n" - << " \"" + name + "\"\n"; - - fc::reflector::visit( serialize_member_visitor() ); - - current_stream() <<")\n\n"; - } -}; - - -template -std::string get_type_description() -{ - current_stream( std::unique_ptr(new std::stringstream()) ); - processed_types().clear(); - serializer::init(); - for( const auto& gen : serializers ) - gen(); - return current_stream().str(); -} - - -} } // graphene::db - - - - - - - - diff --git a/libraries/db/type_serializer.cpp b/libraries/db/type_serializer.cpp deleted file mode 100644 index 167528d4..00000000 --- a/libraries/db/type_serializer.cpp +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright (c) 2015 Cryptonomex, Inc., and contributors. - * - * The MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -#include - -namespace graphene { namespace db { -map st; -vector> serializers; - -std::set& processed_types() -{ - static std::set p; - return p; -} - -std::stringstream& current_stream( std::unique_ptr&& s ) -{ - static std::unique_ptr stream; - if( s ) stream = std::move(s); - FC_ASSERT( stream ); - return *stream; -} - -string remove_tail_if( const string& str, char c, const string& match ) -{ - auto last = str.find_last_of( c ); - if( last != std::string::npos ) - if( str.substr( last + 1 ) == match ) - return str.substr( 0, last ); - return str; -} -string remove_namespace_if( const string& str, const string& match ) -{ - auto last = str.find( match ); - if( last != std::string::npos ) - return str.substr( match.size()+2 ); - return str; -} - -string remove_namespace( string str ) -{ - str = remove_tail_if( str, '_', "operation" ); - str = remove_tail_if( str, '_', "t" ); - str = remove_tail_if( str, '_', "object" ); - str = remove_tail_if( str, '_', "type" ); - str = remove_namespace_if( str, "graphene::chain" ); - str = remove_namespace_if( str, "graphene::db" ); - str = remove_namespace_if( str, "std" ); - str = remove_namespace_if( str, "fc" ); - auto pos = str.find( ":" ); - if( pos != str.npos ) - str.replace( pos, 2, "_" ); - return str; -} -bool register_serializer( const string& name, std::function sr ) -{ - if( st.find(name) == st.end() ) - { - serializers.push_back( sr ); - st[name] = serializers.size() - 1; - return true; - } - return false; -} - -} }// graphene diff --git a/libraries/wallet/include/graphene/wallet/reflect_util.hpp b/libraries/wallet/include/graphene/wallet/reflect_util.hpp index 2d0b77ac..497303c5 100644 --- a/libraries/wallet/include/graphene/wallet/reflect_util.hpp +++ b/libraries/wallet/include/graphene/wallet/reflect_util.hpp @@ -37,6 +37,22 @@ struct static_variant_map namespace impl { +std::string clean_name( const std::string& name ) +{ + std::string result; + const static std::string prefix = "graphene::chain::"; + const static std::string suffix = "_operation"; + // graphene::chain::.*_operation + if( (name.size() >= prefix.size() + suffix.size()) + && (name.substr( 0, prefix.size() ) == prefix) + && (name.substr( name.size()-suffix.size(), suffix.size() ) == suffix ) + ) + return name.substr( prefix.size(), name.size() - prefix.size() - suffix.size() ); + + wlog( "don't know how to clean name: ${name}", ("name", name) ); + return name; +} + struct static_variant_map_visitor { static_variant_map_visitor() {} @@ -47,7 +63,7 @@ struct static_variant_map_visitor result_type operator()( const T& dummy ) { assert( which == m.which_to_name.size() ); - std::string name = js_name::name(); + std::string name = clean_name( fc::get_typename::name() ); m.name_to_which[ name ] = which; m.which_to_name.push_back( name ); }