Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions include/ExampleRegistry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once
#include <functional>
#include <memory>
#include <unordered_map>
#include "IExample.h"

#define REGISTER_EXAMPLE(CLASS, GROUP, NAME) \
namespace { \
struct CLASS##Registrar { \
CLASS##Registrar() { \
ExampleRegistry::instance().registerExample( \
GROUP, NAME, []() { return std::make_unique<CLASS>(); }); \
} \
}; \
static CLASS##Registrar global_##CLASS##Registrar; \
}

class ExampleRegistry {
public:
using Factory = std::function<std::unique_ptr<IExample>()>;

static ExampleRegistry& instance() {
static ExampleRegistry inst;
return inst;
}

void registerExample(const std::string& group, const std::string& name,
Factory factory) {
registry_[group][name] = std::move(factory);
}

const auto& getAll() const { return registry_; }

std::unique_ptr<IExample> create(const std::string& group,
const std::string& name) {

return registry_.at(group).at(name)();
}

private:
std::unordered_map<std::string, std::unordered_map<std::string, Factory>>
registry_;
};
12 changes: 12 additions & 0 deletions include/IExample.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#include <string>
class IExample {
public:
virtual ~IExample() = default;

virtual std::string group() const = 0;
virtual std::string name() const = 0;
virtual std::string description() const = 0;

virtual void execute() = 0;
};
13 changes: 10 additions & 3 deletions src/controller/pid/PIDSim.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include <iostream>
#include "pid.h"

#include "ExampleRegistry.h"
#include "IExample.h"

