Skip to content

Commit e9d2aa8

Browse files
committed
Some code improvements
1 parent bbd9190 commit e9d2aa8

6 files changed

Lines changed: 37 additions & 41 deletions

File tree

include/ecsm.hpp

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ struct SvEqual
6767
#define ECSM_UNSUBSCRIBE_FROM_EVENT(name, func) manager->unsubscribeFromEvent(name, std::bind(&func, this))
6868

6969
/**
70-
* @brief Subscribes @ref ecsm::System function to the event if exist.
70+
* @brief Subscribes @ref ecsm::System function to the event if exists.
7171
*/
7272
#define ECSM_TRY_SUBSCRIBE_TO_EVENT(name, func) manager->trySubscribeToEvent(name, std::bind(&func, this))
7373
/**
74-
* @brief Unsubscribes @ref ecsm::System function from the event if exist.
74+
* @brief Unsubscribes @ref ecsm::System function from the event if exists.
7575
*/
7676
#define ECSM_TRY_UNSUBSCRIBE_FROM_EVENT(name, func) manager->tryUnsubscribeFromEvent(name, std::bind(&func, this))
7777

@@ -196,7 +196,7 @@ class Entity final
196196
uint32_t capacity = 0;
197197
uint32_t count = 0;
198198

199-
inline static int compareComps(const void* a, const void* b) noexcept
199+
static int compareComps(const void* a, const void* b) noexcept
200200
{
201201
const auto l = (const ComponentData*)a;
202202
const auto r = (const ComponentData*)b;
@@ -210,8 +210,7 @@ class Entity final
210210
this->capacity = capacity;
211211
if (this->capacity == 0)
212212
components = (ComponentData*)malloc(sizeof(ComponentData) * capacity);
213-
else
214-
components = (ComponentData*)realloc(components, sizeof(ComponentData) * capacity);
213+
else components = (ComponentData*)realloc(components, sizeof(ComponentData) * capacity);
215214
if (!components) abort();
216215
}
217216
void addComponent(size_t type, System* system, ID<Component> instance)
@@ -425,13 +424,13 @@ class Manager final : public Singleton<Manager, false>
425424
}
426425

427426
/**
428-
* @brief Terminates and destroys system if exist.
427+
* @brief Terminates and destroys system if exists.
429428
* @param type target system typeid()
430429
* @return True if system is destroyed, otherwise false.
431430
*/
432431
bool tryDestroySystem(std::type_index type);
433432
/**
434-
* @brief Terminates and destroys system if exist.
433+
* @brief Terminates and destroys system if exists.
435434
* @tparam T target system type
436435
* @return True if system is destroyed, otherwise false.
437436
*/
@@ -469,7 +468,7 @@ class Manager final : public Singleton<Manager, false>
469468
* @brief Adds specified system to the system group.
470469
*
471470
* @tparam G type of the system group
472-
* @tparam T target system type
471+
* @tparam S target system type
473472
*
474473
* @throw EcsmError if system is not found or already added.
475474
*/
@@ -482,7 +481,7 @@ class Manager final : public Singleton<Manager, false>
482481
}
483482

484483
/**
485-
* @brief Adds specified system to the system group if exist.
484+
* @brief Adds specified system to the system group if exists.
486485
*
487486
* @param groupType typeid() of the system group
488487
* @param[in] system target system instance
@@ -491,7 +490,7 @@ class Manager final : public Singleton<Manager, false>
491490
*/
492491
bool tryAddGroupSystem(std::type_index groupType, System* system);
493492
/**
494-
* @brief Adds specified system to the system group if exist.
493+
* @brief Adds specified system to the system group if exists.
495494
*
496495
* @tparam T type of the system group
497496
* @param[in] system target system instance
@@ -505,10 +504,10 @@ class Manager final : public Singleton<Manager, false>
505504
return tryAddGroupSystem(typeid(T), system);
506505
}
507506
/**
508-
* @brief Adds specified system to the system group if exist.
507+
* @brief Adds specified system to the system group if exists.
509508
*
510509
* @tparam G type of the system group
511-
* @tparam T target system type
510+
* @tparam S target system type
512511
*
513512
* @return True if system is added to the group, otherwise false.
514513
*/
@@ -550,7 +549,7 @@ class Manager final : public Singleton<Manager, false>
550549
* @brief Removes specified system from the system group.
551550
*
552551
* @tparam G type of the system group
553-
* @tparam T target system type
552+
* @tparam S target system type
554553
*
555554
* @throw EcsmError if system or group is not found, or system is not added.
556555
*/
@@ -563,7 +562,7 @@ class Manager final : public Singleton<Manager, false>
563562
}
564563

