Skip to content

Commit db83f6c

Browse files
authored
id 1771219530
id 1771219530
2 parents d43e26b + 2168c31 commit db83f6c

64 files changed

Lines changed: 810 additions & 359 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

include/ExampleRegistry.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma once
2+
#include <functional>
3+
#include <memory>
4+
#include <unordered_map>
5+
#include "IExample.h"
6+
7+
#define REGISTER_EXAMPLE(CLASS, GROUP, NAME) \
8+
namespace { \
9+
struct CLASS##Registrar { \
10+
CLASS##Registrar() { \
11+
ExampleRegistry::instance().registerExample( \
12+
GROUP, NAME, []() { return std::make_unique<CLASS>(); }); \
13+
} \
14+
}; \
15+
static CLASS##Registrar global_##CLASS##Registrar; \
16+
}
17+
18+
class ExampleRegistry {
19+
public:
20+
using Factory = std::function<std::unique_ptr<IExample>()>;
21+
22+
static ExampleRegistry& instance() {
23+
static ExampleRegistry inst;
24+
return inst;
25+
}
26+
27+
void registerExample(const std::string& group, const std::string& name,
28+
Factory factory) {
29+
registry_[group][name] = std::move(factory);
30+
}
31+
32+
const auto& getAll() const { return registry_; }
33+
34+
std::unique_ptr<IExample> create(const std::string& group,
35+
const std::string& name) {
36+
37+
return registry_.at(group).at(name)();
38+
}
39+
40+
private:
41+
std::unordered_map<std::string, std::unordered_map<std::string, Factory>>
42+
registry_;
43+
};

include/IExample.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
#include <string>
3+
class IExample {
4+
public:
5+
virtual ~IExample() = default;
6+
7+
virtual std::string group() const = 0;
8+
virtual std::string name() const = 0;
9+
virtual std::string description() const = 0;
10+
11+
virtual void execute() = 0;
12+
};

src/controller/pid/PIDSim.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include <iostream>
22
#include "pid.h"
33

