From 8ff89409c7ee5399d98fe4a8e2a67961fab8add1 Mon Sep 17 00:00:00 2001 From: max-001 Date: Fri, 15 Nov 2024 17:24:03 +0100 Subject: Added code style --- contributing.md | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) (limited to 'contributing.md') diff --git a/contributing.md b/contributing.md index 3090727..64afb44 100644 --- a/contributing.md +++ b/contributing.md @@ -678,6 +678,86 @@ that you can click on to open them. uint64_t foo(); ``` +-
+ Throw exceptions using standard C++ exceptions (such as std::runtime_error) +
GoodBad
+ + ```cpp + if (error) { + throw std::runtime_error("An error occurred"); + } + ``` + + + ```cpp + if (error) { + throw "An error occurred"; + } + ``` +
+-
+ Constructors of classes derived from Component should be protected and ComponentManager should be declared as a friend class. +
GoodBad
+ + ```cpp + class MyComponent : public Component { + protected: + MyComponent() = default; + friend class ComponentManager; + }; + ``` + + + ```cpp + class MyComponent : public Component { + public: + MyComponent() = default; + }; + ``` +
+-
+ Use of format specifiers (like %s and %n) are not allowed (use {} with std::format instead) +
GoodBad
+ + ```cpp + std::string message = std::format("Hello, {}", name); + ``` + + + ```cpp + char message[50]; + sprintf(message, "Hello, %s", name); + ``` +
+-
+ Argument names should be added in .h files (not only in .cpp and .hpp files) +
GoodBad
+ + Foo.h: + ```cpp + void foo(int bar); + ``` + + Foo.cpp: + ```cpp + void foo(int bar) { + // ... + } + ``` + + + Foo.h: + ```cpp + void foo(int); + ``` + + Foo.cpp: + ```cpp + void foo(int bar) { + // ... + } + ``` +
## CMakeLists-specific -- cgit v1.2.3 From 424611c6016e4a189e3a887fabe8e45b637e80f4 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 15 Nov 2024 17:39:04 +0100 Subject: merge #36 --- contributing.md | 52 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 10 deletions(-) (limited to 'contributing.md') diff --git a/contributing.md b/contributing.md index 64afb44..5b0c79d 100644 --- a/contributing.md +++ b/contributing.md @@ -679,30 +679,57 @@ that you can click on to open them. ``` -
- Throw exceptions using standard C++ exceptions (such as std::runtime_error) + Utilize standard exceptions where appropriate (those found in <stdexcept>)
GoodBad
```cpp - if (error) { - throw std::runtime_error("An error occurred"); + #include + + // ... + + if (foo == nullptr) { + throw std::runtime_error("What is wrong"); } ``` ```cpp - if (error) { - throw "An error occurred"; + if (foo == nullptr) { + std::cout << "What is wrong" << std::endl; + exit(1); } ```
-
- Constructors of classes derived from Component should be protected and ComponentManager should be declared as a friend class. + Mention the name of the class when throwing an exception +
GoodBad
+ + ```cpp + Foo::bar() { + if (...) + throw std::runtime_error("Foo: big error!"); + } + ``` + + + ```cpp + Foo::bar() { + if (...) + throw std::runtime_error("big error!"); + } + ``` +
+-
+ Constructors of classes derived from Component should be + protected and ComponentManager should be declared as a friend + class.
GoodBad
```cpp class MyComponent : public Component { protected: - MyComponent() = default; + MyComponent(...); + //! Only ComponentManager is allowed to create components friend class ComponentManager; }; ``` @@ -711,16 +738,20 @@ that you can click on to open them. ```cpp class MyComponent : public Component { public: - MyComponent() = default; + MyComponent(...); }; ```
-
- Use of format specifiers (like %s and %n) are not allowed (use {} with std::format instead) + C++ std::format should be used instead of C-style format specifiers
GoodBad
```cpp std::string message = std::format("Hello, {}", name); + + dbg_logf("Here too: {}", 3); + + throw std::runtime_error(std::format("Or here: {}", 5)); ``` @@ -730,7 +761,8 @@ that you can click on to open them. ```
-
- Argument names should be added in .h files (not only in .cpp and .hpp files) + Argument names should be added in .h files (not only in + .cpp and .hpp files)
GoodBad
Foo.h: -- cgit v1.2.3