diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2025-05-19 16:28:02 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2025-05-19 16:28:02 +0200 |
| commit | f61df0b33ea2a2de064f1fa2a9cde191b790a5ba (patch) | |
| tree | 45c5cda251be02edefde3c975b555e7c504726a6 /lib/src | |
| parent | 0a5f3b25214c11556f62ce04601c06812a3464d8 (diff) | |
| download | turns-f61df0b33ea2a2de064f1fa2a9cde191b790a5ba.tar.xz turns-f61df0b33ea2a2de064f1fa2a9cde191b790a5ba.zip | |
lib: rescope enums
Diffstat (limited to 'lib/src')
| -rw-r--r-- | lib/src/turns-disposition.h | 18 | ||||
| -rw-r--r-- | lib/src/turns-init.cpp | 3 | ||||
| -rw-r--r-- | lib/src/turns-participant.cpp | 17 | ||||
| -rw-r--r-- | lib/src/turns-participant.h | 17 | ||||
| -rw-r--r-- | lib/src/turnsmm/enum_helpers.hpp | 34 | ||||
| -rw-r--r-- | lib/src/turnsmm/enums.cpp | 43 | ||||
| -rw-r--r-- | lib/src/turnsmm/enums.hpp | 44 | ||||
| -rw-r--r-- | lib/src/turnsmm/participant.cpp | 11 | ||||
| -rw-r--r-- | lib/src/turnsmm/participant.hpp | 13 | ||||
| -rw-r--r-- | lib/src/turnsmm/turn-order.cpp | 10 | ||||
| -rw-r--r-- | lib/src/turnsmm/turn-order.hpp | 16 |
11 files changed, 101 insertions, 125 deletions
diff --git a/lib/src/turns-disposition.h b/lib/src/turns-disposition.h deleted file mode 100644 index c5bb174..0000000 --- a/lib/src/turns-disposition.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef TURNS_DISPOSITION_H -#define TURNS_DISPOSITION_H - -#include <glib-object.h> - -G_BEGIN_DECLS - -typedef enum -{ - TURNS_DISPOSITION_NEUTRAL, - TURNS_DISPOSITION_FRIENDLY, - TURNS_DISPOSITION_HOSTILE, - TURNS_DISPOSITION_SECRET, -} TurnsDisposition; - -G_END_DECLS - -#endif diff --git a/lib/src/turns-init.cpp b/lib/src/turns-init.cpp index 6fbe837..243434d 100644 --- a/lib/src/turns-init.cpp +++ b/lib/src/turns-init.cpp @@ -11,9 +11,10 @@ G_BEGIN_DECLS auto turns_init() -> void { - g_type_ensure(TURNS_TYPE_DISPOSITION); g_type_ensure(TURNS_TYPE_PARTICIPANT); + g_type_ensure(TURNS_TYPE_PARTICIPANT_DISPOSITION); g_type_ensure(TURNS_TYPE_TURN_ORDER); + g_type_ensure(TURNS_TYPE_TURN_ORDER_SORT_MODE); } G_END_DECLS
\ No newline at end of file diff --git a/lib/src/turns-participant.cpp b/lib/src/turns-participant.cpp index 49f29ff..1e03c8b 100644 --- a/lib/src/turns-participant.cpp +++ b/lib/src/turns-participant.cpp @@ -1,6 +1,5 @@ #include "turns-participant.h" -#include "turns-disposition.h" #include "turns-enums.h" #include <glib-object.h> @@ -19,7 +18,7 @@ struct _TurnsParticipant gboolean active; gboolean defeated; - TurnsDisposition disposition; + TurnsParticipantDisposition disposition; gchar * name; gfloat priority; }; @@ -87,7 +86,7 @@ namespace case property::Priority: return turns_participant_set_priority(participant, g_value_get_float(value)); case property::Disposition: - return turns_participant_set_disposition(participant, static_cast<TurnsDisposition>(g_value_get_enum(value))); + return turns_participant_set_disposition(participant, static_cast<TurnsParticipantDisposition>(g_value_get_enum(value))); default: G_OBJECT_WARN_INVALID_PROPERTY_ID(self, id, specification); } @@ -140,8 +139,8 @@ static void turns_participant_class_init(TurnsParticipantClass * klass) g_param_spec_enum("disposition", "Disposition", "Disposition of the participant toward the players", - turns_disposition_get_type(), - TURNS_DISPOSITION_NEUTRAL, + turns_participant_disposition_get_type(), + TURNS_PARTICIPANT_DISPOSITION_NEUTRAL, static_cast<GParamFlags>(G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY)); g_object_class_install_properties(object_class, static_cast<guint>(property::N_PROPERTIES), properties.data()); @@ -157,7 +156,7 @@ TurnsParticipant * turns_participant_new() return static_cast<TurnsParticipant *>(g_object_new(TURNS_TYPE_PARTICIPANT, nullptr)); } -TurnsParticipant * turns_participant_new_with(gchar const * name, gfloat priority, TurnsDisposition disposition) +TurnsParticipant * turns_participant_new_with(gchar const * name, gfloat priority, TurnsParticipantDisposition disposition) { g_return_val_if_fail(name != nullptr, nullptr); @@ -177,9 +176,9 @@ gboolean turns_participant_get_defeated(TurnsParticipant const * self) return self->defeated; } -TurnsDisposition turns_participant_get_disposition(TurnsParticipant const * self) +TurnsParticipantDisposition turns_participant_get_disposition(TurnsParticipant const * self) { - g_return_val_if_fail(TURNS_IS_PARTICIPANT(const_cast<TurnsParticipant *>(self)), TurnsDisposition::TURNS_DISPOSITION_NEUTRAL); + g_return_val_if_fail(TURNS_IS_PARTICIPANT(const_cast<TurnsParticipant *>(self)), TurnsParticipantDisposition::TURNS_PARTICIPANT_DISPOSITION_NEUTRAL); return self->disposition; } @@ -221,7 +220,7 @@ void turns_participant_set_defeated(TurnsParticipant * self, gboolean value) g_object_notify_by_pspec(G_OBJECT(self), properties[static_cast<std::size_t>(property::Defeated)]); } -void turns_participant_set_disposition(TurnsParticipant * self, TurnsDisposition value) +void turns_participant_set_disposition(TurnsParticipant * self, TurnsParticipantDisposition value) { g_return_if_fail(TURNS_IS_PARTICIPANT(self)); diff --git a/lib/src/turns-participant.h b/lib/src/turns-participant.h index 3ddfaa6..f951ddb 100644 --- a/lib/src/turns-participant.h +++ b/lib/src/turns-participant.h @@ -1,7 +1,7 @@ #ifndef TURNS_PARTICIPANT_H #define TURNS_PARTICIPANT_H -#include "turns-disposition.h" +#include "turns-enums.h" // IWYU pragma: export #include <glib-object.h> #include <glib.h> @@ -9,6 +9,14 @@ G_BEGIN_DECLS +typedef enum +{ + TURNS_PARTICIPANT_DISPOSITION_NEUTRAL, + TURNS_PARTICIPANT_DISPOSITION_FRIENDLY, + TURNS_PARTICIPANT_DISPOSITION_HOSTILE, + TURNS_PARTICIPANT_DISPOSITION_SECRET, +} TurnsParticipantDisposition; + #define TURNS_TYPE_PARTICIPANT turns_participant_get_type() G_DECLARE_FINAL_TYPE(TurnsParticipant, turns_participant, TURNS, PARTICIPANT, GObject) @@ -29,7 +37,8 @@ TurnsParticipant * turns_participant_new(void) G_GNUC_WARN_UNUSED_RESULT; * @param priority The priority of the new instance. * @param disposition The disposition of the new instance. */ -TurnsParticipant * turns_participant_new_with(gchar const * name, gfloat priority, TurnsDisposition disposition) G_GNUC_WARN_UNUSED_RESULT; +TurnsParticipant * +turns_participant_new_with(gchar const * name, gfloat priority, TurnsParticipantDisposition disposition) G_GNUC_WARN_UNUSED_RESULT; /** * @brief Get the active state of a participant. @@ -53,7 +62,7 @@ gboolean turns_participant_get_defeated(TurnsParticipant const * self); * @param self A Participant instance. The value *must not* be NULL. * @return The disposition of the instance. */ -TurnsDisposition turns_participant_get_disposition(TurnsParticipant const * self); +TurnsParticipantDisposition turns_participant_get_disposition(TurnsParticipant const * self); /** * @brief Get the name of a participant. @@ -93,7 +102,7 @@ void turns_participant_set_defeated(TurnsParticipant * self, gboolean value); * @param self A Participant instance. The value *must not* be NULL. * @param value The new disposition. */ -void turns_participant_set_disposition(TurnsParticipant * self, TurnsDisposition value); +void turns_participant_set_disposition(TurnsParticipant * self, TurnsParticipantDisposition value); /** * @brief Set the name of a participant. diff --git a/lib/src/turnsmm/enum_helpers.hpp b/lib/src/turnsmm/enum_helpers.hpp new file mode 100644 index 0000000..a3f1455 --- /dev/null +++ b/lib/src/turnsmm/enum_helpers.hpp @@ -0,0 +1,34 @@ +#ifndef TURNSMM_ENUM_HELPERS_HPP +#define TURNSMM_ENUM_HELPERS_HPP + +#include <glibmm/value.h> + +#include <glib-object.h> +#include <glib.h> + +#include <type_traits> + +#define TURNS_DECLARE_ENUM_VALUE_SPECIALIZATION(Enum) \ + template<> \ + class Value<::Turns::Enum> : public Glib::Value_Enum<::Turns::Enum> \ + { \ + public: \ + auto static value_type() -> GType; \ + } + +#define TURNS_DEFINE_VALUE_SPECIALIZATION(Enum, TurnsEnumName) \ + auto Value<::Turns::Enum>::value_type() -> GType \ + { \ + return ::turns_##TurnsEnumName##_get_type(); \ + } + +namespace Turns +{ + + template<auto Wrapped, auto Unwrapped> + auto constexpr enum_matches = + static_cast<std::underlying_type_t<decltype(Wrapped)>>(Wrapped) == static_cast<std::underlying_type_t<decltype(Unwrapped)>>(Unwrapped); + +} // namespace Turns + +#endif
\ No newline at end of file diff --git a/lib/src/turnsmm/enums.cpp b/lib/src/turnsmm/enums.cpp deleted file mode 100644 index 475d6f8..0000000 --- a/lib/src/turnsmm/enums.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "turnsmm/enums.hpp" - -#include "turns-disposition.h" -#include "turns-enums.h" // IWYU pragma: keep - -#include <glib-object.h> -#include <turns-turn-order.h> - -#include <type_traits> - -namespace -{ - template<auto Wrapped, auto Unwrapped> - auto constexpr matches = - static_cast<std::underlying_type_t<decltype(Wrapped)>>(Wrapped) == static_cast<std::underlying_type_t<decltype(Unwrapped)>>(Unwrapped); -} // namespace - -namespace Turns -{ - - static_assert(matches<Disposition::Neutral, TURNS_DISPOSITION_NEUTRAL>); - static_assert(matches<Disposition::Friendly, TURNS_DISPOSITION_FRIENDLY>); - static_assert(matches<Disposition::Hostile, TURNS_DISPOSITION_HOSTILE>); - static_assert(matches<Disposition::Secret, TURNS_DISPOSITION_SECRET>); - - static_assert(matches<SortMode::Descending, TURNS_TURN_ORDER_SORT_MODE_DESCENDING>); - static_assert(matches<SortMode::Ascending, TURNS_TURN_ORDER_SORT_MODE_ASCENDING>); - -} // namespace Turns - -namespace Glib -{ -#define VALUE_SPECIALIZATION(Enum, TurnsEnumName) \ - auto Value<Turns::Enum>::value_type() -> GType \ - { \ - return turns_##TurnsEnumName##_get_type(); \ - } - - VALUE_SPECIALIZATION(Disposition, disposition) - VALUE_SPECIALIZATION(SortMode, turn_order_sort_mode) - -#undef VALUE_SPECIALIZATION -} // namespace Glib
\ No newline at end of file diff --git a/lib/src/turnsmm/enums.hpp b/lib/src/turnsmm/enums.hpp deleted file mode 100644 index b866b33..0000000 --- a/lib/src/turnsmm/enums.hpp +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef TURNSMM_ENUMS_HPP -#define TURNSMM_ENUMS_HPP - -#include <glibmm/value.h> - -#include <glib-object.h> - -namespace Turns -{ - enum struct Disposition - { - Neutral, - Friendly, - Hostile, - Secret, - }; - - enum struct SortMode - { - Descending, - Ascending, - }; - -} // namespace Turns - -namespace Glib -{ - -#define VALUE_SPECIALIZATION(Enum) \ - template<> \ - class Value<Turns::Enum> : public Glib::Value_Enum<Turns::Enum> \ - { \ - public: \ - auto static value_type() -> GType; \ - } - - VALUE_SPECIALIZATION(Disposition); - VALUE_SPECIALIZATION(SortMode); - -#undef VALUE_SPECIALIZATION - -} // namespace Glib - -#endif
\ No newline at end of file diff --git a/lib/src/turnsmm/participant.cpp b/lib/src/turnsmm/participant.cpp index 8932b83..38dfb3e 100644 --- a/lib/src/turnsmm/participant.cpp +++ b/lib/src/turnsmm/participant.cpp @@ -1,8 +1,7 @@ #include "turnsmm/participant.hpp" -#include "turns-disposition.h" #include "turns-participant.h" -#include "turnsmm/enums.hpp" +#include "turnsmm/enum_helpers.hpp" #include "turnsmm/private/participant_p.hpp" #include <glibmm/class.h> @@ -133,7 +132,7 @@ namespace Turns auto Participant::set_disposition(Disposition value) noexcept -> void { - return turns_participant_set_disposition(unwrap(this), static_cast<TurnsDisposition>(value)); + return turns_participant_set_disposition(unwrap(this), static_cast<TurnsParticipantDisposition>(value)); } auto Participant::set_name(Glib::ustring const & value) noexcept -> void @@ -199,12 +198,18 @@ namespace Turns Participant::Participant(BaseObjectType * gobj) : Glib::Object((GObject *)gobj) { + static_assert(enum_matches<Disposition::Neutral, TURNS_PARTICIPANT_DISPOSITION_NEUTRAL>); + static_assert(enum_matches<Disposition::Friendly, TURNS_PARTICIPANT_DISPOSITION_FRIENDLY>); + static_assert(enum_matches<Disposition::Hostile, TURNS_PARTICIPANT_DISPOSITION_HOSTILE>); + static_assert(enum_matches<Disposition::Secret, TURNS_PARTICIPANT_DISPOSITION_SECRET>); } } // namespace Turns namespace Glib { + TURNS_DEFINE_VALUE_SPECIALIZATION(Participant::Disposition, participant_disposition); + auto wrap(TurnsParticipant * object, bool copy) -> Glib::RefPtr<Turns::Participant> { return Glib::make_refptr_for_instance<Turns::Participant>(dynamic_cast<Turns::Participant *>(Glib::wrap_auto(G_OBJECT(object), copy))); diff --git a/lib/src/turnsmm/participant.hpp b/lib/src/turnsmm/participant.hpp index 53a7787..b23fd42 100644 --- a/lib/src/turnsmm/participant.hpp +++ b/lib/src/turnsmm/participant.hpp @@ -2,7 +2,7 @@ #define TURNSMM_PARTICIPANT_HPP #include "turns-participant.h" -#include "turnsmm/enums.hpp" +#include "turnsmm/enum_helpers.hpp" #include <glibmm/object.h> #include <glibmm/propertyproxy.h> @@ -10,6 +10,7 @@ #include <glibmm/ustring.h> #include <glib-object.h> +#include <glib.h> namespace Turns { @@ -22,6 +23,14 @@ namespace Turns using CppClassType = class Participant_Class; using CppObjectType = Participant; + enum struct Disposition : guint + { + Neutral, + Friendly, + Hostile, + Secret, + }; + auto static get_base_type() -> GType; auto static get_type() -> GType; @@ -68,6 +77,8 @@ namespace Turns namespace Glib { + TURNS_DECLARE_ENUM_VALUE_SPECIALIZATION(Participant::Disposition); + auto wrap(TurnsParticipant * object, bool copy = false) -> Glib::RefPtr<Turns::Participant>; } // namespace Glib diff --git a/lib/src/turnsmm/turn-order.cpp b/lib/src/turnsmm/turn-order.cpp index 1f795d2..1636be9 100644 --- a/lib/src/turnsmm/turn-order.cpp +++ b/lib/src/turnsmm/turn-order.cpp @@ -1,7 +1,7 @@ #include "turnsmm/turn-order.hpp" #include "turns-turn-order.h" -#include "turnsmm/enums.hpp" +#include "turnsmm/enum_helpers.hpp" #include "turnsmm/participant.hpp" #include "turnsmm/private/turn-order_p.hpp" @@ -16,6 +16,7 @@ #include <giomm/listmodel.h> #include <glib-object.h> +#include <turns-enums.h> #include <bit> #include <cstddef> @@ -63,6 +64,8 @@ namespace Turns : Glib::ObjectBase{nullptr} , Glib::Object{Glib::ConstructParams{_class.init()}} { + static_assert(enum_matches<SortMode::Descending, TURNS_TURN_ORDER_SORT_MODE_DESCENDING>); + static_assert(enum_matches<SortMode::Ascending, TURNS_TURN_ORDER_SORT_MODE_ASCENDING>); } auto TurnOrder::gobj() noexcept -> BaseObjectType * @@ -130,6 +133,11 @@ namespace Turns namespace Glib { + auto Value<Turns ::TurnOrder ::SortMode>::value_type() -> GType + { + return turns_turn_order_sort_mode_get_type(); + } + auto wrap(TurnsTurnOrder * object, bool copy) -> Glib::RefPtr<Turns::TurnOrder> { return Glib::make_refptr_for_instance<Turns::TurnOrder>(dynamic_cast<Turns::TurnOrder *>(Glib::wrap_auto(G_OBJECT(object), copy))); diff --git a/lib/src/turnsmm/turn-order.hpp b/lib/src/turnsmm/turn-order.hpp index c135ab2..0283cb3 100644 --- a/lib/src/turnsmm/turn-order.hpp +++ b/lib/src/turnsmm/turn-order.hpp @@ -2,17 +2,18 @@ #define TURNSMM_TURN_ORDER_HPP #include "turns-turn-order.h" -#include "turnsmm/enums.hpp" #include "turnsmm/participant.hpp" #include <glibmm/object.h> #include <glibmm/propertyproxy.h> #include <glibmm/refptr.h> #include <glibmm/ustring.h> +#include <glibmm/value.h> #include <giomm/listmodel.h> #include <glib-object.h> +#include <glib.h> #include <cstddef> @@ -28,6 +29,12 @@ namespace Turns using CppClassType = class TurnOrder_Class; using CppObjectType = TurnOrder; + enum struct SortMode : guint + { + Descending, + Ascending, + }; + auto static get_base_type() -> GType; auto static get_type() -> GType; @@ -59,6 +66,13 @@ namespace Turns namespace Glib { + template<> + class Value<Turns::TurnOrder::SortMode> : public Glib ::Value_Enum<Turns::TurnOrder::SortMode> + { + public: + auto static value_type() -> GType; + }; + auto wrap(TurnsTurnOrder * object, bool copy = false) -> Glib::RefPtr<Turns::TurnOrder>; } // namespace Glib |
