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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ coverage_gcovr
coverage_lcov
!.vscode/launch.json
!.vscode/tasks.json
.cache
.cache
__pycache__/
Binary file removed docs/image/future_promis.png
Binary file not shown.
Binary file removed docs/image/future_promis_flow.png
Binary file not shown.
3 changes: 2 additions & 1 deletion scripts/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ cppcheck \
--inline-suppr \
--quiet \
--error-exitcode=1 \
./src ./include
./src ./include \
-isrc/embedded/

echo "[OK] Static analysis passed"

Expand Down
34 changes: 18 additions & 16 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
set(CORE_SOURCES

# Basic C++ language features
${CMAKE_CURRENT_SOURCE_DIR}/basics/InitializeVariable.cpp
${CMAKE_CURRENT_SOURCE_DIR}/basics/Initialization.cpp
${CMAKE_CURRENT_SOURCE_DIR}/basics/Operations.cpp
${CMAKE_CURRENT_SOURCE_DIR}/basics/TypeQualifier.cpp
${CMAKE_CURRENT_SOURCE_DIR}/basics/ControlFlow.cpp
Expand Down Expand Up @@ -67,26 +67,27 @@ set(CORE_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/container/sequence/Deque.cpp
${CMAKE_CURRENT_SOURCE_DIR}/container/associative/Set.cpp

${CMAKE_CURRENT_SOURCE_DIR}/expression/Lambda.cpp
${CMAKE_CURRENT_SOURCE_DIR}/expression/FunctionPointer.cpp

# Smart pointers
${CMAKE_CURRENT_SOURCE_DIR}/smart_pointer/Unique.cpp
${CMAKE_CURRENT_SOURCE_DIR}/smart_pointer/Shared.cpp
${CMAKE_CURRENT_SOURCE_DIR}/smart_pointer/Weak.cpp

# Operator overloading
${CMAKE_CURRENT_SOURCE_DIR}/overloading/ArithmeticOperator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/overloading/IOOperator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/overloading/UnaryOperator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/overloading/ComparisonOperator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/overloading/InDecOperator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/overloading/SubscriptOperator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/overloading/ParenthesisOperator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/overloading/TypeCast.cpp
${CMAKE_CURRENT_SOURCE_DIR}/overloading/AssignmentOperator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/overloading/ClassMemberAccessOperator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/overloading/AllocationOperator.cpp
# Function
${CMAKE_CURRENT_SOURCE_DIR}/function/Lambda.cpp
${CMAKE_CURRENT_SOURCE_DIR}/function/FunctionPointer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/function/Functional.cpp
## Operator overloading
${CMAKE_CURRENT_SOURCE_DIR}/function/operator_overloading/ArithmeticOperator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/function/operator_overloading/StreamOperator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/function/operator_overloading/UnaryOperator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/function/operator_overloading/ComparisonOperator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/function/operator_overloading/InDecOperator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/function/operator_overloading/SubscriptOperator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/function/operator_overloading/FunctionCallOperator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/function/operator_overloading/TypeCast.cpp
${CMAKE_CURRENT_SOURCE_DIR}/function/operator_overloading/AssignmentOperator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/function/operator_overloading/ClassMemberAccessOperator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/function/operator_overloading/AllocationOperator.cpp

# Date and time
${CMAKE_CURRENT_SOURCE_DIR}/datetime/Time.cpp
Expand All @@ -97,6 +98,7 @@ set(CORE_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/concurrency/RaceCondition.cpp
${CMAKE_CURRENT_SOURCE_DIR}/concurrency/ConditionVariable.cpp
${CMAKE_CURRENT_SOURCE_DIR}/concurrency/FuturePromise.cpp
${CMAKE_CURRENT_SOURCE_DIR}/concurrency/Timing.cpp

# Utils
${CMAKE_CURRENT_SOURCE_DIR}/utils/Algorithm.cpp
Expand Down
50 changes: 25 additions & 25 deletions src/core/basics/ControlFlow.cpp
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
#include <iostream>
#include "ExampleRegistry.h"
#include "Logger.h"

namespace {
void conditionals() {
std::cout << "\n--- Conditional Examples ---\n";
LOG("Conditional Examples");
// if-else
int x = rand();
std::cout << "x = " << x << "\n";
LOG_S("x = " << x);
if (x > 0) {
std::cout << "x is positive\n";
LOG("x is positive");
} else if (x < 0) {
std::cout << "x is negative\n";
LOG("x is negative");
} else {
std::cout << "x is zero\n";
LOG("x is zero");
}

// switch
int choice = rand() % 2 + 1;
std::cout << "choice = " << x << "\n";
LOG_S("Choice = " << std::to_string(x));
switch (choice) {
case 1:
std::cout << "Choice is 1\n";
LOG("Choice is 1");
break;
case 2:
std::cout << "Choice is 2\n";
LOG("Choice is 2");
break;
default:
std::cout << "Choice is something else\n";
LOG("Choice is something else");
break;
}
}

void jumps() {
std::cout << "\n--- Jump Statement Examples ---\n";
LOG("Jump Statement Examples");

// goto
int num = rand();
if (num == 3)
goto jumpLabel;
std::cout << "This line will be skipped.\n";
LOG("This line will be skipped.");

jumpLabel:
std::cout << "Jumped here using goto!\n";
LOG("Jumped here using goto!");

// break / continue
for (int i = 0; i < 5; ++i) {
if (i == 2)
continue; // skip 2
if (i == 4)
break; // stop loop at 4
std::cout << "i = " << i << "\n";
LOG_S("i = " << i);
}
}

Expand All @@ -59,43 +59,43 @@ int square(int n) {
}

void functionCalls() {
std::cout << "\n--- Function Call Examples ---\n";
LOG("Function Call Examples");
// function call
int result = square(5);
std::cout << "square(5) = " << result << "\n";
LOG_S("square(5) = " << result);
}

void loops() {
std::cout << "\n--- Loop Examples ---\n";
LOG("Loop Examples");

// while
int i = 0;
while (i < 3) {
std::cout << "while loop i = " << i << "\n";
LOG_S("while loop i = " << i);
++i;
}

// do-while
int j = 0;
do {
std::cout << "do-while loop j = " << j << "\n";
LOG_S("do-while loop j = " << j);
++j;
} while (j < 2);

// for(initialization; condition; update)
for (int k = 0; k < 3; ++k) {
std::cout << "for loop k = " << k << "\n";
LOG_S("for loop k = " << k);
}

// ranged-for
const int arr[] = {10, 20, 30};
for (int value : arr) {
std::cout << "ranged-for value = " << value << "\n";
LOG_S("ranged-for value = " << value);
}
}

void halts() {
std::cout << "\n--- Halt Examples ---\n";
LOG("Halt Examples");

// std::exit() — terminates the program normally
// std::abort() — terminates abnormally (no cleanup)
Expand All @@ -106,20 +106,20 @@ void halts() {
}

void exceptions() {
std::cout << "\n--- Exception Handling Examples ---\n";
LOG("Exception Handling Examples");

// try - catch - throw
try {
throw std::runtime_error("Something went wrong!");
} catch (const std::exception& e) {
std::cout << "Caught exception: " << e.what() << "\n";
LOG_S("Caught exception: " << e.what());
}
}
} // namespace

class ControlFlow : public IExample {
public:
std::string group() const override { return "core"; }
std::string group() const override { return "core/basics"; }
std::string name() const override { return "ControlFlow"; }
std::string description() const override { return "ControlFlow"; }
void execute() override {
Expand Down
114 changes: 114 additions & 0 deletions src/core/basics/Initialization.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// cppcheck-suppress-file [unreadVariable, unusedVariable, uninitdata, uninitvar, unassignedVariable]

#include "ExampleRegistry.h"
#include "Logger.h"

/// @brief Dummy class
struct Dummy {
Dummy() { LOG("Default ctor"); }
explicit Dummy(int value) { LOG_S("Int ctor: " << value); }
Dummy(const Dummy&) { LOG("Copy ctor"); }
};

/// @brief Aggregate type
struct Aggregate {
int x;
int y;
};

/// @brief Default Initialization
void default_ini() {
int a;
LOG_S("a = " << a);

Dummy object;

auto* p1 = new int;
LOG_S("*p1 = " << *p1);
auto* p2 = new Dummy;

delete p1;
delete p2;
}

/// @brief Value Initialization
void value_ini() {
// int x();Dummy obj(); // Most Vexing Parse:function declaration

int a = int();
int b{};

LOG_S("a = " << a);
LOG_S("b = " << b);

Dummy object1 = Dummy(); // value initialization x copy-initialization
Dummy object2{};

auto* p1 = new int();
LOG_S("*p1 = " << *p1);

auto* p2 = new Dummy();

delete p1;
delete p2;
}

/// @brief Direct Initialization
void direct_ini() {
int a(42);

Dummy object1(1);
Dummy object2(static_cast<int>(2));

LOG_S("a = " << a);
}

/// @brief Copy Initialization
void copy_ini() {
Dummy source{1};

Dummy object1 = source;
Dummy object2 = Dummy{2};

int value = 42;

LOG_S("value = " << value);
}

/// @brief List Initialization (since C++11)
void list_ini() {
Dummy object1{1}; // direct-list-init
Dummy object2 = {static_cast<Dummy>(2)}; // copy-list-init

int values1[]{1, 2, 3};
int values2[] = {4, 5, 6};
}

/// @brief Aggregate Initialization
void aggregate_ini() {
Aggregate a1 = {1, 2};
Aggregate a2{3, 4};
Aggregate a3{.x = 3, .y = 4};

LOG_S("a1 = {" << a1.x << ", " << a1.y << "}");
LOG_S("a2 = {" << a2.x << ", " << a2.y << "}");
LOG_S("a3 = {" << a3.x << ", " << a3.y << "}");
}

class Initialization : public IExample {
public:
std::string group() const override { return "core/basics"; }
std::string name() const override { return "Initialization"; }
std::string description() const override { return "Initialization Examples"; }

void execute() override {
default_ini();
value_ini();
direct_ini();
copy_ini();
list_ini();
aggregate_ini();
}
};

REGISTER_EXAMPLE(Initialization);
Loading
Loading