diff options
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | src/common/Utilities/FuzzyFind.h | 53 | ||||
| -rw-r--r-- | src/common/Utilities/SmartEnum.h | 61 | ||||
| -rw-r--r-- | src/common/Utilities/Util.h | 6 | ||||
| -rw-r--r-- | src/common/Utilities/advstd.h | 6 |
5 files changed, 128 insertions, 1 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index ece3db728ee..17f59f7724a 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -72,7 +72,8 @@ target_link_libraries(common openssl valgrind threads - jemalloc) + jemalloc + smart_enum) add_dependencies(common revision_data.h) diff --git a/src/common/Utilities/FuzzyFind.h b/src/common/Utilities/FuzzyFind.h new file mode 100644 index 00000000000..ab34eb6f146 --- /dev/null +++ b/src/common/Utilities/FuzzyFind.h @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2008-2018 TrinityCore <https://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef TRINITY_FUZZYFIND_H +#define TRINITY_FUZZYFIND_H + +namespace Trinity +{ + namespace Containers + { + template <typename Container, typename NeedleContainer, typename ContainsOperator = bool(std::string const&, std::string const&), typename T = void> + auto FuzzyFindIn(Container const& container, NeedleContainer const& needles, ContainsOperator const& contains = StringContainsStringI, int(*bonus)(decltype((*std::begin(std::declval<Container>())))) = nullptr) + { + using IteratorResult = decltype((*std::begin(container))); + using MappedType = std::conditional_t<advstd::is_reference_v<IteratorResult>, std::reference_wrapper<std::remove_reference_t<IteratorResult>>, IteratorResult>; + std::multimap<size_t, MappedType, std::greater<size_t>> results; + + for (auto outerIt = std::begin(container), outerEnd = std::end(container); outerIt != outerEnd; ++outerIt) + { + size_t count = 0; + for (auto innerIt = std::begin(needles), innerEnd = std::end(needles); innerIt != innerEnd; ++innerIt) + if (contains(*outerIt, *innerIt)) + ++count; + + if (!count) + continue; + + if (bonus) + count += bonus(*outerIt); + + results.emplace(count, *outerIt); + } + + return results; + } + } +} + +#endif diff --git a/src/common/Utilities/SmartEnum.h b/src/common/Utilities/SmartEnum.h new file mode 100644 index 00000000000..3ee19438d51 --- /dev/null +++ b/src/common/Utilities/SmartEnum.h @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2008-2018 TrinityCore <https://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef TRINITY_SMARTENUM_H +#define TRINITY_SMARTENUM_H + +#include "smart_enum.hpp" + +struct EnumText +{ + EnumText(char const* t = nullptr, char const* d = nullptr) : Title(t), Description(d ? d : t) {} + // Human-readable title of the value + char const* const Title; + // Human-readable description of the value + char const* const Description; + + protected: + EnumText(char const* n, EnumText e) : Title(e.Title ? e.Title : n), Description(e.Description ? e.Description : Title) {} +}; + +struct FullEnumText : public EnumText +{ + FullEnumText(char const* constant, EnumText e) : EnumText(constant, e), Constant(constant) {} + // Enum constant of the value + char const* const Constant; +}; + +template <typename E> +class EnumUtils +{ + public: + static constexpr auto Begin() { return smart_enum::begin<E>(); } + static constexpr auto End() { return smart_enum::end<E>(); } + static constexpr auto Iterate() { return smart_enum::range<E>(); } + static constexpr FullEnumText ToString(E value) + { + return { smart_enum::to_string<E>(value), smart_enum::data<E>(value) }; + } + static constexpr char const* ToConstant(E value) { return ToString(value).Constant; } + static constexpr char const* ToTitle(E value) { return ToString(value).Title; } + static constexpr char const* ToDescription(E value) { return ToString(value).Description; } +}; + +#define SMART_ENUM_BOUND(type, name) static constexpr type name = type(smart_enum::value_of<type>(smart_enum::count<type>()-1)+1); +#define SMART_ENUM_BOUND_AFTER(type, name, entry) static constexpr type name = type(entry + 1); + +#endif diff --git a/src/common/Utilities/Util.h b/src/common/Utilities/Util.h index 549984f0e65..992cbc5d788 100644 --- a/src/common/Utilities/Util.h +++ b/src/common/Utilities/Util.h @@ -24,6 +24,7 @@ #include <string> #include <sstream> +#include <utility> #include <vector> class TC_COMMON_API Tokenizer @@ -299,6 +300,11 @@ TC_COMMON_API void HexStrToByteArray(std::string const& str, uint8* out, bool re TC_COMMON_API bool StringToBool(std::string const& str); TC_COMMON_API bool StringContainsStringI(std::string const& haystack, std::string const& needle); +template <typename T> +inline bool ValueContainsStringI(std::pair<T, std::string> const& haystack, std::string const& needle) +{ + return StringContainsStringI(haystack.second, needle); +} // simple class for not-modifyable list template <typename T> diff --git a/src/common/Utilities/advstd.h b/src/common/Utilities/advstd.h index ff39e836aad..37e2d6dfa9e 100644 --- a/src/common/Utilities/advstd.h +++ b/src/common/Utilities/advstd.h @@ -73,6 +73,12 @@ namespace advstd // C++17 std::tuple_size_v forward_1v(tuple_size, size_t); + // C++17 std::is_enum_v + forward_1v(is_enum, bool); + + // C++17 std::is_arithmetic_v + forward_1v(is_arithmetic, bool); + #undef forward_1v #undef forward_2v |
