diff --git a/include/ecsm-error.hpp b/include/ecsm-error.hpp index 74fd4ab..bcd91e1 100644 --- a/include/ecsm-error.hpp +++ b/include/ecsm-error.hpp @@ -24,20 +24,18 @@ namespace ecsm { -using namespace std; - /** * @brief ECSM error (exception) class. */ -class EcsmError : public exception +class EcsmError : public std::exception { - string message; + std::string message; public: /** * @brief Creates a new ECSM error (exception) instance. * @param message target error message */ - EcsmError(const string& message) : message(message) { } + EcsmError(const std::string& message) : message(message) { } /** * @brief Returns ECSM error message C-string. diff --git a/include/ecsm.hpp b/include/ecsm.hpp index 2138f0d..69a14db 100644 --- a/include/ecsm.hpp +++ b/include/ecsm.hpp @@ -129,12 +129,12 @@ class System * @brief Returns specific component name of the system. * @note Override it to define a custom component of the system. */ - virtual const string& getComponentName() const; + virtual const std::string& getComponentName() const; /** * @brief Returns specific component typeid() of the system. * @note Override it to define a custom component of the system. */ - virtual type_index getComponentType() const; + virtual std::type_index getComponentType() const; /** * @brief Returns specific system component @ref View. * @note Override it to define a custom component of the system. @@ -158,8 +158,8 @@ class System class Entity final { public: - using SystemComponent = pair>; - using Components = map; + using SystemComponent = std::pair>; + using Components = std::map; private: Components components; friend class Manager; @@ -207,21 +207,21 @@ class Manager final : public Singleton { using Subscribers = std::vector>; - string name; + std::string name; Subscribers subscribers; bool isOrdered = false; - Event(const string& name, bool isOrdered = true) : name(name), isOrdered(isOrdered) { } + Event(const std::string& name, bool isOrdered = true) : name(name), isOrdered(isOrdered) { } }; - using Systems = unordered_map; - using ComponentTypes = unordered_map; - using ComponentNames = map; - using Events = unordered_map; - using OrderedEvents = vector; + using Systems = std::unordered_map; + using ComponentTypes = std::unordered_map; + using ComponentNames = std::map; + using Events = std::unordered_map; + using OrderedEvents = std::vector; using EntityPool = LinearPool; - using GarbageComponent = pair>; - using GarbageComponents = set; + using GarbageComponent = std::pair>; + using GarbageComponents = std::set; private: Systems systems; ComponentTypes componentTypes; @@ -230,14 +230,14 @@ class Manager final : public Singleton Events events; OrderedEvents orderedEvents; GarbageComponents garbageComponents; - mutex locker; + std::mutex locker; bool initialized = false; #ifndef NDEBUG bool isChanging = false; #endif - void addSystem(System* system, type_index type); + void addSystem(System* system, std::type_index type); public: bool isRunning = false; @@ -267,7 +267,7 @@ class Manager final : public Singleton template void createSystem(Args&&... args) { - static_assert(is_base_of_v, "Must be derived from the System class."); + static_assert(std::is_base_of_v, "Must be derived from the System class."); #ifndef NDEBUG if (isChanging) throw EcsmError("Creation of the system inside other create/destroy is not allowed."); @@ -287,7 +287,7 @@ class Manager final : public Singleton * @param type target system typeid() * @throw EcsmError if system is not found. */ - void destroySystem(type_index type); + void destroySystem(std::type_index type); /** * @brief Terminates and destroys system. * @tparam T target system type @@ -296,7 +296,7 @@ class Manager final : public Singleton template void destroySystem() { - static_assert(is_base_of_v, "Must be derived from the System class."); + static_assert(std::is_base_of_v, "Must be derived from the System class."); destroySystem(typeid(T)); } @@ -305,7 +305,7 @@ class Manager final : public Singleton * @param type target system typeid() * @return True if system is destroyed, otherwise false. */ - bool tryDestroySystem(type_index type); + bool tryDestroySystem(std::type_index type); /** * @brief Terminates and destroys system if exist. * @tparam T target system type @@ -314,7 +314,7 @@ class Manager final : public Singleton template bool tryDestroySystem() { - static_assert(is_base_of_v, "Must be derived from the System class."); + static_assert(std::is_base_of_v, "Must be derived from the System class."); return tryDestroySystem(typeid(T)); } @@ -322,7 +322,7 @@ class Manager final : public Singleton * @brief Returns true if system is created. * @param type target system typeid() */ - bool has(type_index type) const noexcept + bool has(std::type_index type) const noexcept { return systems.find(type) != systems.end(); } @@ -333,7 +333,7 @@ class Manager final : public Singleton template bool has() const noexcept { - static_assert(is_base_of_v, "Must be derived from the System class."); + static_assert(std::is_base_of_v, "Must be derived from the System class."); return has(typeid(T)); } @@ -343,7 +343,7 @@ class Manager final : public Singleton * @param type target system typeid() * @throw EcsmError if system is not found. */ - System* get(type_index type) const + System* get(std::type_index type) const { auto result = systems.find(type); if (result == systems.end()) @@ -358,7 +358,7 @@ class Manager final : public Singleton template T* get() const { - static_assert(is_base_of_v, "Must be derived from the System class."); + static_assert(std::is_base_of_v, "Must be derived from the System class."); return (T*)get(typeid(T)); } @@ -366,7 +366,7 @@ class Manager final : public Singleton * @brief Returns system instance if created, otherwise nullptr. * @param type target system typeid() */ - System* tryGet(type_index type) const noexcept + System* tryGet(std::type_index type) const noexcept { auto result = systems.find(type); return result == systems.end() ? nullptr : result->second; @@ -378,7 +378,7 @@ class Manager final : public Singleton template T* tryGet() const noexcept { - static_assert(is_base_of_v, "Must be derived from the System class."); + static_assert(std::is_base_of_v, "Must be derived from the System class."); return (T*)tryGet(typeid(T)); } @@ -411,7 +411,7 @@ class Manager final : public Singleton * @return Returns @ref View of the created component. * @throw EcsmError if component type is not registered, or component is already added. */ - View add(ID entity, type_index componentType); + View add(ID entity, std::type_index componentType); /** * @brief Adds a new component to the entity. @@ -431,7 +431,7 @@ class Manager final : public Singleton template View add(ID entity) { - static_assert(is_base_of_v, "Must be derived from the Component struct."); + static_assert(std::is_base_of_v, "Must be derived from the Component struct."); return View(add(entity, typeid(T))); } @@ -445,7 +445,7 @@ class Manager final : public Singleton * * @throw EcsmError if component is not found. */ - void remove(ID entity, type_index componentType); + void remove(ID entity, std::type_index componentType); /** * @brief Removes component from the entity. * @details Component data destruction is handled by the @ref System. @@ -459,7 +459,7 @@ class Manager final : public Singleton template void remove(ID entity) { - static_assert(is_base_of_v, "Must be derived from the Component struct."); + static_assert(std::is_base_of_v, "Must be derived from the Component struct."); remove(entity, typeid(T)); } @@ -471,9 +471,9 @@ class Manager final : public Singleton * @param entity entity instance * @param componentType target component typeid() */ - bool isGarbage(ID entity, type_index componentType) const noexcept + bool isGarbage(ID entity, std::type_index componentType) const noexcept { - return garbageComponents.find(make_pair(componentType, entity)) != garbageComponents.end(); + return garbageComponents.find(std::make_pair(componentType, entity)) != garbageComponents.end(); } /** * @brief Returns true if target entity component was removed and is in the garbage pool. @@ -486,7 +486,7 @@ class Manager final : public Singleton template bool isGarbage(ID entity) const noexcept { - static_assert(is_base_of_v, "Must be derived from the Component struct."); + static_assert(std::is_base_of_v, "Must be derived from the Component struct."); return isGarbage(entity, typeid(T)); } @@ -500,7 +500,7 @@ class Manager final : public Singleton * * @throw EcsmError if source or destination component is not found. */ - void copy(ID source, ID destination, type_index componentType); + void copy(ID source, ID destination, std::type_index componentType); /** * @brief Copies component data from source entity to destination. * @details Component data copying is handled by the @ref System. @@ -514,7 +514,7 @@ class Manager final : public Singleton template void copy(ID source, ID destination) { - static_assert(is_base_of_v, "Must be derived from the Component struct."); + static_assert(std::is_base_of_v, "Must be derived from the Component struct."); copy(source, destination, typeid(T)); } @@ -532,7 +532,7 @@ class Manager final : public Singleton * @param entity entity instance * @param componentType target component typeid() */ - bool has(ID entity, type_index componentType) const noexcept + bool has(ID entity, std::type_index componentType) const noexcept { assert(entity); const auto& components = entities.get(entity)->components; @@ -549,7 +549,7 @@ class Manager final : public Singleton template bool has(ID entity) const noexcept { - static_assert(is_base_of_v, "Must be derived from the Component struct."); + static_assert(std::is_base_of_v, "Must be derived from the Component struct."); return has(entity, typeid(T)); } @@ -562,7 +562,7 @@ class Manager final : public Singleton * * @throw EcsmError if component is not found. */ - View get(ID entity, type_index componentType) const + View get(ID entity, std::type_index componentType) const { assert(entity); auto entityView = entities.get(entity); @@ -572,7 +572,7 @@ class Manager final : public Singleton { throw EcsmError("Component is not added. (" "type: " + typeToString(componentType) + - "entity:" + to_string(*entity) + ")"); + "entity:" + std::to_string(*entity) + ")"); } auto pair = result->second; @@ -590,7 +590,7 @@ class Manager final : public Singleton template View get(ID entity) const { - static_assert(is_base_of_v, "Must be derived from the Component struct."); + static_assert(std::is_base_of_v, "Must be derived from the Component struct."); return View(get(entity, typeid(T))); } @@ -602,7 +602,7 @@ class Manager final : public Singleton * @param entity entity instance * @param componentType target component typeid() */ - View tryGet(ID entity, type_index componentType) const noexcept + View tryGet(ID entity, std::type_index componentType) const noexcept { assert(entity); const auto& components = entities.get(entity)->components; @@ -628,7 +628,7 @@ class Manager final : public Singleton template View tryGet(ID entity) const noexcept { - static_assert(is_base_of_v, "Must be derived from the Component struct."); + static_assert(std::is_base_of_v, "Must be derived from the Component struct."); return View(tryGet(entity, typeid(T))); } @@ -641,7 +641,7 @@ class Manager final : public Singleton * * @throw EcsmError if component is not found. */ - ID getID(ID entity, type_index componentType) const + ID getID(ID entity, std::type_index componentType) const { assert(entity); auto entityView = entities.get(entity); @@ -651,7 +651,7 @@ class Manager final : public Singleton { throw EcsmError("Component is not added. (" "type: " + typeToString(componentType) + - "entity:" + to_string(*entity) + ")"); + "entity:" + std::to_string(*entity) + ")"); } return result->second.second; @@ -668,7 +668,7 @@ class Manager final : public Singleton template ID getID(ID entity) const { - static_assert(is_base_of_v, "Must be derived from the Component struct."); + static_assert(std::is_base_of_v, "Must be derived from the Component struct."); return ID(getID(entity, typeid(T))); } @@ -680,7 +680,7 @@ class Manager final : public Singleton * @param entity entity instance * @param componentType target component typeid() */ - ID tryGetID(ID entity, type_index componentType) const noexcept + ID tryGetID(ID entity, std::type_index componentType) const noexcept { assert(entity); const auto& components = entities.get(entity)->components; @@ -705,7 +705,7 @@ class Manager final : public Singleton template ID tryGetID(ID entity) const noexcept { - static_assert(is_base_of_v, "Must be derived from the Component struct."); + static_assert(std::is_base_of_v, "Must be derived from the Component struct."); return ID(tryGetID(entity, typeid(T))); } @@ -723,13 +723,13 @@ class Manager final : public Singleton * @param[in] name target event name * @throw EcsmError if event is already registered. */ - void registerEvent(const string& name); + void registerEvent(const std::string& name); /** * @brief Registers a new unordered event if not exist. * @param[in] name target event name * @return True if event is registered, otherwise false. */ - bool tryRegisterEvent(const string& name); + bool tryRegisterEvent(const std::string& name); /** * @brief Registers a new ordered event before another. @@ -739,7 +739,7 @@ class Manager final : public Singleton * * @throw EcsmError if event is already registered. */ - void registerEventBefore(const string& newEvent, const string& beforeEvent); + void registerEventBefore(const std::string& newEvent, const std::string& beforeEvent); /** * @brief Registers a new ordered event after another. * @@ -748,26 +748,26 @@ class Manager final : public Singleton * * @throw EcsmError if event is already registered. */ - void registerEventAfter(const string& newEvent, const string& afterEvent); + void registerEventAfter(const std::string& newEvent, const std::string& afterEvent); /** * @brief Unregisters existing event. * @param[in] name target event name * @throw EcsmError if event is not registered, or not found. */ - void unregisterEvent(const string& name); + void unregisterEvent(const std::string& name); /** * @brief Unregisters event if exist. * @param[in] name target event name * @return True if event is unregistered, otherwise false. */ - bool tryUnregisterEvent(const string& name); + bool tryUnregisterEvent(const std::string& name); /** * @brief Returns true if event is registered. * @param[in] name target event name */ - bool hasEvent(const string& name) const noexcept + bool hasEvent(const std::string& name) const noexcept { assert(!name.empty()); return events.find(name) != events.end(); @@ -777,32 +777,32 @@ class Manager final : public Singleton * @param[in] name target event name * @throw EcsmError if event is not registered. */ - bool isEventOrdered(const string& name) const; + bool isEventOrdered(const std::string& name) const; /** * @brief Returns all event subscribers. * @param[in] name target event name * @throw EcsmError if event is not registered. */ - const Event::Subscribers& getEventSubscribers(const string& name) const; + const Event::Subscribers& getEventSubscribers(const std::string& name) const; /** * @brief Returns true if event has subscribers. * @param[in] name target event name * @throw EcsmError if event is not registered. */ - bool isEventHasSubscribers(const string& name) const; + bool isEventHasSubscribers(const std::string& name) const; /** * @brief Calls all event subscribers. * @param[in] name target event name * @throw EcsmError if event is not registered. */ - void runEvent(const string& name); + void runEvent(const std::string& name); /** * @brief Calls all event subscribers if event exist. * @param[in] name target event name * @return True if event is found. */ - bool tryRunEvent(const string& name); + bool tryRunEvent(const std::string& name); /** * @brief Runs all ordered events. * @details Unordered events subscribers are not called. @@ -817,7 +817,7 @@ class Manager final : public Singleton * * @throw EcsmError if event is not registered. */ - void subscribeToEvent(const string& name, const std::function& onEvent); + void subscribeToEvent(const std::string& name, const std::function& onEvent); /** * @brief Removes existing event subscriber. * @@ -826,7 +826,7 @@ class Manager final : public Singleton * * @throw EcsmError if event is not registered, or not subscribed. */ - void unsubscribeFromEvent(const string& name, const std::function& onEvent); + void unsubscribeFromEvent(const std::string& name, const std::function& onEvent); /** * @brief Adds a new event subscriber if not exist. @@ -836,7 +836,7 @@ class Manager final : public Singleton * * @return True if subscribed to the event, otherwise false. */ - bool trySubscribeToEvent(const string& name, const std::function& onEvent); + bool trySubscribeToEvent(const std::string& name, const std::function& onEvent); /** * @brief Removes existing event subscriber if exist. * @@ -845,7 +845,7 @@ class Manager final : public Singleton * * @throw True if unsubscribed from the event, otherwise false. */ - bool tryUnsubscribeFromEvent(const string& name, const std::function& onEvent); + bool tryUnsubscribeFromEvent(const std::string& name, const std::function& onEvent); /******************************************************************************************************************* * @brief Returns all manager systems. @@ -1003,16 +1003,16 @@ class ComponentSystem : public System /** * @brief Returns specific component name of the system. */ - const string& getComponentName() const override + const std::string& getComponentName() const override { - static const string name = typeToString(typeid(T)); + static const std::string name = typeToString(typeid(T)); return name; } /** * @brief Returns specific component typeid() of the system. * @note Override it to define a custom component of the system. */ - type_index getComponentType() const override + std::type_index getComponentType() const override { return typeid(T); } @@ -1096,7 +1096,7 @@ class DoNotDestroySystem : public ComponentSystem, */ ~DoNotDestroySystem() override; - const string& getComponentName() const override; + const std::string& getComponentName() const override; friend class ecsm::Manager; }; @@ -1123,7 +1123,7 @@ class DoNotDuplicateSystem : public ComponentSystem #include #include +#include #include #include +#include namespace ecsm { @@ -230,7 +232,7 @@ template struct Ref final { private: - atomic* counter = nullptr; + std::atomic* counter = nullptr; ID item = {}; public: constexpr Ref() = default; @@ -243,7 +245,7 @@ struct Ref final constexpr explicit Ref(ID item) : item(item) { if (item) - counter = new atomic(1); + counter = new std::atomic(1); } /** * @brief Destroys item reference. (Decrements or deallocates counter) @@ -254,7 +256,7 @@ struct Ref final return; if (counter->fetch_sub(1, memory_order_release) == 1) { - atomic_thread_fence(memory_order_acquire); + std::atomic_thread_fence(memory_order_acquire); delete counter; } } @@ -281,7 +283,7 @@ struct Ref final { if (item && counter->fetch_sub(1, memory_order_release) == 1) { - atomic_thread_fence(memory_order_acquire); + std::atomic_thread_fence(memory_order_acquire); delete counter; } @@ -298,7 +300,7 @@ struct Ref final { if (item && counter->fetch_sub(1, memory_order_release) == 1) { - atomic_thread_fence(memory_order_acquire); + std::atomic_thread_fence(memory_order_acquire); delete counter; } @@ -406,12 +408,12 @@ static constexpr bool operator<(ID i, const Ref& r) noexcept { return i < * @tparam DestroyItems linear pool should call destroy() function of the items */ template -class LinearPool +class LinearPool final { T* items = nullptr; uint32_t occupancy = 0, capacity = 1; - stack> freeItems; - vector> garbageItems; + std::stack> freeItems; + std::vector> garbageItems; #ifndef NDEBUG uint64_t version = 0; @@ -682,6 +684,98 @@ class LinearPool isChanging = false; #endif } + + /******************************************************************************************************************* + * @brief Linear pool iterator class. + */ + struct Iterator + { + using iterator_category = std::random_access_iterator_tag; + using value_type = T; + using difference_type = ptrdiff_t; + using pointer = value_type*; + using reference = value_type&; + private: + pointer ptr = nullptr; + public: + Iterator(pointer ptr) noexcept : ptr(ptr) { } + Iterator& operator=(const Iterator& i) noexcept = default; + Iterator& operator=(pointer ptr) noexcept { this->ptr = ptr; return (*this); } + + operator bool() const noexcept { return ptr; } + + bool operator==(const Iterator& i) const noexcept { return ptr == i.ptr; } + bool operator!=(const Iterator& i) const noexcept { return ptr != i.ptr; } + + Iterator& operator+=(const difference_type& m) noexcept { ptr += m; return *this; } + Iterator& operator-=(const difference_type& m) noexcept { ptr -= m; return *this; } + Iterator& operator++() noexcept { ++ptr; return *this; } + Iterator& operator--() noexcept { --ptr; return *this; } + Iterator operator++(int) noexcept { auto tmp = *this; ++ptr; return tmp; } + Iterator operator--(int) noexcept { auto tmp = *this; --ptr; return tmp; } + Iterator operator+(const difference_type& m) noexcept { return Iterator(ptr + m); } + Iterator operator-(const difference_type& m) noexcept { return Iterator(ptr - m); } + difference_type operator-(const Iterator& i) noexcept { return std::distance(i.ptr, ptr); } + + reference operator*() noexcept { return *ptr; } + const reference operator*() const noexcept { return *ptr; } + pointer operator->() noexcept { return ptr; } + const pointer operator->() const noexcept { return ptr; } + }; + + /******************************************************************************************************************* + * @brief Linear pool constant iterator class. + */ + struct ConstantIterator + { + using iterator_category = std::random_access_iterator_tag; + using value_type = const T; + using difference_type = ptrdiff_t; + using pointer = value_type*; + using reference = value_type&; + private: + pointer ptr = nullptr; + public: + Iterator(pointer ptr) noexcept : ptr(ptr) { } + Iterator& operator=(const Iterator& i) noexcept = default; + Iterator& operator=(pointer ptr) noexcept { this->ptr = ptr; return (*this); } + + operator bool() const noexcept { return ptr; } + + bool operator==(const Iterator& i) const noexcept { return ptr == i.ptr; } + bool operator!=(const Iterator& i) const noexcept { return ptr != i.ptr; } + + Iterator& operator+=(const difference_type& m) noexcept { ptr += m; return *this; } + Iterator& operator-=(const difference_type& m) noexcept { ptr -= m; return *this; } + Iterator& operator++() noexcept { ++ptr; return *this; } + Iterator& operator--() noexcept { --ptr; return *this; } + Iterator operator++(int) noexcept { auto tmp = *this; ++ptr; return tmp; } + Iterator operator--(int) noexcept { auto tmp = *this; --ptr; return tmp; } + Iterator operator+(const difference_type& m) noexcept { return Iterator(ptr + m); } + Iterator operator-(const difference_type& m) noexcept { return Iterator(ptr - m); } + difference_type operator-(const Iterator& i) noexcept { return std::distance(i.ptr, ptr); } + + reference operator*() const noexcept { return *ptr; } + pointer operator->() const noexcept { return ptr; } + }; + + /** + * @brief Returns an iterator pointing to the first element in the items array. + */ + Iterator begin() noexcept { return Iterator(&items[0]); } + /** + * @brief Returns an iterator pointing to the past-the-end element in the items array. + */ + Iterator end() noexcept { return Iterator(&items[capacity]); } + + /** + * @brief Returns a constant iterator pointing to the first element in the items array. + */ + ConstantIterator begin() const noexcept { return ConstantIterator(&items[0]); } + /** + * @brief Returns a constant iterator pointing to the past-the-end element in the items array. + */ + ConstantIterator end() const noexcept { return ConstantIterator(&items[capacity]); } }; } // namespace ecsm \ No newline at end of file diff --git a/include/singleton.hpp b/include/singleton.hpp index 3af7aab..e0375fd 100644 --- a/include/singleton.hpp +++ b/include/singleton.hpp @@ -25,9 +25,9 @@ namespace ecsm { -void* getManagerSystem(type_index type); -bool hasManagerSystem(type_index type); -void* tryGetManagerSystem(type_index type); +void* getManagerSystem(std::type_index type); +bool hasManagerSystem(std::type_index type); +void* tryGetManagerSystem(std::type_index type); /** * @brief Base singleton class. diff --git a/include/type-string.hpp b/include/type-string.hpp index 2eb6ace..410f47c 100644 --- a/include/type-string.hpp +++ b/include/type-string.hpp @@ -29,13 +29,11 @@ namespace ecsm { -using namespace std; - /** * @brief Returns @ref type_index string representation. * @param type target type */ -static string typeToString(type_index type) +static std::string typeToString(std::type_index type) { auto name = type.name(); @@ -46,11 +44,11 @@ static string typeToString(type_index type) name = demangledName; #endif - string result; + std::string result; if (strlen(name) > 0) result.assign(name); else - result = to_string(type.hash_code()); + result = std::to_string(type.hash_code()); #ifdef __GNUG__ free(demangledName); @@ -62,7 +60,7 @@ static string typeToString(type_index type) * @tparam T target type */ template -static string typeToString() +static std::string typeToString() { return typeToString(typeid(T)); } diff --git a/source/ecsm.cpp b/source/ecsm.cpp index 73671ba..285baf7 100644 --- a/source/ecsm.cpp +++ b/source/ecsm.cpp @@ -30,12 +30,12 @@ void System::copyComponent(View source, View destination) throw EcsmError("System has no components."); } -const string& System::getComponentName() const +const std::string& System::getComponentName() const { - static const string name = ""; + static const std::string name = ""; return name; } -type_index System::getComponentType() const +std::type_index System::getComponentType() const { return typeid(Component); } @@ -100,7 +100,7 @@ Manager::~Manager() unsetSingleton(); } -void Manager::addSystem(System* system, type_index type) +void Manager::addSystem(System* system, std::type_index type) { auto componentType = system->getComponentType(); if (componentType != typeid(Component)) @@ -136,7 +136,7 @@ void Manager::addSystem(System* system, type_index type) } //********************************************************************************************************************** -void Manager::destroySystem(type_index type) +void Manager::destroySystem(std::type_index type) { #ifndef NDEBUG if (isChanging) @@ -188,7 +188,7 @@ void Manager::destroySystem(type_index type) isChanging = false; #endif } -bool Manager::tryDestroySystem(type_index type) +bool Manager::tryDestroySystem(std::type_index type) { #ifndef NDEBUG if (isChanging) @@ -216,7 +216,7 @@ bool Manager::tryDestroySystem(type_index type) } //********************************************************************************************************************** -View Manager::add(ID entity, type_index componentType) +View Manager::add(ID entity, std::type_index componentType) { assert(entity); @@ -225,7 +225,7 @@ View Manager::add(ID entity, type_index componentType) { throw EcsmError("Component is not registered by any system. (" "type: " + typeToString(componentType) + - "entity:" + to_string(*entity) + ")"); + "entity:" + std::to_string(*entity) + ")"); } auto system = result->second; @@ -234,16 +234,16 @@ View Manager::add(ID entity, type_index componentType) componentView->entity = entity; auto entityView = entities.get(entity); - if (!entityView->components.emplace(componentType, make_pair(system, component)).second) + if (!entityView->components.emplace(componentType, std::make_pair(system, component)).second) { throw EcsmError("Component is already added to the entity. (" "type: " + typeToString(componentType) + - "entity:" + to_string(*entity) + ")"); + "entity:" + std::to_string(*entity) + ")"); } return componentView; } -void Manager::remove(ID entity, type_index componentType) +void Manager::remove(ID entity, std::type_index componentType) { assert(entity); auto entityView = entities.get(entity); @@ -254,7 +254,7 @@ void Manager::remove(ID entity, type_index componentType) { throw EcsmError("Component is not added. (" "type: " + typeToString(componentType) + - "entity:" + to_string(*entity) + ")"); + "entity:" + std::to_string(*entity) + ")"); } auto result = garbageComponents.emplace(make_pair(componentType, entity)); @@ -262,10 +262,10 @@ void Manager::remove(ID entity, type_index componentType) { throw EcsmError("Already removed component. (" "type: " + typeToString(componentType) + - "entity: " + to_string(*entity) + ")"); + "entity: " + std::to_string(*entity) + ")"); } } -void Manager::copy(ID source, ID destination, type_index componentType) +void Manager::copy(ID source, ID destination, std::type_index componentType) { assert(source); assert(destination); @@ -279,13 +279,13 @@ void Manager::copy(ID source, ID destination, type_index compone { throw EcsmError("Source component is not added. (" "type: " + typeToString(componentType) + - "entity:" + to_string(*source) + ")"); + "entity:" + std::to_string(*source) + ")"); } if (destinationIter == destinationView->components.end()) { throw EcsmError("Destination component is not added. (" "type: " + typeToString(componentType) + - "entity:" + to_string(*destination) + ")"); + "entity:" + std::to_string(*destination) + ")"); } auto system = sourceIter->second.first; @@ -310,11 +310,11 @@ ID Manager::duplicate(ID entity) system->copyComponent(sourceView, destinationView); auto duplicateView = entities.get(duplicateEntity); // Do not optimize/move getter here! - if (!duplicateView->components.emplace(pair.first, make_pair(system, duplicateComponent)).second) + if (!duplicateView->components.emplace(pair.first, std::make_pair(system, duplicateComponent)).second) { throw EcsmError("Component is already added to the entity. (" "type: " + typeToString(pair.first) + - "entity:" + to_string(*entity) + ")"); + "entity:" + std::to_string(*entity) + ")"); } } @@ -322,19 +322,19 @@ ID Manager::duplicate(ID entity) } //********************************************************************************************************************** -void Manager::registerEvent(const string& name) +void Manager::registerEvent(const std::string& name) { assert(!name.empty()); if (!events.emplace(name, new Event(name, false)).second) throw EcsmError("Event is already registered. (name: " + name + ")"); } -bool Manager::tryRegisterEvent(const string& name) +bool Manager::tryRegisterEvent(const std::string& name) { assert(!name.empty()); return events.emplace(name, new Event(name, false)).second; } -void Manager::registerEventBefore(const string& newEvent, const string& beforeEvent) +void Manager::registerEventBefore(const std::string& newEvent, const std::string& beforeEvent) { assert(!newEvent.empty()); assert(!beforeEvent.empty()); @@ -354,7 +354,7 @@ void Manager::registerEventBefore(const string& newEvent, const string& beforeEv throw EcsmError("Before event is not registered. (" "newEvent: " + newEvent + ", beforeEvent: " + beforeEvent + ")"); } -void Manager::registerEventAfter(const string& newEvent, const string& afterEvent) +void Manager::registerEventAfter(const std::string& newEvent, const std::string& afterEvent) { assert(!newEvent.empty()); assert(!afterEvent.empty()); @@ -376,7 +376,7 @@ void Manager::registerEventAfter(const string& newEvent, const string& afterEven } //********************************************************************************************************************** -void Manager::unregisterEvent(const string& name) +void Manager::unregisterEvent(const std::string& name) { assert(!name.empty()); auto iterator = events.find(name); @@ -404,7 +404,7 @@ void Manager::unregisterEvent(const string& name) delete event; } -bool Manager::tryUnregisterEvent(const string& name) +bool Manager::tryUnregisterEvent(const std::string& name) { assert(!name.empty()); auto iterator = events.find(name); @@ -435,7 +435,7 @@ bool Manager::tryUnregisterEvent(const string& name) } //********************************************************************************************************************** -bool Manager::isEventOrdered(const string& name) const +bool Manager::isEventOrdered(const std::string& name) const { assert(!name.empty()); auto result = events.find(name); @@ -443,7 +443,7 @@ bool Manager::isEventOrdered(const string& name) const throw EcsmError("Event is not registered. (name: " + name + ")"); return result->second->isOrdered; } -const Manager::Event::Subscribers& Manager::getEventSubscribers(const string& name) const +const Manager::Event::Subscribers& Manager::getEventSubscribers(const std::string& name) const { assert(!name.empty()); auto result = events.find(name); @@ -451,7 +451,7 @@ const Manager::Event::Subscribers& Manager::getEventSubscribers(const string& na throw EcsmError("Event is not registered. (name: " + name + ")"); return result->second->subscribers; } -bool Manager::isEventHasSubscribers(const string& name) const +bool Manager::isEventHasSubscribers(const std::string& name) const { assert(!name.empty()); auto result = events.find(name); @@ -461,7 +461,7 @@ bool Manager::isEventHasSubscribers(const string& name) const } //********************************************************************************************************************** -void Manager::runEvent(const string& name) +void Manager::runEvent(const std::string& name) { assert(!name.empty()); auto result = events.find(name); @@ -472,7 +472,7 @@ void Manager::runEvent(const string& name) for (const auto& onEvent : subscribers) onEvent(); } -bool Manager::tryRunEvent(const string& name) +bool Manager::tryRunEvent(const std::string& name) { assert(!name.empty()); auto result = events.find(name); @@ -495,7 +495,7 @@ void Manager::runOrderedEvents() } //********************************************************************************************************************** -void Manager::subscribeToEvent(const string& name, const std::function& onEvent) +void Manager::subscribeToEvent(const std::string& name, const std::function& onEvent) { assert(!name.empty()); assert(onEvent); @@ -505,7 +505,7 @@ void Manager::subscribeToEvent(const string& name, const std::function& throw EcsmError("Event is not registered. (name: " + name + ")"); result->second->subscribers.push_back(onEvent); } -void Manager::unsubscribeFromEvent(const string& name, const std::function& onEvent) +void Manager::unsubscribeFromEvent(const std::string& name, const std::function& onEvent) { assert(!name.empty()); assert(onEvent); @@ -527,7 +527,7 @@ void Manager::unsubscribeFromEvent(const string& name, const std::function& onEvent) +bool Manager::trySubscribeToEvent(const std::string& name, const std::function& onEvent) { assert(!name.empty()); assert(onEvent); @@ -539,7 +539,7 @@ bool Manager::trySubscribeToEvent(const string& name, const std::functionsecond->subscribers.push_back(onEvent); return true; } -bool Manager::tryUnsubscribeFromEvent(const string& name, const std::function& onEvent) +bool Manager::tryUnsubscribeFromEvent(const std::string& name, const std::function& onEvent) { assert(!name.empty()); assert(onEvent); @@ -617,17 +617,17 @@ void Manager::disposeSystemComponents() DoNotDestroySystem::DoNotDestroySystem(bool setSingleton) : Singleton(setSingleton) { } DoNotDestroySystem::~DoNotDestroySystem() { unsetSingleton(); } -const string& DoNotDestroySystem::getComponentName() const +const std::string& DoNotDestroySystem::getComponentName() const { - static const string name = "Do Not Destroy"; + static const std::string name = "Do Not Destroy"; return name; } DoNotDuplicateSystem::DoNotDuplicateSystem(bool setSingleton) : Singleton(setSingleton) { } DoNotDuplicateSystem::~DoNotDuplicateSystem() { unsetSingleton(); } -const string& DoNotDuplicateSystem::getComponentName() const +const std::string& DoNotDuplicateSystem::getComponentName() const { - static const string name = "Do Not Duplicate"; + static const std::string name = "Do Not Duplicate"; return name; } \ No newline at end of file diff --git a/source/singleton.cpp b/source/singleton.cpp index 02c0c03..1ba8f66 100644 --- a/source/singleton.cpp +++ b/source/singleton.cpp @@ -16,17 +16,17 @@ #include "ecsm.hpp" //********************************************************************************************************************** -void* ecsm::getManagerSystem(type_index type) +void* ecsm::getManagerSystem(std::type_index type) { auto manager = Manager::Instance::get(); return manager->get(type); } -bool ecsm::hasManagerSystem(type_index type) +bool ecsm::hasManagerSystem(std::type_index type) { auto manager = Manager::Instance::get(); return manager->has(type); } -void* ecsm::tryGetManagerSystem(type_index type) +void* ecsm::tryGetManagerSystem(std::type_index type) { auto manager = Manager::Instance::get(); return manager->tryGet(type); diff --git a/tests/test-ecsm.cpp b/tests/test-ecsm.cpp index c5b8e39..be5a40f 100644 --- a/tests/test-ecsm.cpp +++ b/tests/test-ecsm.cpp @@ -14,6 +14,7 @@ #include "ecsm.hpp" +using namespace std; using namespace ecsm; struct TestComponent final : public Component