Skip to content

Commit 1d193ba

Browse files
authored
Merge pull request #63 from urbytes21/dev
id 1785075812
2 parents 5a6bc9e + c524c81 commit 1d193ba

52 files changed

Lines changed: 1678 additions & 1889 deletions

Some content is hidden

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

‎.gitignore‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ coverage_gcovr
77
coverage_lcov
88
!.vscode/launch.json
99
!.vscode/tasks.json
10-
.cache
10+
.cache
11+
__pycache__/

‎docs/image/future_promis.png‎

-118 KB
Binary file not shown.

‎docs/image/future_promis_flow.png‎

-35.5 KB
Binary file not shown.

‎scripts/run.sh‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ cppcheck \
4545
--inline-suppr \
4646
--quiet \
4747
--error-exitcode=1 \
48-
./src ./include
48+
./src ./include \
49+
-isrc/embedded/
4950

5051
echo "[OK] Static analysis passed"
5152

‎src/core/CMakeLists.txt‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
set(CORE_SOURCES
88

99
# Basic C++ language features
10-
${CMAKE_CURRENT_SOURCE_DIR}/basics/InitializeVariable.cpp
10+
${CMAKE_CURRENT_SOURCE_DIR}/basics/Initialization.cpp
1111
${CMAKE_CURRENT_SOURCE_DIR}/basics/Operations.cpp
1212
${CMAKE_CURRENT_SOURCE_DIR}/basics/TypeQualifier.cpp
1313
${CMAKE_CURRENT_SOURCE_DIR}/basics/ControlFlow.cpp
@@ -67,26 +67,27 @@ set(CORE_SOURCES
6767
${CMAKE_CURRENT_SOURCE_DIR}/container/sequence/Deque.cpp
6868
${CMAKE_CURRENT_SOURCE_DIR}/container/associative/Set.cpp
6969

70-
${CMAKE_CURRENT_SOURCE_DIR}/expression/Lambda.cpp
71-
${CMAKE_CURRENT_SOURCE_DIR}/expression/FunctionPointer.cpp
72-
7370
# Smart pointers
7471
${CMAKE_CURRENT_SOURCE_DIR}/smart_pointer/Unique.cpp
7572
${CMAKE_CURRENT_SOURCE_DIR}/smart_pointer/Shared.cpp
7673
${CMAKE_CURRENT_SOURCE_DIR}/smart_pointer/Weak.cpp
7774

78-
# Operator overloading
79-
${CMAKE_CURRENT_SOURCE_DIR}/overloading/ArithmeticOperator.cpp
80-
${CMAKE_CURRENT_SOURCE_DIR}/overloading/IOOperator.cpp
81-
${CMAKE_CURRENT_SOURCE_DIR}/overloading/UnaryOperator.cpp
82-
${CMAKE_CURRENT_SOURCE_DIR}/overloading/ComparisonOperator.cpp
83-
${CMAKE_CURRENT_SOURCE_DIR}/overloading/InDecOperator.cpp
84-
${CMAKE_CURRENT_SOURCE_DIR}/overloading/SubscriptOperator.cpp
85-
${CMAKE_CURRENT_SOURCE_DIR}/overloading/ParenthesisOperator.cpp
86-
${CMAKE_CURRENT_SOURCE_DIR}/overloading/TypeCast.cpp
87-
${CMAKE_CURRENT_SOURCE_DIR}/overloading/AssignmentOperator.cpp
88-
${CMAKE_CURRENT_SOURCE_DIR}/overloading/ClassMemberAccessOperator.cpp
89-
${CMAKE_CURRENT_SOURCE_DIR}/overloading/AllocationOperator.cpp
75+
# Function
76+
${CMAKE_CURRENT_SOURCE_DIR}/function/Lambda.cpp
77+
${CMAKE_CURRENT_SOURCE_DIR}/function/FunctionPointer.cpp
78+
${CMAKE_CURRENT_SOURCE_DIR}/function/Functional.cpp
79+
## Operator overloading
80+
${CMAKE_CURRENT_SOURCE_DIR}/function/operator_overloading/ArithmeticOperator.cpp
81+
${CMAKE_CURRENT_SOURCE_DIR}/function/operator_overloading/StreamOperator.cpp
82+
${CMAKE_CURRENT_SOURCE_DIR}/function/operator_overloading/UnaryOperator.cpp
83+
${CMAKE_CURRENT_SOURCE_DIR}/function/operator_overloading/ComparisonOperator.cpp
84+
${CMAKE_CURRENT_SOURCE_DIR}/function/operator_overloading/InDecOperator.cpp
85+
${CMAKE_CURRENT_SOURCE_DIR}/function/operator_overloading/SubscriptOperator.cpp
86+
${CMAKE_CURRENT_SOURCE_DIR}/function/operator_overloading/FunctionCallOperator.cpp
87+
${CMAKE_CURRENT_SOURCE_DIR}/function/operator_overloading/TypeCast.cpp
88+
${CMAKE_CURRENT_SOURCE_DIR}/function/operator_overloading/AssignmentOperator.cpp
89+
${CMAKE_CURRENT_SOURCE_DIR}/function/operator_overloading/ClassMemberAccessOperator.cpp
90+
${CMAKE_CURRENT_SOURCE_DIR}/function/operator_overloading/AllocationOperator.cpp
9091

9192
# Date and time
9293
${CMAKE_CURRENT_SOURCE_DIR}/datetime/Time.cpp
@@ -97,6 +98,7 @@ set(CORE_SOURCES
9798
${CMAKE_CURRENT_SOURCE_DIR}/concurrency/RaceCondition.cpp
9899
${CMAKE_CURRENT_SOURCE_DIR}/concurrency/ConditionVariable.cpp
99100
${CMAKE_CURRENT_SOURCE_DIR}/concurrency/FuturePromise.cpp
101+
${CMAKE_CURRENT_SOURCE_DIR}/concurrency/Timing.cpp
100102

101103
# Utils
102104
${CMAKE_CURRENT_SOURCE_DIR}/utils/Algorithm.cpp

‎src/core/basics/ControlFlow.cpp‎

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
1-
#include <iostream>
21
#include "ExampleRegistry.h"
2+
#include "Logger.h"
33

44
namespace {
55
void conditionals() {
6-
std::cout << "\n--- Conditional Examples ---\n";
6+
LOG("Conditional Examples");
77
// if-else
88
int x = rand();
9-
std::cout << "x = " << x << "\n";
9+
LOG_S("x = " << x);
1010
if (x > 0) {
11-
std::cout << "x is positive\n";
11+
LOG("x is positive");
1212
} else if (x < 0) {
13-
std::cout << "x is negative\n";
13+
LOG("x is negative");
1414
} else {
15-
std::cout << "x is zero\n";
15+
LOG("x is zero");
1616
}
1717

1818
// switch
1919
int choice = rand() % 2 + 1;
20-
std::cout << "choice = " << x << "\n";
20+
LOG_S("Choice = " << std::to_string(x));
2121
switch (choice) {
2222
case 1:
23-
std::cout << "Choice is 1\n";
23+
LOG("Choice is 1");
2424
break;
2525
case 2:
26-
std::cout << "Choice is 2\n";
26+
LOG("Choice is 2");
2727
break;
2828
default:
29-
std::cout << "Choice is something else\n";
29+
LOG("Choice is something else");
3030
break;
3131
}
3232
}
3333

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

3737
// goto
3838
int num = rand();
3939
if (num == 3)
4040
goto jumpLabel;
41-
std::cout << "This line will be skipped.\n";
41+
LOG("This line will be skipped.");
4242

4343
jumpLabel:
44-
std::cout << "Jumped here using goto!\n";
44+
LOG("Jumped here using goto!");
4545

4646
// break / continue
4747
for (int i = 0; i < 5; ++i) {
4848
if (i == 2)
4949
continue; // skip 2
5050
if (i == 4)
5151
break; // stop loop at 4
52-
std::cout << "i = " << i << "\n";
52+
LOG_S("i = " << i);
5353
}
5454
}
5555

@@ -59,43 +59,43 @@ int square(int n) {
5959
}
6060

6161
void functionCalls() {
62-
std::cout << "\n--- Function Call Examples ---\n";
62+
LOG("Function Call Examples");
6363
// function call
6464
int result = square(5);
65-
std::cout << "square(5) = " << result << "\n";
65+
LOG_S("square(5) = " << result);
6666
}
6767

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

7171
// while
7272
int i = 0;
7373
while (i < 3) {
74-
std::cout << "while loop i = " << i << "\n";
74+
LOG_S("while loop i = " << i);
7575
++i;
7676
}
7777

7878
// do-while
7979
int j = 0;
8080
do {
81-
std::cout << "do-while loop j = " << j << "\n";
81+
LOG_S("do-while loop j = " << j);
8282
++j;
8383
} while (j < 2);
8484

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

9090
// ranged-for
9191
const int arr[] = {10, 20, 30};
9292
for (int value : arr) {
93-
std::cout << "ranged-for value = " << value << "\n";
93+
LOG_S("ranged-for value = " << value);
9494
}
9595
}
9696

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

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

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

111111
// try - catch - throw
112112
try {
113113
throw std::runtime_error("Something went wrong!");
114114
} catch (const std::exception& e) {
115-
std::cout << "Caught exception: " << e.what() << "\n";
115+
LOG_S("Caught exception: " << e.what());
116116
}
117117
}
118118
} // namespace
119119

