From c2a70c379c3fb9c60798fd2c290922c8d4cb8e05 Mon Sep 17 00:00:00 2001 From: MasterLaplace Date: Fri, 8 Nov 2024 00:39:14 -0500 Subject: [PATCH] refactor: replace PACKED macros with LPL_PACKED macros for better struct packing --- .../Components/2D/Collider.hpp | 4 +- .../Components/2D/Control.hpp | 4 +- .../Components/2D/Movable.hpp | 4 +- .../Components/2D/RigidBody.hpp | 4 +- .../Components/2D/Transform.hpp | 4 +- .../Components/Common/Health.hpp | 4 +- .../Components/Common/Id.hpp | 4 +- .../Components/Common/Parent.hpp | 4 +- .../Components/Common/Spawned.hpp | 4 +- .../Components/Common/Weapon.hpp | 4 +- Flakkari/Engine/Math/Vector.hpp | 4 +- Flakkari/Network/Packed.hpp | 41 +++++++++++++ Flakkari/Protocol/Events.hpp | 4 +- Flakkari/Protocol/Header.hpp | 4 +- Flakkari/config.h.in | 60 +++++++++---------- 15 files changed, 97 insertions(+), 56 deletions(-) create mode 100644 Flakkari/Network/Packed.hpp diff --git a/Flakkari/Engine/EntityComponentSystem/Components/2D/Collider.hpp b/Flakkari/Engine/EntityComponentSystem/Components/2D/Collider.hpp index 2fd5cc10..3ad413e0 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/2D/Collider.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/2D/Collider.hpp @@ -14,7 +14,7 @@ #include namespace Flakkari::Engine::ECS::Components::_2D { -PACKED_START +LPL_PACKED_START /** * @brief Collider component for ECS entities that have a script attached to them @@ -39,7 +39,7 @@ struct Collider { std::size_t size() const { return sizeof(_size); } }; -PACKED_END +LPL_PACKED_END } // namespace Flakkari::Engine::ECS::Components::_2D #endif /* !COLLIDER_HPP_ */ diff --git a/Flakkari/Engine/EntityComponentSystem/Components/2D/Control.hpp b/Flakkari/Engine/EntityComponentSystem/Components/2D/Control.hpp index 0157a984..ec22c8ba 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/2D/Control.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/2D/Control.hpp @@ -13,7 +13,7 @@ #include "../../../Math/Vector.hpp" namespace Flakkari::Engine::ECS::Components::_2D { -PACKED_START +LPL_PACKED_START /** * @brief Control component for 2D entities (player, enemies, etc...) @@ -55,7 +55,7 @@ struct Control { std::size_t size() const { return sizeof(*this); } }; -PACKED_END +LPL_PACKED_END } // namespace Flakkari::Engine::ECS::Components::_2D #endif /* !FLAKKARI_CONTROL_HPP_ */ diff --git a/Flakkari/Engine/EntityComponentSystem/Components/2D/Movable.hpp b/Flakkari/Engine/EntityComponentSystem/Components/2D/Movable.hpp index 1feeb760..92c1d1fe 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/2D/Movable.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/2D/Movable.hpp @@ -13,7 +13,7 @@ #include "../../../Math/Vector.hpp" namespace Flakkari::Engine::ECS::Components::_2D { -PACKED_START +LPL_PACKED_START struct Movable { Math::Vector2f velocity; // pixels / second @@ -38,7 +38,7 @@ struct Movable { std::size_t size() const { return sizeof(*this); } }; -PACKED_END +LPL_PACKED_END } // namespace Flakkari::Engine::ECS::Components::_2D #endif /* !FLAKKARI_MOVABLE_HPP_ */ diff --git a/Flakkari/Engine/EntityComponentSystem/Components/2D/RigidBody.hpp b/Flakkari/Engine/EntityComponentSystem/Components/2D/RigidBody.hpp index 4336cb6d..da9214cc 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/2D/RigidBody.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/2D/RigidBody.hpp @@ -13,7 +13,7 @@ #include "../../../Math/Vector.hpp" namespace Flakkari::Engine::ECS::Components::_2D { -PACKED_START +LPL_PACKED_START /** * @brief RigidBody represent the physical properties of a rigid body in a game engine @@ -53,7 +53,7 @@ struct RigidBody { std::size_t size() const { return sizeof(*this); } }; -PACKED_END +LPL_PACKED_END } // namespace Flakkari::Engine::ECS::Components::_2D #endif /* !RIGIDBODY_HPP_ */ diff --git a/Flakkari/Engine/EntityComponentSystem/Components/2D/Transform.hpp b/Flakkari/Engine/EntityComponentSystem/Components/2D/Transform.hpp index 6b986304..012445a9 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/2D/Transform.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/2D/Transform.hpp @@ -13,7 +13,7 @@ #include "../../../Math/Vector.hpp" namespace Flakkari::Engine::ECS::Components::_2D { -PACKED_START +LPL_PACKED_START struct Transform { Math::Vector2f position; @@ -40,7 +40,7 @@ struct Transform { std::size_t size() const { return sizeof(*this); } }; -PACKED_END +LPL_PACKED_END } // namespace Flakkari::Engine::ECS::Components::_2D #endif /* !FLAKKARI_TRANSFORM_HPP_ */ diff --git a/Flakkari/Engine/EntityComponentSystem/Components/Common/Health.hpp b/Flakkari/Engine/EntityComponentSystem/Components/Common/Health.hpp index 3ed30c1a..a967e4aa 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/Common/Health.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/Common/Health.hpp @@ -15,7 +15,7 @@ #include "config.h.in" namespace Flakkari::Engine::ECS::Components::Common { -PACKED_START +LPL_PACKED_START /** * @brief Health is a structure that represents the life of an "living object" @@ -50,7 +50,7 @@ struct Health { std::size_t size() const { return sizeof(*this); } }; -PACKED_END +LPL_PACKED_END } // namespace Flakkari::Engine::ECS::Components::Common #endif /* !Health_HPP_ */ diff --git a/Flakkari/Engine/EntityComponentSystem/Components/Common/Id.hpp b/Flakkari/Engine/EntityComponentSystem/Components/Common/Id.hpp index 7d2502a7..3a65fe60 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/Common/Id.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/Common/Id.hpp @@ -15,7 +15,7 @@ #include "config.h.in" namespace Flakkari::Engine::ECS::Components::Common { -PACKED_START +LPL_PACKED_START struct Id { std::size_t id; @@ -35,7 +35,7 @@ struct Id { std::size_t size() const { return sizeof(id); } }; -PACKED_END +LPL_PACKED_END } // namespace Flakkari::Engine::ECS::Components::Common #endif /* !FLAKKARI_ID_HPP_ */ diff --git a/Flakkari/Engine/EntityComponentSystem/Components/Common/Parent.hpp b/Flakkari/Engine/EntityComponentSystem/Components/Common/Parent.hpp index 87c24fed..b5ca6ef8 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/Common/Parent.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/Common/Parent.hpp @@ -15,7 +15,7 @@ #include "config.h.in" namespace Flakkari::Engine::ECS::Components::Common { -PACKED_START +LPL_PACKED_START /** * @brief Parent component for ECS entities that have a parent entity attached to them @@ -40,7 +40,7 @@ struct Parent { std::size_t size() const { return sizeof(*this); } }; -PACKED_END +LPL_PACKED_END } // namespace Flakkari::Engine::ECS::Components::Common #endif /* !FLAKKARI_PARENT_HPP_ */ diff --git a/Flakkari/Engine/EntityComponentSystem/Components/Common/Spawned.hpp b/Flakkari/Engine/EntityComponentSystem/Components/Common/Spawned.hpp index 527ff653..a536edb0 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/Common/Spawned.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/Common/Spawned.hpp @@ -16,7 +16,7 @@ #include "config.h.in" namespace Flakkari::Engine::ECS::Components::Common { -PACKED_START +LPL_PACKED_START /** * @brief Spawned component for ECS entities that have a script attached to them @@ -41,7 +41,7 @@ struct Spawned { std::size_t size() const { return sizeof(has_spawned); } }; -PACKED_END +LPL_PACKED_END } // namespace Flakkari::Engine::ECS::Components::Common #endif /* !SPAWNED_HPP_ */ diff --git a/Flakkari/Engine/EntityComponentSystem/Components/Common/Weapon.hpp b/Flakkari/Engine/EntityComponentSystem/Components/Common/Weapon.hpp index 34ef415f..d3d01924 100644 --- a/Flakkari/Engine/EntityComponentSystem/Components/Common/Weapon.hpp +++ b/Flakkari/Engine/EntityComponentSystem/Components/Common/Weapon.hpp @@ -16,7 +16,7 @@ #include "config.h.in" namespace Flakkari::Engine::ECS::Components::Common { -PACKED_START +LPL_PACKED_START /** * @brief Weapon is a structure that defines the characteristics of a weapon. @@ -58,7 +58,7 @@ struct Weapon { std::size_t size() const { return sizeof(*this); }; }; -PACKED_END +LPL_PACKED_END } // namespace Flakkari::Engine::ECS::Components::Common #endif /* !WEAPON_HPP_ */ diff --git a/Flakkari/Engine/Math/Vector.hpp b/Flakkari/Engine/Math/Vector.hpp index 29bc308e..06e011b0 100644 --- a/Flakkari/Engine/Math/Vector.hpp +++ b/Flakkari/Engine/Math/Vector.hpp @@ -27,7 +27,7 @@ namespace Flakkari::Engine::Math { template struct Vector { - PACKED_START + LPL_PACKED_START union { struct { Type x; @@ -56,7 +56,7 @@ template struct Vector { Vector(Type x, Type y) : v{x, y, 0, 1} {}; Vector(Type x) : v{x, 0, 0, 1} {}; Vector(const Vector &other) : v{other.v[0], other.v[1], other.v[2], other.v[3]} {}; - PACKED_END + LPL_PACKED_END Vector &operator=(const Vector &other) { diff --git a/Flakkari/Network/Packed.hpp b/Flakkari/Network/Packed.hpp new file mode 100644 index 00000000..d68e227f --- /dev/null +++ b/Flakkari/Network/Packed.hpp @@ -0,0 +1,41 @@ +/************************************************************************** + * Flakkari Library v0.3.0 + * + * Flakkari Library is a C++ Library for Network. + * @file Packed.hpp + * @brief Packed header. Contains PACKED macros. + * (LPL_PACKED_START, LPL_PACKED_END, PACKED) + * + * @details LPL_PACKED_START and LPL_PACKED_END macros are used to pack structs: + * PACKED_<_> macros are used to pack structs: + * LPL_PACKED_START struct _ {}; LPL_PACKED_END + * PACKED macros are used to pack structs: + * struct _ {} PACKED; + * + * Flakkari Library is under MIT License. + * https://opensource.org/licenses/MIT + * © 2023 @MasterLaplace + * @version 0.3.0 + * @date 2023-01-10 + **************************************************************************/ + +#ifndef PACKED_HPP_ +#define PACKED_HPP_ + +#ifdef LPL_PACKED_START +# undef LPL_PACKED_START +#endif + +#ifdef LPL_PACKED_END +# undef LPL_PACKED_END +#endif + +#ifdef _MSC_VER +# define LPL_PACKED_START __pragma(pack(push, 1)) +# define LPL_PACKED_END __pragma(pack(pop)) +#else +# define LPL_PACKED_START _Pragma("pack(1)") +# define LPL_PACKED_END _Pragma("pack()") +#endif + +#endif /* !PACKED_HPP_ */ diff --git a/Flakkari/Protocol/Events.hpp b/Flakkari/Protocol/Events.hpp index 86272e37..0c3b1d8e 100644 --- a/Flakkari/Protocol/Events.hpp +++ b/Flakkari/Protocol/Events.hpp @@ -39,14 +39,14 @@ enum class EventState : uint8_t { MAX_STATE }; -PACKED_START +LPL_PACKED_START struct Event { EventId id; EventState state; }; -PACKED_END +LPL_PACKED_END } /* namespace V_0 */ diff --git a/Flakkari/Protocol/Header.hpp b/Flakkari/Protocol/Header.hpp index 2d1d0c70..f4fa79bf 100644 --- a/Flakkari/Protocol/Header.hpp +++ b/Flakkari/Protocol/Header.hpp @@ -57,7 +57,7 @@ enum class Priority : byte { MAX_PRIORITY = 4 }; -PACKED_START +LPL_PACKED_START template struct Header { Priority _priority : 4 = Priority::LOW; @@ -69,7 +69,7 @@ template struct Header { .count()); }; -PACKED_END +LPL_PACKED_END } // namespace V_0 diff --git a/Flakkari/config.h.in b/Flakkari/config.h.in index c46553ce..c812d19b 100644 --- a/Flakkari/config.h.in +++ b/Flakkari/config.h.in @@ -36,32 +36,32 @@ #endif -#ifndef CONFIG_UTILS - #define CONFIG_UTILS +#ifndef LAPLACE_CONFIG_UTILS + #define LAPLACE_CONFIG_UTILS //////////////////////////////////////////////////////////// // Define shared portable macros for various compilers //////////////////////////////////////////////////////////// -#define NEED_COMMA struct _ -#define ATTRIBUTE(key) __attribute__((key)) -#define UNUSED_ATTRIBUTE ATTRIBUTE(unused) -#define UNUSED(x) (void)(x) +#define LPL_NEED_COMMA struct _ +#define LPL_ATTRIBUTE(key) __attribute__((key)) +#define LPL_UNUSED_ATTRIBUTE LPL_ATTRIBUTE(unused) +#define LPL_UNUSED(x) (void)(x) //////////////////////////////////////////////////////////// // Define portable NULL pointer using C++11 nullptr keyword //////////////////////////////////////////////////////////// #if defined(__cplusplus) && __cplusplus >= 201103L + #define lpl_nullptr nullptr #elif !defined(NULL) - #define nullptr ((void*)0) + #define lpl_nullptr ((void*)0) #else - #define nullptr NULL + #define lpl_nullptr NULL #endif //////////////////////////////////////////////////////////// // Define boolean type and values //////////////////////////////////////////////////////////// -#if defined(__cplusplus) -#elif !defined(__bool_true_false_are_defined) +#if !defined(__bool_true_false_are_defined) && !defined(__cplusplus) #define bool _Bool #define true 1 #define false 0 @@ -71,7 +71,7 @@ #if defined __GNUC__ && defined __GNUC_MINOR__ # define __GNUC_PREREQ(maj, min) \ ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) -#else +#elif !defined(__GNUC_PREREQ) # define __GNUC_PREREQ(maj, min) 0 #endif @@ -80,34 +80,34 @@ //////////////////////////////////////////////////////////// /** Usage: * @example - * PACKED(struct MyStruct + * LPL_PACKED(struct MyStruct * { * int a; * char b; * ... * }); \**********************************************************/ -#if defined(__GNUC__) || defined(__GNUG__) - #define PACKED( __Declaration__ ) __Declaration__ __attribute__((__packed__)) - #define PACKED_START _Pragma("pack(1)") - #define PACKED_END _Pragma("pack()") -#elif _MSC_VER - #define PACKED( __Declaration__ ) __pragma(pack(push, 1)) __Declaration__ __pragma(pack(pop)) - #define PACKED_START __pragma(pack(push, 1)) - #define PACKED_END __pragma(pack(pop)) +#if defined(_MSC_VER) || defined(_MSVC_LANG) + #define LPL_PACKED( __Declaration__ ) __pragma(pack(push, 1)) __Declaration__ __pragma(pack(pop)) + #define LPL_PACKED_START __pragma(pack(push, 1)) + #define LPL_PACKED_END __pragma(pack(pop)) +#elif defined(__GNUC__) || defined(__GNUG__) + #define LPL_PACKED( __Declaration__ ) __Declaration__ __attribute__((__packed__)) + #define LPL_PACKED_START _Pragma("pack(1)") + #define LPL_PACKED_END _Pragma("pack()") #else - #define PACKED( __Declaration__ ) __Declaration__ - #define PACKED_START _Pragma("pack(1)") - #define PACKED_END _Pragma("pack()") + #define LPL_PACKED( __Declaration__ ) __Declaration__ + #define LPL_PACKED_START + #define LPL_PACKED_END #endif //////////////////////////////////////////////////////////// // Helper macro to convert a macro to a string //////////////////////////////////////////////////////////// -#define STRINGIFY(x) #x -#define TOSTRING(x) STRINGIFY(x) +#define LPL_STRINGIFY(x) #x +#define LPL_TOSTRING(x) LPL_STRINGIFY(x) -#endif /* !CONFIG_UTILS */ +#endif /* !LAPLACE_CONFIG_UTILS */ #ifndef FLAKKARI_DISTRIBUTION_H_ @@ -474,10 +474,10 @@ // Define the FLAKKARI version string //////////////////////////////////////////////////////////// #define FLAKKARI_VERSION_STRING \ - TOSTRING(FLAKKARI_VERSION_MAJOR) "." \ - TOSTRING(FLAKKARI_VERSION_MINOR) "." \ - TOSTRING(FLAKKARI_VERSION_PATCH) "." \ - TOSTRING(FLAKKARI_VERSION_TWEAK) + LPL_TOSTRING(FLAKKARI_VERSION_MAJOR) "." \ + LPL_TOSTRING(FLAKKARI_VERSION_MINOR) "." \ + LPL_TOSTRING(FLAKKARI_VERSION_PATCH) "." \ + LPL_TOSTRING(FLAKKARI_VERSION_TWEAK) #endif /* !FLAKKARI_VERSION_H_ */