Skip to content

Commit c7e87d8

Browse files
authored
Merge pull request #1 from tomoss/dev
Reworked the examples.
2 parents 8558cf0 + 7ad89e7 commit c7e87d8

33 files changed

Lines changed: 260 additions & 235 deletions

File tree

.clang-format

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
2-
Language: Cpp
2+
Language: Cpp
33
# BasedOnStyle: LLVM
44
AccessModifierOffset: -4
55
AlignAfterOpenBracket: Align
66
AlignConsecutiveAssignments: false
77
AlignConsecutiveDeclarations: false
88
AlignEscapedNewlines: Right
9-
AlignOperands: true
9+
AlignOperands: true
1010
AlignTrailingComments: true
1111
AllowAllParametersOfDeclarationOnNextLine: true
1212
AllowShortBlocksOnASingleLine: false
@@ -21,18 +21,18 @@ AlwaysBreakTemplateDeclarations: Yes
2121
BinPackArguments: true
2222
BinPackParameters: true
2323
BraceWrapping:
24-
AfterClass: false
24+
AfterClass: false
2525
AfterControlStatement: false
26-
AfterEnum: false
27-
AfterFunction: false
28-
AfterNamespace: false
26+
AfterEnum: false
27+
AfterFunction: false
28+
AfterNamespace: false
2929
AfterObjCDeclaration: false
30-
AfterStruct: false
31-
AfterUnion: false
30+
AfterStruct: false
31+
AfterUnion: false
3232
AfterExternBlock: false
33-
BeforeCatch: false
34-
BeforeElse: true
35-
IndentBraces: false
33+
BeforeCatch: false
34+
BeforeElse: true
35+
IndentBraces: false
3636
SplitEmptyFunction: true
3737
SplitEmptyRecord: true
3838
SplitEmptyNamespace: true
@@ -45,39 +45,39 @@ BreakConstructorInitializersBeforeComma: true
4545
BreakConstructorInitializers: BeforeColon
4646
BreakAfterJavaFieldAnnotations: false
4747
BreakStringLiterals: true
48-
ColumnLimit: 160
49-
CommentPragmas: '^ IWYU pragma:'
48+
ColumnLimit: 160
49+
CommentPragmas: "^ IWYU pragma:"
5050
CompactNamespaces: false
5151
ConstructorInitializerAllOnOneLineOrOnePerLine: false
5252
ConstructorInitializerIndentWidth: 4
5353
ContinuationIndentWidth: 4
5454
Cpp11BracedListStyle: true
5555
DerivePointerAlignment: false
56-
DisableFormat: false
56+
DisableFormat: false
5757
ExperimentalAutoDetectBinPacking: false
5858
FixNamespaceComments: true
5959
ForEachMacros:
6060
- foreach
6161
- Q_FOREACH
6262
- BOOST_FOREACH
63-
IncludeBlocks: Preserve
63+
IncludeBlocks: Preserve
6464
IncludeCategories:
65-
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
66-
Priority: 2
67-
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
68-
Priority: 3
69-
- Regex: '.*'
70-
Priority: 1
71-
IncludeIsMainRegex: '(Test)?$'
65+
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
66+
Priority: 2
67+
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
68+
Priority: 3
69+
- Regex: ".*"
70+
Priority: 1
71+
IncludeIsMainRegex: "(Test)?$"
7272
IndentCaseLabels: false
7373
IndentPPDirectives: None
74-
IndentWidth: 4
74+
IndentWidth: 4
7575
IndentWrappedFunctionNames: false
7676
JavaScriptQuotes: Leave
7777
JavaScriptWrapImports: true
7878
KeepEmptyLinesAtTheStartOfBlocks: true
79-
MacroBlockBegin: ''
80-
MacroBlockEnd: ''
79+
MacroBlockBegin: ""
80+
MacroBlockEnd: ""
8181
MaxEmptyLinesToKeep: 1
8282
NamespaceIndentation: All
8383
ObjCBinPackProtocolList: Auto
@@ -93,8 +93,8 @@ PenaltyBreakTemplateDeclaration: 10
9393
PenaltyExcessCharacter: 1000000
9494
PenaltyReturnTypeOnItsOwnLine: 60
9595
PointerAlignment: Left
96-
ReflowComments: true
97-
SortIncludes: true
96+
ReflowComments: true
97+
SortIncludes: true
9898
SortUsingDeclarations: true
9999
SpaceAfterCStyleCast: false
100100
SpaceAfterTemplateKeyword: false
@@ -106,13 +106,13 @@ SpaceBeforeParens: ControlStatements
106106
SpaceBeforeRangeBasedForLoopColon: true
107107
SpaceInEmptyParentheses: false
108108
SpacesBeforeTrailingComments: 1
109-
SpacesInAngles: false
109+
SpacesInAngles: false
110110
SpacesInContainerLiterals: true
111111
SpacesInCStyleCastParentheses: false
112112
SpacesInParentheses: false
113113
SpacesInSquareBrackets: false
114-
Standard: Cpp11
115-
TabWidth: 4
116-
UseTab: Never
114+
Standard: Latest
115+
TabWidth: 4
116+
UseTab: Never
117117
...
118118

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
cmake_minimum_required(VERSION 3.21)
2-
project(singleton-smart-pointer-example VERSION 0.1.0 LANGUAGES CXX)
2+
project(01-singleton-classic-example VERSION 0.1.0 LANGUAGES CXX)
33