565564
/**
566-
* @brief Removes specified system from the system group if exist.
565+
* @brief Removes specified system from the system group if exists.
567566
*
568567
* @param groupType typeid() of the system group
569568
* @param[in] system target system instance
@@ -572,7 +571,7 @@ class Manager final : public Singleton<Manager, false>
572571
*/
573572
bool tryRemoveGroupSystem(std::type_index groupType, System* system);
574573
/**
575-
* @brief Removes specified system from the system group if exist.
574+
* @brief Removes specified system from the system group if exists.
576575
*
577576
* @tparam T type of the system group
578577
* @param[in] system target system instance
@@ -586,10 +585,10 @@ class Manager final : public Singleton<Manager, false>
586585
return tryRemoveGroupSystem(typeid(T), system);
587586
}
588587
/**
589-
* @brief Removes specified system from the system group if exist.
588+
* @brief Removes specified system from the system group if exists.
590589
*
591590
* @tparam G type of the system group
592-
* @tparam T target system type
591+
* @tparam S target system type
593592
*
594593
* @return True if system is removed from the group, otherwise false.
595594
*/
@@ -637,7 +636,7 @@ class Manager final : public Singleton<Manager, false>
637636
const std::vector<System*>& getSystemGroup() const { return getSystemGroup(typeid(T)); }
638637

639638
/**
640-
* @brief Returns specified system group if exist, otherwise nullptr.
639+
* @brief Returns specified system group if exists, otherwise nullptr.
641640
* @param type target system group typeid()
642641
*/
643642
const std::vector<System*>* tryGetSystemGroup(std::type_index type) const noexcept
@@ -648,7 +647,7 @@ class Manager final : public Singleton<Manager, false>
648647
return &result->second;
649648
}
650649
/**
651-
* @brief Returns specified system group if exist, otherwise nullptr.
650+
* @brief Returns specified system group if exists, otherwise nullptr.
652651
* @tparam T target system group type
653652
*/
654653
template<class T>
@@ -672,7 +671,7 @@ class Manager final : public Singleton<Manager, false>
672671

673672
/**
674673
* @brief Returns system instance.
675-
* @warning Be carefull with system pointer, it can be destroyed later.
674+
* @warning Be careful with system pointer, it can be destroyed later.
676675
* @param type target system typeid()
677676
* @throw EcsmError if system is not found.
678677
*/
@@ -721,7 +720,7 @@ class Manager final : public Singleton<Manager, false>
721720
*/
722721
ID<Entity> createEntity() { return entities.create(); }
723722
/**
724-
* @brief Destroys entity instance and it components.
723+
* @brief Destroys entity instance and its components.
725724
* @note Entities are not destroyed immediately, only after the dispose call.
726725
* @param[in,out] instance target entity instance or null
727726
*/
@@ -922,7 +921,7 @@ class Manager final : public Singleton<Manager, false>
922921
}
923922

924923
/*******************************************************************************************************************
925-
* @brief Returns component data accessor if exist, otherwise null. (@ref OptView)
924+
* @brief Returns component data accessor if exists, otherwise null. (@ref OptView)
926925
* @warning Do not store views, use them only in place. Because component memory can be reallocated later.
927926
* @note It also checks for component in the garbage pool.
928927
*
@@ -938,7 +937,7 @@ class Manager final : public Singleton<Manager, false>
938937
return OptView<Component>(componentData->system->getComponent(componentData->instance));
939938
}
940939
/**
941-
* @brief Returns component data accessor if exist, otherwise null. (@ref OptView)
940+
* @brief Returns component data accessor if exists, otherwise null. (@ref OptView)
942941
* @warning Do not store views, use them only in place. Because component memory can be reallocated later.
943942
* @note It also checks for component in the garbage pool.
944943
*
@@ -953,7 +952,7 @@ class Manager final : public Singleton<Manager, false>
953952
}
954953

955954
/**
956-
* @brief Returns component data accessor if exist, otherwise creates a new one. (@ref View)
955+
* @brief Returns component data accessor if exists, otherwise creates a new one. (@ref View)
957956
* @warning Do not store views, use them only in place. Because component memory can be reallocated later.
958957
* @note It also checks for component in the garbage pool.
959958
*
@@ -968,7 +967,7 @@ class Manager final : public Singleton<Manager, false>
968967
return add(entity, componentType);
969968
}
970969
/**
971-
* @brief Returns component data accessor if exist, otherwise creates a new one. (@ref View)
970+
* @brief Returns component data accessor if exists, otherwise creates a new one. (@ref View)
972971
* @warning Do not store views, use them only in place. Because component memory can be reallocated later.
973972
* @note It also checks for component in the garbage pool.
974973
*
@@ -1062,7 +1061,7 @@ class Manager final : public Singleton<Manager, false>
10621061
*/
10631062
uint32_t getComponentCount(ID<Entity> entity) const noexcept
10641063
{
1065-
return (uint32_t)entities.get(entity)->getComponentCount();
1064+
return entities.get(entity)->getComponentCount();
10661065
}
10671066

