-
Notifications
You must be signed in to change notification settings - Fork 768
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use CTTI instead of RTTI where possible
- Loading branch information
Showing
25 changed files
with
2,957 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef CTTI_LANGUAGE_FEATURES_HPP | ||
#define CTTI_LANGUAGE_FEATURES_HPP | ||
|
||
#ifdef __cpp_variable_templates | ||
#define CTTI_HAS_VARIABLE_TEMPLATES | ||
#endif // __cpp_variable_templates | ||
|
||
#define CTTI_HAS_CONSTEXPR_PRETTY_FUNCTION | ||
|
||
#if defined(__GCC__) && __GCC__ < 5 | ||
#undef CTTI_HAS_CONSTEXPR_PRETTY_FUNCTION | ||
#endif // GCC 4.x | ||
|
||
#ifdef __clang__ | ||
#define CTTI_HAS_ENUM_AWARE_PRETTY_FUNCTION | ||
#endif // __clang__ | ||
|
||
#endif // CTTI_LANGUAGE_FEATURES_HPP |
Oops, something went wrong.