From f1bcf8674df11db95d624329bef823405c251f97 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 24 Jun 2015 22:13:48 +0200 Subject: [PATCH] Implement the type_name() function. Only tested on gcc 5.1 Add Sprout library. --- .gitmodules | 3 + CMakeLists.txt | 13 ++-- game/main.cpp | 2 +- .../doorkeeper/components/basemapsource.hpp | 2 +- include/doorkeeper/helpers/maploader.hpp | 2 +- .../helpers/{hashing.hpp => typename.hpp} | 37 ++++++++-- include/doorkeeper/implem/hashing.inl | 19 ----- include/doorkeeper/implem/tiger_bt.hpp | 2 +- include/doorkeeper/implem/typename.inl | 71 +++++++++++++++++++ lib/sprout | 1 + src/CMakeLists.txt | 6 +- src/asciimapsource.cpp | 4 +- src/{hashing.cpp => typename.cpp} | 2 +- test/main.cpp | 15 ++++ 14 files changed, 141 insertions(+), 38 deletions(-) create mode 100644 .gitmodules rename include/doorkeeper/helpers/{hashing.hpp => typename.hpp} (56%) delete mode 100644 include/doorkeeper/implem/hashing.inl create mode 100644 include/doorkeeper/implem/typename.inl create mode 160000 lib/sprout rename src/{hashing.cpp => typename.cpp} (93%) diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..177b0fb --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "lib/sprout"] + path = lib/sprout + url = https://github.com/bolero-MURAKAMI/Sprout.git diff --git a/CMakeLists.txt b/CMakeLists.txt index c29b2ce..2d92cfa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,6 @@ cmake_minimum_required(VERSION 2.8 FATAL_ERROR) cmake_policy(SET CMP0040 NEW) #add_custom_command's target must exist +project(doorkeeper_lib CXX C) set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -std=c++11 -g -O0 -Wall -Wextra") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -std=c++11 -O3 -Wall -Wextra") @@ -9,14 +10,18 @@ add_definitions(-DWITH_VECTOR_IOSTREAM) find_library(Boost 1.53.0 REQUIRED) -include_directories(SYSTEM - ${Boost_INCLUDE_DIRECTORIES} +add_library(${PROJECT_NAME} INTERFACE) + +target_include_directories(${PROJECT_NAME} SYSTEM + INTERFACE ${Boost_INCLUDE_DIRECTORIES} ) -include_directories( - include/ +target_include_directories(${PROJECT_NAME} + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/lib/sprout ) +add_subdirectory(lib/sprout) add_subdirectory(src) add_subdirectory(test) add_subdirectory(game) diff --git a/game/main.cpp b/game/main.cpp index dff75d6..a2322b7 100644 --- a/game/main.cpp +++ b/game/main.cpp @@ -16,7 +16,7 @@ int main() { IntPoolType pool; pool.opener = [](const std::string& parName) { std::cout << "Opening " << parName << std::endl; return new dkh::AsciiMapSource(parName, coords2(10, 8), dk::MapType_IsometricStaggered, coords2(64, 64)); }; dkh::PushLayerMapType<2> pushers; - pushers.insert(std::make_pair(dk::make_signature_hash(), &Tyler2d::push_layer_void)); + pushers.insert(std::make_pair(dk::type_name_hash(), &Tyler2d::push_layer_void)); Tyler2d tiler(dkh::call_map_load(pool, std::string("test_level.dk"), pushers)); return 0; diff --git a/include/doorkeeper/components/basemapsource.hpp b/include/doorkeeper/components/basemapsource.hpp index 9ae0dda..3ed13ee 100644 --- a/include/doorkeeper/components/basemapsource.hpp +++ b/include/doorkeeper/components/basemapsource.hpp @@ -3,7 +3,7 @@ #include "doorkeeper/implem/maptypes.hpp" #include "doorkeeper/primitivetypes.hpp" -#include "doorkeeper/helpers/hashing.hpp" +#include "doorkeeper/helpers/typename.hpp" #include "doorkeeper/implem/coords_utils.hpp" #include #include diff --git a/include/doorkeeper/helpers/maploader.hpp b/include/doorkeeper/helpers/maploader.hpp index 9fba305..cc67839 100644 --- a/include/doorkeeper/helpers/maploader.hpp +++ b/include/doorkeeper/helpers/maploader.hpp @@ -3,7 +3,7 @@ #include "doorkeeper/components/tyler.hpp" #include "doorkeeper/components/basemapsource.hpp" -#include "doorkeeper/helpers/hashing.hpp" +#include "doorkeeper/helpers/typename.hpp" #include "doorkeeper/components/exception.hpp" #include #include diff --git a/include/doorkeeper/helpers/hashing.hpp b/include/doorkeeper/helpers/typename.hpp similarity index 56% rename from include/doorkeeper/helpers/hashing.hpp rename to include/doorkeeper/helpers/typename.hpp index 46c75c4..7c52b69 100644 --- a/include/doorkeeper/helpers/hashing.hpp +++ b/include/doorkeeper/helpers/typename.hpp @@ -1,5 +1,5 @@ -#ifndef id4E5F8A0ABA6047BA988D512351B9DD2D -#define id4E5F8A0ABA6047BA988D512351B9DD2D +#ifndef id3A8D22AD652142C9A44142EC942E4482 +#define id3A8D22AD652142C9A44142EC942E4482 #include "doorkeeper/implem/doorkeeper_conf.h" @@ -16,7 +16,7 @@ #endif #if defined(IS_PRETTY_FUNC_CONSTEXPR) -# include "doorkeeper/implem/string_bt.hpp" +# include "sprout/string.hpp" # include "doorkeeper/implem/tiger_bt.hpp" # include #endif @@ -35,15 +35,38 @@ namespace dk { bool operator< ( const HashType& parL, const HashType& parR ); - template - HashType make_signature_hash ( void ) a_pure; - namespace implem { HashType hash_string ( const char* parString, std::size_t parLen ) a_pure; + +#if defined(IS_PRETTY_FUNC_CONSTEXPR) + template + struct type_name_info { + constexpr type_name_info ( void ); + + const std::size_t len; + const char* const str; + }; + + template + constexpr std::size_t type_name_len ( void ) a_pure; +#endif } //namespace implem + + template +#if defined(IS_PRETTY_FUNC_CONSTEXPR) + constexpr +#endif + HashType type_name_hash ( void ) a_pure; + + template +#if defined(IS_PRETTY_FUNC_CONSTEXPR) + constexpr sprout::string()> type_name ( void ) a_pure; +#else + std::string type_name ( void ) a_pure; +#endif } //namespace dk -#include "doorkeeper/implem/hashing.inl" +#include "doorkeeper/implem/typename.inl" #if defined(IS_PRETTY_FUNC_CONSTEXPR) # undef IS_PRETTY_FUNC_CONSTEXPR diff --git a/include/doorkeeper/implem/hashing.inl b/include/doorkeeper/implem/hashing.inl deleted file mode 100644 index 08499ee..0000000 --- a/include/doorkeeper/implem/hashing.inl +++ /dev/null @@ -1,19 +0,0 @@ -namespace dk { - template - inline HashType make_signature_hash() { -#if defined(IS_PRETTY_FUNC_CONSTEXPR) - //static_assert(dk::tiger("a", 1, 0x01).a == 0xABF87E2EEFFBBE77ULL, "Bug in tiger hash"); - //static_assert(dk::tiger("message digest", 14, 0x01).a == 0x951A2078CBF881D9ULL, "Bug in tiger hash"); - - constexpr bt::string func_pretty(__PRETTY_FUNCTION__); - // set padding to 0x80 for tiger V2 - constexpr HashType retval = dk::tiger(func_pretty.data(), func_pretty.size(), 0x01); - - return retval; -#else - const char* const pf = __PRETTY_FUNCTION__; - const std::size_t len = sizeof(__PRETTY_FUNCTION__) - 1; - return implem::hash_string(pf, len); -#endif - } -} //namespace dk diff --git a/include/doorkeeper/implem/tiger_bt.hpp b/include/doorkeeper/implem/tiger_bt.hpp index 7f01b07..0c8da69 100644 --- a/include/doorkeeper/implem/tiger_bt.hpp +++ b/include/doorkeeper/implem/tiger_bt.hpp @@ -1,7 +1,7 @@ #ifndef id1E6CCE44307549A9B6C9E4E5923AC002 #define id1E6CCE44307549A9B6C9E4E5923AC002 -#include "doorkeeper/helpers/hashing.hpp" +//#include "doorkeeper/helpers/hashing.hpp" #include "doorkeeper/implem/compatibility.h" #include #include diff --git a/include/doorkeeper/implem/typename.inl b/include/doorkeeper/implem/typename.inl new file mode 100644 index 0000000..5cc478b --- /dev/null +++ b/include/doorkeeper/implem/typename.inl @@ -0,0 +1,71 @@ + +namespace dk { + template +#if defined(IS_PRETTY_FUNC_CONSTEXPR) + constexpr +#endif + inline HashType type_name_hash() { +#if defined(IS_PRETTY_FUNC_CONSTEXPR) + //static_assert(dk::tiger("a", 1, 0x01).a == 0xABF87E2EEFFBBE77ULL, "Bug in tiger hash"); + //static_assert(dk::tiger("message digest", 14, 0x01).a == 0x951A2078CBF881D9ULL, "Bug in tiger hash"); + + // set padding to 0x80 for tiger V2 + return dk::tiger(type_name().data(), type_name().size(), 0x01); +#else + const auto retstring = type_name(); + return implem::hash_string(retstring.data(), retstring.size()); +#endif + } + + template +#if defined(IS_PRETTY_FUNC_CONSTEXPR) + constexpr inline sprout::string()> type_name() +#else + inline std::string type_name() +#endif + { +#if defined(IS_PRETTY_FUNC_CONSTEXPR) +# if defined(__GNUC__) + typedef sprout::string().len> sproutstring; + typedef sprout::string()> sproutstringret; + + return sproutstringret( + implem::type_name_info().str + sproutstring(implem::type_name_info().str).find( + '=', + sproutstring(implem::type_name_info().str).find('[') + ) + 2, + implem::type_name_len() - 1 + ); +# else +# error "not implemented" +# endif +#else +# error "not implemented" + const char* const pf = __PRETTY_FUNCTION__; + const std::size_t len = sizeof(__PRETTY_FUNCTION__) - 1; +#endif + } + + namespace implem { + template + constexpr type_name_info::type_name_info() : + len(sizeof(__PRETTY_FUNCTION__)), + str(__PRETTY_FUNCTION__) + { + } + +#if defined(IS_PRETTY_FUNC_CONSTEXPR) + template + constexpr std::size_t type_name_len() { + typedef sprout::string().len> sproutstring; + +# if defined(__GNUC__) + return type_name_info().len - sproutstring( + implem::type_name_info().str).find('=', sproutstring(implem::type_name_info().str).find('[')) - 3; +# else +# error "not implemented" +# endif + } +#endif + } //namespace implem +} //namespace dk diff --git a/lib/sprout b/lib/sprout new file mode 160000 index 0000000..79ed131 --- /dev/null +++ b/lib/sprout @@ -0,0 +1 @@ +Subproject commit 79ed131e71ab4ec5b19d6bc1b285f9080acaaa45 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 173f74d..fc3586e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -16,10 +16,14 @@ add_library(${PROJECT_NAME} asciimapsource.cpp tylermapsource.cpp tiger.c - hashing.cpp + typename.cpp ) target_include_directories(${PROJECT_NAME} PRIVATE . PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/include ) + +target_link_libraries(${PROJECT_NAME} + doorkeeper_lib +) diff --git a/src/asciimapsource.cpp b/src/asciimapsource.cpp index b241151..e22670a 100644 --- a/src/asciimapsource.cpp +++ b/src/asciimapsource.cpp @@ -1,6 +1,6 @@ #include "doorkeeper/helpers/asciimapsource.hpp" #include "doorkeeper/implem/compatibility.h" -#include "doorkeeper/helpers/hashing.hpp" +#include "doorkeeper/helpers/typename.hpp" #include "asciimap_parser.hpp" #include #include @@ -115,6 +115,6 @@ namespace dkh { #else (void)parIndex; #endif - return dk::make_signature_hash(); + return dk::type_name_hash(); } } //namespace dkh diff --git a/src/hashing.cpp b/src/typename.cpp similarity index 93% rename from src/hashing.cpp rename to src/typename.cpp index b26f9e9..231b74c 100644 --- a/src/hashing.cpp +++ b/src/typename.cpp @@ -1,4 +1,4 @@ -#include "doorkeeper/helpers/hashing.hpp" +#include "doorkeeper/helpers/typename.hpp" #include "tiger.h" namespace dk { diff --git a/test/main.cpp b/test/main.cpp index a9eeab1..66344a9 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -23,6 +23,13 @@ struct LayerWithData { std::string path; }; +#include "doorkeeper/helpers/typename.hpp" + +std::ostream& operator<< (std::ostream& parStream, const dk::HashType& parHash) { + parStream << std::hex << parHash.a << parHash.b << parHash.c; + return parStream; +} + namespace { typedef std::unique_ptr SDLRendererUPtr; typedef std::unique_ptr SDLWindowUPtr; @@ -42,6 +49,14 @@ namespace { } //unnamed namespace int main() { + typedef dk::Tyler<2>::coords coords2; + std::cout << dk::type_name() << '\n'; + std::cout << dk::type_name::coords>() << '\n'; + std::cout << dk::type_name() << '\n'; + std::cout << dk::type_name() << '\n'; + std::cout << dk::type_name, bool>>>() << '\n'; + std::cout << dk::type_name_hash>() << '\n'; + static constexpr auto h = dk::tiger("message digest", 14, 0x01); std::cout << std::hex << h.a << '\n' << 0x951A2078CBF881D9ULL << std::endl;