Skip to content

Commit 0757651

Browse files
committed
refactor: replace C++17-style SFINAE patterns with concepts
1 parent 39af30e commit 0757651

5 files changed

Lines changed: 125 additions & 116 deletions

File tree

include/zenkit/Archive.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ namespace zenkit {
131131
static std::unique_ptr<ReadArchive> from(Read* r);
132132

133133
template <typename T>
134-
std::enable_if_t<std::is_base_of_v<Object, T>, std::shared_ptr<T>> //
135-
read_object(GameVersion version) {
134+
requires std::derived_from<T, Object>
135+
std::shared_ptr<T> read_object(GameVersion version) {
136136
auto obj = this->read_object(version);
137137
if (obj != nullptr && obj->get_object_type() != T::TYPE) {
138138
throw ParserError {"ReadArchive", "Read unexcected object!"};

include/zenkit/DaedalusScript.hh

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,8 @@ namespace zenkit {
549549
/// \tparam T The type of instance to check for.
550550
/// \return <tt>true</tt> if the symbol contains an instance of the given type, <tt>false</tt> if not.
551551
template <typename T>
552-
ZKAPI std::enable_if_t<std::is_base_of_v<DaedalusInstance, T>, bool> is_instance_of() {
552+
requires std::derived_from<T, DaedalusInstance>
553+
ZKAPI bool is_instance_of() {
553554
return this->type() == DaedalusDataType::INSTANCE && this->get_instance() != nullptr &&
554555
this->get_instance()->_m_type == &typeid(T);
555556
}
@@ -747,6 +748,10 @@ namespace zenkit {
747748
ZKINT static DaedalusInstruction decode(Read* r);
748749
};
749750

751+
template <typename T>
752+
concept DaedalusValue = std::same_as<T, std::string> || std::same_as<T, float> || std::same_as<T, int32_t> ||
753+
(std::is_enum_v<T> && sizeof(T) == 4);
754+
750755
/// \brief Represents a compiled daedalus script
751756
class DaedalusScript {
752757
public:
@@ -764,10 +769,8 @@ namespace zenkit {
764769
/// \throws DaedalusInvalidRegistrationDataType If the datatype of \p _member is different than that of the
765770
/// symbol.
766771
template <typename _class, typename _member, int N>
767-
std::enable_if_t<std::is_same_v<_member, std::string> || std::is_same_v<_member, float> ||
768-
std::is_same_v<_member, std::int32_t> || (std::is_enum_v<_member> && sizeof(_member) == 4),
769-
void>
770-
register_member(std::string_view name, _member (_class::*field)[N]) {
772+
requires DaedalusValue<_member>
773+
void register_member(std::string_view name, _member (_class::*field)[N]) {
771774
auto* type = &typeid(_class);
772775
auto* sym = _check_member<_member, N>(name, type);
773776

@@ -785,10 +788,8 @@ namespace zenkit {
785788
/// \throws DaedalusInvalidRegistrationDataType If the datatype of \p _member is different than that of the
786789
/// symbol.
787790
template <typename _class, typename _member>
788-
std::enable_if_t<std::is_same_v<_member, std::string> || std::is_same_v<_member, float> ||
789-
std::is_same_v<_member, std::int32_t> || (std::is_enum_v<_member> && sizeof(_member) == 4),
790-
void>
791-
register_member(std::string_view name, _member _class::*field) {
791+
requires DaedalusValue<_member>
792+
void register_member(std::string_view name, _member _class::* field) {
792793
auto* type = &typeid(_class);
793794
auto* sym = _check_member<_member, 1>(name, type);
794795

@@ -885,8 +886,8 @@ namespace zenkit {
885886
/// \return The symbol associated with that instance or <tt>nullptr</tt> if the symbol is not associated
886887
/// with any instance.
887888
template <typename T>
888-
ZKAPI std::enable_if_t<std::is_base_of_v<DaedalusInstance, T>, DaedalusSymbol const*>
889-
find_symbol_by_instance(std::shared_ptr<T> const& inst) const {
889+
requires std::derived_from<T, DaedalusInstance>
890+
ZKAPI DaedalusSymbol const* find_symbol_by_instance(std::shared_ptr<T> const& inst) const {
890891
return find_symbol_by_index(inst->_m_symbol_index);
891892
}
892893

@@ -895,8 +896,8 @@ namespace zenkit {
895896
/// \return The symbol associated with that instance or <tt>nullptr</tt> if the symbol is not associated
896897
/// with any instance.
897898
template <typename T>
898-
ZKAPI std::enable_if_t<std::is_base_of_v<DaedalusInstance, T>, DaedalusSymbol*>
899-
find_symbol_by_instance(std::shared_ptr<T> const& inst) {
899+
requires std::derived_from<T, DaedalusInstance>
900+
ZKAPI DaedalusSymbol* find_symbol_by_instance(std::shared_ptr<T> const& inst) {
900901
return find_symbol_by_index(inst->_m_symbol_index);
901902
}
902903

@@ -933,11 +934,11 @@ namespace zenkit {
933934
}
934935

935936
// check type matches
936-
if constexpr (std::is_same_v<std::string, _member>) {
937+
if constexpr (std::same_as<std::string, _member>) {
937938
if (sym->type() != DaedalusDataType::STRING) throw DaedalusInvalidRegistrationDataType {sym, "string"};
938-
} else if constexpr (std::is_same_v<float, _member>) {
939+
} else if constexpr (std::same_as<float, _member>) {
939940
if (sym->type() != DaedalusDataType::FLOAT) throw DaedalusInvalidRegistrationDataType {sym, "float"};
940-
} else if constexpr (std::is_same_v<int32_t, _member> || std::is_enum_v<_member>) {
941+
} else if constexpr (std::same_as<int32_t, _member> || std::is_enum_v<_member>) {
941942
if (sym->type() != DaedalusDataType::INT && sym->type() != DaedalusDataType::FUNCTION)
942943
throw DaedalusInvalidRegistrationDataType {sym, "int"};
943944
} else {

0 commit comments

Comments
 (0)