44
# Export compile commands for clangd
55
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
66

7-
add_executable(singleton-smart-pointer-example
7+
add_executable(01-singleton-classic-example
88
src/main.cpp
99
src/singleton.cpp
1010
)
1111

12-
target_include_directories(singleton-smart-pointer-example PRIVATE
12+
target_include_directories(01-singleton-classic-example PRIVATE
1313
${CMAKE_CURRENT_SOURCE_DIR}/inc
1414
)

singleton-classic-static-example/inc/singleton.h renamed to 01-singleton-classic-example/inc/singleton.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Singleton {
1010
return instance;
1111
}
1212

13-
void func();
13+
void info();
1414

1515
private:
1616
Singleton();
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "singleton.h"
2+
#include <iostream>
3+
#include <thread>
4+
#include <vector>
5+
6+
/*
7+
* Example with static member variable
8+
* Static memory allocation
9+
* Eager initialization
10+
* Singleton is created before main() and destroyed after main() call
11+
* Initialization is thread-safe - The static member is initialized before main() in a single-threaded context, so no construction race is possible.
12+
* The Static Initialization Order Fiasco - If the singleton instance is accessed during the initialization of another static object.
13+
*/
14+
15+
int main() {
16+
std::cout << "--- main start ---" << std::endl;
17+
18+
std::vector<std::thread> threads;
19+
20+
// Launch 10 threads
21+
for (int i = 0; i < 10; ++i) {
22+
threads.emplace_back([]() { Singleton::getInstance().info(); });
23+
}
24+
25+
for (auto& t : threads) {
26+
t.join();
27+
}
28+
29+
std::cout << "--- main end ---" << std::endl;
30+
}

singleton-classic-static-example/src/singleton.cpp renamed to 01-singleton-classic-example/src/singleton.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
#include "singleton.h"
22
#include <iostream>
3+
#include <syncstream>
4+
#include <thread>
35

46
Singleton Singleton::instance;
57

68
Singleton::Singleton() {
79
std::cout << "Singleton created." << std::endl;
810
}
911

10-
void Singleton::func() {
11-
std::cout << "Doing something..." << std::endl;
12+
void Singleton::info() {
13+
std::osyncstream(std::cout) << "Current instance address: " << this << " | Current thread ID: " << std::this_thread::get_id() << '\n';
1214
}
1315

1416
Singleton::~Singleton() {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
cmake_minimum_required(VERSION 3.21)
2-
project(singleton-classic-static-example VERSION 0.1.0 LANGUAGES CXX)
2+
project(02-singleton-meyers-example VERSION 0.1.0 LANGUAGES CXX)
33

44
# Export compile commands for clangd
55
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
66

7-
add_executable(singleton-classic-static-example
7+
add_executable(02-singleton-meyers-example
88
src/main.cpp
99
src/singleton.cpp
1010
)
1111

12-
target_include_directories(singleton-classic-static-example PRIVATE
12+
target_include_directories(02-singleton-meyers-example PRIVATE
1313
${CMAKE_CURRENT_SOURCE_DIR}/inc
1414
)

singleton-meyers-example/inc/singleton.h renamed to 02-singleton-meyers-example/inc/singleton.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Singleton {
1010
return instance;
1111
}
1212

13-
void func();
13+
void info();
1414

1515
private:
1616
Singleton();
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <vector>
55

66
/*
7-
* Meyer’s Singleton - Example with STATIC LOCAL VARIABLE
7+
* Meyer’s Singleton - Example with static local variable
88
* Static memory allocation
99
* Lazy initialization
1010
* Singleton is created only after first call of getInstance() and destroyed after main() call
@@ -20,7 +20,7 @@ int main() {
2020

2121
// Launch 10 threads
2222
for (int i = 0; i < 10; ++i) {
23-
threads.emplace_back([]() { Singleton::getInstance().func(); });
23+
threads.emplace_back([]() { Singleton::getInstance().info(); });
2424
}
2525

2626
for (auto& t : threads) {

singleton-meyers-example/src/singleton.cpp renamed to 02-singleton-meyers-example/src/singleton.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#include "singleton.h"
22
#include <iostream>
3+
#include <syncstream>
4+
#include <thread>
35

46
Singleton::Singleton() {
57
std::cout << "Singleton created." << std::endl;
68
}
79

8-
void Singleton::func() {
9-
std::cout << "Doing something..." << std::endl;
10+
void Singleton::info() {
11+
std::osyncstream(std::cout) << "Current instance address: " << this << " | Current thread ID: " << std::this_thread::get_id() << '\n';
1012
}
1113

1214
Singleton::~Singleton() {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
cmake_minimum_required(VERSION 3.21)
2+
project(03-singleton-classic-dynamic-example VERSION 0.1.0 LANGUAGES CXX)
3+
4+
# Export compile commands for clangd
5+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
6+
7+
add_executable(03-singleton-classic-dynamic-example
8+
src/main.cpp
9+
src/singleton.cpp
10+
)
11+
12+
target_include_directories(03-singleton-classic-dynamic-example PRIVATE
13+
${CMAKE_CURRENT_SOURCE_DIR}/inc
14+
)

0 commit comments

Comments
 (0)