Implement the type_name() function. Only tested on gcc 5.1

Add Sprout library.
This commit is contained in:
King_DuckZ 2015-06-24 22:13:48 +02:00
parent 6a39b82e5e
commit f1bcf8674d
14 changed files with 141 additions and 38 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "lib/sprout"]
path = lib/sprout
url = https://github.com/bolero-MURAKAMI/Sprout.git

View File

@ -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)

View File

@ -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<int, 2>(), &Tyler2d::push_layer_void<int>));
pushers.insert(std::make_pair(dk::type_name_hash<int>(), &Tyler2d::push_layer_void<int>));
Tyler2d tiler(dkh::call_map_load(pool, std::string("test_level.dk"), pushers));
return 0;

View File

@ -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 <cstdint>
#include <vector>

View File

@ -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 <cstdint>
#include <utility>

View File

@ -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 <stdexcept>
#endif
@ -35,15 +35,38 @@ namespace dk {
bool operator< ( const HashType& parL, const HashType& parR );
template <typename T, uint32_t D>
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 <typename T>
struct type_name_info {
constexpr type_name_info ( void );
const std::size_t len;
const char* const str;
};
template <typename T>
constexpr std::size_t type_name_len ( void ) a_pure;
#endif
} //namespace implem
template <typename T>
#if defined(IS_PRETTY_FUNC_CONSTEXPR)
constexpr
#endif
HashType type_name_hash ( void ) a_pure;
template <typename T>
#if defined(IS_PRETTY_FUNC_CONSTEXPR)
constexpr sprout::string<implem::type_name_len<T>()> 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

View File

@ -1,19 +0,0 @@
namespace dk {
template <typename T, uint32_t D>
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<sizeof(__PRETTY_FUNCTION__)-1> 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

View File

@ -1,7 +1,7 @@
#ifndef id1E6CCE44307549A9B6C9E4E5923AC002
#define id1E6CCE44307549A9B6C9E4E5923AC002
#include "doorkeeper/helpers/hashing.hpp"
//#include "doorkeeper/helpers/hashing.hpp"
#include "doorkeeper/implem/compatibility.h"
#include <cstdint>
#include <ciso646>

View File

@ -0,0 +1,71 @@
namespace dk {
template <typename T>
#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<T>().data(), type_name<T>().size(), 0x01);
#else
const auto retstring = type_name<T>();
return implem::hash_string(retstring.data(), retstring.size());
#endif
}
template <typename T>
#if defined(IS_PRETTY_FUNC_CONSTEXPR)
constexpr inline sprout::string<implem::type_name_len<T>()> type_name()
#else
inline std::string type_name()
#endif
{
#if defined(IS_PRETTY_FUNC_CONSTEXPR)
# if defined(__GNUC__)
typedef sprout::string<implem::type_name_info<T>().len> sproutstring;
typedef sprout::string<implem::type_name_len<T>()> sproutstringret;
return sproutstringret(
implem::type_name_info<T>().str + sproutstring(implem::type_name_info<T>().str).find(
'=',
sproutstring(implem::type_name_info<T>().str).find('[')
) + 2,
implem::type_name_len<T>() - 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 <typename T>
constexpr type_name_info<T>::type_name_info() :
len(sizeof(__PRETTY_FUNCTION__)),
str(__PRETTY_FUNCTION__)
{
}
#if defined(IS_PRETTY_FUNC_CONSTEXPR)
template <typename T>
constexpr std::size_t type_name_len() {
typedef sprout::string<implem::type_name_info<T>().len> sproutstring;
# if defined(__GNUC__)
return type_name_info<T>().len - sproutstring(
implem::type_name_info<T>().str).find('=', sproutstring(implem::type_name_info<T>().str).find('[')) - 3;
# else
# error "not implemented"
# endif
}
#endif
} //namespace implem
} //namespace dk

1
lib/sprout Submodule

@ -0,0 +1 @@
Subproject commit 79ed131e71ab4ec5b19d6bc1b285f9080acaaa45

View File

@ -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
)

View File

@ -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 <fstream>
#include <iomanip>
@ -115,6 +115,6 @@ namespace dkh {
#else
(void)parIndex;
#endif
return dk::make_signature_hash<int, 2>();
return dk::type_name_hash<int>();
}
} //namespace dkh

View File

@ -1,4 +1,4 @@
#include "doorkeeper/helpers/hashing.hpp"
#include "doorkeeper/helpers/typename.hpp"
#include "tiger.h"
namespace dk {

View File

@ -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<SDL_Renderer, void(*)(SDL_Renderer*)> SDLRendererUPtr;
typedef std::unique_ptr<SDL_Window, void(*)(SDL_Window*)> SDLWindowUPtr;
@ -42,6 +49,14 @@ namespace {
} //unnamed namespace
int main() {
typedef dk::Tyler<2>::coords coords2;
std::cout << dk::type_name<coords2>() << '\n';
std::cout << dk::type_name<dk::Tyler<2>::coords>() << '\n';
std::cout << dk::type_name<int>() << '\n';
std::cout << dk::type_name<float>() << '\n';
std::cout << dk::type_name<std::vector<std::pair<std::map<std::string, unsigned short int>, bool>>>() << '\n';
std::cout << dk::type_name_hash<dk::Tyler<2>>() << '\n';
static constexpr auto h = dk::tiger("message digest", 14, 0x01);
std::cout << std::hex << h.a << '\n' << 0x951A2078CBF881D9ULL << std::endl;