4+
#include "ExampleRegistry.h"
5+
#include "IExample.h"
6+
47
namespace {
58
void run() {
69
PID pid = PID(0.1, 100, -100, 0.1, 0.01, 0.5);
@@ -15,8 +18,12 @@ void run() {
1518
}
1619
} // namespace
1720

18-
struct PIDSimRunner {
19-
PIDSimRunner() { run(); }
21+
class PIDSim : public IExample {
22+
public:
23+
std::string group() const override { return "controller"; }
24+
std::string name() const override { return "PIDSim"; }
25+
std::string description() const override { return "PID example"; }
26+
void execute() override { run(); }
2027
};
2128

22-
static PIDSimRunner autoRunner;
29+
REGISTER_EXAMPLE(PIDSim, "controller", "PIDSim");

src/core/basics/ControlFlow.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,15 @@ void exceptions() {
116116
}
117117
}
118118

119-
// A struct that runs code when its object is created
120-
struct ControlFlow {
121-
ControlFlow() {
122-
cout << "\n"
123-
<< "\n"
124-
<< "ControlFlow\n";
119+
#include "ExampleRegistry.h"
120+
#include "IExample.h"
121+
122+
class ControlFlow : public IExample {
123+
public:
124+
std::string group() const override { return "core"; }
125+
std::string name() const override { return "ControlFlow"; }
126+
std::string description() const override { return "ControlFlow"; }
127+
void execute() override {
125128
conditionals();
126129
jumps();
127130
functionCalls();
@@ -131,5 +134,4 @@ struct ControlFlow {
131134
}
132135
};
133136

134-
// All global and static objects are constructed before main() begins.
135-
static ControlFlow autoRunInstance;
137+
REGISTER_EXAMPLE(ControlFlow, "core", "ControlFlow");

src/core/basics/InitializeVariable.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
#include <iostream>
22
using namespace std;
33

4+
#include "ExampleRegistry.h"
5+
#include "IExample.h"
6+
47
void initialize_variable();
58

6-
// A struct that runs code when its object is created
7-
struct InitializeVariable {
8-
InitializeVariable() {
9-
cout << "\n"
10-
<< "\n"
11-
<< "InitializeVariable\n";
12-
initialize_variable();
13-
}
9+
class InitializeVariable : public IExample {
10+
public:
11+
std::string group() const override { return "core"; }
12+
std::string name() const override { return "InitializeVariable"; }
13+
std::string description() const override { return "InitializeVariable"; }
14+
void execute() override { initialize_variable(); }
1415
};
1516

16-
// All global and static objects are constructed before main() begins.
17-
static InitializeVariable autoRunInstance;
17+
REGISTER_EXAMPLE(InitializeVariable, "core", "InitializeVariable");
1818

1919
struct Foo {
2020
Foo() { cout << "Default constructor/ default init\n"; }

src/core/basics/Operations.cpp

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
#include <iostream>
22
using namespace std;
33

4+
#include "ExampleRegistry.h"
5+
#include "IExample.h"
6+
47
void arithmeticOperator();
58
void logicalOperator();
69
void bitWiseOperator();
710

8-
struct Operations {
9-
Operations() {
10-
cout << "\n"
11-
<< "\n"
12-
<< "Operation\n";
11+
class Operations : public IExample {
12+
public:
13+
std::string group() const override { return "core"; }
14+
std::string name() const override { return "Operations"; }
15+
std::string description() const override { return "Operation"; }
16+
void execute() override {
1317
arithmeticOperator();
1418
logicalOperator();
1519
bitWiseOperator();
1620
}
1721
};
1822

19-
static Operations autoRunInstance;
23+
REGISTER_EXAMPLE(Operations, "core", "Operations");
2024

2125
void arithmeticOperator() {
2226
cout << "\n--- ArithmeticOperator Examples ---\n";
@@ -94,33 +98,32 @@ void logicalOperator() {
9498
void bitWiseOperator() {
9599
cout << "\n--- BitWiseOperator Examples ---\n";
96100
bitset<8> bitsA {
97-
0b1111'1111}; bitset<8> bitsB {
98-
0b1111'0000};
101+
0b1111'1111}; bitset<8> bitsB { 0b1111'0000};
99102

100-
cout
101-
<< "bitA = " << bitsA << ", bitB = " << bitsB << "\n";
103+
cout
104+
<< "bitA = " << bitsA << ", bitB = " << bitsB << "\n";
102105

103-
// AND
104-
bitset<8> result = bitsA & bitsB;
105-
cout << "bitA && bitB= " << result << "\n";
106+
// AND
107+
bitset<8> result = bitsA & bitsB;
108+
cout << "bitA && bitB= " << result << "\n";
106109

107-
// OR
108-
result = bitsA | bitsB;
109-
cout << "bitA | bitB= " << result << "\n";
110+
// OR
111+
result = bitsA | bitsB;
112+
cout << "bitA | bitB= " << result << "\n";
110113

111-
// XOR
112-
result = bitsA ^ bitsB;
113-
cout << "bitA ^ bitB= " << result << "\n";
114+
// XOR
115+
result = bitsA ^ bitsB;
116+
cout << "bitA ^ bitB= " << result << "\n";
114117

115-
// NOT
116-
result = ~bitsA;
117-
cout << "~bitA = " << result << "\n";
118+
// NOT
119+
result = ~bitsA;
120+
cout << "~bitA = " << result << "\n";
118121

119-
// LEFT SHIFT
120-
result = bitsA << 1;
121-
cout << "bitA << 1 = " << result << "\n";
122+
// LEFT SHIFT
123+
result = bitsA << 1;
124+
cout << "bitA << 1 = " << result << "\n";
122125

123-
// RIGHT SHIFT
124-
result = bitsA >> 1;
125-
cout << "bitA >> 1 = " << result << "\n";
126-
}
126+
// RIGHT SHIFT
127+
result = bitsA >> 1;
128+
cout << "bitA >> 1 = " << result << "\n";
129+
}

src/core/basics/TypeQualifier.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ void run() {
3030

3131
} // namespace
3232

33-
struct TypeQualifier {
34-
TypeQualifier() {
35-
cout << "\n"
36-
<< "\n"
37-
<< "TypeQualifier\n";
38-
Constant::run();
39-
}
33+
#include "ExampleRegistry.h"
34+
#include "IExample.h"
35+
36+
class TypeQualifier : public IExample {
37+
public:
38+
std::string group() const override { return "core"; }
39+
std::string name() const override { return "TypeQualifier"; }
40+
std::string description() const override { return "TypeQualifier"; }
41+
void execute() override { Constant::run(); }
4042
};
4143

42-
static TypeQualifier autoRunInstance;
44+
REGISTER_EXAMPLE(TypeQualifier, "core", "TypeQualifier");

src/core/datatypes/CArray.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include <iostream>
22

3+
#include "ExampleRegistry.h"
4+
#include "IExample.h"
5+
36
void arrayExamples() {
47
std::cout << "\n--- Array Examples ---\n";
58

@@ -17,8 +20,12 @@ void arrayExamples() {
1720
}
1821
}
1922

20-
struct CArray {
21-
CArray() { arrayExamples(); }
23+
class CArray : public IExample {
24+
public:
25+
std::string group() const override { return "core"; }
26+
std::string name() const override { return "CArray"; }
27+
std::string description() const override { return ""; }
28+
void execute() override { arrayExamples(); }
2229
};
2330

24-
static CArray autoRunArray;
31+
REGISTER_EXAMPLE(CArray, "core", "CArray");

src/core/datatypes/CEnum.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,15 @@ void enums() {
3939
[[maybe_unused]] ScopeEnumClassB st = ScopeEnumClassB::enumratorA;
4040
}
4141

42-
// A struct that runs code when its object is created
43-
struct CEnum {
44-
CEnum() {
45-
cout << "\n"
46-
<< "\n"
47-
<< "Compound type: Enum\n";
48-
49-
enums();
50-
}
42+
#include "ExampleRegistry.h"
43+
#include "IExample.h"
44+
45+
class CEnum : public IExample {
46+
public:
47+
std::string group() const override { return "core"; }
48+
std::string name() const override { return "CEnum"; }
49+
std::string description() const override { return "Compound type: Enum"; }
50+
void execute() override { enums(); }
5151
};
5252

53-
// All global and static objects are constructed before main() begins.
54-
static CEnum autoRunInstance;
53+
REGISTER_EXAMPLE(CEnum, "core", "CEnum");

src/core/datatypes/CPointers.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,15 @@ void pointers() {
103103
std::cout << "By ptr: " << *b_ptr << '\n';
104104
}
105105

106-
// A struct that runs code when its object is created
107-
struct CPointers {
108-
CPointers() {
109-
cout << "\n"
110-
<< "\n"
111-
<< "Compound type: Pointers\n";
112-
113-
pointers();
114-
}
106+
#include "ExampleRegistry.h"
107+
#include "IExample.h"
108+
109+
class CPointers : public IExample {
110+
public:
111+
std::string group() const override { return "core"; }
112+
std::string name() const override { return "CPointers"; }
113+
std::string description() const override { return "Compound type: Pointers"; }
114+
void execute() override { pointers(); }
115115
};
116116

117-
// All global and static objects are constructed before main() begins.
118-
static CPointers autoRunInstance;
117+
REGISTER_EXAMPLE(CPointers, "core", "CPointers");

0 commit comments

Comments
 (0)