From 098ae26d16188b276af77b8bfe9975fcabe8d7f4 Mon Sep 17 00:00:00 2001 From: max-001 Date: Thu, 7 Nov 2024 10:10:07 +0100 Subject: Added code styles --- contributing.md | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/contributing.md b/contributing.md index cd1b6a6..c7f836e 100644 --- a/contributing.md +++ b/contributing.md @@ -541,6 +541,53 @@ that you can click on to open them. #include ``` +-
+ Ensure exception safety by using RAII classes +
GoodBad
+ + ```cpp + std::unique_ptr foo = std::make_unique(); + ``` + + + ```cpp + Foo* foo = new Foo(); + // ... + delete foo; + ``` +
+-
+ Do not use malloc, calloc, or free (instead use new and delete) +
GoodBad
+ + ```cpp + Foo* foo = new Foo(); + delete foo; + ``` + + + ```cpp + Foo* foo = (Foo*)malloc(sizeof(Foo)); + free(foo); + ``` +
+-
+ Prefix member variables with this-> +
GoodBad
+ + ```cpp + void Foo::set_value(int value) { + this->value = value; + } + ``` + + + ```cpp + void Foo::set_value(int value) { + value = value; + } + ``` +
## CMakeLists-specific -- cgit v1.2.3 From 0756fe048916e696431e1ae99f95ada06d0de7a0 Mon Sep 17 00:00:00 2001 From: max-001 Date: Thu, 7 Nov 2024 10:10:44 +0100 Subject: Fix --- contributing.md | 1 - 1 file changed, 1 deletion(-) diff --git a/contributing.md b/contributing.md index c7f836e..9f39e41 100644 --- a/contributing.md +++ b/contributing.md @@ -523,7 +523,6 @@ that you can click on to open them. -
Use angle brackets (<>) only for including system headers and double quotes ("") for including other engine files. - > [!NOTE] > Only files in the examples folder should include engine headers with angle > brackets -- cgit v1.2.3 From dc093939558e2d71ad3dd322f1f75dfba893783f Mon Sep 17 00:00:00 2001 From: max-001 Date: Thu, 7 Nov 2024 10:11:09 +0100 Subject: Fix --- contributing.md | 1 + 1 file changed, 1 insertion(+) diff --git a/contributing.md b/contributing.md index 9f39e41..c7f836e 100644 --- a/contributing.md +++ b/contributing.md @@ -523,6 +523,7 @@ that you can click on to open them. -
Use angle brackets (<>) only for including system headers and double quotes ("") for including other engine files. + > [!NOTE] > Only files in the examples folder should include engine headers with angle > brackets -- cgit v1.2.3 From 54f4a322e939dcdb62ab9fbf919bf8cb4b6f7b75 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 7 Nov 2024 10:46:46 +0100 Subject: fix script example segmentation fault --- src/crepe/ComponentManager.hpp | 14 +++++++++----- src/crepe/api/BehaviorScript.h | 5 +---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index e74f2e9..89ed111 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -30,12 +30,16 @@ T & ComponentManager::add_component(uint32_t id, Args &&... args) { // Create a new component of type T (arguments directly forwarded). The // constructor must be called by ComponentManager. - T * instance_pointer = new T(id, forward(args)...); - unique_ptr instance = unique_ptr(instance_pointer); + T * instance_ptr = new T(id, forward(args)...); + if (instance_ptr == nullptr) + throw std::bad_alloc(); + + T & instance_ref = *instance_ptr; + unique_ptr instance = unique_ptr(instance_ptr); // Check if the vector size is not greater than get_instances_max - if (instance->get_instances_max() != -1 - && components[type][id].size() >= instance->get_instances_max()) { + int max_instances = instance->get_instances_max(); + if (max_instances != -1 && components[type][id].size() >= max_instances) { // TODO: Exception throw std::runtime_error( "Exceeded maximum number of instances for this component type"); @@ -44,7 +48,7 @@ T & ComponentManager::add_component(uint32_t id, Args &&... args) { // store its unique_ptr in the vector<> components[type][id].push_back(std::move(instance)); - return *instance; + return instance_ref; } template diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index 21638f4..6b1fec7 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -5,12 +5,9 @@ #include "../Component.h" namespace crepe { + class ScriptSystem; class ComponentManager; -} // namespace crepe - -namespace crepe { - class Script; class BehaviorScript : public Component { -- cgit v1.2.3 From ce8debc03d2ed291285535462cbf4411bf298278 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 7 Nov 2024 12:28:39 +0100 Subject: update contributing.md --- contributing.md | 65 +++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 9 deletions(-) diff --git a/contributing.md b/contributing.md index c7f836e..ba8ad68 100644 --- a/contributing.md +++ b/contributing.md @@ -78,8 +78,8 @@ that you can click on to open them. } ```
-- Header includes are split into paragraphs separated by a blank line. The - order is: +- Header includes (at the top of files) are split into paragraphs separated by + a blank line. The order is: 1. system headers (using `<`brackets`>`) 2. relative headers NOT in the same folder as the current file 3. relative headers in the same folder as the current file @@ -111,6 +111,52 @@ that you can click on to open them. #include "api/Sprite.h" ```
+-
+ If there is one, the matching template header (.hpp) is included + at the bottom of the regular header (.h) +
GoodBad
+ + Foo.h: + ```cpp + #pragma once + + template + void foo(); + + #include "Foo.hpp" + ``` + + Foo.hpp: + ```cpp + #pragma once + #include "Foo.h" + + template + void foo() { + // ... + } + ``` + + + Foo.h: + ```cpp + #pragma once + + template + void foo(); + ``` + + Foo.hpp: + ```cpp + #pragma once + #include "Foo.h" + + template + void foo() { + // ... + } + ``` +
-
using namespace may not be used in header files (.h, .hpp), only in source files (.cpp). @@ -546,7 +592,7 @@ that you can click on to open them.
GoodBad
```cpp - std::unique_ptr foo = std::make_unique(); + auto foo = std::make_unique(); ``` @@ -557,22 +603,23 @@ that you can click on to open them. ```
-
- Do not use malloc, calloc, or free (instead use new and delete) + Do not use C-style memory management APIs (malloc, + calloc, free)
GoodBad
```cpp - Foo* foo = new Foo(); + Foo * foo = new Foo(); delete foo; ``` ```cpp - Foo* foo = (Foo*)malloc(sizeof(Foo)); + Foo * foo = (Foo *) malloc(sizeof(Foo)); free(foo); ```
-
- Prefix member variables with this-> + Prefix all class members with this->
GoodBad
```cpp @@ -583,8 +630,8 @@ that you can click on to open them. ```cpp - void Foo::set_value(int value) { - value = value; + void Foo::set_value(int new_value) { + value = new_value; } ```
-- cgit v1.2.3