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
62 changes: 31 additions & 31 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
Language: Cpp
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
Expand All @@ -21,18 +21,18 @@ AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: true
IndentBraces: false
BeforeCatch: false
BeforeElse: true
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
Expand All @@ -45,39 +45,39 @@ BreakConstructorInitializersBeforeComma: true
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 160
CommentPragmas: '^ IWYU pragma:'
ColumnLimit: 160
CommentPragmas: "^ IWYU pragma:"
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '(Test)?$'
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
- Regex: ".*"
Priority: 1
IncludeIsMainRegex: "(Test)?$"
IndentCaseLabels: false
IndentPPDirectives: None
IndentWidth: 4
IndentWidth: 4
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MacroBlockBegin: ""
MacroBlockEnd: ""
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
ObjCBinPackProtocolList: Auto
Expand All @@ -93,8 +93,8 @@ PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: false
Expand All @@ -106,13 +106,13 @@ SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 4
UseTab: Never
Standard: Latest
TabWidth: 4
UseTab: Never
...

Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
cmake_minimum_required(VERSION 3.21)
project(singleton-smart-pointer-example VERSION 0.1.0 LANGUAGES CXX)
project(01-singleton-classic-example VERSION 0.1.0 LANGUAGES CXX)

# Export compile commands for clangd
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

add_executable(singleton-smart-pointer-example
add_executable(01-singleton-classic-example
src/main.cpp
src/singleton.cpp
)

target_include_directories(singleton-smart-pointer-example PRIVATE
target_include_directories(01-singleton-classic-example PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/inc
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Singleton {
return instance;
}

void func();
void info();

private:
Singleton();
Expand Down
30 changes: 30 additions & 0 deletions 01-singleton-classic-example/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "singleton.h"
#include <iostream>
#include <thread>
#include <vector>

/*
* Example with static member variable
* Static memory allocation
* Eager initialization
* Singleton is created before main() and destroyed after main() call
* Initialization is thread-safe - The static member is initialized before main() in a single-threaded context, so no construction race is possible.
* The Static Initialization Order Fiasco - If the singleton instance is accessed during the initialization of another static object.
*/

int main() {
std::cout << "--- main start ---" << std::endl;

std::vector<std::thread> threads;

// Launch 10 threads
for (int i = 0; i < 10; ++i) {
threads.emplace_back([]() { Singleton::getInstance().info(); });
}

for (auto& t : threads) {
t.join();
}

std::cout << "--- main end ---" << std::endl;
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#include "singleton.h"
#include <iostream>
#include <syncstream>
#include <thread>

Singleton Singleton::instance;

Singleton::Singleton() {
std::cout << "Singleton created." << std::endl;
}

void Singleton::func() {
std::cout << "Doing something..." << std::endl;
void Singleton::info() {
std::osyncstream(std::cout) << "Current instance address: " << this << " | Current thread ID: " << std::this_thread::get_id() << '\n';
}

Singleton::~Singleton() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
cmake_minimum_required(VERSION 3.21)
project(singleton-classic-static-example VERSION 0.1.0 LANGUAGES CXX)
project(02-singleton-meyers-example VERSION 0.1.0 LANGUAGES CXX)

# Export compile commands for clangd
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

add_executable(singleton-classic-static-example
add_executable(02-singleton-meyers-example
src/main.cpp
src/singleton.cpp
)

target_include_directories(singleton-classic-static-example PRIVATE
target_include_directories(02-singleton-meyers-example PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/inc
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Singleton {
return instance;
}

void func();
void info();

private:
Singleton();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <vector>

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

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

for (auto& t : threads) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include "singleton.h"
#include <iostream>
#include <syncstream>
#include <thread>

Singleton::Singleton() {
std::cout << "Singleton created." << std::endl;
}

void Singleton::func() {
std::cout << "Doing something..." << std::endl;
void Singleton::info() {
std::osyncstream(std::cout) << "Current instance address: " << this << " | Current thread ID: " << std::this_thread::get_id() << '\n';
}

Singleton::~Singleton() {
Expand Down
14 changes: 14 additions & 0 deletions 03-singleton-classic-dynamic-example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.21)
project(03-singleton-classic-dynamic-example VERSION 0.1.0 LANGUAGES CXX)

# Export compile commands for clangd
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

add_executable(03-singleton-classic-dynamic-example
src/main.cpp
src/singleton.cpp
)

target_include_directories(03-singleton-classic-dynamic-example PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/inc
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Singleton {

static Singleton& getInstance();
static void delInstance();
void func();
void info();

private:
Singleton();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
#include <iostream>

/*
* Example from Cherno with STATIC GLOBAL VARIABLE
* Example with static member pointer
* Dynamic memory allocation
* Lazy initialization
* Singleton is created only after first call of getInstance() and destroyed by calling delInstance()
* Thread-safety not guaranteed
* Not Thread-safe
*/

int main() {
std::cout << "--- main start ---" << std::endl;

Singleton::getInstance().func();

Singleton::getInstance().info();
Singleton::getInstance().info();
Singleton::delInstance();

std::cout << "--- main end ---" << std::endl;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "singleton.h"
#include <iostream>
#include <thread>

Singleton* Singleton::instance = nullptr;

Expand All @@ -21,8 +22,8 @@ Singleton::Singleton() {
std::cout << "Singleton created." << std::endl;
}

void Singleton::func() {
std::cout << "Doing something..." << std::endl;
void Singleton::info() {
std::cout << "Current instance address: " << this << " | Current thread ID: " << std::this_thread::get_id() << '\n';
}

Singleton::~Singleton() {
Expand Down
11 changes: 11 additions & 0 deletions 04-singleton-cherno-example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.21)
project(04-singleton-cherno-example VERSION 0.1.0 LANGUAGES CXX)

add_executable(04-singleton-cherno-example
src/main.cpp
src/singleton.cpp
)

target_include_directories(04-singleton-cherno-example PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/inc
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Singleton {

static Singleton& getInstance();
static void delInstance();
void func();
void info();

private:
Singleton();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
#include <iostream>

/*
* Example with STATIC MEMBER VARIABLE
* Example from Cherno with static global variable in cpp file
* Dynamic memory allocation
* Lazy initialization
* Singleton is created only after first call of getInstance() and destroyed by calling delInstance()
* Thread-safety not guaranteed
* Not Thread-safe
*/

int main() {
std::cout << "--- main start ---" << std::endl;

Singleton::getInstance().func();
Singleton::getInstance().func();
Singleton::getInstance().info();
Singleton::getInstance().info();
Singleton::delInstance();

std::cout << "--- main end ---" << std::endl;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "../inc/singleton.h"
#include <iostream>
#include <thread>

/* it's private to this file */
static Singleton* instance = nullptr;
Expand All @@ -22,8 +23,8 @@ Singleton::Singleton() {
std::cout << "Singleton created." << std::endl;
}

void Singleton::func() {
std::cout << "Doing something..." << std::endl;
void Singleton::info() {
std::cout << "Current instance address: " << this << " | Current thread ID: " << std::this_thread::get_id() << '\n';
}

Singleton::~Singleton() {
Expand Down
Loading