10681067
/**
@@ -1127,7 +1126,7 @@ class Manager final : public Singleton<Manager, false>
11271126

11281127
/**
11291128
* @brief Resets entity component data, if added.
1130-
* @return True if component exists and was resetted.
1129+
* @return True if component exists and was reset.
11311130
*
11321131
* @param entity target entity instance
11331132
* @param componentType target component typeid()
@@ -1146,7 +1145,7 @@ class Manager final : public Singleton<Manager, false>
11461145
}
11471146
/**
11481147
* @brief Resets entity component data, if added.
1149-
* @return True if component exists and was resetted.
1148+
* @return True if component exists and was reset.
11501149
*
11511150
* @param entity entity instance
11521151
* @param full reset all component data
@@ -1198,7 +1197,7 @@ class Manager final : public Singleton<Manager, false>
11981197
*/
11991198
void unregisterEvent(std::string_view name);
12001199
/**
1201-
* @brief Unregisters event if exist.
1200+
* @brief Unregisters event if exists.
12021201
* @param name target event name
12031202
* @return True if event is unregistered, otherwise false.
12041203
*/
@@ -1220,7 +1219,7 @@ class Manager final : public Singleton<Manager, false>
12201219
*/
12211220
const Event& getEvent(std::string_view name) const;
12221221
/**
1223-
* @brief Returns event data container by name if it exist, otherwise null.
1222+
* @brief Returns event data container by name if exists, otherwise null.
12241223
* @param name target event name
12251224
*/
12261225
const Event* tryGetEvent(std::string_view name) const;
@@ -1275,7 +1274,7 @@ class Manager final : public Singleton<Manager, false>
12751274
*/
12761275
bool trySubscribeToEvent(std::string_view name, const std::function<void()>& onEvent);
12771276
/**
1278-
* @brief Removes existing event subscriber if exist.
1277+
* @brief Removes existing event subscriber if exists.
12791278
*
12801279
* @param name target event name
12811280
* @param[in] onEvent on event function callback
@@ -1369,7 +1368,7 @@ class Manager final : public Singleton<Manager, false>
13691368
*/
13701369
void lock() { locker.lock(); }
13711370
/**
1372-
* @brief Tries to locks manager for synchronous access. (MT-Safe)
1371+
* @brief Tries to lock manager for synchronous access. (MT-Safe)
13731372
* @note Use it if you want to access manager from multiple threads asynchronously.
13741373
*/
13751374
bool tryLock() noexcept { return locker.try_lock(); }

include/linear-pool.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ class LinearPool final
772772

773773
/*******************************************************************************************************************
774774
* @brief Actually destroys items.
775-
* @details See the @ref destroy(). It marks destroyed item memory as free, and can reuse it later.
775+
* @details See the @ref LinearPool::destroy(). It marks destroyed item memory as free, and can reuse it later.
776776
*/
777777
void dispose()
778778
{

include/singleton.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ class Singleton
7070
"type: " + typeToString(typeid(T)) + ")");
7171
}
7272
singletonInstance = (T*)this;
73-
return;
7473
}
7574
/**
7675
* @brief Unsets this class singleton instance.
@@ -106,7 +105,7 @@ class Singleton
106105
"type: " + typeToString(typeid(T)) + ")");
107106
}
108107
/**
109-
* @brief Returns class singleton or manager instance if exist.
108+
* @brief Returns class singleton or manager instance if exists.
110109
*/
111110
static T* tryGet()
112111
{

include/type-string.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ static std::string typeToString(std::type_index type)
4747
std::string result;
4848
if (strlen(name) > 0)
4949
result.assign(name);
50-
else
51-
result = std::to_string(type.hash_code());
50+
else result = std::to_string(type.hash_code());
5251

5352
#ifdef __GNUG__
5453
free(demangledName);

source/ecsm.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,6 @@ void Manager::resetComponents(ID<Entity> entity, bool full)
417417
{
418418
const auto& componentData = components[i];
419419
auto componentView = componentData.system->getComponent(componentData.instance);
420-
auto entity = componentView->getEntity();
421420
componentData.system->resetComponent(componentView, full);
422421
componentView->entity = entity; // Note: full reset may clear all data.
423422
}

tests/test-ecsm.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class TestSystem final : public ComponentSystem<TestComponent>, public Singleton
4040
ECSM_SUBSCRIBE_TO_EVENT("Update", TestSystem::update);
4141
ECSM_SUBSCRIBE_TO_EVENT("PostUpdate", TestSystem::postUpdate);
4242
}
43-
~TestSystem() final
43+
~TestSystem() override
4444
{
4545
if (Manager::Instance::get()->isRunning)
4646
{
@@ -53,14 +53,14 @@ class TestSystem final : public ComponentSystem<TestComponent>, public Singleton
5353
unsetSingleton();
5454
}
5555

56-
void copyComponent(View<Component> source, View<Component> destination) final
56+
void copyComponent(View<Component> source, View<Component> destination) override
5757
{
5858
const auto sourceView = View<TestComponent>(source);
5959
auto destinationView = View<TestComponent>(destination);
6060
destinationView->ID = sourceView->ID;
6161
destinationView->someData = sourceView->someData;
6262
}
63-
string_view getComponentName() const final
63+
string_view getComponentName() const override
6464
{
6565
return "Test";
6666
}

0 commit comments

Comments
 (0)