namespace {
void run() {
PID pid = PID(0.1, 100, -100, 0.1, 0.01, 0.5);
Expand All @@ -15,8 +18,12 @@ void run() {
}
} // namespace

struct PIDSimRunner {
PIDSimRunner() { run(); }
class PIDSim : public IExample {
public:
std::string group() const override { return "controller"; }
std::string name() const override { return "PIDSim"; }
std::string description() const override { return "PID example"; }
void execute() override { run(); }
};

static PIDSimRunner autoRunner;
REGISTER_EXAMPLE(PIDSim, "controller", "PIDSim");
18 changes: 10 additions & 8 deletions src/core/basics/ControlFlow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,15 @@ void exceptions() {
}
}

// A struct that runs code when its object is created
struct ControlFlow {
ControlFlow() {
cout << "\n"
<< "\n"
<< "ControlFlow\n";
#include "ExampleRegistry.h"
#include "IExample.h"

class ControlFlow : public IExample {
public:
std::string group() const override { return "core"; }
std::string name() const override { return "ControlFlow"; }
std::string description() const override { return "ControlFlow"; }
void execute() override {
conditionals();
jumps();
functionCalls();
Expand All @@ -131,5 +134,4 @@ struct ControlFlow {
}
};

// All global and static objects are constructed before main() begins.
static ControlFlow autoRunInstance;
REGISTER_EXAMPLE(ControlFlow, "core", "ControlFlow");
20 changes: 10 additions & 10 deletions src/core/basics/InitializeVariable.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#include <iostream>
using namespace std;

#include "ExampleRegistry.h"
#include "IExample.h"

void initialize_variable();

// A struct that runs code when its object is created
struct InitializeVariable {
InitializeVariable() {
cout << "\n"
<< "\n"
<< "InitializeVariable\n";
initialize_variable();
}
class InitializeVariable : public IExample {
public:
std::string group() const override { return "core"; }
std::string name() const override { return "InitializeVariable"; }
std::string description() const override { return "InitializeVariable"; }
void execute() override { initialize_variable(); }
};

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

struct Foo {
Foo() { cout << "Default constructor/ default init\n"; }
Expand Down
61 changes: 32 additions & 29 deletions src/core/basics/Operations.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
#include <iostream>
using namespace std;

#include "ExampleRegistry.h"
#include "IExample.h"

void arithmeticOperator();
void logicalOperator();
void bitWiseOperator();

struct Operations {
Operations() {
cout << "\n"
<< "\n"
<< "Operation\n";
class Operations : public IExample {
public:
std::string group() const override { return "core"; }
std::string name() const override { return "Operations"; }
std::string description() const override { return "Operation"; }
void execute() override {
arithmeticOperator();
logicalOperator();
bitWiseOperator();
}
};

static Operations autoRunInstance;
REGISTER_EXAMPLE(Operations, "core", "Operations");

void arithmeticOperator() {
cout << "\n--- ArithmeticOperator Examples ---\n";
Expand Down Expand Up @@ -94,33 +98,32 @@ void logicalOperator() {
void bitWiseOperator() {
cout << "\n--- BitWiseOperator Examples ---\n";
bitset<8> bitsA {
0b1111'1111}; bitset<8> bitsB {
0b1111'0000};
0b1111'1111}; bitset<8> bitsB { 0b1111'0000};

cout
<< "bitA = " << bitsA << ", bitB = " << bitsB << "\n";
cout
<< "bitA = " << bitsA << ", bitB = " << bitsB << "\n";

// AND
bitset<8> result = bitsA & bitsB;
cout << "bitA && bitB= " << result << "\n";
// AND
bitset<8> result = bitsA & bitsB;
cout << "bitA && bitB= " << result << "\n";

// OR
result = bitsA | bitsB;
cout << "bitA | bitB= " << result << "\n";
// OR
result = bitsA | bitsB;
cout << "bitA | bitB= " << result << "\n";

// XOR
result = bitsA ^ bitsB;
cout << "bitA ^ bitB= " << result << "\n";
// XOR
result = bitsA ^ bitsB;
cout << "bitA ^ bitB= " << result << "\n";

// NOT
result = ~bitsA;
cout << "~bitA = " << result << "\n";
// NOT
result = ~bitsA;
cout << "~bitA = " << result << "\n";

// LEFT SHIFT
result = bitsA << 1;
cout << "bitA << 1 = " << result << "\n";
// LEFT SHIFT
result = bitsA << 1;
cout << "bitA << 1 = " << result << "\n";

// RIGHT SHIFT
result = bitsA >> 1;
cout << "bitA >> 1 = " << result << "\n";
}
// RIGHT SHIFT
result = bitsA >> 1;
cout << "bitA >> 1 = " << result << "\n";
}
18 changes: 10 additions & 8 deletions src/core/basics/TypeQualifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ void run() {

} // namespace

struct TypeQualifier {
TypeQualifier() {
cout << "\n"
<< "\n"
<< "TypeQualifier\n";
Constant::run();
}
#include "ExampleRegistry.h"
#include "IExample.h"

class TypeQualifier : public IExample {
public:
std::string group() const override { return "core"; }
std::string name() const override { return "TypeQualifier"; }
std::string description() const override { return "TypeQualifier"; }
void execute() override { Constant::run(); }
};

static TypeQualifier autoRunInstance;
REGISTER_EXAMPLE(TypeQualifier, "core", "TypeQualifier");
13 changes: 10 additions & 3 deletions src/core/datatypes/CArray.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include <iostream>

#include "ExampleRegistry.h"
#include "IExample.h"

void arrayExamples() {
std::cout << "\n--- Array Examples ---\n";

Expand All @@ -17,8 +20,12 @@ void arrayExamples() {
}
}

struct CArray {
CArray() { arrayExamples(); }
class CArray : public IExample {
public:
std::string group() const override { return "core"; }
std::string name() const override { return "CArray"; }
std::string description() const override { return ""; }
void execute() override { arrayExamples(); }
};

static CArray autoRunArray;
REGISTER_EXAMPLE(CArray, "core", "CArray");
21 changes: 10 additions & 11 deletions src/core/datatypes/CEnum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,15 @@ void enums() {
[[maybe_unused]] ScopeEnumClassB st = ScopeEnumClassB::enumratorA;
}

// A struct that runs code when its object is created
struct CEnum {
CEnum() {
cout << "\n"
<< "\n"
<< "Compound type: Enum\n";

enums();
}
#include "ExampleRegistry.h"
#include "IExample.h"

class CEnum : public IExample {
public:
std::string group() const override { return "core"; }
std::string name() const override { return "CEnum"; }
std::string description() const override { return "Compound type: Enum"; }
void execute() override { enums(); }
};

// All global and static objects are constructed before main() begins.
static CEnum autoRunInstance;
REGISTER_EXAMPLE(CEnum, "core", "CEnum");
21 changes: 10 additions & 11 deletions src/core/datatypes/CPointers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,15 @@ void pointers() {
std::cout << "By ptr: " << *b_ptr << '\n';
}

// A struct that runs code when its object is created
struct CPointers {
CPointers() {
cout << "\n"
<< "\n"
<< "Compound type: Pointers\n";

pointers();
}
#include "ExampleRegistry.h"
#include "IExample.h"

class CPointers : public IExample {
public:
std::string group() const override { return "core"; }
std::string name() const override { return "CPointers"; }
std::string description() const override { return "Compound type: Pointers"; }
void execute() override { pointers(); }
};

// All global and static objects are constructed before main() begins.
static CPointers autoRunInstance;
REGISTER_EXAMPLE(CPointers, "core", "CPointers");
21 changes: 12 additions & 9 deletions src/core/datatypes/CReferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,20 @@ void run() {

} // namespace RvalueReference

// A struct that runs code when its object is created
struct CReferences {
CReferences() {
cout << "\n"
<< "\n"
<< "Compound type: References\n";

#include "ExampleRegistry.h"
#include "IExample.h"

class CReferences : public IExample {
public:
std::string group() const override { return "core"; }
std::string name() const override { return "CReferences"; }
std::string description() const override {
return "Compound type: References";
}
void execute() override {
references();
RvalueReference::run();
}
};

// All global and static objects are constructed before main() begins.
static CReferences autoRunInstance;
REGISTER_EXAMPLE(CReferences, "core", "CReferences");
19 changes: 9 additions & 10 deletions src/core/datatypes/CStruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,15 @@ void structs() {
s2.print();
}

// A struct that runs code when its object is created
struct CStruct {
CStruct() {
cout << "\n"
<< "\n"
<< "Compound type: Struct\n";
#include "ExampleRegistry.h"
#include "IExample.h"

structs();
}
class CStruct : public IExample {
public:
std::string group() const override { return "core"; }
std::string name() const override { return "CStruct"; }
std::string description() const override { return "Compound type: Struct"; }
void execute() override { structs(); }
};

// All global and static objects are constructed before main() begins.
static CStruct autoRunInstance;
REGISTER_EXAMPLE(CStruct, "core", "CStruct");
Loading