120120
class ControlFlow : public IExample {
121121
public:
122-
std::string group() const override { return "core"; }
122+
std::string group() const override { return "core/basics"; }
123123
std::string name() const override { return "ControlFlow"; }
124124
std::string description() const override { return "ControlFlow"; }
125125
void execute() override {

‎src/core/basics/Initialization.cpp‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// cppcheck-suppress-file [unreadVariable, unusedVariable, uninitdata, uninitvar, unassignedVariable]
2+
3+
#include "ExampleRegistry.h"
4+
#include "Logger.h"
5+
6+
/// @brief Dummy class
7+
struct Dummy {
8+
Dummy() { LOG("Default ctor"); }
9+
explicit Dummy(int value) { LOG_S("Int ctor: " << value); }
10+
Dummy(const Dummy&) { LOG("Copy ctor"); }
11+
};
12+
13+
/// @brief Aggregate type
14+
struct Aggregate {
15+
int x;
16+
int y;
17+
};
18+
19+
/// @brief Default Initialization
20+
void default_ini() {
21+
int a;
22+
LOG_S("a = " << a);
23+
24+
Dummy object;
25+
26+
auto* p1 = new int;
27+
LOG_S("*p1 = " << *p1);
28+
auto* p2 = new Dummy;
29+
30+
delete p1;
31+
delete p2;
32+
}
33+
34+
/// @brief Value Initialization
35+
void value_ini() {
36+
// int x();Dummy obj(); // Most Vexing Parse:function declaration
37+
38+
int a = int();
39+
int b{};
40+
41+
LOG_S("a = " << a);
42+
LOG_S("b = " << b);
43+
44+
Dummy object1 = Dummy(); // value initialization x copy-initialization
45+
Dummy object2{};
46+
47+
auto* p1 = new int();
48+
LOG_S("*p1 = " << *p1);
49+
50+
auto* p2 = new Dummy();
51+
52+
delete p1;
53+
delete p2;
54+
}
55+
56+
/// @brief Direct Initialization
57+
void direct_ini() {
58+
int a(42);
59+
60+
Dummy object1(1);
61+
Dummy object2(static_cast<int>(2));
62+
63+
LOG_S("a = " << a);
64+
}
65+
66+
/// @brief Copy Initialization
67+
void copy_ini() {
68+
Dummy source{1};
69+
70+
Dummy object1 = source;
71+
Dummy object2 = Dummy{2};
72+
73+
int value = 42;
74+
75+
LOG_S("value = " << value);
76+
}
77+
78+
/// @brief List Initialization (since C++11)
79+
void list_ini() {
80+
Dummy object1{1}; // direct-list-init
81+
Dummy object2 = {static_cast<Dummy>(2)}; // copy-list-init
82+
83+
int values1[]{1, 2, 3};
84+
int values2[] = {4, 5, 6};
85+
}
86+
87+
/// @brief Aggregate Initialization
88+
void aggregate_ini() {
89+
Aggregate a1 = {1, 2};
90+
Aggregate a2{3, 4};
91+
Aggregate a3{.x = 3, .y = 4};
92+
93+
LOG_S("a1 = {" << a1.x << ", " << a1.y << "}");
94+
LOG_S("a2 = {" << a2.x << ", " << a2.y << "}");
95+
LOG_S("a3 = {" << a3.x << ", " << a3.y << "}");
96+
}
97+
98+
class Initialization : public IExample {
99+
public:
100+
std::string group() const override { return "core/basics"; }
101+
std::string name() const override { return "Initialization"; }
102+
std::string description() const override { return "Initialization Examples"; }
103+
104+
void execute() override {
105+
default_ini();
106+
value_ini();
107+
direct_ini();
108+
copy_ini();
109+
list_ini();
110+
aggregate_ini();
111+
}
112+
};
113+
114+
REGISTER_EXAMPLE(Initialization);

0 commit comments

Comments
 (0)