Skip to content

Commit

Permalink
Use CTTI instead of RTTI where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkov63 committed Jul 2, 2023
1 parent d1fcec8 commit b661c57
Show file tree
Hide file tree
Showing 27 changed files with 2,964 additions and 13 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@ out.xml
cereal_version.out
xml_ordering.out
build
/out/
/out/
compile_commands.json
.cache
16 changes: 10 additions & 6 deletions include/cereal/cereal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
#include "cereal/details/helpers.hpp"
#include "cereal/types/base_class.hpp"

#include "cereal/external/ctti/type_id.hpp"

namespace cereal
{
// ######################################################################
Expand Down Expand Up @@ -269,8 +271,9 @@ namespace cereal
{ \
static std::uint32_t registerVersion() \
{ \
::cereal::detail::StaticObject<Versions>::getInstance().mapping.emplace( \
std::type_index(typeid(TYPE)).hash_code(), VERSION_NUMBER ); \
::cereal::detail::StaticObject<Versions>::getInstance().mapping \
.emplace(std::hash<ctti::type_id_t>()(ctti::type_id<TYPE>()), \
VERSION_NUMBER); \
return VERSION_NUMBER; \
} \
static inline const std::uint32_t version = registerVersion(); \
Expand All @@ -285,8 +288,9 @@ namespace cereal
static const std::uint32_t version; \
static std::uint32_t registerVersion() \
{ \
::cereal::detail::StaticObject<Versions>::getInstance().mapping.emplace( \
std::type_index(typeid(TYPE)).hash_code(), VERSION_NUMBER ); \
::cereal::detail::StaticObject<Versions>::getInstance().mapping \
.emplace(std::hash<ctti::type_id_t>()(ctti::type_id<TYPE>()), \
VERSION_NUMBER); \
return VERSION_NUMBER; \
} \
CEREAL_UNUSED_FUNCTION \
Expand Down Expand Up @@ -595,7 +599,7 @@ namespace cereal
template <class T> inline
std::uint32_t registerClassVersion()
{
static const auto hash = std::type_index(typeid(T)).hash_code();
static const auto hash = std::hash<ctti::type_id_t>()(ctti::type_id<T>());
const auto insertResult = itsVersionedTypes.insert( hash );
const auto lock = detail::StaticObject<detail::Versions>::lock();
const auto version =
Expand Down Expand Up @@ -1013,7 +1017,7 @@ namespace cereal
template <class T> inline
std::uint32_t loadClassVersion()
{
static const auto hash = std::type_index(typeid(T)).hash_code();
static const auto hash = std::hash<ctti::type_id_t>()(ctti::type_id<T>());
auto lookupResult = itsVersionedTypes.find( hash );

if( lookupResult != itsVersionedTypes.end() ) // already exists
Expand Down
8 changes: 5 additions & 3 deletions include/cereal/details/traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
#include "cereal/macros.hpp"
#include "cereal/access.hpp"

#include "cereal/external/ctti/type_id.hpp"

namespace cereal
{
namespace traits
Expand Down Expand Up @@ -1146,15 +1148,15 @@ namespace cereal
{
template<class T>
base_class_id(T const * const t) :
type(typeid(T)),
type(ctti::type_id<T>()),
ptr(t),
hash(std::hash<std::type_index>()(typeid(T)) ^ (std::hash<void const *>()(t) << 1))
hash(std::hash<ctti::type_id_t>()(ctti::type_id<T>()) ^ (std::hash<void const *>()(t) << 1))
{ }

bool operator==(base_class_id const & other) const
{ return (type == other.type) && (ptr == other.ptr); }

std::type_index type;
ctti::type_id_t type;
void const * ptr;
size_t hash;
};
Expand Down
7 changes: 4 additions & 3 deletions include/cereal/details/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@
#ifndef CEREAL_DETAILS_UTIL_HPP_
#define CEREAL_DETAILS_UTIL_HPP_

#include <typeinfo>
#include <string>

#include "cereal/external/ctti/nameof.hpp"

#ifdef _MSC_VER
namespace cereal
{
Expand All @@ -47,7 +48,7 @@ namespace cereal
/*! @internal */
template <class T> inline
std::string demangledName()
{ return typeid( T ).name(); }
{ return ctti::nameof<T>().str(); }
} // namespace util
} // namespace cereal
#else // clang or gcc
Expand Down Expand Up @@ -77,7 +78,7 @@ namespace cereal
/*! @internal */
template<class T> inline
std::string demangledName()
{ return demangle(typeid(T).name()); }
{ return ctti::nameof<T>().str(); }
}
} // namespace cereal
#endif // clang or gcc branch of _MSC_VER
Expand Down
21 changes: 21 additions & 0 deletions include/cereal/external/ctti/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) 2015 Manuel Sánchez

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.
47 changes: 47 additions & 0 deletions include/cereal/external/ctti/detail/algorithm.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef CTTI_DETAIL_ALGORITHM_HPP
#define CTTI_DETAIL_ALGORITHM_HPP

#include <cstdint>

namespace ctti
{

namespace detail
{

template<typename T, std::size_t N>
constexpr const T* begin(const T(&array)[N])
{
return &array[0];
}

template<typename T, std::size_t N>
constexpr const T* end(const T(&array)[N])
{
return &array[N];
}

template<typename LhsIt, typename RhsIt>
constexpr bool equal_range(LhsIt lhsBegin, LhsIt lhsEnd, RhsIt rhsBegin, RhsIt rhsEnd)
{
return (lhsBegin != lhsEnd && rhsBegin != rhsEnd) ? *lhsBegin == *rhsBegin &&
equal_range(lhsBegin + 1, lhsEnd, rhsBegin + 1, rhsEnd) : (lhsBegin == lhsEnd && rhsBegin == rhsEnd);
}

template<typename T>
constexpr const T& max(const T& lhs, const T& rhs)
{
return (lhs >= rhs) ? lhs : rhs;
}

template<typename T>
constexpr const T& min(const T& lhs, const T& rhs)
{
return (lhs <= rhs) ? lhs : rhs;
}

}

}

#endif // CTTI_DETAIL_ALGORITHM_HPP
125 changes: 125 additions & 0 deletions include/cereal/external/ctti/detail/cstring.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#ifndef CTTI_DETAIL_CSTRING_HPP
#define CTTI_DETAIL_CSTRING_HPP

#include "hash.hpp"
#include "algorithm.hpp"
#include <ostream>
#include <string>

namespace ctti
{

namespace detail
{

class cstring
{
public:
template<std::size_t N>
constexpr cstring(const char (&str)[N]) :
cstring{&str[0], N - 1}
{}

constexpr cstring(const char* begin, std::size_t length) :
_str{begin},
_length{length}
{}

constexpr cstring(const char* begin, const char* end) :
cstring{begin, static_cast<std::size_t>(end - begin)}
{}

constexpr cstring(const char* begin) :
cstring{begin, length(begin)}
{}

static constexpr std::size_t length(const char* str)
{
return *str ? 1 + length(str + 1) : 0;
}

constexpr std::size_t length() const
{
return _length;
}

constexpr std::size_t size() const
{
return length();
}

constexpr hash_t hash() const
{
return fnv1a_hash(length(), begin());
}

std::string cppstring() const
{
return {begin(), end()};
}

std::string str() const
{
return cppstring();
}

constexpr const char* begin() const
{
return _str;
}

constexpr const char* end() const
{
return _str + _length;
}

constexpr char operator[](std::size_t i) const
{
return _str[i];
}

constexpr const char* operator()(std::size_t i) const
{
return _str + i;
}

constexpr cstring operator()(std::size_t begin, std::size_t end) const
{
return {_str + begin, _str + end};
}

constexpr cstring pad(std::size_t begin_offset, std::size_t end_offset) const
{
return operator()(begin_offset, size() - end_offset);
}

friend std::ostream& operator<<(std::ostream& os, const cstring& str)
{
for(const char c : str)
{
os << c;
}

return os;
}

private:
const char* _str;
std::size_t _length;
};

constexpr bool operator==(const cstring& lhs, const cstring& rhs)
{
return ctti::detail::equal_range(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}

constexpr bool operator!=(const cstring& lhs, const cstring& rhs)
{
return !(lhs == rhs);
}

}

}

#endif // CTTI_DETAIL_CSTRING_HPP
46 changes: 46 additions & 0 deletions include/cereal/external/ctti/detail/entity_name.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef CTTI_DETAIL_ENTITY_NAME_HPP
#define CTTI_DETAIL_ENTITY_NAME_HPP

#include "cstring.hpp"

namespace ctti
{

namespace detail
{

class entity_name
{
public:
constexpr entity_name(const ctti::detail::cstring& str) :
_str{str}
{}

constexpr ctti::detail::cstring str() const
{
return _str;
}

constexpr ctti::detail::cstring operator[](std::size_t i) const
{
return colon_scan(_str.begin(), _str.end(), i);
}

private:
ctti::detail::cstring _str;

constexpr ctti::detail::cstring colon_scan(const char* begin, const char* end, std::size_t i) const
{
return (begin == end) ? {begin, end} :
(i == 0) ? {begin, end}
(colon_count == 0 && *begin == ':') ? colon_scan(++begin, end, i, ++colon_count) :
(colon_count == 1 && *begin == ':') ? colon_scan(++begin, end, i - 1, 0)
(
}
};

}

}

#endif // CTTI_DETAIL_ENTITY_NAME_HPP
32 changes: 32 additions & 0 deletions include/cereal/external/ctti/detail/hash.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef CTTI_DETAIL_HASH_HPP
#define CTTI_DETAIL_HASH_HPP

#include <cstdint>

namespace ctti
{
namespace detail
{
// From https://github.com/foonathan/string_id. As usually, thanks Jonathan.

using hash_t = std::uint64_t;

// See http://www.isthe.com/chongo/tech/comp/fnv/#FNV-param
constexpr hash_t fnv_basis = 14695981039346656037ull;
constexpr hash_t fnv_prime = 1099511628211ull;

// FNV-1a 64 bit hash
constexpr hash_t fnv1a_hash(std::size_t n, const char *str, hash_t hash = fnv_basis)
{
return n > 0 ? fnv1a_hash(n - 1, str + 1, (hash ^ *str) * fnv_prime) : hash;
}

template<std::size_t N>
constexpr hash_t fnv1a_hash(const char (&array)[N])
{
return fnv1a_hash(N - 1, &array[0]);
}
}
}

#endif /* CTTI_DETAIL_HASH_HPP */
Loading

0 comments on commit b661c57

Please sign in to comment.