├── LICENSE ├── Lesson1 ├── Activity01 │ ├── CMakeLists.txt │ └── src │ │ ├── ANewClass.cpp │ │ ├── ANewClass.h │ │ ├── CxxTemplate.cpp │ │ ├── CxxTemplate.h │ │ ├── SumFunc.cpp │ │ └── SumFunc.h ├── Activity02 │ ├── CMakeLists.txt │ ├── src │ │ ├── ANewClass.cpp │ │ ├── ANewClass.h │ │ ├── CxxTemplate.cpp │ │ ├── CxxTemplate.h │ │ ├── LinearMotion1D.cpp │ │ ├── LinearMotion1D.h │ │ ├── SumFunc.cpp │ │ └── SumFunc.h │ └── tests │ │ ├── CMakeLists.txt │ │ ├── CanTest.cpp │ │ ├── LinearMotion1DTest.cpp │ │ └── SumFuncTest.cpp ├── Exercise01 │ └── project │ │ └── CxxTemplate │ │ ├── .cproject │ │ ├── .gitignore │ │ ├── .project │ │ ├── .settings │ │ ├── language.settings.xml │ │ └── org.eclipse.cdt.managedbuilder.core.prefs │ │ ├── CMakeLists.txt │ │ ├── LICENSE │ │ ├── README.md │ │ ├── cmake │ │ └── modules │ │ │ ├── DumpProps.cmake │ │ │ └── DumpVariables.cmake │ │ ├── include │ │ └── CxxTemplatePublic.h │ │ └── src │ │ ├── CxxTemplate.cpp │ │ └── CxxTemplate.h ├── Exercise02 │ ├── .cproject │ ├── .gitignore │ ├── .project │ ├── .settings │ │ ├── language.settings.xml │ │ └── org.eclipse.cdt.managedbuilder.core.prefs │ ├── CMakeLists.txt │ ├── LICENSE │ ├── README.md │ ├── cmake │ │ └── modules │ │ │ ├── DumpProps.cmake │ │ │ └── DumpVariables.cmake │ ├── include │ │ └── CxxTemplatePublic.h │ └── src │ │ ├── CxxTemplate.cpp │ │ └── CxxTemplate.h ├── Exercise03 │ ├── CMakeLists.txt │ └── src │ │ ├── ANewClass.cpp │ │ ├── ANewClass.h │ │ ├── CxxTemplate.cpp │ │ └── CxxTemplate.h ├── Exercise04 │ ├── CMakeLists.txt │ ├── src │ │ ├── ANewClass.cpp │ │ ├── ANewClass.h │ │ ├── CxxTemplate.cpp │ │ ├── CxxTemplate.h │ │ ├── SumFunc.cpp │ │ └── SumFunc.h │ └── tests │ │ ├── CMakeLists.txt │ │ └── CanTest.cpp ├── Exercise06 │ ├── CMakeLists.txt │ ├── src │ │ ├── ANewClass.cpp │ │ ├── ANewClass.h │ │ ├── CxxTemplate.cpp │ │ ├── CxxTemplate.h │ │ ├── SumFunc.cpp │ │ └── SumFunc.h │ └── tests │ │ ├── CMakeLists.txt │ │ ├── CanTest.cpp │ │ └── SumFuncTest.cpp ├── Exercise08 │ └── sample.cpp ├── Exercise09 │ ├── CMakeLists.txt │ ├── src │ │ ├── ANewClass.cpp │ │ ├── ANewClass.h │ │ ├── CxxTemplate.cpp │ │ ├── CxxTemplate.h │ │ ├── LinearMotion1D.cpp │ │ ├── LinearMotion1D.h │ │ ├── SumFunc.cpp │ │ └── SumFunc.h │ └── tests │ │ ├── CMakeLists.txt │ │ ├── CanTest.cpp │ │ ├── LinearMotion1DTest.cpp │ │ └── SumFuncTest.cpp ├── Exercise10 │ ├── CMakeLists.txt │ ├── src │ │ ├── ANewClass.cpp │ │ ├── ANewClass.h │ │ ├── ArrayPlay.cpp │ │ ├── CxxTemplate.cpp │ │ ├── CxxTemplate.h │ │ ├── LinearMotion1D.cpp │ │ ├── LinearMotion1D.h │ │ ├── SumFunc.cpp │ │ └── SumFunc.h │ └── tests │ │ ├── CMakeLists.txt │ │ ├── CanTest.cpp │ │ ├── LinearMotion1DTest.cpp │ │ └── SumFuncTest.cpp └── readme.md ├── Lesson2A ├── Activity01 │ ├── CMakeLists.txt │ ├── answers │ │ ├── CMakeLists.txt │ │ ├── main.cpp │ │ ├── matrix3d.cpp │ │ ├── matrix3d.hpp │ │ ├── point3d.cpp │ │ ├── point3d.hpp │ │ └── tests │ │ │ ├── CMakeLists.txt │ │ │ ├── matrix3dTests.cpp │ │ │ └── point3dTests.cpp │ ├── main.cpp │ ├── point3d.cpp │ ├── point3d.hpp │ └── tests │ │ ├── CMakeLists.txt │ │ └── point3dTests.cpp ├── CMakeLists.txt ├── Exercise01 │ └── Exercise1.cpp ├── Exercise02 │ └── Exercise2.cpp ├── Exercise03 │ └── Exercise3.cpp ├── Exercise04 │ └── Exercise4.cpp ├── Exercise05 │ └── Exercise5.cpp ├── Exercise06 │ └── Exercise6.cpp ├── Exercise07 │ ├── Exercise7.cpp │ ├── Fraction.cpp │ ├── Fraction.hpp │ ├── answers │ │ ├── Fraction.cpp │ │ └── Fraction.hpp │ └── tests │ │ ├── CMakeLists.txt │ │ └── FractionTests.cpp └── readme.md ├── Lesson2B ├── Activity01 │ ├── CMakeLists.txt │ ├── answers │ │ ├── CMakeLists.txt │ │ ├── contains.hpp │ │ ├── main.cpp │ │ └── tests │ │ │ ├── CMakeLists.txt │ │ │ └── containsTests.cpp │ ├── contains.hpp │ ├── main.cpp │ └── tests │ │ ├── CMakeLists.txt │ │ └── containsTests.cpp ├── CMakeLists.txt ├── Exercise01 │ └── Exercise1.cpp ├── Exercise02 │ └── Exercise2.cpp ├── Exercise03 │ └── Exercise3.cpp └── Exercise04 │ ├── Exercise4.cpp │ ├── Stack.hpp │ └── tests │ ├── CMakeLists.txt │ └── StackTests.cpp ├── Lesson3 ├── Activity01 │ ├── CMakeLists.txt │ ├── answers │ │ ├── main.cpp │ │ ├── matrix3d.cpp │ │ ├── matrix3d.hpp │ │ ├── point3d.cpp │ │ ├── point3d.hpp │ │ └── tests │ │ │ ├── CMakeLists.txt │ │ │ ├── matrix3dTests.cpp │ │ │ └── point3dTests.cpp │ ├── main.cpp │ ├── matrix3d.cpp │ ├── matrix3d.hpp │ ├── point3d.cpp │ ├── point3d.hpp │ └── tests │ │ ├── CMakeLists.txt │ │ ├── matrix3dTests.cpp │ │ └── point3dTests.cpp ├── Activity02 │ ├── CMakeLists.txt │ ├── answers │ │ ├── CMakeLists.txt │ │ ├── date.cpp │ │ ├── date.hpp │ │ ├── main.cpp │ │ └── tests │ │ │ ├── CMakeLists.txt │ │ │ └── dateTests.cpp │ ├── date.cpp │ ├── date.hpp │ ├── main.cpp │ └── tests │ │ ├── CMakeLists.txt │ │ └── dateTests.cpp ├── CMakeLists.txt ├── Exercise01 │ ├── Fraction.cpp │ ├── Fraction.hpp │ ├── Stack.hpp │ ├── answers │ │ ├── Fraction.cpp │ │ └── Stack.hpp │ └── tests │ │ ├── CMakeLists.txt │ │ ├── FractionTests.cpp │ │ └── StackTests.cpp ├── Exercise02 │ └── exceptions.cpp ├── Exercise03 │ ├── Exercise3.cpp │ ├── answers │ │ └── Exercise3.cpp │ ├── monitor.cpp │ └── monitor.h ├── Exercise04 │ ├── Exercise4.cpp │ └── sensors.hpp ├── Exercise05 │ └── Exercise5.cpp ├── Exercise06 │ └── Exercise6.cpp └── readme.md ├── Lesson4 ├── Activity01 │ ├── CMakeLists.txt │ ├── answers │ │ ├── CMakeLists.txt │ │ ├── delegate.hpp │ │ ├── main.cpp │ │ └── tests │ │ │ ├── CMakeLists.txt │ │ │ └── delegateTests.cpp │ ├── delegate.hpp │ ├── main.cpp │ └── tests │ │ ├── CMakeLists.txt │ │ └── delegateTests.cpp ├── CMakeLists.txt ├── Exercise01 │ ├── Exercise1.cpp │ ├── dessert.hpp │ ├── kitchen.cpp │ ├── kitchen.hpp │ └── recipe.hpp ├── Exercise02 │ └── Exercise2.cpp ├── Exercise03 │ └── Exercise3.cpp ├── Exercise04 │ └── Exercise4.cpp └── readme.md ├── Lesson5 ├── Activity01 │ ├── Common.hpp │ ├── Person.cpp │ ├── Person.hpp │ ├── PersonGenerator.cpp │ ├── PersonGenerator.hpp │ ├── Persons.cpp │ ├── Persons.hpp │ ├── Simulator.cpp │ ├── Watchman.cpp │ └── Watchman.hpp ├── Exercise01 │ └── Exercise1.cpp ├── Exercise02 │ └── Exercise2.cpp ├── Exercise03 │ └── Exercise3.cpp ├── Exercise04 │ └── Exercise4.cpp ├── Exercise05 │ └── Exercise5.cpp └── readme.md ├── Lesson6 ├── Activity01 │ ├── Common.hpp │ ├── Person.cpp │ ├── Person.hpp │ ├── PersonGenerator.cpp │ ├── PersonGenerator.hpp │ ├── Persons.cpp │ ├── Persons.hpp │ ├── Simulator.cpp │ ├── Watchman.cpp │ ├── Watchman.hpp │ └── logger │ │ ├── Logger.hpp │ │ ├── LoggerMacroses.hpp │ │ ├── LoggerUtils.cpp │ │ ├── LoggerUtils.hpp │ │ ├── StreamLogger.cpp │ │ └── StreamLogger.hpp ├── Exercise01 │ └── Exercise1.cpp ├── Exercise02 │ └── Exercise2.cpp ├── Exercise03 │ └── Exercise3.cpp ├── Exercise04 │ └── Exercise4.cpp ├── Exercise05 │ └── Exercise5.cpp ├── Exercise06 │ └── Exercise6.cpp ├── Exercise07 │ └── Exercise7.cpp ├── Exercise08 │ └── Exercise8.cpp ├── Exercise09 │ └── Exercise9.cpp └── readme.md ├── Lesson7 ├── Activity01 │ ├── CommonHeader.h │ ├── CurrencyConversion.txt │ ├── CurrencyExtraColumn.txt │ ├── CurrencyWrongDelimiter.txt │ ├── IdentifyTestCases.txt │ ├── ParseFileTestCases.cpp │ ├── ParseFiles.cpp │ ├── RecordExtraColumn.txt │ ├── RecordFile.txt │ ├── RecordWrongDelimiter.txt │ ├── Test.make │ ├── Util.cpp │ ├── build.sh │ ├── correctHeaderFile │ ├── emptyFile │ ├── noHeaderFile │ ├── parse.conf │ ├── parseMissingCurrency.conf │ └── parseMissingRecord.conf ├── Exercise01 │ └── AssertSample.cpp ├── Exercise02 │ ├── PersonLibrary.h │ ├── PersonLibrary_ver1.h │ ├── PersonLibrary_ver2.h │ └── StaticAssertionSample.cpp ├── Exercise03 │ └── ExceptionSample.cpp ├── Exercise04 │ ├── .ExceptionSample.cpp.swp │ ├── CheckIp.cpp │ ├── CheckIp.h │ ├── MainIp.cpp │ ├── Test.make │ ├── TestCases.cpp │ ├── TestMain.cpp │ └── TestRun ├── Exercise05 │ ├── Misc.cpp │ ├── Misc.h │ ├── MockMisc.h │ ├── RunApp.cpp │ ├── TestApp.cpp │ └── TestApp.make └── readme.md ├── Lesson8 ├── Activity01 │ ├── Speller.cpp │ ├── Speller1.cpp │ ├── Speller2.cpp │ ├── Speller3.cpp │ ├── Timer.cpp │ ├── Timer.h │ └── activity1.7z ├── Exercise01 │ └── Snippet1.cpp ├── Exercise02 │ └── Snippet2.cpp ├── Exercise03 │ └── Snippet2.cpp ├── Exercise04 │ └── Snippet3.cpp ├── Exercise05 │ └── Snippet3.cpp ├── Exercise06 │ └── Snippet4.cpp ├── Exercise07 │ ├── Snippet5.cpp │ ├── Timer.cpp │ └── Timer.h ├── Exercise08 │ ├── Snippet6.cpp │ ├── Timer.cpp │ └── Timer.h ├── Exercise09 │ ├── Snippet7.cpp │ ├── Timer.cpp │ └── Timer.h ├── Exercise10 │ ├── Snippet8.cpp │ ├── Timer.cpp │ └── Timer.h ├── Exercise11 │ ├── Snippet10.cpp │ ├── Snippet9.cpp │ ├── Timer.cpp │ └── Timer.h ├── Exercise12 │ ├── Snippet11.cpp │ ├── Timer.cpp │ └── Timer.h ├── Exercise13 │ ├── Snippet12.cpp │ ├── Timer.cpp │ └── Timer.h ├── Exercise14 │ ├── Snippet13.cpp │ ├── Snippet14.cpp │ ├── Snippet15.cpp │ ├── Snippet16.cpp │ ├── Snippet17.cpp │ ├── Snippet18.cpp │ ├── Snippet19.cpp │ ├── Snippet20.cpp │ ├── Snippet21.cpp │ ├── Snippet22.cpp │ ├── SnippetWC.cpp │ ├── SnippetWC1.cpp │ ├── SnippetWC2.cpp │ ├── SnippetWC3.cpp │ ├── SnippetWC4.cpp │ ├── SnippetWC5.cpp │ ├── data.7z │ └── data.txt └── readme.md └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Training By Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Lesson1/Activity01/CMakeLists.txt: -------------------------------------------------------------------------------- 1 |  2 | cmake_minimum_required(VERSION 3.10.0) 3 | 4 | project(CxxTemplate 5 | VERSION 0.1.0.0 6 | LANGUAGES C CXX 7 | DESCRIPTION "A project containing the essentials for other projects" 8 | ) 9 | set(CxxTemplate_VENDOR "Aron Rubin") 10 | set(PROJECT_VENDOR "${CxxTemplate_VENDOR}") 11 | 12 | 13 | set(CMAKE_DEBUG_POSTFIX d) 14 | 15 | set(CMAKE_CXX_STANDARD 17) 16 | set(CMAKE_C_STANDARD 11) 17 | 18 | list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules") 19 | 20 | 21 | if(POLICY CMP0074) 22 | cmake_policy(SET CMP0074 NEW) 23 | endif() 24 | 25 | find_package(GTest) 26 | if(GTEST_FOUND) 27 | set(Gtest_FOUND TRUE) 28 | endif() 29 | if(GTest_FOUND) 30 | include(GoogleTest) 31 | endif() 32 | 33 | include(CheckIncludeFiles) 34 | CHECK_INCLUDE_FILES(sys/prctl.h HAVE_SYS_PRCTL_H) 35 | 36 | find_package(Threads REQUIRED) 37 | 38 | add_executable(CxxTemplate 39 | src/CxxTemplate.cpp 40 | src/ANewClass.cpp 41 | src/SumFunc.cpp 42 | ) 43 | 44 | target_link_libraries(CxxTemplate 45 | PUBLIC Threads::Threads 46 | ) 47 | 48 | include(FeatureSummary) 49 | feature_summary(WHAT ALL) 50 | 51 | # include(DumpProps) 52 | # print_target_properties(diva) 53 | # include(DumpVariables) 54 | # dump_all_variables() 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /Lesson1/Activity01/src/ANewClass.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ANewClass.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #include "ANewClass.h" 9 | #include 10 | 11 | void ANewClass::run() { 12 | std::cout << "Hello from ANewClass." << std::endl; 13 | } 14 | 15 | ANewClass::ANewClass() { 16 | // TODO Auto-generated constructor stub 17 | 18 | } 19 | 20 | ANewClass::~ANewClass() { 21 | // TODO Auto-generated destructor stub 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Lesson1/Activity01/src/ANewClass.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ANewClass.h 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #ifndef SRC_ANEWCLASS_H_ 9 | #define SRC_ANEWCLASS_H_ 10 | 11 | class ANewClass { 12 | public: 13 | void run(); 14 | ANewClass(); 15 | virtual ~ANewClass(); 16 | }; 17 | 18 | #endif /* SRC_ANEWCLASS_H_ */ 19 | -------------------------------------------------------------------------------- /Lesson1/Activity01/src/CxxTemplate.cpp: -------------------------------------------------------------------------------- 1 |  2 | #include "CxxTemplate.h" 3 | #include "ANewClass.h" 4 | #include "SumFunc.h" 5 | 6 | #include 7 | #include 8 | #include 9 | using namespace std::chrono_literals; 10 | 11 | namespace cxxt { 12 | 13 | CxxApplication::CxxApplication( int argc, char *argv[] ) { 14 | std::cout << "Hello CMake." << std::endl; 15 | ::ANewClass anew; 16 | anew.run(); 17 | std::cout << sum(3, 4) << std::endl; 18 | } 19 | 20 | CxxApplication::~CxxApplication() { 21 | // TODO cleanup 22 | } 23 | 24 | } // namespace cxxt 25 | 26 | 27 | int main( int argc, char *argv[] ) { 28 | cxxt::CxxApplication app( argc, argv ); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Lesson1/Activity01/src/SumFunc.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * SumFunc.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #include "SumFunc.h" 9 | #include 10 | 11 | int sum(int a, int b) { 12 | return a + b; 13 | } 14 | -------------------------------------------------------------------------------- /Lesson1/Activity01/src/SumFunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SumFunc.h 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #ifndef SRC_SUMFUNC_H_ 9 | #define SRC_SUMFUNC_H_ 10 | 11 | int sum(int a, int b); 12 | 13 | #endif /* SRC_SUMFUNC_H_ */ 14 | -------------------------------------------------------------------------------- /Lesson1/Activity02/CMakeLists.txt: -------------------------------------------------------------------------------- 1 |  2 | cmake_minimum_required(VERSION 3.10.0) 3 | 4 | project(CxxTemplate 5 | VERSION 0.1.0.0 6 | LANGUAGES C CXX 7 | DESCRIPTION "A project containing the essentials for other projects" 8 | ) 9 | set(CxxTemplate_VENDOR "Aron Rubin") 10 | set(PROJECT_VENDOR "${CxxTemplate_VENDOR}") 11 | 12 | 13 | set(CMAKE_DEBUG_POSTFIX d) 14 | 15 | set(CMAKE_CXX_STANDARD 17) 16 | set(CMAKE_C_STANDARD 11) 17 | 18 | list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules") 19 | 20 | 21 | if(POLICY CMP0074) 22 | cmake_policy(SET CMP0074 NEW) 23 | endif() 24 | 25 | find_package(GTest) 26 | if(GTEST_FOUND) 27 | set(Gtest_FOUND TRUE) 28 | endif() 29 | if(GTest_FOUND) 30 | include(GoogleTest) 31 | endif() 32 | enable_testing() 33 | add_subdirectory(tests) 34 | 35 | include(CheckIncludeFiles) 36 | CHECK_INCLUDE_FILES(sys/prctl.h HAVE_SYS_PRCTL_H) 37 | 38 | find_package(Threads REQUIRED) 39 | 40 | add_executable(CxxTemplate 41 | src/CxxTemplate.cpp 42 | src/ANewClass.cpp 43 | src/SumFunc.cpp 44 | src/LinearMotion1D.cpp 45 | ) 46 | 47 | target_link_libraries(CxxTemplate 48 | PUBLIC Threads::Threads 49 | ) 50 | 51 | include(FeatureSummary) 52 | feature_summary(WHAT ALL) 53 | 54 | # include(DumpProps) 55 | # print_target_properties(diva) 56 | # include(DumpVariables) 57 | # dump_all_variables() 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /Lesson1/Activity02/src/ANewClass.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ANewClass.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #include "ANewClass.h" 9 | #include 10 | 11 | void ANewClass::run() { 12 | std::cout << "Hello from ANewClass." << std::endl; 13 | } 14 | 15 | ANewClass::ANewClass() { 16 | // TODO Auto-generated constructor stub 17 | 18 | } 19 | 20 | ANewClass::~ANewClass() { 21 | // TODO Auto-generated destructor stub 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Lesson1/Activity02/src/ANewClass.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ANewClass.h 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #ifndef SRC_ANEWCLASS_H_ 9 | #define SRC_ANEWCLASS_H_ 10 | 11 | class ANewClass { 12 | public: 13 | void run(); 14 | ANewClass(); 15 | virtual ~ANewClass(); 16 | }; 17 | 18 | #endif /* SRC_ANEWCLASS_H_ */ 19 | -------------------------------------------------------------------------------- /Lesson1/Activity02/src/CxxTemplate.cpp: -------------------------------------------------------------------------------- 1 |  2 | #include "CxxTemplate.h" 3 | #include "ANewClass.h" 4 | #include "SumFunc.h" 5 | 6 | #include 7 | #include 8 | #include 9 | using namespace std::chrono_literals; 10 | 11 | namespace cxxt { 12 | 13 | CxxApplication::CxxApplication( int argc, char *argv[] ) { 14 | std::cout << "Hello CMake." << std::endl; 15 | ::ANewClass anew; 16 | anew.run(); 17 | std::cout << sum(3, 4) << std::endl; 18 | } 19 | 20 | CxxApplication::~CxxApplication() { 21 | // TODO cleanup 22 | } 23 | 24 | } // namespace cxxt 25 | 26 | 27 | int main( int argc, char *argv[] ) { 28 | cxxt::CxxApplication app( argc, argv ); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Lesson1/Activity02/src/LinearMotion1D.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * LinearMotion1D.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #include "LinearMotion1D.h" 9 | 10 | void LinearMotion1D::advanceTimeBy(double dt) { 11 | position += velocity * dt; 12 | } 13 | 14 | LinearMotion1D::LinearMotion1D() { 15 | position = 0; 16 | velocity = 0; 17 | } 18 | 19 | LinearMotion1D::~LinearMotion1D() { 20 | // TODO Auto-generated destructor stub 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Lesson1/Activity02/src/LinearMotion1D.h: -------------------------------------------------------------------------------- 1 | /* 2 | * LinearMotion1D.h 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #ifndef SRC_LINEARMOTION1D_H_ 9 | #define SRC_LINEARMOTION1D_H_ 10 | 11 | class LinearMotion1D { 12 | public: 13 | double position; 14 | double velocity; 15 | void advanceTimeBy(double dt); 16 | LinearMotion1D(); 17 | virtual ~LinearMotion1D(); 18 | }; 19 | 20 | #endif /* SRC_LINEARMOTION1D_H_ */ 21 | -------------------------------------------------------------------------------- /Lesson1/Activity02/src/SumFunc.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * SumFunc.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #include "SumFunc.h" 9 | #include 10 | 11 | int sum(int a, int b) { 12 | return a + b; 13 | } 14 | -------------------------------------------------------------------------------- /Lesson1/Activity02/src/SumFunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SumFunc.h 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #ifndef SRC_SUMFUNC_H_ 9 | #define SRC_SUMFUNC_H_ 10 | 11 | int sum(int a, int b); 12 | 13 | #endif /* SRC_SUMFUNC_H_ */ 14 | -------------------------------------------------------------------------------- /Lesson1/Activity02/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include(GoogleTest) 2 | add_executable(tests 3 | CanTest.cpp 4 | SumFuncTest.cpp 5 | ../src/SumFunc.cpp 6 | LinearMotion1DTest.cpp 7 | ../src/LinearMotion1D.cpp 8 | ) 9 | target_link_libraries(tests GTest::GTest) 10 | gtest_discover_tests(tests) 11 | -------------------------------------------------------------------------------- /Lesson1/Activity02/tests/CanTest.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * CanTest.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | 9 | #include "gtest/gtest.h" 10 | 11 | namespace { 12 | 13 | class CanTest: public ::testing::Test {}; 14 | 15 | TEST_F(CanTest, CanReallyTest) { 16 | EXPECT_EQ(0, 0); 17 | } 18 | 19 | } 20 | 21 | int main(int argc, char **argv) { 22 | ::testing::InitGoogleTest(&argc, argv); 23 | return RUN_ALL_TESTS(); 24 | } 25 | 26 | 27 | -------------------------------------------------------------------------------- /Lesson1/Activity02/tests/LinearMotion1DTest.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * LinearMotion1DTest.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #include "gtest/gtest.h" 9 | #include "../src/LinearMotion1D.h" 10 | 11 | namespace { 12 | 13 | class LinearMotion1DTest: public ::testing::Test { 14 | }; 15 | 16 | TEST_F(LinearMotion1DTest, CanMoveRight) { 17 | LinearMotion1D l; 18 | l.position = 10; 19 | l.velocity = 2; 20 | l.advanceTimeBy(3); 21 | EXPECT_DOUBLE_EQ(16, l.position); 22 | } 23 | 24 | TEST_F(LinearMotion1DTest, CanMoveLeft) { 25 | LinearMotion1D l; 26 | l.position = 10; 27 | l.velocity = -2; 28 | l.advanceTimeBy(3); 29 | EXPECT_DOUBLE_EQ(4, l.position); 30 | } 31 | 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Lesson1/Activity02/tests/SumFuncTest.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * SumFuncTest.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | #include "gtest/gtest.h" 8 | #include "../src/SumFunc.h" 9 | 10 | namespace { 11 | 12 | class SumFuncTest: public ::testing::Test {}; 13 | 14 | TEST_F(SumFuncTest, CanSumCorrectly) { 15 | EXPECT_EQ(7, sum(3, 4)); 16 | } 17 | 18 | TEST_F(SumFuncTest, CanUseNegativeValues) { 19 | EXPECT_EQ(0, sum(3, -3)); 20 | } 21 | 22 | } 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Lesson1/Exercise01/project/CxxTemplate/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | /build/ 34 | -------------------------------------------------------------------------------- /Lesson1/Exercise01/project/CxxTemplate/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | CxxTemplate 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder 10 | clean,full,incremental, 11 | 12 | 13 | 14 | 15 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder 16 | full,incremental, 17 | 18 | 19 | 20 | 21 | 22 | org.eclipse.cdt.core.cnature 23 | org.eclipse.cdt.core.ccnature 24 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 25 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 26 | 27 | 28 | -------------------------------------------------------------------------------- /Lesson1/Exercise01/project/CxxTemplate/.settings/org.eclipse.cdt.managedbuilder.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.cross.exe.debug.695101011/CPATH/delimiter=\: 3 | environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.cross.exe.debug.695101011/CPATH/operation=remove 4 | environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.cross.exe.debug.695101011/CPLUS_INCLUDE_PATH/delimiter=\: 5 | environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.cross.exe.debug.695101011/CPLUS_INCLUDE_PATH/operation=remove 6 | environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.cross.exe.debug.695101011/C_INCLUDE_PATH/delimiter=\: 7 | environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.cross.exe.debug.695101011/C_INCLUDE_PATH/operation=remove 8 | environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.cross.exe.debug.695101011/append=true 9 | environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.cross.exe.debug.695101011/appendContributed=true 10 | environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.cross.exe.debug.695101011/LIBRARY_PATH/delimiter=\: 11 | environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.cross.exe.debug.695101011/LIBRARY_PATH/operation=remove 12 | environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.cross.exe.debug.695101011/append=true 13 | environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.cross.exe.debug.695101011/appendContributed=true 14 | -------------------------------------------------------------------------------- /Lesson1/Exercise01/project/CxxTemplate/CMakeLists.txt: -------------------------------------------------------------------------------- 1 |  2 | cmake_minimum_required(VERSION 3.10.0) 3 | 4 | project(CxxTemplate 5 | VERSION 0.1.0.0 6 | LANGUAGES C CXX 7 | DESCRIPTION "A project containing the essentials for other projects" 8 | ) 9 | set(CxxTemplate_VENDOR "Aron Rubin") 10 | set(PROJECT_VENDOR "${CxxTemplate_VENDOR}") 11 | 12 | 13 | set(CMAKE_DEBUG_POSTFIX d) 14 | 15 | set(CMAKE_CXX_STANDARD 17) 16 | set(CMAKE_C_STANDARD 11) 17 | 18 | list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules") 19 | 20 | 21 | if(POLICY CMP0074) 22 | cmake_policy(SET CMP0074 NEW) 23 | endif() 24 | 25 | find_package(GTest) 26 | if(GTEST_FOUND) 27 | set(Gtest_FOUND TRUE) 28 | endif() 29 | if(GTest_FOUND) 30 | include(GoogleTest) 31 | endif() 32 | 33 | include(CheckIncludeFiles) 34 | CHECK_INCLUDE_FILES(sys/prctl.h HAVE_SYS_PRCTL_H) 35 | 36 | find_package(Threads REQUIRED) 37 | 38 | add_executable(CxxTemplate 39 | src/CxxTemplate.cpp 40 | ) 41 | 42 | target_link_libraries(CxxTemplate 43 | PUBLIC Threads::Threads 44 | ) 45 | 46 | include(FeatureSummary) 47 | feature_summary(WHAT ALL) 48 | 49 | # include(DumpProps) 50 | # print_target_properties(diva) 51 | # include(DumpVariables) 52 | # dump_all_variables() 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /Lesson1/Exercise01/project/CxxTemplate/README.md: -------------------------------------------------------------------------------- 1 | # CxxTemplate 2 | A project containing the essentials for other projects 3 | -------------------------------------------------------------------------------- /Lesson1/Exercise01/project/CxxTemplate/cmake/modules/DumpProps.cmake: -------------------------------------------------------------------------------- 1 | # Get all propreties that cmake supports 2 | execute_process(COMMAND cmake --help-property-list OUTPUT_VARIABLE CMAKE_PROPERTY_LIST) 3 | 4 | # Convert command output into a CMake list 5 | STRING(REGEX REPLACE ";" "\\\\;" CMAKE_PROPERTY_LIST "${CMAKE_PROPERTY_LIST}") 6 | STRING(REGEX REPLACE "\n" ";" CMAKE_PROPERTY_LIST "${CMAKE_PROPERTY_LIST}") 7 | 8 | function(print_properties) 9 | message ("CMAKE_PROPERTY_LIST = ${CMAKE_PROPERTY_LIST}") 10 | endfunction(print_properties) 11 | 12 | function(print_target_properties tgt) 13 | if(NOT TARGET ${tgt}) 14 | message("There is no target named '${tgt}'") 15 | return() 16 | endif() 17 | 18 | foreach (prop ${CMAKE_PROPERTY_LIST}) 19 | string(REPLACE "" "${CMAKE_BUILD_TYPE}" prop ${prop}) 20 | # Fix https://stackoverflow.com/questions/32197663/how-can-i-remove-the-the-location-property-may-not-be-read-from-target-error-i 21 | if(prop STREQUAL "LOCATION" OR prop MATCHES "^LOCATION_" OR prop MATCHES "_LOCATION$") 22 | continue() 23 | endif() 24 | # message ("Checking ${prop}") 25 | get_property(propval TARGET ${tgt} PROPERTY ${prop} SET) 26 | if (propval) 27 | get_target_property(propval ${tgt} ${prop}) 28 | message ("${tgt} ${prop} = ${propval}") 29 | endif() 30 | endforeach(prop) 31 | endfunction(print_target_properties) 32 | -------------------------------------------------------------------------------- /Lesson1/Exercise01/project/CxxTemplate/cmake/modules/DumpVariables.cmake: -------------------------------------------------------------------------------- 1 | 2 | macro(dump_all_variables) 3 | get_cmake_property(_variableNames VARIABLES) 4 | list (SORT _variableNames) 5 | foreach (_variableName ${_variableNames}) 6 | message(STATUS "${_variableName}=${${_variableName}}") 7 | endforeach() 8 | endmacro(dump_all_variables) -------------------------------------------------------------------------------- /Lesson1/Exercise01/project/CxxTemplate/include/CxxTemplatePublic.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TrainingByPackt/Advanced-CPlusPlus/b2ba3fc955b1dc0c617b0b2543fc82ad36573ff2/Lesson1/Exercise01/project/CxxTemplate/include/CxxTemplatePublic.h -------------------------------------------------------------------------------- /Lesson1/Exercise01/project/CxxTemplate/src/CxxTemplate.cpp: -------------------------------------------------------------------------------- 1 |  2 | #include "CxxTemplate.h" 3 | 4 | #include 5 | #include 6 | #include 7 | using namespace std::chrono_literals; 8 | 9 | namespace cxxt { 10 | 11 | CxxApplication::CxxApplication( int argc, char *argv[] ) { 12 | std::cout << "Hello CMake." << std::endl; 13 | } 14 | 15 | CxxApplication::~CxxApplication() { 16 | // TODO cleanup 17 | } 18 | 19 | } // namespace cxxt 20 | 21 | 22 | int main( int argc, char *argv[] ) { 23 | cxxt::CxxApplication app( argc, argv ); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Lesson1/Exercise02/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | /build/ 34 | -------------------------------------------------------------------------------- /Lesson1/Exercise02/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | CxxTemplate 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder 10 | clean,full,incremental, 11 | 12 | 13 | 14 | 15 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder 16 | full,incremental, 17 | 18 | 19 | 20 | 21 | 22 | org.eclipse.cdt.core.cnature 23 | org.eclipse.cdt.core.ccnature 24 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 25 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 26 | 27 | 28 | -------------------------------------------------------------------------------- /Lesson1/Exercise02/.settings/org.eclipse.cdt.managedbuilder.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.cross.exe.debug.695101011/CPATH/delimiter=\: 3 | environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.cross.exe.debug.695101011/CPATH/operation=remove 4 | environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.cross.exe.debug.695101011/CPLUS_INCLUDE_PATH/delimiter=\: 5 | environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.cross.exe.debug.695101011/CPLUS_INCLUDE_PATH/operation=remove 6 | environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.cross.exe.debug.695101011/C_INCLUDE_PATH/delimiter=\: 7 | environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.cross.exe.debug.695101011/C_INCLUDE_PATH/operation=remove 8 | environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.cross.exe.debug.695101011/append=true 9 | environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.cross.exe.debug.695101011/appendContributed=true 10 | environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.cross.exe.debug.695101011/LIBRARY_PATH/delimiter=\: 11 | environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.cross.exe.debug.695101011/LIBRARY_PATH/operation=remove 12 | environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.cross.exe.debug.695101011/append=true 13 | environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.cross.exe.debug.695101011/appendContributed=true 14 | -------------------------------------------------------------------------------- /Lesson1/Exercise02/CMakeLists.txt: -------------------------------------------------------------------------------- 1 |  2 | cmake_minimum_required(VERSION 3.10.0) 3 | 4 | project(CxxTemplate 5 | VERSION 0.1.0.0 6 | LANGUAGES C CXX 7 | DESCRIPTION "A project containing the essentials for other projects" 8 | ) 9 | set(CxxTemplate_VENDOR "Aron Rubin") 10 | set(PROJECT_VENDOR "${CxxTemplate_VENDOR}") 11 | 12 | 13 | set(CMAKE_DEBUG_POSTFIX d) 14 | 15 | set(CMAKE_CXX_STANDARD 17) 16 | set(CMAKE_C_STANDARD 11) 17 | 18 | list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules") 19 | 20 | 21 | if(POLICY CMP0074) 22 | cmake_policy(SET CMP0074 NEW) 23 | endif() 24 | 25 | find_package(GTest) 26 | if(GTEST_FOUND) 27 | set(Gtest_FOUND TRUE) 28 | endif() 29 | if(GTest_FOUND) 30 | include(GoogleTest) 31 | endif() 32 | 33 | include(CheckIncludeFiles) 34 | CHECK_INCLUDE_FILES(sys/prctl.h HAVE_SYS_PRCTL_H) 35 | 36 | find_package(Threads REQUIRED) 37 | 38 | add_executable(CxxTemplate 39 | src/CxxTemplate.cpp 40 | ) 41 | 42 | target_link_libraries(CxxTemplate 43 | PUBLIC Threads::Threads 44 | ) 45 | 46 | include(FeatureSummary) 47 | feature_summary(WHAT ALL) 48 | 49 | # include(DumpProps) 50 | # print_target_properties(diva) 51 | # include(DumpVariables) 52 | # dump_all_variables() 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /Lesson1/Exercise02/README.md: -------------------------------------------------------------------------------- 1 | # CxxTemplate 2 | A project containing the essentials for other projects 3 | -------------------------------------------------------------------------------- /Lesson1/Exercise02/cmake/modules/DumpProps.cmake: -------------------------------------------------------------------------------- 1 | # Get all propreties that cmake supports 2 | execute_process(COMMAND cmake --help-property-list OUTPUT_VARIABLE CMAKE_PROPERTY_LIST) 3 | 4 | # Convert command output into a CMake list 5 | STRING(REGEX REPLACE ";" "\\\\;" CMAKE_PROPERTY_LIST "${CMAKE_PROPERTY_LIST}") 6 | STRING(REGEX REPLACE "\n" ";" CMAKE_PROPERTY_LIST "${CMAKE_PROPERTY_LIST}") 7 | 8 | function(print_properties) 9 | message ("CMAKE_PROPERTY_LIST = ${CMAKE_PROPERTY_LIST}") 10 | endfunction(print_properties) 11 | 12 | function(print_target_properties tgt) 13 | if(NOT TARGET ${tgt}) 14 | message("There is no target named '${tgt}'") 15 | return() 16 | endif() 17 | 18 | foreach (prop ${CMAKE_PROPERTY_LIST}) 19 | string(REPLACE "" "${CMAKE_BUILD_TYPE}" prop ${prop}) 20 | # Fix https://stackoverflow.com/questions/32197663/how-can-i-remove-the-the-location-property-may-not-be-read-from-target-error-i 21 | if(prop STREQUAL "LOCATION" OR prop MATCHES "^LOCATION_" OR prop MATCHES "_LOCATION$") 22 | continue() 23 | endif() 24 | # message ("Checking ${prop}") 25 | get_property(propval TARGET ${tgt} PROPERTY ${prop} SET) 26 | if (propval) 27 | get_target_property(propval ${tgt} ${prop}) 28 | message ("${tgt} ${prop} = ${propval}") 29 | endif() 30 | endforeach(prop) 31 | endfunction(print_target_properties) 32 | -------------------------------------------------------------------------------- /Lesson1/Exercise02/cmake/modules/DumpVariables.cmake: -------------------------------------------------------------------------------- 1 | 2 | macro(dump_all_variables) 3 | get_cmake_property(_variableNames VARIABLES) 4 | list (SORT _variableNames) 5 | foreach (_variableName ${_variableNames}) 6 | message(STATUS "${_variableName}=${${_variableName}}") 7 | endforeach() 8 | endmacro(dump_all_variables) -------------------------------------------------------------------------------- /Lesson1/Exercise02/include/CxxTemplatePublic.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TrainingByPackt/Advanced-CPlusPlus/b2ba3fc955b1dc0c617b0b2543fc82ad36573ff2/Lesson1/Exercise02/include/CxxTemplatePublic.h -------------------------------------------------------------------------------- /Lesson1/Exercise02/src/CxxTemplate.cpp: -------------------------------------------------------------------------------- 1 |  2 | #include "CxxTemplate.h" 3 | 4 | #include 5 | #include 6 | #include 7 | using namespace std::chrono_literals; 8 | 9 | namespace cxxt { 10 | 11 | CxxApplication::CxxApplication( int argc, char *argv[] ) { 12 | std::cout << "Hello CMake." << std::endl; 13 | } 14 | 15 | CxxApplication::~CxxApplication() { 16 | // TODO cleanup 17 | } 18 | 19 | } // namespace cxxt 20 | 21 | 22 | int main( int argc, char *argv[] ) { 23 | cxxt::CxxApplication app( argc, argv ); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Lesson1/Exercise03/CMakeLists.txt: -------------------------------------------------------------------------------- 1 |  2 | cmake_minimum_required(VERSION 3.10.0) 3 | 4 | project(CxxTemplate 5 | VERSION 0.1.0.0 6 | LANGUAGES C CXX 7 | DESCRIPTION "A project containing the essentials for other projects" 8 | ) 9 | set(CxxTemplate_VENDOR "Aron Rubin") 10 | set(PROJECT_VENDOR "${CxxTemplate_VENDOR}") 11 | 12 | 13 | set(CMAKE_DEBUG_POSTFIX d) 14 | 15 | set(CMAKE_CXX_STANDARD 17) 16 | set(CMAKE_C_STANDARD 11) 17 | 18 | list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules") 19 | 20 | 21 | if(POLICY CMP0074) 22 | cmake_policy(SET CMP0074 NEW) 23 | endif() 24 | 25 | find_package(GTest) 26 | if(GTEST_FOUND) 27 | set(Gtest_FOUND TRUE) 28 | endif() 29 | if(GTest_FOUND) 30 | include(GoogleTest) 31 | endif() 32 | 33 | include(CheckIncludeFiles) 34 | CHECK_INCLUDE_FILES(sys/prctl.h HAVE_SYS_PRCTL_H) 35 | 36 | find_package(Threads REQUIRED) 37 | 38 | add_executable(CxxTemplate 39 | src/CxxTemplate.cpp 40 | src/ANewClass.cpp 41 | ) 42 | 43 | target_link_libraries(CxxTemplate 44 | PUBLIC Threads::Threads 45 | ) 46 | 47 | include(FeatureSummary) 48 | feature_summary(WHAT ALL) 49 | 50 | # include(DumpProps) 51 | # print_target_properties(diva) 52 | # include(DumpVariables) 53 | # dump_all_variables() 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /Lesson1/Exercise03/src/ANewClass.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ANewClass.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #include "ANewClass.h" 9 | #include 10 | 11 | void ANewClass::run() { 12 | std::cout << "Hello from ANewClass." << std::endl; 13 | } 14 | 15 | ANewClass::ANewClass() { 16 | // TODO Auto-generated constructor stub 17 | 18 | } 19 | 20 | ANewClass::~ANewClass() { 21 | // TODO Auto-generated destructor stub 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Lesson1/Exercise03/src/ANewClass.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ANewClass.h 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #ifndef SRC_ANEWCLASS_H_ 9 | #define SRC_ANEWCLASS_H_ 10 | 11 | class ANewClass { 12 | public: 13 | void run(); 14 | ANewClass(); 15 | virtual ~ANewClass(); 16 | }; 17 | 18 | #endif /* SRC_ANEWCLASS_H_ */ 19 | -------------------------------------------------------------------------------- /Lesson1/Exercise03/src/CxxTemplate.cpp: -------------------------------------------------------------------------------- 1 |  2 | #include "CxxTemplate.h" 3 | #include "ANewClass.h" 4 | 5 | #include 6 | #include 7 | #include 8 | using namespace std::chrono_literals; 9 | 10 | namespace cxxt { 11 | 12 | CxxApplication::CxxApplication( int argc, char *argv[] ) { 13 | std::cout << "Hello CMake." << std::endl; 14 | ::ANewClass anew; 15 | anew.run(); 16 | } 17 | 18 | CxxApplication::~CxxApplication() { 19 | // TODO cleanup 20 | } 21 | 22 | } // namespace cxxt 23 | 24 | 25 | int main( int argc, char *argv[] ) { 26 | cxxt::CxxApplication app( argc, argv ); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Lesson1/Exercise04/CMakeLists.txt: -------------------------------------------------------------------------------- 1 |  2 | cmake_minimum_required(VERSION 3.10.0) 3 | 4 | project(CxxTemplate 5 | VERSION 0.1.0.0 6 | LANGUAGES C CXX 7 | DESCRIPTION "A project containing the essentials for other projects" 8 | ) 9 | set(CxxTemplate_VENDOR "Aron Rubin") 10 | set(PROJECT_VENDOR "${CxxTemplate_VENDOR}") 11 | 12 | 13 | set(CMAKE_DEBUG_POSTFIX d) 14 | 15 | set(CMAKE_CXX_STANDARD 17) 16 | set(CMAKE_C_STANDARD 11) 17 | 18 | list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules") 19 | 20 | 21 | if(POLICY CMP0074) 22 | cmake_policy(SET CMP0074 NEW) 23 | endif() 24 | 25 | find_package(GTest) 26 | if(GTEST_FOUND) 27 | set(Gtest_FOUND TRUE) 28 | endif() 29 | if(GTest_FOUND) 30 | include(GoogleTest) 31 | endif() 32 | enable_testing() 33 | add_subdirectory(tests) 34 | 35 | include(CheckIncludeFiles) 36 | CHECK_INCLUDE_FILES(sys/prctl.h HAVE_SYS_PRCTL_H) 37 | 38 | find_package(Threads REQUIRED) 39 | 40 | add_executable(CxxTemplate 41 | src/CxxTemplate.cpp 42 | src/ANewClass.cpp 43 | src/SumFunc.cpp 44 | ) 45 | 46 | target_link_libraries(CxxTemplate 47 | PUBLIC Threads::Threads 48 | ) 49 | 50 | include(FeatureSummary) 51 | feature_summary(WHAT ALL) 52 | 53 | # include(DumpProps) 54 | # print_target_properties(diva) 55 | # include(DumpVariables) 56 | # dump_all_variables() 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /Lesson1/Exercise04/src/ANewClass.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ANewClass.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #include "ANewClass.h" 9 | #include 10 | 11 | void ANewClass::run() { 12 | std::cout << "Hello from ANewClass." << std::endl; 13 | } 14 | 15 | ANewClass::ANewClass() { 16 | // TODO Auto-generated constructor stub 17 | 18 | } 19 | 20 | ANewClass::~ANewClass() { 21 | // TODO Auto-generated destructor stub 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Lesson1/Exercise04/src/ANewClass.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ANewClass.h 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #ifndef SRC_ANEWCLASS_H_ 9 | #define SRC_ANEWCLASS_H_ 10 | 11 | class ANewClass { 12 | public: 13 | void run(); 14 | ANewClass(); 15 | virtual ~ANewClass(); 16 | }; 17 | 18 | #endif /* SRC_ANEWCLASS_H_ */ 19 | -------------------------------------------------------------------------------- /Lesson1/Exercise04/src/CxxTemplate.cpp: -------------------------------------------------------------------------------- 1 |  2 | #include "CxxTemplate.h" 3 | #include "ANewClass.h" 4 | #include "SumFunc.h" 5 | 6 | #include 7 | #include 8 | #include 9 | using namespace std::chrono_literals; 10 | 11 | namespace cxxt { 12 | 13 | CxxApplication::CxxApplication( int argc, char *argv[] ) { 14 | std::cout << "Hello CMake." << std::endl; 15 | ::ANewClass anew; 16 | anew.run(); 17 | std::cout << sum(3, 4) << std::endl; 18 | } 19 | 20 | CxxApplication::~CxxApplication() { 21 | // TODO cleanup 22 | } 23 | 24 | } // namespace cxxt 25 | 26 | 27 | int main( int argc, char *argv[] ) { 28 | cxxt::CxxApplication app( argc, argv ); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Lesson1/Exercise04/src/SumFunc.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * SumFunc.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #include "SumFunc.h" 9 | #include 10 | 11 | int sum(int a, int b) { 12 | return a + b; 13 | } 14 | -------------------------------------------------------------------------------- /Lesson1/Exercise04/src/SumFunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SumFunc.h 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #ifndef SRC_SUMFUNC_H_ 9 | #define SRC_SUMFUNC_H_ 10 | 11 | int sum(int a, int b); 12 | 13 | #endif /* SRC_SUMFUNC_H_ */ 14 | -------------------------------------------------------------------------------- /Lesson1/Exercise04/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include(GoogleTest) 2 | add_executable(tests CanTest.cpp) 3 | target_link_libraries(tests GTest::GTest) 4 | gtest_discover_tests(tests) 5 | -------------------------------------------------------------------------------- /Lesson1/Exercise04/tests/CanTest.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * CanTest.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | 9 | #include "gtest/gtest.h" 10 | 11 | namespace { 12 | 13 | class CanTest: public ::testing::Test {}; 14 | 15 | TEST_F(CanTest, CanReallyTest) { 16 | EXPECT_EQ(0, 0); 17 | } 18 | 19 | } 20 | 21 | int main(int argc, char **argv) { 22 | ::testing::InitGoogleTest(&argc, argv); 23 | return RUN_ALL_TESTS(); 24 | } 25 | 26 | 27 | -------------------------------------------------------------------------------- /Lesson1/Exercise06/CMakeLists.txt: -------------------------------------------------------------------------------- 1 |  2 | cmake_minimum_required(VERSION 3.10.0) 3 | 4 | project(CxxTemplate 5 | VERSION 0.1.0.0 6 | LANGUAGES C CXX 7 | DESCRIPTION "A project containing the essentials for other projects" 8 | ) 9 | set(CxxTemplate_VENDOR "Aron Rubin") 10 | set(PROJECT_VENDOR "${CxxTemplate_VENDOR}") 11 | 12 | 13 | set(CMAKE_DEBUG_POSTFIX d) 14 | 15 | set(CMAKE_CXX_STANDARD 17) 16 | set(CMAKE_C_STANDARD 11) 17 | 18 | list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules") 19 | 20 | 21 | if(POLICY CMP0074) 22 | cmake_policy(SET CMP0074 NEW) 23 | endif() 24 | 25 | find_package(GTest) 26 | if(GTEST_FOUND) 27 | set(Gtest_FOUND TRUE) 28 | endif() 29 | if(GTest_FOUND) 30 | include(GoogleTest) 31 | endif() 32 | enable_testing() 33 | add_subdirectory(tests) 34 | 35 | include(CheckIncludeFiles) 36 | CHECK_INCLUDE_FILES(sys/prctl.h HAVE_SYS_PRCTL_H) 37 | 38 | find_package(Threads REQUIRED) 39 | 40 | add_executable(CxxTemplate 41 | src/CxxTemplate.cpp 42 | src/ANewClass.cpp 43 | src/SumFunc.cpp 44 | ) 45 | 46 | target_link_libraries(CxxTemplate 47 | PUBLIC Threads::Threads 48 | ) 49 | 50 | include(FeatureSummary) 51 | feature_summary(WHAT ALL) 52 | 53 | # include(DumpProps) 54 | # print_target_properties(diva) 55 | # include(DumpVariables) 56 | # dump_all_variables() 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /Lesson1/Exercise06/src/ANewClass.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ANewClass.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #include "ANewClass.h" 9 | #include 10 | 11 | void ANewClass::run() { 12 | std::cout << "Hello from ANewClass." << std::endl; 13 | } 14 | 15 | ANewClass::ANewClass() { 16 | // TODO Auto-generated constructor stub 17 | 18 | } 19 | 20 | ANewClass::~ANewClass() { 21 | // TODO Auto-generated destructor stub 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Lesson1/Exercise06/src/ANewClass.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ANewClass.h 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #ifndef SRC_ANEWCLASS_H_ 9 | #define SRC_ANEWCLASS_H_ 10 | 11 | class ANewClass { 12 | public: 13 | void run(); 14 | ANewClass(); 15 | virtual ~ANewClass(); 16 | }; 17 | 18 | #endif /* SRC_ANEWCLASS_H_ */ 19 | -------------------------------------------------------------------------------- /Lesson1/Exercise06/src/CxxTemplate.cpp: -------------------------------------------------------------------------------- 1 |  2 | #include "CxxTemplate.h" 3 | #include "ANewClass.h" 4 | #include "SumFunc.h" 5 | 6 | #include 7 | #include 8 | #include 9 | using namespace std::chrono_literals; 10 | 11 | namespace cxxt { 12 | 13 | CxxApplication::CxxApplication( int argc, char *argv[] ) { 14 | std::cout << "Hello CMake." << std::endl; 15 | ::ANewClass anew; 16 | anew.run(); 17 | std::cout << sum(3, 4) << std::endl; 18 | } 19 | 20 | CxxApplication::~CxxApplication() { 21 | // TODO cleanup 22 | } 23 | 24 | } // namespace cxxt 25 | 26 | 27 | int main( int argc, char *argv[] ) { 28 | cxxt::CxxApplication app( argc, argv ); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Lesson1/Exercise06/src/SumFunc.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * SumFunc.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #include "SumFunc.h" 9 | #include 10 | 11 | int sum(int a, int b) { 12 | return a + b; 13 | } 14 | -------------------------------------------------------------------------------- /Lesson1/Exercise06/src/SumFunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SumFunc.h 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #ifndef SRC_SUMFUNC_H_ 9 | #define SRC_SUMFUNC_H_ 10 | 11 | int sum(int a, int b); 12 | 13 | #endif /* SRC_SUMFUNC_H_ */ 14 | -------------------------------------------------------------------------------- /Lesson1/Exercise06/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include(GoogleTest) 2 | add_executable(tests CanTest.cpp SumFuncTest.cpp ../src/SumFunc.cpp) 3 | target_link_libraries(tests GTest::GTest) 4 | gtest_discover_tests(tests) 5 | -------------------------------------------------------------------------------- /Lesson1/Exercise06/tests/CanTest.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * CanTest.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | 9 | #include "gtest/gtest.h" 10 | 11 | namespace { 12 | 13 | class CanTest: public ::testing::Test {}; 14 | 15 | TEST_F(CanTest, CanReallyTest) { 16 | EXPECT_EQ(0, 0); 17 | } 18 | 19 | } 20 | 21 | int main(int argc, char **argv) { 22 | ::testing::InitGoogleTest(&argc, argv); 23 | return RUN_ALL_TESTS(); 24 | } 25 | 26 | 27 | -------------------------------------------------------------------------------- /Lesson1/Exercise06/tests/SumFuncTest.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * SumFuncTest.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | #include "gtest/gtest.h" 8 | #include "../src/SumFunc.h" 9 | 10 | namespace { 11 | 12 | class SumFuncTest: public ::testing::Test {}; 13 | 14 | TEST_F(SumFuncTest, CanSumCorrectly) { 15 | EXPECT_EQ(7, sum(3, 4)); 16 | } 17 | 18 | TEST_F(SumFuncTest, CanUseNegativeValues) { 19 | EXPECT_EQ(0, sum(3, -3)); 20 | } 21 | 22 | } 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Lesson1/Exercise08/sample.cpp: -------------------------------------------------------------------------------- 1 | 2 | int sum(int a, int b) { 3 | return a + b; 4 | } 5 | 6 | int callSum() { 7 | return sum(4, 5); 8 | } 9 | 10 | class MyClass { 11 | private: 12 | int a = 5; 13 | int myPrivateFunc(int i) { 14 | a = 4; 15 | return i + a; 16 | } 17 | public: 18 | int b = 6; 19 | int myFunc(){ 20 | return sum(1, myPrivateFunc(b)); 21 | } 22 | }; 23 | 24 | MyClass myObject; 25 | 26 | int main() { 27 | myObject.myFunc(); 28 | } 29 | 30 | 31 | -------------------------------------------------------------------------------- /Lesson1/Exercise09/CMakeLists.txt: -------------------------------------------------------------------------------- 1 |  2 | cmake_minimum_required(VERSION 3.10.0) 3 | 4 | project(CxxTemplate 5 | VERSION 0.1.0.0 6 | LANGUAGES C CXX 7 | DESCRIPTION "A project containing the essentials for other projects" 8 | ) 9 | set(CxxTemplate_VENDOR "Aron Rubin") 10 | set(PROJECT_VENDOR "${CxxTemplate_VENDOR}") 11 | 12 | 13 | set(CMAKE_DEBUG_POSTFIX d) 14 | 15 | set(CMAKE_CXX_STANDARD 17) 16 | set(CMAKE_C_STANDARD 11) 17 | 18 | list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules") 19 | 20 | 21 | if(POLICY CMP0074) 22 | cmake_policy(SET CMP0074 NEW) 23 | endif() 24 | 25 | find_package(GTest) 26 | if(GTEST_FOUND) 27 | set(Gtest_FOUND TRUE) 28 | endif() 29 | if(GTest_FOUND) 30 | include(GoogleTest) 31 | endif() 32 | enable_testing() 33 | add_subdirectory(tests) 34 | 35 | include(CheckIncludeFiles) 36 | CHECK_INCLUDE_FILES(sys/prctl.h HAVE_SYS_PRCTL_H) 37 | 38 | find_package(Threads REQUIRED) 39 | 40 | add_executable(CxxTemplate 41 | src/CxxTemplate.cpp 42 | src/ANewClass.cpp 43 | src/SumFunc.cpp 44 | src/LinearMotion1D.cpp 45 | ) 46 | 47 | target_link_libraries(CxxTemplate 48 | PUBLIC Threads::Threads 49 | ) 50 | 51 | include(FeatureSummary) 52 | feature_summary(WHAT ALL) 53 | 54 | # include(DumpProps) 55 | # print_target_properties(diva) 56 | # include(DumpVariables) 57 | # dump_all_variables() 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /Lesson1/Exercise09/src/ANewClass.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ANewClass.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #include "ANewClass.h" 9 | #include 10 | 11 | void ANewClass::run() { 12 | std::cout << "Hello from ANewClass." << std::endl; 13 | } 14 | 15 | ANewClass::ANewClass() { 16 | // TODO Auto-generated constructor stub 17 | 18 | } 19 | 20 | ANewClass::~ANewClass() { 21 | // TODO Auto-generated destructor stub 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Lesson1/Exercise09/src/ANewClass.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ANewClass.h 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #ifndef SRC_ANEWCLASS_H_ 9 | #define SRC_ANEWCLASS_H_ 10 | 11 | class ANewClass { 12 | public: 13 | void run(); 14 | ANewClass(); 15 | virtual ~ANewClass(); 16 | }; 17 | 18 | #endif /* SRC_ANEWCLASS_H_ */ 19 | -------------------------------------------------------------------------------- /Lesson1/Exercise09/src/CxxTemplate.cpp: -------------------------------------------------------------------------------- 1 |  2 | #include "CxxTemplate.h" 3 | #include "ANewClass.h" 4 | #include "SumFunc.h" 5 | 6 | #include 7 | #include 8 | #include 9 | using namespace std::chrono_literals; 10 | 11 | namespace cxxt { 12 | 13 | CxxApplication::CxxApplication( int argc, char *argv[] ) { 14 | std::cout << "Hello CMake." << std::endl; 15 | ::ANewClass anew; 16 | anew.run(); 17 | std::cout << sum(3, 4) << std::endl; 18 | } 19 | 20 | CxxApplication::~CxxApplication() { 21 | // TODO cleanup 22 | } 23 | 24 | } // namespace cxxt 25 | 26 | 27 | int main( int argc, char *argv[] ) { 28 | cxxt::CxxApplication app( argc, argv ); 29 | 30 | int i = 1, t = 0; 31 | do { 32 | t += i++; 33 | } while (i <= 3); 34 | std::cout << t << std::endl; 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Lesson1/Exercise09/src/LinearMotion1D.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * LinearMotion1D.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #include "LinearMotion1D.h" 9 | 10 | void LinearMotion1D::advanceTimeBy(double dt) { 11 | position += velocity * dt; 12 | } 13 | 14 | LinearMotion1D::LinearMotion1D() { 15 | position = 0; 16 | velocity = 0; 17 | } 18 | 19 | LinearMotion1D::~LinearMotion1D() { 20 | // TODO Auto-generated destructor stub 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Lesson1/Exercise09/src/LinearMotion1D.h: -------------------------------------------------------------------------------- 1 | /* 2 | * LinearMotion1D.h 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #ifndef SRC_LINEARMOTION1D_H_ 9 | #define SRC_LINEARMOTION1D_H_ 10 | 11 | class LinearMotion1D { 12 | public: 13 | double position; 14 | double velocity; 15 | void advanceTimeBy(double dt); 16 | LinearMotion1D(); 17 | virtual ~LinearMotion1D(); 18 | }; 19 | 20 | #endif /* SRC_LINEARMOTION1D_H_ */ 21 | -------------------------------------------------------------------------------- /Lesson1/Exercise09/src/SumFunc.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * SumFunc.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #include "SumFunc.h" 9 | #include 10 | 11 | int sum(int a, int b) { 12 | return a + b; 13 | } 14 | -------------------------------------------------------------------------------- /Lesson1/Exercise09/src/SumFunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SumFunc.h 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #ifndef SRC_SUMFUNC_H_ 9 | #define SRC_SUMFUNC_H_ 10 | 11 | int sum(int a, int b); 12 | 13 | #endif /* SRC_SUMFUNC_H_ */ 14 | -------------------------------------------------------------------------------- /Lesson1/Exercise09/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include(GoogleTest) 2 | add_executable(tests 3 | CanTest.cpp 4 | SumFuncTest.cpp 5 | ../src/SumFunc.cpp 6 | LinearMotion1DTest.cpp 7 | ../src/LinearMotion1D.cpp 8 | ) 9 | target_link_libraries(tests GTest::GTest) 10 | gtest_discover_tests(tests) 11 | -------------------------------------------------------------------------------- /Lesson1/Exercise09/tests/CanTest.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * CanTest.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | 9 | #include "gtest/gtest.h" 10 | 11 | namespace { 12 | 13 | class CanTest: public ::testing::Test {}; 14 | 15 | TEST_F(CanTest, CanReallyTest) { 16 | EXPECT_EQ(0, 0); 17 | } 18 | 19 | } 20 | 21 | int main(int argc, char **argv) { 22 | ::testing::InitGoogleTest(&argc, argv); 23 | return RUN_ALL_TESTS(); 24 | } 25 | 26 | 27 | -------------------------------------------------------------------------------- /Lesson1/Exercise09/tests/LinearMotion1DTest.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * LinearMotion1DTest.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #include "gtest/gtest.h" 9 | #include "../src/LinearMotion1D.h" 10 | 11 | namespace { 12 | 13 | class LinearMotion1DTest: public ::testing::Test { 14 | }; 15 | 16 | TEST_F(LinearMotion1DTest, CanMoveRight) { 17 | LinearMotion1D l; 18 | l.position = 10; 19 | l.velocity = 2; 20 | l.advanceTimeBy(3); 21 | EXPECT_DOUBLE_EQ(16, l.position); 22 | } 23 | 24 | TEST_F(LinearMotion1DTest, CanMoveLeft) { 25 | LinearMotion1D l; 26 | l.position = 10; 27 | l.velocity = -2; 28 | l.advanceTimeBy(3); 29 | EXPECT_DOUBLE_EQ(4, l.position); 30 | } 31 | 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Lesson1/Exercise09/tests/SumFuncTest.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * SumFuncTest.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | #include "gtest/gtest.h" 8 | #include "../src/SumFunc.h" 9 | 10 | namespace { 11 | 12 | class SumFuncTest: public ::testing::Test {}; 13 | 14 | TEST_F(SumFuncTest, CanSumCorrectly) { 15 | EXPECT_EQ(7, sum(3, 4)); 16 | } 17 | 18 | TEST_F(SumFuncTest, CanUseNegativeValues) { 19 | EXPECT_EQ(0, sum(3, -3)); 20 | } 21 | 22 | } 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Lesson1/Exercise10/CMakeLists.txt: -------------------------------------------------------------------------------- 1 |  2 | cmake_minimum_required(VERSION 3.10.0) 3 | 4 | project(CxxTemplate 5 | VERSION 0.1.0.0 6 | LANGUAGES C CXX 7 | DESCRIPTION "A project containing the essentials for other projects" 8 | ) 9 | set(CxxTemplate_VENDOR "Aron Rubin") 10 | set(PROJECT_VENDOR "${CxxTemplate_VENDOR}") 11 | 12 | 13 | set(CMAKE_DEBUG_POSTFIX d) 14 | 15 | set(CMAKE_CXX_STANDARD 17) 16 | set(CMAKE_C_STANDARD 11) 17 | 18 | list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules") 19 | 20 | 21 | if(POLICY CMP0074) 22 | cmake_policy(SET CMP0074 NEW) 23 | endif() 24 | 25 | find_package(GTest) 26 | if(GTEST_FOUND) 27 | set(Gtest_FOUND TRUE) 28 | endif() 29 | if(GTest_FOUND) 30 | include(GoogleTest) 31 | endif() 32 | enable_testing() 33 | add_subdirectory(tests) 34 | 35 | include(CheckIncludeFiles) 36 | CHECK_INCLUDE_FILES(sys/prctl.h HAVE_SYS_PRCTL_H) 37 | 38 | find_package(Threads REQUIRED) 39 | 40 | add_executable(CxxTemplate 41 | src/CxxTemplate.cpp 42 | src/ANewClass.cpp 43 | src/SumFunc.cpp 44 | src/LinearMotion1D.cpp 45 | ) 46 | 47 | target_link_libraries(CxxTemplate 48 | PUBLIC Threads::Threads 49 | ) 50 | 51 | include(FeatureSummary) 52 | feature_summary(WHAT ALL) 53 | 54 | # include(DumpProps) 55 | # print_target_properties(diva) 56 | # include(DumpVariables) 57 | # dump_all_variables() 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /Lesson1/Exercise10/src/ANewClass.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ANewClass.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #include "ANewClass.h" 9 | #include 10 | 11 | void ANewClass::run() { 12 | std::cout << "Hello from ANewClass." << std::endl; 13 | } 14 | 15 | ANewClass::ANewClass() { 16 | // TODO Auto-generated constructor stub 17 | 18 | } 19 | 20 | ANewClass::~ANewClass() { 21 | // TODO Auto-generated destructor stub 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Lesson1/Exercise10/src/ANewClass.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ANewClass.h 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #ifndef SRC_ANEWCLASS_H_ 9 | #define SRC_ANEWCLASS_H_ 10 | 11 | class ANewClass { 12 | public: 13 | void run(); 14 | ANewClass(); 15 | virtual ~ANewClass(); 16 | }; 17 | 18 | #endif /* SRC_ANEWCLASS_H_ */ 19 | -------------------------------------------------------------------------------- /Lesson1/Exercise10/src/ArrayPlay.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ArrayPlay.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | void arrayPlay(int *input, int inputLength) { 9 | 10 | // find the sum of the input 11 | int sum = 0; 12 | for (int i = 0; i < inputLength; ++i) { 13 | sum += input[i]; 14 | } 15 | 16 | // find the smallest element 17 | int smallest = 0; 18 | for (int i = 0; i < inputLength; ++i) { 19 | if (i == 0 || smallest > input[i]) { 20 | smallest = input[i]; 21 | } 22 | } 23 | 24 | // determine whether top threshold is crossed 25 | int topThreshold = 100; 26 | bool isTopThresholdCrossed = true; 27 | for (int i = 0; i < inputLength; ++i) { 28 | if (input[i] >= topThreshold) { 29 | isTopThresholdCrossed = true; 30 | } 31 | } 32 | 33 | // multiply each element by 2 34 | for (int i = 0; i < inputLength; ++i) { 35 | if (input[i] < 0) { 36 | input[i] *= 2; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Lesson1/Exercise10/src/CxxTemplate.cpp: -------------------------------------------------------------------------------- 1 |  2 | #include "CxxTemplate.h" 3 | #include "ANewClass.h" 4 | #include "SumFunc.h" 5 | 6 | #include 7 | #include 8 | #include 9 | using namespace std::chrono_literals; 10 | 11 | namespace cxxt { 12 | 13 | CxxApplication::CxxApplication( int argc, char *argv[] ) { 14 | std::cout << "Hello CMake." << std::endl; 15 | ::ANewClass anew; 16 | anew.run(); 17 | std::cout << sum(3, 4) << std::endl; 18 | } 19 | 20 | CxxApplication::~CxxApplication() { 21 | // TODO cleanup 22 | } 23 | 24 | } // namespace cxxt 25 | 26 | 27 | int main( int argc, char *argv[] ) { 28 | cxxt::CxxApplication app( argc, argv ); 29 | 30 | int i = 1, t = 0; 31 | do { 32 | t += i++; 33 | } while (i <= 3); 34 | std::cout << t << std::endl; 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Lesson1/Exercise10/src/LinearMotion1D.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * LinearMotion1D.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #include "LinearMotion1D.h" 9 | 10 | void LinearMotion1D::advanceTimeBy(double dt) { 11 | position += velocity * dt; 12 | } 13 | 14 | LinearMotion1D::LinearMotion1D() { 15 | position = 0; 16 | velocity = 0; 17 | } 18 | 19 | LinearMotion1D::~LinearMotion1D() { 20 | // TODO Auto-generated destructor stub 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Lesson1/Exercise10/src/LinearMotion1D.h: -------------------------------------------------------------------------------- 1 | /* 2 | * LinearMotion1D.h 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #ifndef SRC_LINEARMOTION1D_H_ 9 | #define SRC_LINEARMOTION1D_H_ 10 | 11 | class LinearMotion1D { 12 | public: 13 | double position; 14 | double velocity; 15 | void advanceTimeBy(double dt); 16 | LinearMotion1D(); 17 | virtual ~LinearMotion1D(); 18 | }; 19 | 20 | #endif /* SRC_LINEARMOTION1D_H_ */ 21 | -------------------------------------------------------------------------------- /Lesson1/Exercise10/src/SumFunc.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * SumFunc.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #include "SumFunc.h" 9 | #include 10 | 11 | int sum(int a, int b) { 12 | return a + b; 13 | } 14 | -------------------------------------------------------------------------------- /Lesson1/Exercise10/src/SumFunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SumFunc.h 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #ifndef SRC_SUMFUNC_H_ 9 | #define SRC_SUMFUNC_H_ 10 | 11 | int sum(int a, int b); 12 | 13 | #endif /* SRC_SUMFUNC_H_ */ 14 | -------------------------------------------------------------------------------- /Lesson1/Exercise10/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include(GoogleTest) 2 | add_executable(tests 3 | CanTest.cpp 4 | SumFuncTest.cpp 5 | ../src/SumFunc.cpp 6 | LinearMotion1DTest.cpp 7 | ../src/LinearMotion1D.cpp 8 | ) 9 | target_link_libraries(tests GTest::GTest) 10 | gtest_discover_tests(tests) 11 | -------------------------------------------------------------------------------- /Lesson1/Exercise10/tests/CanTest.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * CanTest.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | 9 | #include "gtest/gtest.h" 10 | 11 | namespace { 12 | 13 | class CanTest: public ::testing::Test {}; 14 | 15 | TEST_F(CanTest, CanReallyTest) { 16 | EXPECT_EQ(0, 0); 17 | } 18 | 19 | } 20 | 21 | int main(int argc, char **argv) { 22 | ::testing::InitGoogleTest(&argc, argv); 23 | return RUN_ALL_TESTS(); 24 | } 25 | 26 | 27 | -------------------------------------------------------------------------------- /Lesson1/Exercise10/tests/LinearMotion1DTest.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * LinearMotion1DTest.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | 8 | #include "gtest/gtest.h" 9 | #include "../src/LinearMotion1D.h" 10 | 11 | namespace { 12 | 13 | class LinearMotion1DTest: public ::testing::Test { 14 | }; 15 | 16 | TEST_F(LinearMotion1DTest, CanMoveRight) { 17 | LinearMotion1D l; 18 | l.position = 10; 19 | l.velocity = 2; 20 | l.advanceTimeBy(3); 21 | EXPECT_DOUBLE_EQ(16, l.position); 22 | } 23 | 24 | TEST_F(LinearMotion1DTest, CanMoveLeft) { 25 | LinearMotion1D l; 26 | l.position = 10; 27 | l.velocity = -2; 28 | l.advanceTimeBy(3); 29 | EXPECT_DOUBLE_EQ(4, l.position); 30 | } 31 | 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Lesson1/Exercise10/tests/SumFuncTest.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * SumFuncTest.cpp 3 | * 4 | * Created on: Aug 1, 2019 5 | * Author: gazihan 6 | */ 7 | #include "gtest/gtest.h" 8 | #include "../src/SumFunc.h" 9 | 10 | namespace { 11 | 12 | class SumFuncTest: public ::testing::Test {}; 13 | 14 | TEST_F(SumFuncTest, CanSumCorrectly) { 15 | EXPECT_EQ(7, sum(3, 4)); 16 | } 17 | 18 | TEST_F(SumFuncTest, CanUseNegativeValues) { 19 | EXPECT_EQ(0, sum(3, -3)); 20 | } 21 | 22 | } 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Lesson1/readme.md: -------------------------------------------------------------------------------- 1 | # Advanced C++ 2 | Write robust C++ code so fast they will think you cheated 3 | 4 | Exercises related to Lesson 1 - Anatomy of Portable C++ Software 5 | -------------------------------------------------------------------------------- /Lesson2A/Activity01/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #============================================================================ 2 | # Name : CMakeLists.txt 3 | # Author : Brian Price 4 | # Version : 5 | # Copyright : Your copyright notice 6 | # Description : Lesson 2 Activity 1 build file 7 | #============================================================================ 8 | cmake_minimum_required(VERSION 3.11.0) 9 | 10 | project(Activty01 11 | VERSION 0.1.0.0 12 | DESCRIPTION "Activity 1 for Lesson 2 of Advanced C++ course." 13 | ) 14 | 15 | 16 | set(CMAKE_DEBUG_POSTFIX d) 17 | 18 | set(CMAKE_CXX_STANDARD 17) 19 | set(CMAKE_C_STANDARD 11) 20 | 21 | if(POLICY CMP0074) 22 | cmake_policy(SET CMP0074 NEW) 23 | endif() 24 | 25 | include(FetchContent) 26 | 27 | # ------------------------------------------------------------------ 28 | # Activity 1 29 | # ------------------------------------------------------------------ 30 | add_executable(graphics point3d.cpp main.cpp) 31 | find_package(GTest) 32 | 33 | enable_testing() 34 | add_subdirectory(tests) 35 | 36 | 37 | -------------------------------------------------------------------------------- /Lesson2A/Activity01/answers/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #============================================================================ 2 | # Name : CMakeLists.txt 3 | # Author : Brian Price 4 | # Version : 5 | # Copyright : Your copyright notice 6 | # Description : Lesson 2 Activity 1 build file 7 | #============================================================================ 8 | cmake_minimum_required(VERSION 3.11.0) 9 | 10 | project(Activity01 11 | VERSION 0.1.0.0 12 | DESCRIPTION "Activity 1 for Lesson 2 of Advanced C++ course." 13 | ) 14 | 15 | 16 | set(CMAKE_DEBUG_POSTFIX d) 17 | 18 | set(CMAKE_CXX_STANDARD 17) 19 | set(CMAKE_C_STANDARD 11) 20 | 21 | if(POLICY CMP0074) 22 | cmake_policy(SET CMP0074 NEW) 23 | endif() 24 | 25 | include(FetchContent) 26 | 27 | # ------------------------------------------------------------------ 28 | # Activity 1 29 | # ------------------------------------------------------------------ 30 | add_executable(graphics point3d.cpp main.cpp matrix3d.cpp) 31 | find_package(GTest) 32 | 33 | enable_testing() 34 | add_subdirectory(tests) 35 | 36 | 37 | -------------------------------------------------------------------------------- /Lesson2A/Activity01/answers/matrix3d.hpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : matrix3d.hpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Activity 1 Lesson 2 7 | //============================================================================ 8 | #pragma once 9 | #include 10 | #include 11 | #include "point3d.hpp" 12 | 13 | class Matrix3d 14 | { 15 | public: 16 | static constexpr size_t NumberRows{4}; 17 | static constexpr size_t NumberColumns{4}; 18 | 19 | Matrix3d(); 20 | Matrix3d(std::initializer_list> list); 21 | 22 | Matrix3d& operator*=(const Matrix3d& rhs); 23 | 24 | float operator()(const int row, const int column) const 25 | { 26 | return m_data[row][column]; 27 | } 28 | 29 | float& operator()(const int row, const int column) 30 | { 31 | return m_data[row][column]; 32 | } 33 | 34 | 35 | private: 36 | float m_data[NumberRows][NumberColumns]; 37 | }; 38 | 39 | inline Matrix3d operator*(const Matrix3d& lhs, const Matrix3d& rhs) 40 | { 41 | Matrix3d temp(lhs); 42 | temp *= rhs; 43 | return temp; 44 | } 45 | 46 | Point3d operator*(const Matrix3d& lhs, const Point3d& rhs); 47 | 48 | Matrix3d createTranslationMatrix(float dx, float dy, float dz); 49 | Matrix3d createScaleMatrix(float sx, float sy, float sz); 50 | Matrix3d createRotationMatrixAboutX(float degrees); 51 | Matrix3d createRotationMatrixAboutY(float degrees); 52 | Matrix3d createRotationMatrixAboutZ(float degrees); 53 | 54 | 55 | -------------------------------------------------------------------------------- /Lesson2A/Activity01/answers/point3d.cpp: -------------------------------------------------------------------------------- 1 | ///============================================================================ 2 | // Name : point3d.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Activity 1 Lesson 2 7 | //============================================================================ 8 | #include "point3d.hpp" 9 | 10 | Point3d::Point3d() 11 | { 12 | for(auto& item : m_data) 13 | { 14 | item = 0; 15 | } 16 | m_data[NumberRows-1] = 1; 17 | } 18 | 19 | Point3d::Point3d(std::initializer_list list) 20 | { 21 | m_data[NumberRows-1] = 1; 22 | int i{0}; 23 | for(auto it1 = list.begin(); i 10 | #include 11 | #include 12 | 13 | class Point3d 14 | { 15 | public: 16 | static constexpr size_t NumberRows{4}; 17 | 18 | Point3d(); 19 | Point3d(std::initializer_list list); 20 | 21 | float operator()(const int index) const 22 | { 23 | return m_data[index]; 24 | } 25 | 26 | float& operator()(const int index) 27 | { 28 | return m_data[index]; 29 | } 30 | 31 | bool operator==(const Point3d& rhs) const; 32 | bool operator!=(const Point3d& rhs) const 33 | { 34 | return !operator==(rhs); 35 | } 36 | 37 | 38 | private: 39 | 40 | float m_data[NumberRows]; 41 | 42 | friend std::ostream& operator<<(std::ostream& , const Point3d& ); 43 | }; 44 | 45 | inline std::ostream& 46 | operator<<(std::ostream& os, const Point3d& pt) 47 | { 48 | const char* sep = "[ "; 49 | for(auto value : pt.m_data) 50 | { 51 | os << sep << value; 52 | sep = ", "; 53 | } 54 | os << " ]"; 55 | return os; 56 | } 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /Lesson2A/Activity01/answers/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #============================================================================ 2 | # Name : CMakeLists.txt 3 | # Author : Brian Price 4 | # Version : 5 | # Copyright : Your copyright notice 6 | # Description : Lesson 2 Activity 1 tests build file 7 | #============================================================================ 8 | include(GoogleTest) 9 | 10 | SET(SRC_FILES 11 | ../matrix3d.cpp 12 | ../point3d.cpp) 13 | 14 | SET(TEST_FILES 15 | matrix3dTests.cpp 16 | point3dTests.cpp) 17 | 18 | add_executable(graphicstests 19 | ${SRC_FILES} 20 | ${TEST_FILES} 21 | ) 22 | 23 | target_link_libraries(graphicstests GTest::GTest GTest::Main) 24 | 25 | gtest_discover_tests(graphicstests) 26 | -------------------------------------------------------------------------------- /Lesson2A/Activity01/point3d.cpp: -------------------------------------------------------------------------------- 1 | ///============================================================================ 2 | // Name : point3d.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Activity 1 Lesson 2 7 | //============================================================================ 8 | #include "point3d.hpp" 9 | 10 | -------------------------------------------------------------------------------- /Lesson2A/Activity01/point3d.hpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : point3d.hpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Activity 1 Lesson 2 7 | //============================================================================ 8 | #pragma once 9 | 10 | 11 | class Point3d 12 | { 13 | public: 14 | 15 | 16 | private: 17 | 18 | }; 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /Lesson2A/Activity01/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #============================================================================ 2 | # Name : CMakeLists.txt 3 | # Author : Brian Price 4 | # Version : 5 | # Copyright : Your copyright notice 6 | # Description : Lesson 2 Activity 1 tests build file 7 | #============================================================================ 8 | include(GoogleTest) 9 | 10 | SET(SRC_FILES 11 | ../point3d.cpp) 12 | 13 | SET(TEST_FILES 14 | point3dTests.cpp) 15 | 16 | add_executable(graphicstests 17 | ${SRC_FILES} 18 | ${TEST_FILES} 19 | ) 20 | 21 | target_link_libraries(graphicstests GTest::GTest GTest::Main) 22 | 23 | gtest_discover_tests(graphicstests) 24 | -------------------------------------------------------------------------------- /Lesson2A/Activity01/tests/point3dTests.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : point3dtest.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Activity 1 Lesson 2 7 | //============================================================================ 8 | #include "gtest/gtest.h" 9 | #include "../point3d.hpp" 10 | 11 | 12 | 13 | class Point3dTest : public ::testing::Test 14 | { 15 | public: 16 | }; 17 | 18 | 19 | TEST_F(Point3dTest, DummyTest) 20 | { 21 | ASSERT_FALSE(true); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Lesson2A/Exercise02/Exercise2.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : Exercise2.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Exercise 2 Lesson 2 7 | //============================================================================ 8 | #include 9 | 10 | 11 | int gcd(int x, int y) 12 | { 13 | while(y!=0) 14 | { 15 | auto c{x%y}; 16 | x = y; 17 | y = c; 18 | } 19 | return x; 20 | } 21 | 22 | int main(int argc, char**argv) 23 | { 24 | std::cout << "\n\n------ Exercise 2 ------\n"; 25 | std::cout << "The greatest common divisor of 44 and 121 is " << gcd(44, 121) << "\n"; 26 | std::cout << "Complete.\n"; 27 | return 0; 28 | } 29 | 30 | 31 | -------------------------------------------------------------------------------- /Lesson2A/Exercise04/Exercise4.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : Exercise4.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Exercise 4 Lesson 2 7 | //============================================================================ 8 | 9 | #include 10 | #include 11 | 12 | enum Apple { 13 | RedDelicious, 14 | GoldenDelicious, 15 | Gala, 16 | Fuji, 17 | GrannySmith 18 | }; 19 | 20 | enum Orange { 21 | Navel, 22 | Valencia, 23 | Hamlin, 24 | ParsonBrown 25 | }; 26 | 27 | void printOrange(int orange) 28 | { 29 | std::cout << "printOrange => " << orange << "\n"; 30 | } 31 | 32 | int main(int argc, char**argv) 33 | { 34 | std::cout << "\n\n------ Exercise 4 ------\n"; 35 | 36 | Apple apple = RedDelicious; 37 | Orange orange{Hamlin}; 38 | 39 | std::cout << "apple = " << apple << "\n"; 40 | std::cout << "orange = " << orange << "\n"; 41 | 42 | apple = Gala; 43 | std::cout << "apple = " << apple << "\n"; 44 | 45 | if (apple == orange) 46 | { 47 | std::cout << "apple == orange" << "\n"; 48 | orange = Valencia; 49 | } 50 | else 51 | { 52 | std::cout << "apple != orange" << "\n"; 53 | } 54 | 55 | printOrange(orange); 56 | printOrange(apple); 57 | 58 | std::cout << "Complete.\n"; 59 | return 0; 60 | } 61 | 62 | -------------------------------------------------------------------------------- /Lesson2A/Exercise05/Exercise5.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : Exercise5.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Exercise 5 Lesson 2 7 | //============================================================================ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | class Fraction 14 | { 15 | public: 16 | Fraction() 17 | { 18 | 19 | } 20 | 21 | int getNumerator() const 22 | { 23 | return m_numerator; 24 | } 25 | 26 | int getDenominator() const 27 | { 28 | return m_denominator; 29 | } 30 | 31 | private: 32 | int m_numerator; 33 | int m_denominator; 34 | }; 35 | 36 | int main(int argc, char**argv) 37 | { 38 | std::cout << "\n\n------ Exercise 5 ------\n"; 39 | 40 | Fraction fraction; 41 | 42 | std::cout << "fraction = " << fraction.getNumerator() << "/" << fraction.getDenominator() << "\n"; 43 | 44 | std::cout << "sizeof(Fraction) = " << sizeof(Fraction) << " bytes\n"; 45 | std::cout << "sizeof(Fraction.m_numerator) = " << sizeof(int) << " bytes\n"; 46 | std::cout << "sizeof(Fraction.m_denominator) = " << sizeof(int) << " bytes\n"; 47 | 48 | 49 | std::cout << "Complete.\n"; 50 | return 0; 51 | } 52 | 53 | -------------------------------------------------------------------------------- /Lesson2A/Exercise06/Exercise6.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : Exercise6.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Exercise 6 Lesson 2 7 | //============================================================================ 8 | 9 | #include 10 | #include 11 | 12 | void swap(int& lhs, int& rhs) 13 | { 14 | int tmp{lhs}; 15 | lhs = rhs; 16 | rhs = tmp; 17 | } 18 | 19 | int main(int argc, char**argv) 20 | { 21 | std::cout << "\n\n------ Exercise 6 ------\n"; 22 | 23 | int value = 42; // declare value to be an integer and initialise to 42 24 | int& rvalue{value}; // declare a reference to an int and initialise it at value 25 | 26 | std::cout << "value = " << value << "\n"; 27 | std::cout << "Setting value via rvalue\n"; 28 | rvalue = 96; 29 | std::cout << "value = " << value << "\n\n"; 30 | 31 | int a{123}; 32 | int b{456}; 33 | std::cout << "Swapping " << a << " and " << b << "\n"; 34 | swap(a,b); 35 | std::cout << "Swapped " << a << " and " << b << "\n"; 36 | 37 | 38 | std::cout << "value = " << value << "; rvalue = " << rvalue << "; a = " << a << "\n"; 39 | std::cout << "Setting rvalue to a and then assigning 32 to rvalue\n"; 40 | 41 | rvalue = 32; 42 | std::cout << "value = " << value << "; rvalue = " << rvalue << "; a = " << a << "\n"; 43 | 44 | std::cout << "Complete.\n"; 45 | return 0; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /Lesson2A/Exercise07/Exercise7.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : Exercise7.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Exercise 7 Lesson 2 7 | //============================================================================ 8 | 9 | #include 10 | #include "Fraction.hpp" 11 | 12 | 13 | int main(int argc, char**argv) 14 | { 15 | std::cout << "\n\n------ Exercise 7 ------\n"; 16 | 17 | Fraction fraction(5); 18 | 19 | std::cout << fraction << "\n"; 20 | Fraction f1(1, 24); 21 | Fraction f2(3, 5); 22 | std::cout << f1 << " * " << f2 << " = " << f1*f2 << "\n"; 23 | std::cout << f1 << " + " << f2 << " = " << f1+f2 << "\n"; 24 | std::cout << f1 << " - " << f2 << " = " << f1-f2 << "\n"; 25 | std::cout << f1 << " / " << f2 << " = " << f1/f2 << "\n"; 26 | 27 | 28 | std::cout << "sizeof(Fraction) = " << sizeof(Fraction) << " bytes\n"; 29 | std::cout << "sizeof(Fraction.m_numerator) = " << sizeof(int) << " bytes\n"; 30 | std::cout << "sizeof(Fraction.m_denominator) = " << sizeof(int) << " bytes\n"; 31 | 32 | 33 | std::cout << "Complete.\n"; 34 | return 0; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /Lesson2A/Exercise07/Fraction.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : Fraction.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Exercise 7 Lesson 2 7 | //============================================================================ 8 | #include "Fraction.hpp" 9 | #include 10 | #include 11 | 12 | namespace { 13 | inline int signum(int val) { 14 | return (0 < val) - (val < 0); 15 | } 16 | 17 | } 18 | 19 | Fraction::Fraction(int numerator, int denominator) : m_numerator{numerator}, m_denominator{denominator} 20 | { 21 | int sign{(signum(m_numerator)*signum(m_denominator))}; 22 | m_numerator = std::abs(m_numerator); 23 | m_denominator = std::abs(m_denominator); 24 | 25 | int divisor{std::gcd(m_numerator, m_denominator)}; 26 | m_numerator = sign * m_numerator / divisor; 27 | m_denominator = m_denominator / divisor; 28 | } 29 | 30 | Fraction& Fraction::operator*=(const Fraction& rhs) 31 | { 32 | return *this; 33 | } 34 | 35 | Fraction& Fraction::operator+=(const Fraction& rhs) 36 | { 37 | return *this; 38 | } 39 | 40 | Fraction& Fraction::operator-=(const Fraction& rhs) 41 | { 42 | return *this; 43 | } 44 | 45 | Fraction& Fraction::operator/=(const Fraction& rhs) 46 | { 47 | return *this; 48 | } 49 | -------------------------------------------------------------------------------- /Lesson2A/Exercise07/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include(GoogleTest) 2 | add_executable(tests FractionTests.cpp ../Fraction.cpp) 3 | target_link_libraries(tests GTest::GTest GTest::Main) 4 | gtest_discover_tests(tests) 5 | -------------------------------------------------------------------------------- /Lesson2A/readme.md: -------------------------------------------------------------------------------- 1 | # Advanced C++ 2 | Write robust C++ code so fast they will think you cheated 3 | 4 | Exercises related to Lesson 2 - No Ducks Allowed - Types, Templates, and Deduction 5 | -------------------------------------------------------------------------------- /Lesson2B/Activity01/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #============================================================================ 2 | # Name : CMakeLists.txt 3 | # Author : Brian Price 4 | # Version : 5 | # Copyright : Your copyright notice 6 | # Description : Lesson 3 Activity 1 build file 7 | #============================================================================ 8 | cmake_minimum_required(VERSION 3.11.0) 9 | 10 | project(Activty01 11 | VERSION 0.1.0.0 12 | DESCRIPTION "Activity 1 for Lesson 3 of Advanced C++ course." 13 | ) 14 | 15 | set(CMAKE_DEBUG_POSTFIX d) 16 | 17 | set(CMAKE_CXX_STANDARD 17) 18 | set(CMAKE_C_STANDARD 11) 19 | 20 | if(POLICY CMP0074) 21 | cmake_policy(SET CMP0074 NEW) 22 | endif() 23 | 24 | include(FetchContent) 25 | 26 | # ------------------------------------------------------------------ 27 | # Activity 1 28 | # ------------------------------------------------------------------ 29 | add_executable(contains main.cpp) 30 | find_package(GTest) 31 | 32 | enable_testing() 33 | add_subdirectory(tests) 34 | 35 | 36 | -------------------------------------------------------------------------------- /Lesson2B/Activity01/answers/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #============================================================================ 2 | # Name : CMakeLists.txt 3 | # Author : Brian Price 4 | # Version : 5 | # Copyright : Your copyright notice 6 | # Description : Lesson 3 Activity 1 build file 7 | #============================================================================ 8 | cmake_minimum_required(VERSION 3.11.0) 9 | 10 | project(Activty01 11 | VERSION 0.1.0.0 12 | DESCRIPTION "Activity 1 for Lesson 3 of Advanced C++ course." 13 | ) 14 | 15 | set(CMAKE_DEBUG_POSTFIX d) 16 | 17 | set(CMAKE_CXX_STANDARD 17) 18 | set(CMAKE_C_STANDARD 11) 19 | 20 | if(POLICY CMP0074) 21 | cmake_policy(SET CMP0074 NEW) 22 | endif() 23 | 24 | include(FetchContent) 25 | 26 | # ------------------------------------------------------------------ 27 | # Activity 1 28 | # ------------------------------------------------------------------ 29 | add_executable(contains main.cpp) 30 | find_package(GTest) 31 | 32 | enable_testing() 33 | add_subdirectory(tests) 34 | 35 | 36 | -------------------------------------------------------------------------------- /Lesson2B/Activity01/answers/contains.hpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : contains.hpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Activity 1 Lesson 3 7 | //============================================================================ 8 | #pragma once 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | 16 | template 17 | auto test_npos(int) -> decltype((void)T::npos, std::true_type{}); 18 | 19 | template 20 | auto test_npos(long) -> std::false_type; 21 | 22 | template 23 | struct has_npos : decltype(test_npos(0)) {}; 24 | 25 | template< class T > 26 | inline constexpr bool has_npos_v = has_npos::value; 27 | 28 | 29 | template 30 | auto test_find(int) -> decltype(void(std::declval().find(std::declval())), std::true_type{}); 31 | 32 | template 33 | auto test_find(long) -> std::false_type; 34 | 35 | template 36 | struct has_find : decltype(test_find(0)) {}; 37 | 38 | template< class T, class A0 > 39 | inline constexpr bool has_find_v = has_find::value; 40 | 41 | template 42 | auto contains(const C& c, const T& key) -> decltype(end(c), true) 43 | { 44 | if constexpr(has_npos_v) 45 | { 46 | return C::npos != c.find(key); 47 | } 48 | else if constexpr(has_find_v) 49 | { 50 | return std::end(c) != c.find(key); 51 | } 52 | else 53 | { 54 | return end(c) != std::find(begin(c), end(c), key); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Lesson2B/Activity01/answers/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #============================================================================ 2 | # Name : CMakeLists.txt 3 | # Author : Brian Price 4 | # Version : 5 | # Copyright : Your copyright notice 6 | # Description : Lesson 2 Activity 1 tests build file 7 | #============================================================================ 8 | include(GoogleTest) 9 | 10 | SET(SRC_FILES ) 11 | 12 | SET(TEST_FILES 13 | containsTests.cpp 14 | ) 15 | 16 | add_executable(containstests 17 | ${SRC_FILES} 18 | ${TEST_FILES} 19 | ) 20 | 21 | target_link_libraries(containstests GTest::GTest GTest::Main) 22 | 23 | gtest_discover_tests(containstests) 24 | -------------------------------------------------------------------------------- /Lesson2B/Activity01/contains.hpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : contains.hpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Activity 1 Lesson 3 7 | //============================================================================ 8 | #pragma once 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | -------------------------------------------------------------------------------- /Lesson2B/Activity01/main.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : main.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Description : Activity 1 Lesson 3 6 | //============================================================================ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "contains.hpp" 12 | 13 | // #define ACTIVITY1 14 | 15 | 16 | int main() 17 | { 18 | std::cout << "\n------ Activity 1 ------\n"; 19 | #if defined(ACTIVITY1) 20 | std::set mySet {1,2,3,4,5}; 21 | 22 | std::cout << std::boolalpha; 23 | std::cout << "Set contains 5: " << contains(mySet,5) << "\n"; 24 | std::cout << "Set contains 15: " << contains(mySet,15) << "\n"; 25 | 26 | std::vector myVector{1,2,3,4,5}; 27 | std::cout << "Vector contains 5: " << contains(myVector,5) << "\n"; 28 | std::cout << "Vector contains 15: " << contains(myVector,15) << "\n"; 29 | 30 | std::string myString{"It was the best of times, it was the worst of times,"}; 31 | 32 | std::cout << "String contains 'times': " << contains(myString, "times") << "\n"; 33 | std::cout << "String contains 'Light': " << contains(myString, "Light") << "\n"; 34 | std::cout << "String contains 'm': " << contains(myString, 'm') << "\n"; 35 | std::cout << "String contains 'z': " << contains(myString, 'z') << "\n"; 36 | 37 | #else 38 | std::cout << "\nWait until the implementation of contains template is complete.\n"; 39 | std::cout << "\nThen remove comment from #define ACTIVITY1 and re-run\n"; 40 | #endif 41 | std::cout << "\nComplete.\n"; 42 | return 0; 43 | } 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /Lesson2B/Activity01/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #============================================================================ 2 | # Name : CMakeLists.txt 3 | # Author : Brian Price 4 | # Version : 5 | # Copyright : Your copyright notice 6 | # Description : Lesson 2 Activity 1 tests build file 7 | #============================================================================ 8 | include(GoogleTest) 9 | 10 | SET(SRC_FILES ) 11 | 12 | SET(TEST_FILES 13 | containsTests.cpp 14 | ) 15 | 16 | add_executable(containstests 17 | ${SRC_FILES} 18 | ${TEST_FILES} 19 | ) 20 | 21 | target_link_libraries(containstests GTest::GTest GTest::Main) 22 | 23 | gtest_discover_tests(containstests) 24 | -------------------------------------------------------------------------------- /Lesson2B/Activity01/tests/containsTests.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : containsTests.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Activity 1 Lesson 3 7 | //============================================================================ 8 | #include "gtest/gtest.h" 9 | #include "../contains.hpp" 10 | #include 11 | #include 12 | #include 13 | 14 | 15 | class containsTest : public ::testing::Test 16 | { 17 | public: 18 | }; 19 | 20 | 21 | TEST_F(containsTest, Dummy) 22 | { 23 | ASSERT_TRUE(false); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Lesson2B/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #============================================================================ 2 | # Name : CMakeLists.txt 3 | # Author : Brian Price 4 | # Version : 5 | # Description : Lesson 2B build file 6 | #============================================================================ 7 | cmake_minimum_required(VERSION 3.11.0) 8 | 9 | project(Lesson03 10 | VERSION 0.1.0.0 11 | DESCRIPTION "Lesson 2B of Advanced C++ course." 12 | ) 13 | 14 | set(CMAKE_DEBUG_POSTFIX d) 15 | set(CMAKE_CXX_STANDARD 17) 16 | set(CMAKE_C_STANDARD 11) 17 | 18 | 19 | if(POLICY CMP0074) 20 | cmake_policy(SET CMP0074 NEW) 21 | endif() 22 | 23 | # ------------------------------------------------------------------ 24 | # Exercise 1 25 | # ------------------------------------------------------------------ 26 | add_executable(Exercise1 Exercise01/Exercise1.cpp) 27 | 28 | # ------------------------------------------------------------------ 29 | # Exercise 2 30 | # ------------------------------------------------------------------ 31 | add_executable(Exercise2 Exercise02/Exercise2.cpp) 32 | 33 | # ------------------------------------------------------------------ 34 | # Exercise 3 35 | # ------------------------------------------------------------------ 36 | add_executable(Exercise3 Exercise03/Exercise3.cpp) 37 | 38 | # ------------------------------------------------------------------ 39 | # Exercise 4 40 | # ------------------------------------------------------------------ 41 | add_executable(Exercise4 Exercise04/Exercise4.cpp) 42 | find_package(GTest) 43 | 44 | enable_testing() 45 | add_subdirectory(Exercise04/tests) 46 | 47 | -------------------------------------------------------------------------------- /Lesson2B/Exercise02/Exercise2.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : Exercise8.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Exercise 2 Lesson 3 7 | //============================================================================ 8 | 9 | #include 10 | #include 11 | 12 | void printVector(std::vector& ary) 13 | { 14 | std::cout << "Array is : "; 15 | for(std::vector::iterator it{ary.begin()}; it != ary.end() ; ++it) 16 | { 17 | std::cout << *it << ","; 18 | } 19 | std::cout << "\n"; 20 | } 21 | 22 | int main(int argc, char**argv) 23 | { 24 | std::cout << "\n\n------ Exercise 2 ------\n"; 25 | 26 | std::vector ary = {2,4,6,1,3,5}; 27 | 28 | printVector(ary); 29 | 30 | std::cout << "Complete.\n"; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Lesson2B/Exercise04/Exercise4.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : Exercise4.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Exercise 4 Lesson 3 7 | //============================================================================ 8 | 9 | #include 10 | #include "Stack.hpp" 11 | 12 | 13 | int main(int argc, char**argv) 14 | { 15 | std::cout << "\n\n------ Exercise 4 ------\n"; 16 | 17 | #if EXERCISE4_STEP >= 20 18 | const char* emptyStr[2] = {"not empty", "empty"}; 19 | acpp::Stack mystack; 20 | 21 | std::cout << "Stack is " << emptyStr[mystack.empty()] << "\n"; 22 | std::cout << "Pushing 0.0F onto stack\n"; 23 | mystack.push(0.0F); 24 | std::cout << "Stack is " << emptyStr[mystack.empty()] << "\n"; 25 | std::cout << "Pushing 3.14159F onto stack\n"; 26 | mystack.push(3.14159F); 27 | std::cout << "Stack has " << mystack.size() << " items\n"; 28 | std::cout << "Top item is " << mystack.top() << "\n"; 29 | 30 | 31 | #endif 32 | std::cout << "Complete.\n"; 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Lesson2B/Exercise04/Stack.hpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : Stack.hpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Exercise 4 Lesson 3 7 | //============================================================================ 8 | #pragma once 9 | #include 10 | #include 11 | 12 | #define EXERCISE4_STEP 1 13 | 14 | namespace acpp 15 | { 16 | 17 | template 18 | class Stack 19 | { 20 | public: 21 | 22 | private: 23 | std::vector m_stack; 24 | }; 25 | 26 | } // namespace acpp 27 | -------------------------------------------------------------------------------- /Lesson2B/Exercise04/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include(GoogleTest) 2 | add_executable(L2BEx4tests StackTests.cpp) 3 | target_link_libraries(L2BEx4tests GTest::GTest GTest::Main) 4 | gtest_discover_tests(L2BEx4tests) 5 | -------------------------------------------------------------------------------- /Lesson3/Activity01/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #============================================================================ 2 | # Name : CMakeLists.txt 3 | # Author : Brian Price 4 | # Version : 5 | # Copyright : Your copyright notice 6 | # Description : Lesson 3 Activity 1 build file 7 | #============================================================================ 8 | cmake_minimum_required(VERSION 3.11.0) 9 | 10 | project(Activty01 11 | VERSION 0.1.0.0 12 | DESCRIPTION "Activity 1 for Lesson 3 of Advanced C++ course." 13 | ) 14 | 15 | 16 | set(CMAKE_DEBUG_POSTFIX d) 17 | 18 | set(CMAKE_CXX_STANDARD 17) 19 | set(CMAKE_C_STANDARD 11) 20 | 21 | if(POLICY CMP0074) 22 | cmake_policy(SET CMP0074 NEW) 23 | endif() 24 | 25 | # ------------------------------------------------------------------ 26 | # Activity 1 27 | # ------------------------------------------------------------------ 28 | add_executable(L3graphics matrix3d.cpp point3d.cpp main.cpp) 29 | find_package(GTest) 30 | 31 | enable_testing() 32 | add_subdirectory(tests) 33 | 34 | 35 | -------------------------------------------------------------------------------- /Lesson3/Activity01/answers/main.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : main.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Activity 1 Lesson 2 7 | //============================================================================ 8 | #include 9 | 10 | #include "point3d.hpp" 11 | #include "matrix3d.hpp" 12 | 13 | using namespace acpp::gfx; 14 | 15 | int main() 16 | { 17 | std::cout << "\n------ Activity 1 ------\n"; 18 | Point3d thePoint{1,1,1}; 19 | 20 | std::cout << "The point " << thePoint << "\n"; 21 | std::cout << "...rotated around X by 90 degrees\n"; 22 | auto rotX = createRotationMatrixAboutX(90.0F); 23 | thePoint = rotX * thePoint; 24 | 25 | std::cout << "...rotated around Y by 90 degrees\n"; 26 | auto rotY = createRotationMatrixAboutY(90.0F); 27 | thePoint = rotY * thePoint; 28 | 29 | std::cout << "...rotated around Z by 90 degrees\n"; 30 | auto rotZ = createRotationMatrixAboutZ(90.0F); 31 | thePoint = rotZ * thePoint; 32 | 33 | std::cout << "moves to " << thePoint << "\n"; 34 | 35 | std::cout << "...scaled by x=2, y = 3, z = 4\n"; 36 | auto scale = createScaleMatrix(2, 3, 4); 37 | thePoint = scale * thePoint; 38 | 39 | std::cout << "moves to " << thePoint << "\n"; 40 | 41 | std::cout << "...translated by dx=-2, dy = 1, dz = -3\n"; 42 | auto translate = createTranslationMatrix(-2, 1, -3); 43 | thePoint = translate * thePoint; 44 | 45 | std::cout << "moves to " << thePoint << "\n"; 46 | std::cout << "\nComplete.\n"; 47 | return 0; 48 | } 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /Lesson3/Activity01/answers/point3d.cpp: -------------------------------------------------------------------------------- 1 | ///============================================================================ 2 | // Name : point3d.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Activity 1 Lesson 2 7 | //============================================================================ 8 | #include "point3d.hpp" 9 | 10 | namespace acpp::gfx { 11 | 12 | Point3d::Point3d() : m_data{new float[NumberRows]} 13 | { 14 | for(int i{0} ; i < NumberRows-1 ; i++) 15 | { 16 | m_data[i] = 0; 17 | } 18 | m_data[NumberRows-1] = 1; 19 | } 20 | 21 | Point3d::Point3d(std::initializer_list list) : m_data{new float[NumberRows]} 22 | { 23 | m_data[NumberRows-1] = 1; 24 | int i{0}; 25 | for(auto it1 = list.begin(); i 10 | #include 11 | #include 12 | #include 13 | 14 | namespace acpp::gfx { 15 | 16 | class Point3d 17 | { 18 | public: 19 | static constexpr size_t NumberRows{4}; 20 | 21 | Point3d(); 22 | Point3d(std::initializer_list list); 23 | 24 | float operator()(const int index) const 25 | { 26 | return m_data[index]; 27 | } 28 | 29 | float& operator()(const int index) 30 | { 31 | return m_data[index]; 32 | } 33 | 34 | bool operator==(const Point3d& rhs) const; 35 | bool operator!=(const Point3d& rhs) const 36 | { 37 | return !operator==(rhs); 38 | } 39 | 40 | 41 | private: 42 | std::unique_ptr m_data; 43 | 44 | friend std::ostream& operator<<(std::ostream& , const Point3d& ); 45 | }; 46 | 47 | inline std::ostream& 48 | operator<<(std::ostream& os, const Point3d& pt) 49 | { 50 | const char* sep = "[ "; 51 | for(int i{0} ; i < Point3d::NumberRows ; i++) 52 | { 53 | os << sep << pt.m_data[i]; 54 | sep = ", "; 55 | } 56 | os << " ]"; 57 | return os; 58 | } 59 | 60 | 61 | } 62 | -------------------------------------------------------------------------------- /Lesson3/Activity01/answers/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #============================================================================ 2 | # Name : CMakeLists.txt 3 | # Author : Brian Price 4 | # Version : 5 | # Copyright : Your copyright notice 6 | # Description : Lesson 3 Activity 1 tests build file 7 | #============================================================================ 8 | include(GoogleTest) 9 | 10 | SET(SRC_FILES 11 | ../matrix3d.cpp 12 | ../point3d.cpp) 13 | 14 | SET(TEST_FILES 15 | matrix3dTests.cpp 16 | point3dTests.cpp) 17 | 18 | add_executable(L3graphicstests 19 | ${SRC_FILES} 20 | ${TEST_FILES} 21 | ) 22 | 23 | target_link_libraries(L3graphicstests GTest::GTest GTest::Main) 24 | 25 | gtest_discover_tests(L3graphicstests) 26 | -------------------------------------------------------------------------------- /Lesson3/Activity01/main.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : main.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Activity 1 Lesson 2 7 | //============================================================================ 8 | #include 9 | 10 | 11 | #include "point3d.hpp" 12 | #include "matrix3d.hpp" 13 | 14 | int main() 15 | { 16 | std::cout << "\n------ Activity 1 ------\n"; 17 | Point3d thePoint{1,1,1}; 18 | 19 | std::cout << "The point " << thePoint << "\n"; 20 | std::cout << "...rotated around X by 90 degrees\n"; 21 | auto rotX = createRotationMatrixAboutX(90.0F); 22 | thePoint = rotX * thePoint; 23 | 24 | std::cout << "...rotated around Y by 90 degrees\n"; 25 | auto rotY = createRotationMatrixAboutY(90.0F); 26 | thePoint = rotY * thePoint; 27 | 28 | std::cout << "...rotated around Z by 90 degrees\n"; 29 | auto rotZ = createRotationMatrixAboutZ(90.0F); 30 | thePoint = rotZ * thePoint; 31 | 32 | std::cout << "moves to " << thePoint << "\n"; 33 | 34 | std::cout << "...scaled by x=2, y = 3, z = 4\n"; 35 | auto scale = createScaleMatrix(2, 3, 4); 36 | thePoint = scale * thePoint; 37 | 38 | std::cout << "moves to " << thePoint << "\n"; 39 | 40 | std::cout << "...translated by dx=-2, dy = 1, dz = -3\n"; 41 | auto translate = createTranslationMatrix(-2, 1, -3); 42 | thePoint = translate * thePoint; 43 | 44 | std::cout << "moves to " << thePoint << "\n"; 45 | std::cout << "\nComplete.\n"; 46 | return 0; 47 | } 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /Lesson3/Activity01/matrix3d.hpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : matrix3d.hpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Activity 1 Lesson 2 7 | //============================================================================ 8 | #pragma once 9 | #include 10 | #include 11 | #include "point3d.hpp" 12 | 13 | class Matrix3d 14 | { 15 | public: 16 | static constexpr size_t NumberRows{4}; 17 | static constexpr size_t NumberColumns{4}; 18 | 19 | Matrix3d(); 20 | Matrix3d(std::initializer_list> list); 21 | 22 | Matrix3d& operator*=(const Matrix3d& rhs); 23 | 24 | float operator()(const int row, const int column) const 25 | { 26 | return m_data[row][column]; 27 | } 28 | 29 | float& operator()(const int row, const int column) 30 | { 31 | return m_data[row][column]; 32 | } 33 | 34 | 35 | private: 36 | float m_data[NumberRows][NumberColumns]; 37 | }; 38 | 39 | inline Matrix3d operator*(const Matrix3d& lhs, const Matrix3d& rhs) 40 | { 41 | Matrix3d temp(lhs); 42 | temp *= rhs; 43 | return temp; 44 | } 45 | 46 | Point3d operator*(const Matrix3d& lhs, const Point3d& rhs); 47 | 48 | Matrix3d createTranslationMatrix(float dx, float dy, float dz); 49 | Matrix3d createScaleMatrix(float sx, float sy, float sz); 50 | Matrix3d createRotationMatrixAboutX(float degrees); 51 | Matrix3d createRotationMatrixAboutY(float degrees); 52 | Matrix3d createRotationMatrixAboutZ(float degrees); 53 | 54 | 55 | -------------------------------------------------------------------------------- /Lesson3/Activity01/point3d.cpp: -------------------------------------------------------------------------------- 1 | ///============================================================================ 2 | // Name : point3d.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Activity 1 Lesson 2 7 | //============================================================================ 8 | #include "point3d.hpp" 9 | 10 | Point3d::Point3d() 11 | { 12 | for(auto& item : m_data) 13 | { 14 | item = 0; 15 | } 16 | m_data[NumberRows-1] = 1; 17 | } 18 | 19 | Point3d::Point3d(std::initializer_list list) 20 | { 21 | m_data[NumberRows-1] = 1; 22 | int i{0}; 23 | for(auto it1 = list.begin(); i 10 | #include 11 | #include 12 | 13 | class Point3d 14 | { 15 | public: 16 | static constexpr size_t NumberRows{4}; 17 | 18 | Point3d(); 19 | Point3d(std::initializer_list list); 20 | 21 | float operator()(const int index) const 22 | { 23 | return m_data[index]; 24 | } 25 | 26 | float& operator()(const int index) 27 | { 28 | return m_data[index]; 29 | } 30 | 31 | bool operator==(const Point3d& rhs) const; 32 | bool operator!=(const Point3d& rhs) const 33 | { 34 | return !operator==(rhs); 35 | } 36 | 37 | 38 | private: 39 | 40 | float m_data[NumberRows]; 41 | 42 | friend std::ostream& operator<<(std::ostream& , const Point3d& ); 43 | }; 44 | 45 | inline std::ostream& 46 | operator<<(std::ostream& os, const Point3d& pt) 47 | { 48 | const char* sep = "[ "; 49 | for(auto value : pt.m_data) 50 | { 51 | os << sep << value; 52 | sep = ", "; 53 | } 54 | os << " ]"; 55 | return os; 56 | } 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /Lesson3/Activity01/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #============================================================================ 2 | # Name : CMakeLists.txt 3 | # Author : Brian Price 4 | # Version : 5 | # Copyright : Your copyright notice 6 | # Description : Lesson 3 Activity 1 tests build file 7 | #============================================================================ 8 | include(GoogleTest) 9 | 10 | SET(SRC_FILES 11 | ../matrix3d.cpp 12 | ../point3d.cpp) 13 | 14 | SET(TEST_FILES 15 | matrix3dTests.cpp 16 | point3dTests.cpp) 17 | 18 | add_executable(L3graphicstests 19 | ${SRC_FILES} 20 | ${TEST_FILES} 21 | ) 22 | 23 | target_link_libraries(L3graphicstests GTest::GTest GTest::Main) 24 | 25 | gtest_discover_tests(L3graphicstests) 26 | -------------------------------------------------------------------------------- /Lesson3/Activity02/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #============================================================================ 2 | # Name : CMakeLists.txt 3 | # Author : Brian Price 4 | # Version : 5 | # Copyright : Your copyright notice 6 | # Description : Lesson 3 Activity 2 build file 7 | #============================================================================ 8 | cmake_minimum_required(VERSION 3.11.0) 9 | 10 | project(Activty02 11 | VERSION 0.1.0.0 12 | DESCRIPTION "Activity 2 for Lesson 3 of Advanced C++ course." 13 | ) 14 | set(Activty02_VENDOR "Brian Price") 15 | set(PROJECT_VENDOR "${CxxSimulator_VENDOR}") 16 | 17 | 18 | set(CMAKE_DEBUG_POSTFIX d) 19 | 20 | set(CMAKE_CXX_STANDARD 17) 21 | set(CMAKE_C_STANDARD 11) 22 | 23 | if(POLICY CMP0074) 24 | cmake_policy(SET CMP0074 NEW) 25 | endif() 26 | 27 | 28 | # ------------------------------------------------------------------ 29 | # Activity 2 30 | # ------------------------------------------------------------------ 31 | add_executable(datetools date.cpp main.cpp) 32 | find_package(GTest) 33 | 34 | enable_testing() 35 | add_subdirectory(tests) 36 | 37 | -------------------------------------------------------------------------------- /Lesson3/Activity02/answers/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #============================================================================ 2 | # Name : CMakeLists.txt 3 | # Author : Brian Price 4 | # Version : 5 | # Copyright : Your copyright notice 6 | # Description : Lesson 3 Activity 2 build file 7 | #============================================================================ 8 | cmake_minimum_required(VERSION 3.11.0) 9 | 10 | project(Activty02 11 | VERSION 0.1.0.0 12 | DESCRIPTION "Activity 2 for Lesson 3 of Advanced C++ course." 13 | ) 14 | set(Activty02_VENDOR "Brian Price") 15 | set(PROJECT_VENDOR "${CxxSimulator_VENDOR}") 16 | 17 | 18 | set(CMAKE_DEBUG_POSTFIX d) 19 | 20 | set(CMAKE_CXX_STANDARD 17) 21 | set(CMAKE_C_STANDARD 11) 22 | 23 | if(POLICY CMP0074) 24 | cmake_policy(SET CMP0074 NEW) 25 | endif() 26 | 27 | 28 | # ------------------------------------------------------------------ 29 | # Activity 2 30 | # ------------------------------------------------------------------ 31 | add_executable(datetools date.cpp main.cpp) 32 | find_package(GTest) 33 | 34 | enable_testing() 35 | add_subdirectory(tests) 36 | 37 | -------------------------------------------------------------------------------- /Lesson3/Activity02/answers/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #============================================================================ 2 | # Name : CMakeLists.txt 3 | # Author : Brian Price 4 | # Version : 5 | # Copyright : Your copyright notice 6 | # Description : Lesson 3 Activity 2 tests build file 7 | #============================================================================ 8 | include(GoogleTest) 9 | add_executable(datetoolstests dateTests.cpp ../date.cpp) 10 | target_link_libraries(datetoolstests GTest::GTest GTest::Main) 11 | 12 | gtest_discover_tests(datetoolstests) 13 | -------------------------------------------------------------------------------- /Lesson3/Activity02/date.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : date.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Activity 2 Lesson 3 7 | //============================================================================ 8 | #include "date.hpp" 9 | 10 | #include 11 | 12 | namespace { 13 | 14 | 15 | } 16 | 17 | namespace acpp::date 18 | { 19 | 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Lesson3/Activity02/date.hpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : date.hpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Activity 2 Lesson 3 7 | //============================================================================ 8 | #pragma once 9 | #include 10 | 11 | namespace acpp::date 12 | { 13 | 14 | 15 | 16 | class Date 17 | { 18 | public: 19 | 20 | private: 21 | int m_day{1}; 22 | int m_month{1}; 23 | int m_year {1970}; 24 | }; 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Lesson3/Activity02/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #============================================================================ 2 | # Name : CMakeLists.txt 3 | # Author : Brian Price 4 | # Version : 5 | # Copyright : Your copyright notice 6 | # Description : Lesson 3 Activity 2 tests build file 7 | #============================================================================ 8 | include(GoogleTest) 9 | add_executable(datetoolstests dateTests.cpp ../date.cpp) 10 | target_link_libraries(datetoolstests GTest::GTest GTest::Main) 11 | 12 | gtest_discover_tests(datetoolstests) 13 | -------------------------------------------------------------------------------- /Lesson3/Activity02/tests/dateTests.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : datetests.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Activity 2 Lesson 3 7 | //============================================================================ 8 | #include "gtest/gtest.h" 9 | #include "../date.hpp" 10 | 11 | using namespace acpp::date; 12 | 13 | class DateTest : public ::testing::Test 14 | { 15 | public: 16 | }; 17 | 18 | TEST_F(DateTest, DefaultConstructor) 19 | { 20 | ASSERT_FALSE(true); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Lesson3/Exercise01/Stack.hpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : Stack.hpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Exercise 9 Lesson 2 7 | //============================================================================ 8 | #pragma once 9 | #include 10 | #include 11 | 12 | #define EXERCISE1_STEP 1 13 | 14 | namespace acpp 15 | { 16 | 17 | template 18 | class Stack 19 | { 20 | public: 21 | using value_type = T; 22 | using reference = value_type&; 23 | using const_reference = const value_type&; 24 | using size_type = std::size_t; 25 | 26 | bool empty() const 27 | { 28 | return m_stack.empty(); 29 | } 30 | 31 | void push(const value_type& value) 32 | { 33 | m_stack.push_back(value); 34 | } 35 | 36 | size_type size() const 37 | { 38 | return m_stack.size(); 39 | } 40 | 41 | void pop() 42 | { 43 | m_stack.pop_back(); 44 | } 45 | 46 | reference top() 47 | { 48 | return m_stack.back(); 49 | } 50 | 51 | const_reference top() const 52 | { 53 | return m_stack.back(); 54 | } 55 | 56 | private: 57 | std::vector m_stack; 58 | }; 59 | 60 | } // namespace acpp 61 | -------------------------------------------------------------------------------- /Lesson3/Exercise01/answers/Stack.hpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : Stack.hpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Exercise 9 Lesson 2 7 | //============================================================================ 8 | #pragma once 9 | #include 10 | #include 11 | #include 12 | 13 | #define EXERCISE1_STEP 12 14 | 15 | namespace acpp 16 | { 17 | 18 | template 19 | class Stack 20 | { 21 | public: 22 | using value_type = T; 23 | using reference = value_type&; 24 | using const_reference = const value_type&; 25 | using size_type = std::size_t; 26 | 27 | bool empty() const 28 | { 29 | return m_stack.empty(); 30 | } 31 | 32 | void push(const value_type& value) 33 | { 34 | m_stack.push_back(value); 35 | } 36 | 37 | size_type size() const 38 | { 39 | return m_stack.size(); 40 | } 41 | 42 | void pop() 43 | { 44 | if(empty()) 45 | throw std::underflow_error("Pop from empty stack"); 46 | m_stack.pop_back(); 47 | } 48 | 49 | reference top() 50 | { 51 | if(empty()) 52 | throw std::underflow_error("Top from empty stack"); 53 | return m_stack.back(); 54 | } 55 | 56 | const_reference top() const 57 | { 58 | if(empty()) 59 | throw std::underflow_error("Top from empty stack"); 60 | return m_stack.back(); 61 | } 62 | 63 | private: 64 | std::vector m_stack; 65 | }; 66 | 67 | } // namespace acpp 68 | -------------------------------------------------------------------------------- /Lesson3/Exercise01/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include(GoogleTest) 2 | add_executable(L3Ex1tests FractionTests.cpp ../Fraction.cpp StackTests.cpp) 3 | target_link_libraries(L3Ex1tests GTest::GTest GTest::Main) 4 | gtest_discover_tests(L3Ex1tests) 5 | -------------------------------------------------------------------------------- /Lesson3/Exercise02/exceptions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void run_exceptions() 5 | { 6 | try 7 | { 8 | throw std::domain_error("We got one!!!!"); 9 | } 10 | catch(...) 11 | { 12 | std::cout << "Exception caught by default handler\n"; 13 | } 14 | catch(const std::exception& e) 15 | { 16 | std::cout << "Exception '" << e.what() << "' caught by std::exception handler\n"; 17 | } 18 | catch(const std::logic_error& e) 19 | { 20 | std::cout << "Exception '" << e.what() << "' caught by std::logic_error handler\n"; 21 | } 22 | catch(const std::domain_error& e) 23 | { 24 | std::cout << "Exception '" << e.what() << "' caught by std::domain_error handler\n"; 25 | } 26 | } 27 | 28 | int main() 29 | { 30 | std::cout << "\n\n------ Exercise 2 ------\n"; 31 | run_exceptions(); 32 | std::cout << "Complete.\n"; 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Lesson3/Exercise03/Exercise3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "monitor.h" 3 | #include 4 | 5 | void LeakPointers() 6 | { 7 | char* memory[5] {}; 8 | for (auto i{0} ; i<5 ; i++) 9 | { 10 | memory[i] = new char[20]; 11 | std::cout << "allocated 20 bytes @ " << (void *)memory[i] << "\n"; 12 | } 13 | } 14 | 15 | void LeakFiles() 16 | { 17 | FILE* fh1{fopen("HelloB1.txt", "w")}; 18 | fprintf(fh1, "Hello B2\n"); 19 | 20 | FILE* fh2{fopen("HelloB2.txt", "w")}; 21 | fprintf(fh2, "Hello B1\n"); 22 | } 23 | 24 | int main() 25 | { 26 | std::cout << "\n\n------ Exercise 3 ------\n"; 27 | Monitor monitor; 28 | LeakPointers(); 29 | LeakFiles(); 30 | std::cout << "------ Complete. -------\n"; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Lesson3/Exercise03/answers/Exercise3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "monitor.h" 3 | #include 4 | 5 | class File { 6 | public: 7 | File(const char* name, const char* access) { 8 | m_file = fopen(name, access); 9 | if (m_file == nullptr) { 10 | throw std::ios_base::failure("failed to open file"); 11 | } 12 | } 13 | 14 | ~File() { 15 | fclose(m_file); 16 | } 17 | 18 | operator FILE*() { 19 | return m_file; 20 | } 21 | private: 22 | FILE* m_file{}; 23 | }; 24 | 25 | class CharPointer 26 | { 27 | public: 28 | void allocate(size_t size) 29 | { 30 | m_memory = new char[size]; 31 | } 32 | 33 | ~CharPointer() 34 | { 35 | delete [] m_memory; 36 | } 37 | 38 | operator char*() { return m_memory;} 39 | 40 | private: 41 | char* m_memory{}; 42 | }; 43 | 44 | void LeakPointers() 45 | { 46 | CharPointer memory[5]; 47 | for (auto i{0} ; i<5 ; i++) 48 | { 49 | memory[i].allocate(20); 50 | std::cout << "allocated 20 bytes @ " << (void *)memory[i] << "\n"; 51 | } 52 | } 53 | 54 | void LeakFiles() 55 | { 56 | File fh1{"HelloB1.txt", "w"}; 57 | fprintf(fh1, "Hello B2\n"); 58 | 59 | File fh2{"HelloB2.txt", "w"}; 60 | fprintf(fh2, "Hello B1\n"); 61 | } 62 | 63 | 64 | int main() 65 | { 66 | std::cout << "\n\n------ Exercise 3 ------\n"; 67 | Monitor monitor; 68 | LeakPointers(); 69 | LeakFiles(); 70 | std::cout << "------ Complete. -------\n"; 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /Lesson3/Exercise03/monitor.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | void init_monitor(); 5 | void dump_monitor(); 6 | 7 | struct Monitor 8 | { 9 | Monitor() { 10 | init_monitor(); 11 | }; 12 | ~Monitor() { 13 | dump_monitor(); 14 | }; 15 | }; 16 | 17 | 18 | FILE* myfopen(const char* filename, const char* mode); 19 | int myfclose(FILE* stream); 20 | 21 | #define fopen myfopen 22 | #define fclose myfclose 23 | -------------------------------------------------------------------------------- /Lesson3/Exercise04/sensors.hpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : sensors.hpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Exercise 4 Lesson 3 7 | //============================================================================ 8 | 9 | #include 10 | 11 | struct ISensor 12 | { 13 | virtual float read() const = 0; 14 | virtual ~ISensor() = default; 15 | }; 16 | 17 | struct LightSensor : ISensor 18 | { 19 | LightSensor() 20 | { 21 | std::cout << "+++Create Light Sensor\n"; 22 | } 23 | ~LightSensor() 24 | { 25 | std::cout << "---Destroy Light Sensor\n"; 26 | } 27 | 28 | float read() const override 29 | { 30 | return 11.0F; 31 | } 32 | }; 33 | 34 | struct TemperatureSensor : ISensor 35 | { 36 | TemperatureSensor() 37 | { 38 | std::cout << "+++Create Temperature Sensor\n"; 39 | } 40 | ~TemperatureSensor() 41 | { 42 | std::cout << "---Destroy Temperature Sensor\n"; 43 | } 44 | 45 | float read() const override 46 | { 47 | return 32.0F; 48 | } 49 | }; 50 | 51 | struct PressureSensor : ISensor 52 | { 53 | PressureSensor() 54 | { 55 | std::cout << "+++Create Pressure Sensor\n"; 56 | } 57 | ~PressureSensor() 58 | { 59 | std::cout << "---Destroy Pressure Sensor\n"; 60 | } 61 | 62 | float read() const override 63 | { 64 | return 56.0F; 65 | } 66 | }; 67 | -------------------------------------------------------------------------------- /Lesson3/Exercise05/Exercise5.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : exercise5.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Exercise 5 Lesson 3 7 | //============================================================================ 8 | #include 9 | #include 10 | #include 11 | 12 | template 13 | bool empty(const T& x) 14 | { 15 | return x.empty(); 16 | } 17 | 18 | template 19 | int size(const T& x) 20 | { 21 | return x.size(); 22 | } 23 | 24 | int main() 25 | { 26 | std::cout << "\n\n------ Exercise 5 ------\n"; 27 | std::vector numbers; 28 | 29 | std::cout << "Before adding numbers...\n"; 30 | std::cout << std::boolalpha; 31 | std::cout << "numbers.empty => " << numbers.empty() << "\n"; 32 | std::cout << "empty(numbers) => " << empty(numbers) << "\n"; 33 | std::cout << "size(numbers) => " << size(numbers) << "\n"; 34 | 35 | numbers.push_back(1); 36 | numbers.push_back(2); 37 | numbers.push_back(3); 38 | numbers.push_back(4); 39 | 40 | std::cout << "After adding numbers...\n"; 41 | std::cout << "numbers.empty => " << numbers.empty() << "\n"; 42 | std::cout << "empty(numbers) => " << empty(numbers) << "\n"; 43 | std::cout << "size(numbers) => " << size(numbers) << "\n"; 44 | 45 | std::cout << "------ Complete. -------\n"; 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Lesson3/Exercise06/Exercise6.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : exercise5.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Exercise 6 Lesson 3 7 | //============================================================================ 8 | #include 9 | 10 | struct Voltage 11 | { 12 | Voltage (float emf) : m_emf(emf) 13 | { 14 | } 15 | 16 | float m_emf; 17 | }; 18 | 19 | void calculate(Voltage v) 20 | { 21 | std::cout << "Calculate for " << v.m_emf << " volts\n"; 22 | } 23 | 24 | void use_float(float value) 25 | { 26 | std::cout << "use_float for " << value << "\n"; 27 | } 28 | 29 | int main() 30 | { 31 | std::cout << "\n\n------ Exercise 6 ------\n"; 32 | Voltage volts(1.0F); 33 | 34 | calculate(volts); 35 | calculate(32); 36 | 37 | use_float(42); 38 | 39 | std::cout << "------ Complete. -------\n"; 40 | return 0; 41 | } 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /Lesson3/readme.md: -------------------------------------------------------------------------------- 1 | # Advanced C++ 2 | Write robust C++ code so fast they will think you cheated -------------------------------------------------------------------------------- /Lesson4/Activity01/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #============================================================================ 2 | # Name : CMakeLists.txt 3 | # Author : Brian Price 4 | # Version : 5 | # Description : Lesson 4 Activity 1 build file 6 | #============================================================================ 7 | cmake_minimum_required(VERSION 3.11.0) 8 | 9 | project(Activty01 10 | VERSION 0.1.0.0 11 | DESCRIPTION "Activity 1 for Lesson 4 of Advanced C++ course." 12 | ) 13 | 14 | 15 | set(CMAKE_DEBUG_POSTFIX d) 16 | 17 | set(CMAKE_CXX_STANDARD 17) 18 | set(CMAKE_C_STANDARD 11) 19 | 20 | if(POLICY CMP0074) 21 | cmake_policy(SET CMP0074 NEW) 22 | endif() 23 | 24 | # ------------------------------------------------------------------ 25 | # Activity 1 26 | # ------------------------------------------------------------------ 27 | add_executable(L4delegate main.cpp) 28 | find_package(GTest) 29 | 30 | enable_testing() 31 | add_subdirectory(tests) 32 | 33 | 34 | -------------------------------------------------------------------------------- /Lesson4/Activity01/answers/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #============================================================================ 2 | # Name : CMakeLists.txt 3 | # Author : Brian Price 4 | # Version : 5 | # Description : Lesson 4 Activity 1 build file 6 | #============================================================================ 7 | cmake_minimum_required(VERSION 3.11.0) 8 | 9 | project(Activty01 10 | VERSION 0.1.0.0 11 | DESCRIPTION "Activity 1 for Lesson 4 of Advanced C++ course." 12 | ) 13 | 14 | 15 | set(CMAKE_DEBUG_POSTFIX d) 16 | 17 | set(CMAKE_CXX_STANDARD 17) 18 | set(CMAKE_C_STANDARD 11) 19 | 20 | if(POLICY CMP0074) 21 | cmake_policy(SET CMP0074 NEW) 22 | endif() 23 | 24 | # ------------------------------------------------------------------ 25 | # Activity 1 26 | # ------------------------------------------------------------------ 27 | add_executable(L4delegate main.cpp) 28 | find_package(GTest) 29 | 30 | enable_testing() 31 | add_subdirectory(tests) 32 | 33 | 34 | -------------------------------------------------------------------------------- /Lesson4/Activity01/answers/delegate.hpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : delegate.hpp 3 | // Author : Brian Price 4 | // Version : 5 | // Description : Activity 1 Lesson 4 6 | //============================================================================ 7 | #pragma once 8 | 9 | #include 10 | #include 11 | 12 | template 13 | class Delegate 14 | { 15 | public: 16 | Delegate() = default; 17 | 18 | Delegate& operator+=(const std::function& delegate) 19 | { 20 | m_delegates.push_back(delegate); 21 | return *this; 22 | } 23 | 24 | void Notify(ArgTypes&&... args) const 25 | { 26 | for(auto& delegate : m_delegates) 27 | { 28 | delegate(std::forward(args)...); 29 | } 30 | } 31 | 32 | void operator()(ArgTypes&&... args) 33 | { 34 | Notify(std::forward(args)...); 35 | } 36 | private: 37 | std::vector> m_delegates; 38 | }; 39 | 40 | -------------------------------------------------------------------------------- /Lesson4/Activity01/answers/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #============================================================================ 2 | # Name : CMakeLists.txt 3 | # Author : Brian Price 4 | # Version : 5 | # Description : Lesson 4 Activity 1 tests build file 6 | #============================================================================ 7 | include(GoogleTest) 8 | 9 | SET(TEST_FILES delegateTests.cpp) 10 | 11 | add_executable(L4delegatesTests 12 | ${TEST_FILES} 13 | ) 14 | 15 | target_link_libraries(L4delegatesTests GTest::GTest GTest::Main) 16 | 17 | gtest_discover_tests(L4delegatesTests) 18 | -------------------------------------------------------------------------------- /Lesson4/Activity01/delegate.hpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : delegate.hpp 3 | // Author : Brian Price 4 | // Version : 5 | // Description : Activity 1 Lesson 4 6 | //============================================================================ 7 | #pragma once 8 | 9 | #include 10 | 11 | class Delegate 12 | { 13 | public: 14 | Delegate() = default; 15 | 16 | }; 17 | -------------------------------------------------------------------------------- /Lesson4/Activity01/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #============================================================================ 2 | # Name : CMakeLists.txt 3 | # Author : Brian Price 4 | # Version : 5 | # Description : Lesson 4 Activity 1 tests build file 6 | #============================================================================ 7 | include(GoogleTest) 8 | 9 | SET(TEST_FILES delegateTests.cpp) 10 | 11 | add_executable(L4delegatesTests 12 | ${TEST_FILES} 13 | ) 14 | 15 | target_link_libraries(L4delegatesTests GTest::GTest GTest::Main) 16 | 17 | gtest_discover_tests(L4delegatesTests) 18 | -------------------------------------------------------------------------------- /Lesson4/Activity01/tests/delegateTests.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : matrix3dTests.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Activity 1 Lesson 2 7 | //============================================================================ 8 | #include "gtest/gtest.h" 9 | #include "../delegate.hpp" 10 | 11 | 12 | class DelegateTest : public ::testing::Test 13 | { 14 | public: 15 | 16 | }; 17 | 18 | TEST_F(DelegateTest, DefaultConstructorIsIdentity) 19 | { 20 | ASSERT_FALSE(true); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Lesson4/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #============================================================================ 2 | # Name : CMakeLists.txt 3 | # Author : Brian Price 4 | # Version : 5 | # Description : Lesson 4 build file 6 | #============================================================================ 7 | cmake_minimum_required(VERSION 3.11.0) 8 | 9 | project(Lesson4 10 | VERSION 0.1.0.0 11 | DESCRIPTION "Lesson 4 of Advanced C++ course." 12 | ) 13 | 14 | set(CMAKE_DEBUG_POSTFIX d) 15 | set(CMAKE_CXX_STANDARD 17) 16 | set(CMAKE_C_STANDARD 11) 17 | 18 | 19 | if(POLICY CMP0074) 20 | cmake_policy(SET CMP0074 NEW) 21 | endif() 22 | 23 | # ------------------------------------------------------------------ 24 | # Exercise 1 25 | # ------------------------------------------------------------------ 26 | add_executable(Exercise1 Exercise01/Exercise1.cpp Exercise01/kitchen.cpp) 27 | 28 | # ------------------------------------------------------------------ 29 | # Exercise 2 30 | # ------------------------------------------------------------------ 31 | add_executable(Exercise2 Exercise02/Exercise2.cpp) 32 | 33 | # ------------------------------------------------------------------ 34 | # Exercise 3 35 | # ------------------------------------------------------------------ 36 | add_executable(Exercise3 Exercise03/Exercise3.cpp) 37 | 38 | # ------------------------------------------------------------------ 39 | # Exercise 4 40 | # ------------------------------------------------------------------ 41 | add_executable(Exercise4 Exercise04/Exercise4.cpp) 42 | 43 | -------------------------------------------------------------------------------- /Lesson4/Exercise01/Exercise1.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : Exercise1.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Description : Exercise 1 Lesson 4 6 | //============================================================================ 7 | #include 8 | #include "kitchen.hpp" 9 | #include 10 | #include 11 | 12 | 13 | int main(int argc, char**argv) 14 | { 15 | std::cout << "\n------ Kitchen Exercise 1 ------\n"; 16 | Kitchen kitchen("TheChef"); 17 | 18 | std::vector orders {"Lamb Ragout", "Baked Alaska", "Turkey", "Steak Diane", "Pavlova"}; 19 | 20 | for(auto order : orders) 21 | { 22 | try 23 | { 24 | std::cout << kitchen.processOrder(order); 25 | } 26 | catch(std::exception& ex) 27 | { 28 | std::cout << ex.what() << "\n"; 29 | } 30 | } 31 | 32 | std::cout << "Complete.\n"; 33 | return 0; 34 | } 35 | 36 | 37 | -------------------------------------------------------------------------------- /Lesson4/Exercise01/dessert.hpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : dessert.hpp 3 | // Author : Brian Price 4 | // Version : 5 | // Description : Exercise 1 Lesson 4 6 | //============================================================================ 7 | #pragma once 8 | #include 9 | 10 | class Dessert 11 | { 12 | public: 13 | Dessert(std::string name) : m_name {name} {}; 14 | std::string Serve() 15 | { 16 | std::string serve{"---Serving "}; 17 | serve += m_name; 18 | return serve; 19 | }; 20 | 21 | std::string GetName() const 22 | { 23 | return m_name; 24 | } 25 | 26 | 27 | private: 28 | std::string m_name; 29 | }; 30 | -------------------------------------------------------------------------------- /Lesson4/Exercise01/kitchen.hpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : kitchen.hpp 3 | // Author : Brian Price 4 | // Version : 5 | // Description : Exercise 1 Lesson 4 6 | //============================================================================ 7 | #pragma once 8 | #include 9 | #include 10 | 11 | #include "recipe.hpp" 12 | #include "dessert.hpp" 13 | 14 | class Kitchen 15 | { 16 | public: 17 | Kitchen(std::string chef); 18 | std::string processOrder(std::string order); 19 | 20 | private: 21 | std::string searchForRecipe(std::string order); 22 | std::string searchForDessert(std::string order); 23 | std::string cookRecipe(std::string recipe); 24 | std::string serveDessert(std::string dessert); 25 | 26 | std::vector::iterator getRecipe(std::string recipe); 27 | std::vector::iterator getDessert(std::string recipe); 28 | 29 | std::string m_chef; 30 | std::vector m_recipes; 31 | std::vector m_desserts; 32 | 33 | }; 34 | -------------------------------------------------------------------------------- /Lesson4/Exercise01/recipe.hpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : recipe.hpp 3 | // Author : Brian Price 4 | // Version : 5 | // Description : Exercise 1 Lesson 4 6 | //============================================================================ 7 | #pragma once 8 | #include 9 | 10 | class Recipe 11 | { 12 | public: 13 | Recipe(std::string name) : m_name{name} {}; 14 | 15 | std::string Prepare() 16 | { 17 | std::string serve{"...Preparing "}; 18 | serve += m_name; 19 | return serve; 20 | }; 21 | 22 | std::string Cook() 23 | { 24 | std::string serve{"...Cooking "}; 25 | serve += m_name; 26 | return serve; 27 | }; 28 | 29 | std::string Serve() 30 | { 31 | std::string serve{"...Serving "}; 32 | serve += m_name; 33 | return serve; 34 | }; 35 | 36 | std::string GetName() const 37 | { 38 | return m_name; 39 | } 40 | 41 | private: 42 | std::string m_name; 43 | }; 44 | -------------------------------------------------------------------------------- /Lesson4/Exercise02/Exercise2.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : Exercise2.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Description : Exercise 2 Lesson 4 6 | //============================================================================ 7 | #include 8 | #include 9 | #include 10 | 11 | int main(int argc, char**argv) 12 | { 13 | std::cout << "\n------ Exercise 2 ------\n"; 14 | std::vector values {1,2,3,4,5,6,7,8,9,10}; 15 | 16 | std::cout << "Average of ["; 17 | for( auto n : values ) 18 | std::cout << n << ", "; 19 | std::cout << "] = "; 20 | 21 | std::cout << "??\n"; 22 | std::cout << "Complete.\n"; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Lesson4/Exercise03/Exercise3.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : Exercise3.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Description : Exercise 3 Lesson 4 6 | //============================================================================ 7 | #include 8 | #include 9 | 10 | using MyFunction = std::function; 11 | 12 | void TestFunctionTemplate() 13 | { 14 | std::cout << "+++ Entering TestFunctionTemplate()\n"; 15 | MyFunction func; 16 | try 17 | { 18 | func(42); 19 | } 20 | catch(std::bad_function_call& e) 21 | { 22 | std::cout << "exception: " << e.what() << "\n"; 23 | } 24 | std::cout << "+++ Leaving TestFunctionTemplate()\n"; 25 | } 26 | 27 | 28 | int main(int argc, char**argv) 29 | { 30 | std::cout << "\n------ Exercise 3 ------\n"; 31 | TestFunctionTemplate(); 32 | std::cout << "Complete.\n"; 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Lesson4/Exercise04/Exercise4.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : Exercise4.cpp 3 | // Author : Brian Price 4 | // Version : 5 | // Description : Exercise 4 Lesson 4 6 | //============================================================================ 7 | #include 8 | #include 9 | #include 10 | 11 | void PrintVector(const char* prefix, std::vector& values) 12 | { 13 | const char* sep = ""; 14 | std::cout << prefix << " = ["; 15 | for(auto n : values) 16 | { 17 | std::cout << sep << n; 18 | sep = ", "; 19 | } 20 | std::cout << "]\n"; 21 | } 22 | 23 | int main(int argc, char**argv) 24 | { 25 | std::cout << "\n------ Exercise 4 ------\n"; 26 | std::vector values{ 46, 27, 26, 27, 32, 1, 18, 50, 21, 49 }; 27 | 28 | PrintVector("Initial Vector", values); 29 | 30 | std::cout << "Complete.\n"; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Lesson4/readme.md: -------------------------------------------------------------------------------- 1 | # Advanced C++ 2 | Write robust C++ code so fast they will think you cheated -------------------------------------------------------------------------------- /Lesson5/Activity01/Common.hpp: -------------------------------------------------------------------------------- 1 | #ifndef COMMON_HPP 2 | #define COMMON_HPP 3 | constexpr size_t CountPeopleInside = 5; 4 | #endif // COMMON_HPP 5 | -------------------------------------------------------------------------------- /Lesson5/Activity01/Person.cpp: -------------------------------------------------------------------------------- 1 | #include "Person.hpp" 2 | #include 3 | 4 | int Person::m_NextId; 5 | 6 | Person::Person() 7 | : m_Id(m_NextId++) 8 | { 9 | std::cout << "Person | Person | Hello! I'm " << m_Id << std::endl; 10 | } 11 | 12 | Person& Person::operator=(Person&& other) 13 | { 14 | m_Id = other.m_Id; 15 | return *this; 16 | } 17 | 18 | Person::Person(Person&& other) 19 | : m_Id(other.m_Id) 20 | { 21 | } 22 | -------------------------------------------------------------------------------- /Lesson5/Activity01/Person.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PERSON_HPP 2 | #define PERSON_HPP 3 | 4 | class Person 5 | { 6 | public: 7 | Person(); 8 | Person& operator=(Person&&); 9 | Person(Person&&); 10 | ~Person() = default; 11 | 12 | Person(const Person&) = delete; 13 | Person& operator=(const Person&) = delete; 14 | 15 | private: 16 | int m_Id; 17 | static int m_NextId; 18 | }; 19 | 20 | #endif // PERSON_HPP 21 | -------------------------------------------------------------------------------- /Lesson5/Activity01/PersonGenerator.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PERSON_GENERATOR_HPP 2 | #define PERSON_GENERATOR_HPP 3 | 4 | #include "Persons.hpp" 5 | #include 6 | #include 7 | 8 | class PersonGenerator 9 | { 10 | public: 11 | PersonGenerator(Persons& persons, 12 | std::condition_variable& add_person, 13 | std::condition_variable& remove_person, 14 | std::mutex& add_lock, 15 | std::mutex& remove_lock, 16 | bool& addNotified, 17 | bool& removeNotified); 18 | 19 | ~PersonGenerator(); 20 | 21 | PersonGenerator(const PersonGenerator&) = delete; 22 | PersonGenerator(PersonGenerator&&) = delete; 23 | PersonGenerator& operator=(const PersonGenerator&) = delete; 24 | PersonGenerator& operator=(PersonGenerator&&) = delete; 25 | 26 | private: 27 | void runCreating(); 28 | void runRemoving(); 29 | 30 | void notifyCreated(); 31 | void notifyRemoved(); 32 | 33 | private: 34 | std::thread m_CreateThread; 35 | std::thread m_RemoveThread; 36 | 37 | Persons& m_CreatedPersons; 38 | 39 | // to notify about creating new person 40 | std::condition_variable& m_CondVarAddPerson; 41 | std::mutex& m_AddLock; 42 | bool& m_AddNotified; 43 | 44 | // to notify that person needs to be removed 45 | std::condition_variable& m_CondVarRemovePerson; 46 | std::mutex& m_RemoveLock; 47 | bool& m_RemoveNotified; 48 | }; 49 | 50 | #endif // PERSON_GENERATOR_HPP 51 | -------------------------------------------------------------------------------- /Lesson5/Activity01/Persons.cpp: -------------------------------------------------------------------------------- 1 | #include "Persons.hpp" 2 | #include "Common.hpp" 3 | #include 4 | 5 | Persons::Persons() 6 | { 7 | m_Persons.reserve(CountPeopleInside); 8 | } 9 | 10 | void Persons::add(Person&& person) 11 | { 12 | std::lock_guard m_lock(m_Mutex); 13 | m_Persons.emplace_back(std::move(person)); 14 | } 15 | 16 | Person Persons::get() 17 | { 18 | std::lock_guard m_lock(m_Mutex); 19 | if (m_Persons.empty()) 20 | { 21 | throw "Empty Persons storage"; 22 | } 23 | 24 | Person result = std::move(m_Persons.back()); 25 | m_Persons.pop_back(); 26 | return result; 27 | } 28 | 29 | size_t Persons::size() const 30 | { 31 | return m_Persons.size(); 32 | } 33 | 34 | void Persons::removePerson() 35 | { 36 | std::lock_guard m_lock(m_Mutex); 37 | m_Persons.pop_back(); 38 | std::cout << "Persons | removePerson | removed" << std::endl; 39 | } 40 | -------------------------------------------------------------------------------- /Lesson5/Activity01/Persons.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PERSONS_HPP 2 | #define PERSONS_HPP 3 | 4 | #include "Person.hpp" 5 | #include 6 | #include 7 | 8 | class Persons 9 | { 10 | public: 11 | Persons(); 12 | ~Persons() = default; 13 | 14 | void add(Person&& person); 15 | Person get(); 16 | 17 | size_t size() const; 18 | void removePerson(); 19 | 20 | private: 21 | std::mutex m_Mutex; 22 | std::vector m_Persons; 23 | }; 24 | 25 | #endif // PERSONS_HPP 26 | -------------------------------------------------------------------------------- /Lesson5/Activity01/Simulator.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : Simulator.cpp 3 | // Author : Olena Lizina 4 | // Description : The Art Gallery work simulator 5 | //============================================================================ 6 | 7 | #include 8 | #include 9 | 10 | #include "Persons.hpp" 11 | #include "Watchman.hpp" 12 | #include "PersonGenerator.hpp" 13 | 14 | int main() 15 | { 16 | { 17 | std::condition_variable g_CondVarRemovePerson; 18 | std::condition_variable g_CondVarAddPerson; 19 | 20 | std::mutex g_AddMux; 21 | std::mutex g_RemoveMux; 22 | 23 | bool g_AddNotified = false;; 24 | bool g_RemoveNotified = false; 25 | 26 | Persons g_PeopleInside; 27 | Persons g_PeopleInQueue; 28 | Persons g_CreatedPersons; 29 | 30 | PersonGenerator generator(g_CreatedPersons, g_CondVarAddPerson, g_CondVarRemovePerson, 31 | g_AddMux, g_RemoveMux, g_AddNotified, g_RemoveNotified); 32 | 33 | Watchman watchman(g_CondVarAddPerson, 34 | g_CondVarRemovePerson, 35 | g_AddMux, 36 | g_RemoveMux, 37 | g_AddNotified, 38 | g_RemoveNotified, 39 | g_PeopleInside, 40 | g_PeopleInQueue, 41 | g_CreatedPersons); 42 | } 43 | 44 | char a; 45 | std::cin >> a; 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Lesson5/Activity01/Watchman.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WATCHMAN_HPP 2 | #define WATCHMAN_HPP 3 | 4 | #include 5 | #include 6 | #include 7 | #include "Persons.hpp" 8 | 9 | class Watchman 10 | { 11 | public: 12 | Watchman(std::condition_variable&, 13 | std::condition_variable&, 14 | std::mutex&, 15 | std::mutex&, 16 | bool&, 17 | bool&, 18 | Persons&, 19 | Persons&, 20 | Persons&); 21 | 22 | ~Watchman(); 23 | 24 | Watchman(const Watchman&) = delete; 25 | Watchman(Watchman&&) = delete; 26 | Watchman& operator=(const Watchman&) = delete; 27 | Watchman& operator=(Watchman&&) = delete; 28 | 29 | private: 30 | void runAdd(); 31 | void runRemove(); 32 | 33 | private: 34 | std::thread m_ThreadAdd; 35 | std::thread m_ThreadRemove; 36 | 37 | std::condition_variable& m_CondVarRemovePerson; 38 | std::condition_variable& m_CondVarAddPerson; 39 | 40 | std::mutex& m_AddMux; 41 | std::mutex& m_RemoveMux; 42 | 43 | bool& m_AddNotified; 44 | bool& m_RemoveNotified; 45 | 46 | Persons& m_PeopleInside; 47 | Persons& m_PeopleInQueue; 48 | Persons& m_CreatedPeople; 49 | }; 50 | 51 | #endif // WATCHMAN_HPP 52 | -------------------------------------------------------------------------------- /Lesson5/Exercise02/Exercise2.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : chapter5.cpp 3 | // Author : Olena Lizina 4 | // Version : 5 | // Copyright : Your copyright notice 6 | // Description : Exercise 2 Chapter 5 7 | //============================================================================ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | void divide(int& divisor, int& dividend) 14 | { 15 | if (0 != dividend) 16 | { 17 | std::cout << "Child thread goes sleep" << std::endl; 18 | 19 | using namespace std::chrono_literals; 20 | std::this_thread::sleep_for(2s); 21 | 22 | std::cout << "Child thread woke up" << std::endl; 23 | 24 | std::cout << "Dividend = " << dividend << std::endl; 25 | std::cout << (divisor / dividend) << std::endl; 26 | } 27 | else 28 | { 29 | std::cout << "Error: dividend = 0" << std::endl; 30 | } 31 | } 32 | 33 | int main() 34 | { 35 | int divisor = 15; 36 | int dividend = 5; 37 | 38 | std::thread thr1(divide, std::ref(divisor), std::ref(dividend)); 39 | thr1.detach(); 40 | 41 | std::cout << "Main thread goes sleep" << std::endl; 42 | 43 | using namespace std::chrono_literals; 44 | std::this_thread::sleep_for(1s); 45 | 46 | std::cout << "Main thread woke up" << std::endl; 47 | dividend = 0; 48 | std::cout << "Main thread set dividend to 0" << std::endl; 49 | 50 | std::getchar(); 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Lesson5/Exercise03/Exercise3.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : chapter5.cpp 3 | // Author : Olena Lizina 4 | // Description : Exercise 3 Chapter 5 5 | //============================================================================ 6 | 7 | #include 8 | #include 9 | 10 | class Handler 11 | { 12 | public: 13 | 14 | Handler() 15 | { 16 | std::cout << "Handler()" << std::endl; 17 | } 18 | Handler(const Handler&) 19 | { 20 | std::cout << "Handler(const Handler&)" << std::endl; 21 | } 22 | Handler& operator=(const Handler&) 23 | { 24 | std::cout << "Handler& operator=(const Handler&)" << std::endl; 25 | return *this; 26 | } 27 | Handler(Handler && obj) 28 | { 29 | std::cout << "Handler(Handler && obj)" << std::endl; 30 | } 31 | Handler & operator=(Handler && obj) 32 | { 33 | std::cout << "Handler & operator=(Handler && obj)" << std::endl; 34 | return *this; 35 | } 36 | ~Handler() 37 | { 38 | std::cout << "~Handler()" << std::endl; 39 | } 40 | }; 41 | 42 | void doSomeJob(Handler&& h) 43 | { 44 | std::cout << "I'm here" << std::endl; 45 | } 46 | 47 | int main() 48 | { 49 | Handler handler; 50 | std::thread thr1(doSomeJob, std::move(handler)); 51 | 52 | thr1.detach(); 53 | using namespace std::chrono_literals; 54 | std::this_thread::sleep_for(5s); 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Lesson5/readme.md: -------------------------------------------------------------------------------- 1 | # Advanced C++ 2 | Write robust C++ code so fast they will think you cheated 3 | -------------------------------------------------------------------------------- /Lesson6/Activity01/Common.hpp: -------------------------------------------------------------------------------- 1 | #ifndef COMMON_HPP 2 | #define COMMON_HPP 3 | constexpr size_t CountPeopleInside = 5; 4 | #endif // COMMON_HPP 5 | -------------------------------------------------------------------------------- /Lesson6/Activity01/Person.cpp: -------------------------------------------------------------------------------- 1 | #include "Person.hpp" 2 | #include "logger/Logger.hpp" 3 | #include 4 | 5 | int Person::m_NextId; 6 | 7 | Person::Person() 8 | : m_Id(m_NextId++) 9 | { 10 | LOG_TRACE() << "Hello! I'm " << m_Id; 11 | } 12 | 13 | Person& Person::operator=(Person&& other) 14 | { 15 | m_Id = other.m_Id; 16 | return *this; 17 | } 18 | 19 | Person::Person(Person&& other) 20 | : m_Id(other.m_Id) 21 | { 22 | } 23 | -------------------------------------------------------------------------------- /Lesson6/Activity01/Person.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PERSON_HPP 2 | #define PERSON_HPP 3 | 4 | class Person 5 | { 6 | public: 7 | Person(); 8 | Person& operator=(Person&&); 9 | Person(Person&&); 10 | ~Person() = default; 11 | 12 | Person(const Person&) = delete; 13 | Person& operator=(const Person&) = delete; 14 | 15 | private: 16 | int m_Id; 17 | static int m_NextId; 18 | }; 19 | 20 | #endif // PERSON_HPP 21 | -------------------------------------------------------------------------------- /Lesson6/Activity01/PersonGenerator.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PERSON_GENERATOR_HPP 2 | #define PERSON_GENERATOR_HPP 3 | 4 | #include "Persons.hpp" 5 | #include 6 | #include 7 | 8 | class PersonGenerator 9 | { 10 | public: 11 | PersonGenerator(Persons& persons, 12 | std::condition_variable& add_person, 13 | std::condition_variable& remove_person, 14 | std::mutex& add_lock, 15 | std::mutex& remove_lock, 16 | bool& addNotified, 17 | bool& removeNotified); 18 | 19 | ~PersonGenerator(); 20 | 21 | PersonGenerator(const PersonGenerator&) = delete; 22 | PersonGenerator(PersonGenerator&&) = delete; 23 | PersonGenerator& operator=(const PersonGenerator&) = delete; 24 | PersonGenerator& operator=(PersonGenerator&&) = delete; 25 | 26 | private: 27 | void runCreating(); 28 | void runRemoving(); 29 | 30 | void notifyCreated(); 31 | void notifyRemoved(); 32 | 33 | private: 34 | std::thread m_CreateThread; 35 | std::thread m_RemoveThread; 36 | 37 | Persons& m_CreatedPersons; 38 | 39 | // to notify about creating new person 40 | std::condition_variable& m_CondVarAddPerson; 41 | std::mutex& m_AddLock; 42 | bool& m_AddNotified; 43 | 44 | // to notify that person needs to be removed 45 | std::condition_variable& m_CondVarRemovePerson; 46 | std::mutex& m_RemoveLock; 47 | bool& m_RemoveNotified; 48 | }; 49 | 50 | #endif // PERSON_GENERATOR_HPP 51 | -------------------------------------------------------------------------------- /Lesson6/Activity01/Persons.cpp: -------------------------------------------------------------------------------- 1 | #include "Persons.hpp" 2 | #include "logger/Logger.hpp" 3 | #include "Common.hpp" 4 | #include 5 | 6 | Persons::Persons() 7 | { 8 | m_Persons.reserve(CountPeopleInside); 9 | } 10 | 11 | void Persons::add(Person&& person) 12 | { 13 | std::lock_guard m_lock(m_Mutex); 14 | m_Persons.emplace_back(std::move(person)); 15 | LOG_DEBUG() << "Count of persons: " << m_Persons.size(); 16 | } 17 | 18 | Person Persons::get() 19 | { 20 | std::lock_guard m_lock(m_Mutex); 21 | if (m_Persons.empty()) 22 | { 23 | throw "Empty Persons storage"; 24 | } 25 | 26 | Person result = std::move(m_Persons.back()); 27 | m_Persons.pop_back(); 28 | return result; 29 | } 30 | 31 | size_t Persons::size() const 32 | { 33 | return m_Persons.size(); 34 | } 35 | 36 | void Persons::removePerson() 37 | { 38 | std::lock_guard m_lock(m_Mutex); 39 | m_Persons.pop_back(); 40 | LOG_INFO() << "A person has been removed"; 41 | } 42 | -------------------------------------------------------------------------------- /Lesson6/Activity01/Persons.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PERSONS_HPP 2 | #define PERSONS_HPP 3 | 4 | #include "Person.hpp" 5 | #include 6 | #include 7 | 8 | class Persons 9 | { 10 | public: 11 | Persons(); 12 | ~Persons() = default; 13 | 14 | void add(Person&& person); 15 | Person get(); 16 | 17 | size_t size() const; 18 | void removePerson(); 19 | 20 | private: 21 | std::mutex m_Mutex; 22 | std::vector m_Persons; 23 | }; 24 | 25 | #endif // PERSONS_HPP 26 | -------------------------------------------------------------------------------- /Lesson6/Activity01/Simulator.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : Simulator.cpp 3 | // Author : Olena Lizina 4 | // Description : The Art Gallery work simulator 5 | //============================================================================ 6 | 7 | #include 8 | #include 9 | 10 | #include "Persons.hpp" 11 | #include "Watchman.hpp" 12 | #include "PersonGenerator.hpp" 13 | #include "logger/Logger.hpp" 14 | 15 | 16 | int main() 17 | { 18 | { 19 | std::condition_variable g_CondVarRemovePerson; 20 | std::condition_variable g_CondVarAddPerson; 21 | 22 | std::mutex g_AddMux; 23 | std::mutex g_RemoveMux; 24 | 25 | bool g_AddNotified = false;; 26 | bool g_RemoveNotified = false; 27 | 28 | Persons g_PeopleInside; 29 | Persons g_PeopleInQueue; 30 | Persons g_CreatedPersons; 31 | 32 | PersonGenerator generator(g_CreatedPersons, g_CondVarAddPerson, g_CondVarRemovePerson, 33 | g_AddMux, g_RemoveMux, g_AddNotified, g_RemoveNotified); 34 | 35 | Watchman watchman(g_CondVarAddPerson, 36 | g_CondVarRemovePerson, 37 | g_AddMux, 38 | g_RemoveMux, 39 | g_AddNotified, 40 | g_RemoveNotified, 41 | g_PeopleInside, 42 | g_PeopleInQueue, 43 | g_CreatedPersons); 44 | } 45 | 46 | char a; 47 | std::cin >> a; 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Lesson6/Activity01/Watchman.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WATCHMAN_HPP 2 | #define WATCHMAN_HPP 3 | 4 | #include 5 | #include 6 | #include 7 | #include "Persons.hpp" 8 | 9 | class Watchman 10 | { 11 | public: 12 | Watchman(std::condition_variable&, 13 | std::condition_variable&, 14 | std::mutex&, 15 | std::mutex&, 16 | bool&, 17 | bool&, 18 | Persons&, 19 | Persons&, 20 | Persons&); 21 | 22 | ~Watchman(); 23 | 24 | Watchman(const Watchman&) = delete; 25 | Watchman(Watchman&&) = delete; 26 | Watchman& operator=(const Watchman&) = delete; 27 | Watchman& operator=(Watchman&&) = delete; 28 | 29 | private: 30 | void runAdd(); 31 | void runRemove(); 32 | 33 | private: 34 | std::thread m_ThreadAdd; 35 | std::thread m_ThreadRemove; 36 | 37 | std::condition_variable& m_CondVarRemovePerson; 38 | std::condition_variable& m_CondVarAddPerson; 39 | 40 | std::mutex& m_AddMux; 41 | std::mutex& m_RemoveMux; 42 | 43 | bool& m_AddNotified; 44 | bool& m_RemoveNotified; 45 | 46 | Persons& m_PeopleInside; 47 | Persons& m_PeopleInQueue; 48 | Persons& m_CreatedPeople; 49 | }; 50 | 51 | #endif // WATCHMAN_HPP 52 | -------------------------------------------------------------------------------- /Lesson6/Activity01/logger/Logger.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TrainingByPackt/Advanced-CPlusPlus/b2ba3fc955b1dc0c617b0b2543fc82ad36573ff2/Lesson6/Activity01/logger/Logger.hpp -------------------------------------------------------------------------------- /Lesson6/Activity01/logger/LoggerMacroses.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TrainingByPackt/Advanced-CPlusPlus/b2ba3fc955b1dc0c617b0b2543fc82ad36573ff2/Lesson6/Activity01/logger/LoggerMacroses.hpp -------------------------------------------------------------------------------- /Lesson6/Activity01/logger/LoggerUtils.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TrainingByPackt/Advanced-CPlusPlus/b2ba3fc955b1dc0c617b0b2543fc82ad36573ff2/Lesson6/Activity01/logger/LoggerUtils.cpp -------------------------------------------------------------------------------- /Lesson6/Activity01/logger/LoggerUtils.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TrainingByPackt/Advanced-CPlusPlus/b2ba3fc955b1dc0c617b0b2543fc82ad36573ff2/Lesson6/Activity01/logger/LoggerUtils.hpp -------------------------------------------------------------------------------- /Lesson6/Activity01/logger/StreamLogger.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TrainingByPackt/Advanced-CPlusPlus/b2ba3fc955b1dc0c617b0b2543fc82ad36573ff2/Lesson6/Activity01/logger/StreamLogger.cpp -------------------------------------------------------------------------------- /Lesson6/Activity01/logger/StreamLogger.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TrainingByPackt/Advanced-CPlusPlus/b2ba3fc955b1dc0c617b0b2543fc82ad36573ff2/Lesson6/Activity01/logger/StreamLogger.hpp -------------------------------------------------------------------------------- /Lesson6/Exercise03/Exercise3.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : Exercise3.cpp 3 | // Author : Olena Lizina 4 | // Description : Exercise 3 5 | //============================================================================ 6 | 7 | #include 8 | #include 9 | 10 | class Replacer 11 | { 12 | public: 13 | std::string operator()(const std::string& originalString, 14 | const std::string& wordToBeReplaced, 15 | const std::string& wordReplaceBy) 16 | { 17 | std::istringstream iss(originalString); 18 | std::ostringstream oss; 19 | 20 | std::string word; 21 | while (iss >> word) 22 | { 23 | if (0 == word.compare(wordToBeReplaced)) 24 | { 25 | oss << wordReplaceBy << " "; 26 | } 27 | else 28 | { 29 | oss << word << " "; 30 | } 31 | } 32 | 33 | return oss.str(); 34 | } 35 | }; 36 | 37 | int main(int argc, char **argv) 38 | { 39 | Replacer worker; 40 | 41 | std::string foodList("coffee tomatoes coffee cucumbers sugar"); 42 | std::string changedList(worker(foodList, "coffee", "chocolate")); 43 | 44 | std::cout << changedList; 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Lesson6/Exercise09/Exercise9.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================ 2 | // Name : Exercise9.cpp 3 | // Author : Olena Lizina 4 | // Description : Exercise 9 5 | //============================================================================ 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | class mtcout : public std::ostringstream 12 | { 13 | public: 14 | mtcout() = default; 15 | ~mtcout() 16 | { 17 | std::lock_guard lock(m_mux); 18 | std::cout << std::this_thread::get_id() << " " << this->str(); 19 | } 20 | private: 21 | static std::mutex m_mux; 22 | }; 23 | 24 | std::mutex mtcout::m_mux; 25 | 26 | int main(int argc, char* argv[]) 27 | { 28 | auto func = [](const std::string msg) 29 | { 30 | using namespace std::chrono_literals; 31 | for (int i = 0; i < 1000; ++i) 32 | { 33 | // mtcout{} << msg << std::endl; 34 | std::cout << std::this_thread::get_id() << " " << msg << std::endl; 35 | } 36 | }; 37 | 38 | std::thread thr1(func, "111111111"); 39 | std::thread thr2(func, "222222222"); 40 | std::thread thr3(func, "333333333"); 41 | std::thread thr4(func, "444444444"); 42 | 43 | thr1.join(); 44 | thr2.join(); 45 | thr3.join(); 46 | thr4.join(); 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Lesson6/readme.md: -------------------------------------------------------------------------------- 1 | # Advanced C++ 2 | Write robust C++ code so fast they will think you cheated 3 | -------------------------------------------------------------------------------- /Lesson7/Activity01/CurrencyConversion.txt: -------------------------------------------------------------------------------- 1 | Currency | ConversionRatio 2 | USD | 1.00 3 | JPY | 0.00952 4 | GBP | 1.22 5 | AUD | 0.675 6 | NZD | 0.64 7 | INR | 0.01395 8 | CNY | 0.1397 9 | HKD | 0.128 10 | SGP | 0.7194 11 | EUR | 1.11 12 | ASD | 2123. 13 | -------------------------------------------------------------------------------- /Lesson7/Activity01/CurrencyExtraColumn.txt: -------------------------------------------------------------------------------- 1 | Currency | ConversionRatio 2 | 3 | USD | 1.00 4 | 5 | JPY | 0.00952 6 | 7 | GBP | 1.22 | DAA 8 | 9 | AUD | 0.675 10 | 11 | NZD | 0.64 12 | 13 | INR | 0.01395 14 | 15 | CNY | 0.1397 16 | 17 | HKD | 0.128 18 | 19 | SGP | 0.7194 20 | 21 | EUR | 1.11 22 | 23 | ASD | 2123. 24 | -------------------------------------------------------------------------------- /Lesson7/Activity01/CurrencyWrongDelimiter.txt: -------------------------------------------------------------------------------- 1 | Currency , ConversionRatio 2 | 3 | USD , 1.00 4 | 5 | JPY , 0.00952 6 | 7 | GBP , 1.22 8 | 9 | AUD , 0.675 10 | 11 | NZD , 0.64 12 | 13 | INR , 0.01395 14 | 15 | CNY , 0.1397 16 | 17 | HKD , 0.128 18 | 19 | SGP , 0.7194 20 | 21 | EUR , 1.11 22 | 23 | ASD , 2123. 24 | -------------------------------------------------------------------------------- /Lesson7/Activity01/IdentifyTestCases.txt: -------------------------------------------------------------------------------- 1 | function : trim 2 | testcases 3 | 1: " " , result is empty. 4 | 2: " ada" , result "ada" 5 | 3: "sfat " , result "sfat" 6 | 4: " asah " , result "asah" 7 | 5: " a " , result "a" 8 | 9 | 10 | functon : isAllNumbers 11 | testscases: 12 | 1 : 111.111 pass 13 | 2 : .232 pass 14 | 3 : asdas.das fail 15 | 4 : 112.#$ fail 16 | 5 : wr.43432 fail 17 | 18 | 19 | function : isDigit 20 | testcases: 21 | 1 : 234 pass 22 | 2 : 23w23 fail 23 | 3 : 2.3 fail 24 | 25 | function : parseCurrencyParameters 26 | testcases 27 | 1 : pass 28 | 2 : fail 29 | 3 : fail 30 | 31 | function : checkFile 32 | testcases: 33 | 1: No File present: fail 34 | 2: Empty file : fail 35 | 3: Incorrect header contents in file : fail 36 | 4: Correct contents in file : pass 37 | 38 | function : parseConfig 39 | 2. no CurrencyFile parameters set , fail 40 | 3. no RecordFile parameters set , fail 41 | 4. correct file contents , pass 42 | 43 | function : fillCurrencyMap 44 | 1. Wrong delimiter : fail 45 | 2. wrong contents or columns, fail 46 | 3. correct contents, pass 47 | 48 | function : parseRecordFile 49 | 1. Wrong delimiter, fail 50 | 2. Wrong column contents, fail 51 | 3. correct contents, pass -------------------------------------------------------------------------------- /Lesson7/Activity01/ParseFiles.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Global variables ... 4 | string configFile = "./parse.conf"; 5 | string recordFile; 6 | string currencyFile; 7 | map currencyMap; 8 | vector vecRecord; 9 | 10 | 11 | int main(){ 12 | // Read Config file to set global configuration variables. 13 | if (!parseConfig()){ 14 | cerr << "Error parsing Config File " << endl; 15 | return false; 16 | } 17 | 18 | // Read Currency file and fill map 19 | if (!fillCurrencyMap()){ 20 | cerr << "Error setting CurrencyConversion Map " << endl; 21 | return false; 22 | } 23 | 24 | if (!parseRecordFile()){ 25 | cerr << "Error parsing Records File " << endl; 26 | return false; 27 | } 28 | 29 | displayCurrencyMap(); 30 | displayRecords(); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Lesson7/Activity01/RecordExtraColumn.txt: -------------------------------------------------------------------------------- 1 | Customer Id | Customer Name | Customer LastName | Order Id | Product Id | Quantity | TotalPrice(Regional Currency) | Regional Currency | TotalPrice($USD) 2 | 2312|Albert | Springs| 435351| 452536 | 11 | 13243.25 | USD | 12343.25 |asdasda 3 | 52341|Joseph | McRoy |14262 |414 | 4 | 42342.68 | GBP | 51658.07 4 | 43836| Piyush | Jain | 4650 | 69234 | 25 | 45252.65 | INR | 631.27 5 | -------------------------------------------------------------------------------- /Lesson7/Activity01/RecordFile.txt: -------------------------------------------------------------------------------- 1 | Customer Id | Customer Name | Customer LastName | Order Id | Product Id | Quantity | TotalPrice(Regional Currency) | Regional Currency | TotalPrice($USD) 2 | 2312|Albert | Springs| 435351| 452536 | 11 | 13243.25 | USD | 12343.25 3 | 52341|Joseph | McRoy |14262 |414 | 4 | 42342.68 | GBP | 51658.07 4 | 43836| Piyush | Jain | 4650 | 69234 | 25 | 45252.65 | INR | 631.27 5 | -------------------------------------------------------------------------------- /Lesson7/Activity01/RecordWrongDelimiter.txt: -------------------------------------------------------------------------------- 1 | Customer Id , Customer Name , Customer LastName , Order Id , Product Id , Quantity , TotalPrice(Regional Currency) , Regional Currency , TotalPrice($USD) 2 | 2312,Albert , Springs, 435351, 452536 , 11 , 13243.25 , USD , 12343.25 3 | 52341,Joseph , McRoy ,14262 ,414 , 4 , 42342.68 , GBP , 51658.07 4 | 43836, Piyush , Jain , 4650 , 69234 , 25 , 45252.65 , INR , 631.27 5 | -------------------------------------------------------------------------------- /Lesson7/Activity01/Test.make: -------------------------------------------------------------------------------- 1 | g++ -c -g -Wall ../Util.cpp -I../ 2 | 3 | g++ -c -g -Wall ParseFileTestCases.cpp 4 | 5 | g++ -g -Wall Util.o ParseFileTestCases.o -lgtest -lgtest_main -pthread -o ParseFileTestCases 6 | -------------------------------------------------------------------------------- /Lesson7/Activity01/build.sh: -------------------------------------------------------------------------------- 1 | g++ -c -g -I. -Wall Util.cpp 2 | g++ -g -I. -Wall Util.o ParseFiles.cpp -o ParseFiles 3 | -------------------------------------------------------------------------------- /Lesson7/Activity01/correctHeaderFile: -------------------------------------------------------------------------------- 1 | Currency | Val 2 | ADA | 12.19 3 | TRW | 5.1 4 | 5 | -------------------------------------------------------------------------------- /Lesson7/Activity01/emptyFile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TrainingByPackt/Advanced-CPlusPlus/b2ba3fc955b1dc0c617b0b2543fc82ad36573ff2/Lesson7/Activity01/emptyFile -------------------------------------------------------------------------------- /Lesson7/Activity01/noHeaderFile: -------------------------------------------------------------------------------- 1 | "ADA" | 12.19 2 | "TRW" | 5.1 3 | 4 | -------------------------------------------------------------------------------- /Lesson7/Activity01/parse.conf: -------------------------------------------------------------------------------- 1 | CONFIGURATION_FILE 2 | currencyFile = ./CurrencyConversion.txt 3 | recordFile = ./RecordFile.txt 4 | DatabaseServer = 192.123.41.112 5 | UserId = sqluser 6 | Password = sqluser 7 | RestApiServer = 101.21.231.11 8 | LogFilePath = /var/project/logs 9 | -------------------------------------------------------------------------------- /Lesson7/Activity01/parseMissingCurrency.conf: -------------------------------------------------------------------------------- 1 | CONFIGURATION_FILE 2 | recordFile = ./RecordFile.txt 3 | DatabaseServer = 192.123.41.112 4 | UserId = sqluser 5 | Password = sqluser 6 | RestApiServer = 101.21.231.11 7 | LogFilePath = /var/project/logs 8 | -------------------------------------------------------------------------------- /Lesson7/Activity01/parseMissingRecord.conf: -------------------------------------------------------------------------------- 1 | CONFIGURATION_FILE 2 | currencyFile = ./CurrencyConversion.txt 3 | UserId = sqluser 4 | Password = sqluser 5 | RestApiServer = 101.21.231.11 6 | LogFilePath = /var/project/logs 7 | -------------------------------------------------------------------------------- /Lesson7/Exercise01/AssertSample.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using std::cout; 6 | using std::endl; 7 | 8 | bool checkValidIp(const char * ip){ 9 | assert(ip != NULL); 10 | assert(strlen(ip) < 16); 11 | cout << "strlen: " << strlen(ip) << endl; 12 | return true; 13 | } 14 | 15 | int main(){ 16 | const char * ip; 17 | //ip = "111.111.111.111"; 18 | //ip = NULL; 19 | ip = "111.111.111.11111"; 20 | bool check = checkValidIp(ip); 21 | cout << " IP address is validated as :" << (check ? "true" : "false") << endl; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Lesson7/Exercise02/PersonLibrary.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TrainingByPackt/Advanced-CPlusPlus/b2ba3fc955b1dc0c617b0b2543fc82ad36573ff2/Lesson7/Exercise02/PersonLibrary.h -------------------------------------------------------------------------------- /Lesson7/Exercise02/PersonLibrary_ver1.h: -------------------------------------------------------------------------------- 1 | #ifndef __PERSON_H__ 2 | #define __PERSON_H__ 3 | 4 | #include 5 | 6 | using std::string; 7 | 8 | #define PERSON_LIB_VERSION 1 9 | 10 | struct person{ 11 | string name; 12 | int age; 13 | string address; 14 | }; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /Lesson7/Exercise02/PersonLibrary_ver2.h: -------------------------------------------------------------------------------- 1 | #ifndef __PERSON_H__ 2 | #define __PERSON_H__ 3 | 4 | #include 5 | 6 | using std::string; 7 | 8 | #define PERSON_LIB_VERSION 2 9 | 10 | struct person{ 11 | string name; 12 | int age; 13 | string address; 14 | string Mobile_No; 15 | }; 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /Lesson7/Exercise02/StaticAssertionSample.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include"PersonLibrary.h" 3 | 4 | void doSanityCheck(){ 5 | static_assert(PERSON_LIB_VERSION > 1 , "PERSON LIBRARY VERSION not greater than 1"); 6 | // Do any more sanity check before starting app ... 7 | } 8 | 9 | int main(){ 10 | doSanityCheck(); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /Lesson7/Exercise03/ExceptionSample.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::cout; 5 | using std::endl; 6 | 7 | bool checkValidIp(const char * ip){ 8 | try{ 9 | if(ip == NULL) 10 | throw ("ip is NULL"); 11 | 12 | if(strlen(ip) > 15) 13 | throw int(strlen(ip)); 14 | } 15 | catch(const char * str){ 16 | cout << "Error in checkValidIp :"<< str << endl; 17 | return false; 18 | } 19 | catch(int len){ 20 | cout << "Error in checkValidIp, ip len:" << len <<" greater than 15 characters, condition fail" << endl; 21 | return false; 22 | } 23 | 24 | cout << "strlen: " << strlen(ip) << endl; 25 | return true; 26 | } 27 | 28 | int main(){ 29 | const char * ip; 30 | ip = "111.111.111.111"; 31 | //ip = NULL; 32 | //ip = "111.111.111.11111"; 33 | 34 | if (checkValidIp(ip)) 35 | cout << "IP address is correctly validated" << endl; 36 | else { 37 | /// work on error condition 38 | // if needed exit program gracefully. 39 | return -1; 40 | } 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Lesson7/Exercise04/.ExceptionSample.cpp.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TrainingByPackt/Advanced-CPlusPlus/b2ba3fc955b1dc0c617b0b2543fc82ad36573ff2/Lesson7/Exercise04/.ExceptionSample.cpp.swp -------------------------------------------------------------------------------- /Lesson7/Exercise04/CheckIp.h: -------------------------------------------------------------------------------- 1 | #ifndef __CHECK_IP_H__ 2 | #define __CHECK_IP_H__ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | bool checkValidIp(const char *); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /Lesson7/Exercise04/MainIp.cpp: -------------------------------------------------------------------------------- 1 | #include"CheckIp.h" 2 | 3 | int main(){ 4 | const char * ip; 5 | //ip = "111.111.111.111"; 6 | ip = "111.111.111.11111"; 7 | 8 | if (checkValidIp(ip)) 9 | cout << "IP address is correctly validated" << endl; 10 | else { 11 | /// work on error condition 12 | // if needed exit program gracefully. 13 | cout << " Got error in valid ip " << endl; 14 | return -1; 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Lesson7/Exercise04/Test.make: -------------------------------------------------------------------------------- 1 | g++ -c CheckIp.cpp 2 | 3 | g++ -c TestCases.cpp 4 | 5 | #g++ CheckIp.o TestCases.o TestMain.cpp -lgtest -pthread -o TestRun 6 | 7 | g++ CheckIp.o TestCases.o -lgtest -lgtest_main -pthread -o TestRun 8 | -------------------------------------------------------------------------------- /Lesson7/Exercise04/TestCases.cpp: -------------------------------------------------------------------------------- 1 | #include"CheckIp.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | const char * testIp; 7 | 8 | TEST(CheckIp, testNull) { 9 | testIp=NULL; 10 | ASSERT_FALSE(checkValidIp(testIp)); 11 | } 12 | 13 | TEST(CheckIp, BadLength) { 14 | testIp = "232.13.1231.1321.123"; 15 | ASSERT_FALSE(checkValidIp(testIp)); 16 | } 17 | 18 | TEST(CheckIp, WrongTokenCount) { 19 | testIp = "22.1311.11"; 20 | ASSERT_FALSE(checkValidIp(testIp)); 21 | } 22 | 23 | TEST(CheckIp, WrongTokenEmpty) { 24 | testIp = "22.131..11"; 25 | ASSERT_FALSE(checkValidIp(testIp)); 26 | } 27 | 28 | TEST(CheckIp, WrongTokenStart) { 29 | testIp = ".2.1.31.11"; 30 | ASSERT_FALSE(checkValidIp(testIp)); 31 | } 32 | 33 | TEST(CheckIp, WrongTokenEnd) { 34 | testIp = "2.13.11.1."; 35 | ASSERT_FALSE(checkValidIp(testIp)); 36 | } 37 | 38 | TEST(CheckIp, SpaceToken) { 39 | testIp = "2.13.11. 1"; 40 | ASSERT_FALSE(checkValidIp(testIp)); 41 | } 42 | 43 | TEST(CheckIp, NonDigit) { 44 | testIp = "2.13.b1.A1"; 45 | ASSERT_FALSE(checkValidIp(testIp)); 46 | } 47 | 48 | TEST(CheckIp, NonValidDigit) { 49 | testIp = "2.13.521.61"; 50 | ASSERT_FALSE(checkValidIp(testIp)); 51 | } 52 | 53 | TEST(CheckIp, CorrectIp) { 54 | testIp = "232.13.123.1"; 55 | ASSERT_TRUE(checkValidIp(testIp)); 56 | } 57 | 58 | -------------------------------------------------------------------------------- /Lesson7/Exercise04/TestMain.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(int argc, char **argv){ 6 | testing::InitGoogleTest(&argc, argv); 7 | 8 | return RUN_ALL_TESTS(); 9 | } 10 | -------------------------------------------------------------------------------- /Lesson7/Exercise04/TestRun: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TrainingByPackt/Advanced-CPlusPlus/b2ba3fc955b1dc0c617b0b2543fc82ad36573ff2/Lesson7/Exercise04/TestRun -------------------------------------------------------------------------------- /Lesson7/Exercise05/Misc.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | class ConnectDatabase{ 11 | string DBname; 12 | string user; 13 | string passwd; 14 | 15 | public: 16 | ConnectDatabase() {} 17 | ConnectDatabase(string _dbname, string _uname, string _passwd) : 18 | DBname(_dbname), user(_uname), passwd(_passwd) { } 19 | 20 | virtual ~ConnectDatabase() {} 21 | virtual string getResult(string query); 22 | }; 23 | 24 | class WebServerConnect{ 25 | string Webserver; 26 | string uname; 27 | string passwd; 28 | 29 | public : 30 | WebServerConnect(string _sname, string _uname, string _passwd) : 31 | Webserver(_sname), uname(_uname), passwd(_passwd) { } 32 | 33 | virtual ~WebServerConnect() {} 34 | virtual string getRequest(string req); 35 | }; 36 | 37 | class App { 38 | ConnectDatabase *DB; 39 | WebServerConnect *WB; 40 | 41 | public : 42 | App():DB(NULL), WB(NULL) {} 43 | ~App() { 44 | if ( DB ) delete DB; 45 | if ( WB ) delete WB; 46 | } 47 | bool checkValidIp(string ip); 48 | string getDBResult(string query); 49 | string getWebResult(string query); 50 | void connectDB(string, string, string); 51 | void connectDB(ConnectDatabase *db); 52 | void connectWeb(string, string, string); 53 | void run(); 54 | }; 55 | -------------------------------------------------------------------------------- /Lesson7/Exercise05/MockMisc.h: -------------------------------------------------------------------------------- 1 | #include"Misc.h" 2 | #include 3 | #include 4 | 5 | 6 | class MockDB : public ConnectDatabase { 7 | public : 8 | MockDB() {} 9 | virtual ~MockDB(){} 10 | 11 | MOCK_METHOD1(getResult, string( string) ); 12 | }; 13 | -------------------------------------------------------------------------------- /Lesson7/Exercise05/RunApp.cpp: -------------------------------------------------------------------------------- 1 | #include"Misc.h" 2 | 3 | int main(){ 4 | 5 | App app; 6 | app.connectDB("dbname","dbuser", "dbpasswd"); 7 | string ip = app.getDBResult("dummy"); 8 | // DB query to get Webserver IP 9 | // Similarly some miscellaneous activities to get configuratio information 10 | // Like querying DB to get correct username/passwd to connect to WebServer.. 11 | 12 | // After getting IP from DB, check if the IP is valid.. 13 | //app.checkValidIp(ip); 14 | 15 | // Now conect to webserver with parameters extracted from DB. 16 | app.connectWeb("webname","user", "passwd"); 17 | 18 | // Now run the App, like sending some request to webserver, 19 | // getting result and doing activity with received data. 20 | app.run(); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Lesson7/Exercise05/TestApp.cpp: -------------------------------------------------------------------------------- 1 | #include"MockMisc.h" 2 | 3 | using ::testing::_; 4 | using ::testing::Return; 5 | 6 | class TestApp : public ::testing::Test { 7 | protected : 8 | App testApp; 9 | MockDB *mdb; 10 | 11 | void SetUp(){ 12 | mdb = new MockDB(); 13 | testApp.connectDB(mdb); 14 | } 15 | 16 | void TearDown(){ 17 | } 18 | }; 19 | 20 | TEST_F(TestApp, NullIP){ 21 | EXPECT_CALL(*mdb, getResult(_)). 22 | WillOnce(Return("")); 23 | 24 | ASSERT_FALSE(testApp.checkValidIp(testApp.getDBResult(""))); 25 | } 26 | 27 | TEST_F(TestApp, SpaceTokenIP){ 28 | EXPECT_CALL(*mdb, getResult(_)). 29 | WillOnce(Return("13. 21.31.68")); 30 | 31 | ASSERT_FALSE(testApp.checkValidIp(testApp.getDBResult(""))); 32 | } 33 | 34 | TEST_F(TestApp, NonValidDigitIP){ 35 | EXPECT_CALL(*mdb, getResult(_)). 36 | WillOnce(Return("13.521.31.68")); 37 | 38 | ASSERT_FALSE(testApp.checkValidIp(testApp.getDBResult(""))); 39 | } 40 | 41 | TEST_F(TestApp, CorrectIP){ 42 | EXPECT_CALL(*mdb, getResult(_)). 43 | WillOnce(Return("212.121.21.45")); 44 | 45 | ASSERT_TRUE(testApp.checkValidIp(testApp.getDBResult(""))); 46 | } 47 | 48 | -------------------------------------------------------------------------------- /Lesson7/Exercise05/TestApp.make: -------------------------------------------------------------------------------- 1 | g++ -g Misc.cpp TestApp.cpp -lgtest -lgmock -lgtest_main -pthread -o TestApp 2 | -------------------------------------------------------------------------------- /Lesson7/readme.md: -------------------------------------------------------------------------------- 1 | # Advanced C++ 2 | Write robust C++ code so fast they will think you cheated -------------------------------------------------------------------------------- /Lesson8/Activity01/Speller.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "Timer.h" 10 | using namespace std; 11 | 12 | // This method must return a vector containing the strings in vecText that are not in vecDict 13 | vector getMisspelt(const vector &vecDict, const vector &vecText) 14 | { 15 | vector ret; 16 | return ret; 17 | } 18 | 19 | // Reads a file and puts each word in a vector 20 | vector readFile(const char *sFile) 21 | { 22 | ifstream ifs(sFile); 23 | vector ret{istream_iterator(ifs), istream_iterator()}; 24 | return ret; 25 | } 26 | 27 | // Main program reads the data and checks the output against out.txt 28 | int main() 29 | { 30 | // Read data 31 | const vector &vecText = readFile("test.txt"); 32 | const vector &vecDict = readFile("dict.txt"); 33 | 34 | //Get the misspelt words, and time it 35 | vector vecMisspelt; 36 | { 37 | Timer t("Spell check"); 38 | vecMisspelt = getMisspelt(vecDict, vecText); 39 | } 40 | 41 | // Check if we have the the desired output (after getting the unique sorted words in vecMisspelt) 42 | const vector &vecOut = readFile("out.txt"); 43 | set setTemp(vecMisspelt.begin(), vecMisspelt.end()); 44 | if(equal(setTemp.begin(), setTemp.end(), vecOut.begin())) 45 | { 46 | cerr << "\nOutput is correct\n"; 47 | } 48 | else 49 | { 50 | cerr << "\nOutput is incorrect\n"; 51 | } 52 | 53 | Timer::dump(); 54 | } 55 | -------------------------------------------------------------------------------- /Lesson8/Activity01/Timer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | #include "Timer.h" 7 | 8 | 9 | using std::map; 10 | using std::string; 11 | using std::cerr; 12 | using std::endl; 13 | 14 | 15 | // When constructed, save the name and current clock time 16 | Timer::Timer(string sName): m_sName(sName) 17 | { 18 | m_tmStart = std::chrono::high_resolution_clock::now(); 19 | } 20 | 21 | // When destroyed, add the time elapsed and also increment the count under this name 22 | Timer::~Timer() 23 | { 24 | auto tmNow = std::chrono::high_resolution_clock::now(); 25 | auto msElapsed = std::chrono::duration_cast(tmNow - m_tmStart); 26 | 27 | ms_Counts[m_sName]++; 28 | ms_Times[m_sName] += msElapsed.count(); 29 | } 30 | 31 | // Print out the stats for each measured block/function 32 | void Timer::dump() 33 | { 34 | cerr << "Name\t\t\tCount\t\t\tTime(ms)\t\tAverage(ms)\n"; 35 | cerr << "-----------------------------------------------------------------------------------\n"; 36 | for(const auto& it: ms_Times) 37 | { 38 | auto iCount = ms_Counts[it.first]; 39 | cerr << it.first << "\t\t\t" << iCount << "\t\t\t" << it.second << "\t\t\t" << it.second / iCount << "\n"; 40 | } 41 | } 42 | 43 | 44 | // Define static members 45 | map Timer::ms_Counts; 46 | map Timer::ms_Times; 47 | 48 | -------------------------------------------------------------------------------- /Lesson8/Activity01/Timer.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | class Timer 7 | { 8 | static std::map ms_Counts; 9 | static std::map ms_Times; 10 | 11 | std::string m_sName; 12 | std::chrono::time_point m_tmStart; 13 | 14 | public: 15 | 16 | // When constructed, save the name and current clock time 17 | Timer(std::string sName); 18 | 19 | // When destroyed, add the time elapsed and also increment the count under this name 20 | ~Timer(); 21 | 22 | // Print out the stats for each measured block/function 23 | static void dump(); 24 | }; 25 | 26 | 27 | // Helper macro to time functions 28 | #define TIME_IT Timer t(__func__) 29 | 30 | -------------------------------------------------------------------------------- /Lesson8/Activity01/activity1.7z: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TrainingByPackt/Advanced-CPlusPlus/b2ba3fc955b1dc0c617b0b2543fc82ad36573ff2/Lesson8/Activity01/activity1.7z -------------------------------------------------------------------------------- /Lesson8/Exercise01/Snippet1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int sumVector(std::vector &v) 5 | { 6 | int ret = 0; 7 | for(int i: v) 8 | { 9 | ret += i; 10 | } 11 | 12 | return ret; 13 | } 14 | 15 | int main() 16 | { 17 | // Initialize a random number generator 18 | std::random_device dev; 19 | std::mt19937 rng(dev()); 20 | 21 | // Create a distribution range from 0 to 1000 22 | std::uniform_int_distribution dist(0,1000); 23 | 24 | // Fill 10000 numbers in a vector 25 | std::vector v; 26 | v.reserve(10000); 27 | for(int i = 0; i < 10000; ++i) 28 | { 29 | v.push_back(dist(rng)); 30 | } 31 | 32 | // Call out function 1000 times, accumulating to a total sum 33 | double total = 0.0; 34 | for(int i = 0; i < 100000; ++i) 35 | { 36 | total += sumVector(v); 37 | } 38 | 39 | std::cout << "Total: " << total << std::endl; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /Lesson8/Exercise04/Snippet3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int main() 5 | { 6 | int ret = 0; 7 | for(size_t i = 1; i < 10000; ++i) 8 | { 9 | for(size_t j = 1; j < 10000; ++j) 10 | { 11 | if(i % j == 0) 12 | { 13 | ++ret; 14 | } 15 | } 16 | } 17 | 18 | std::cout << "Result: " << ret << std::endl; 19 | } -------------------------------------------------------------------------------- /Lesson8/Exercise05/Snippet3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int main() 5 | { 6 | int ret = 0; 7 | for(size_t i = 1; i < 10000; ++i) 8 | { 9 | for(size_t j = 1; j < 10000; ++j) 10 | { 11 | if(i % j == 0) 12 | { 13 | ++ret; 14 | } 15 | } 16 | } 17 | 18 | std::cout << "Result: " << ret << std::endl; 19 | } -------------------------------------------------------------------------------- /Lesson8/Exercise06/Snippet4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | using std::cerr; 10 | using std::endl; 11 | 12 | 13 | 14 | int main() 15 | { 16 | // Fill 100,000,000 1s in a vector 17 | std::vector v( 100'000'000, 1); 18 | 19 | // Call accumulate 100 times, accumulating to a total sum 20 | uint64_t total = 0; 21 | for(int i = 0; i < 100; ++i) 22 | { 23 | total += std::accumulate(v.begin(), v.end(), 0); 24 | } 25 | 26 | std::cout << "Total: " << total << std::endl; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Lesson8/Exercise07/Snippet5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "Timer.h" 6 | 7 | using std::vector; 8 | using std::cerr; 9 | using std::endl; 10 | 11 | const int N = 1000000; 12 | 13 | void withoutReserve(vector &v) 14 | { 15 | TIME_IT; 16 | for(int i = 0; i < N; ++i) 17 | { 18 | v.push_back(i); 19 | } 20 | } 21 | 22 | void withReserve(vector &v) 23 | { 24 | TIME_IT; 25 | v.reserve(N); 26 | for(int i = 0; i < N; ++i) 27 | { 28 | v.push_back(i); 29 | } 30 | } 31 | 32 | 33 | int main() 34 | { 35 | { 36 | vector v1; 37 | for(int i = 0; i < 100; ++i) 38 | { 39 | withoutReserve(v1); 40 | } 41 | } 42 | 43 | { 44 | vector v2; 45 | for(int i = 0; i < 100; ++i) 46 | { 47 | withReserve(v2); 48 | } 49 | } 50 | 51 | Timer::dump(); 52 | 53 | } 54 | 55 | -------------------------------------------------------------------------------- /Lesson8/Exercise07/Timer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | #include "Timer.h" 7 | 8 | 9 | using std::map; 10 | using std::string; 11 | using std::cerr; 12 | using std::endl; 13 | 14 | 15 | // When constructed, save the name and current clock time 16 | Timer::Timer(string sName): m_sName(sName) 17 | { 18 | m_tmStart = std::chrono::high_resolution_clock::now(); 19 | } 20 | 21 | // When destroyed, add the time elapsed and also increment the count under this name 22 | Timer::~Timer() 23 | { 24 | auto tmNow = std::chrono::high_resolution_clock::now(); 25 | auto msElapsed = std::chrono::duration_cast(tmNow - m_tmStart); 26 | 27 | ms_Counts[m_sName]++; 28 | ms_Times[m_sName] += msElapsed.count(); 29 | } 30 | 31 | // Print out the stats for each measured block/function 32 | void Timer::dump() 33 | { 34 | cerr << "Name\t\t\tCount\t\t\tTime(ms)\t\tAverage(ms)\n"; 35 | cerr << "-----------------------------------------------------------------------------------\n"; 36 | for(const auto& it: ms_Times) 37 | { 38 | auto iCount = ms_Counts[it.first]; 39 | cerr << it.first << "\t\t\t" << iCount << "\t\t\t" << it.second << "\t\t\t" << it.second / iCount << "\n"; 40 | } 41 | } 42 | 43 | 44 | // Define static members 45 | map Timer::ms_Counts; 46 | map Timer::ms_Times; 47 | 48 | -------------------------------------------------------------------------------- /Lesson8/Exercise07/Timer.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | class Timer 7 | { 8 | static std::map ms_Counts; 9 | static std::map ms_Times; 10 | 11 | std::string m_sName; 12 | std::chrono::time_point m_tmStart; 13 | 14 | public: 15 | 16 | // When constructed, save the name and current clock time 17 | Timer(std::string sName); 18 | 19 | // When destroyed, add the time elapsed and also increment the count under this name 20 | ~Timer(); 21 | 22 | // Print out the stats for each measured block/function 23 | static void dump(); 24 | }; 25 | 26 | 27 | // Helper macro to time functions 28 | #define TIME_IT Timer t(__func__) 29 | 30 | -------------------------------------------------------------------------------- /Lesson8/Exercise08/Snippet6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "Timer.h" 7 | 8 | using std::vector; 9 | using std::cerr; 10 | using std::endl; 11 | 12 | const uint64_t N = 100000000; 13 | 14 | uint64_t sum1() 15 | { 16 | TIME_IT; 17 | uint64_t ret = 0; 18 | for(uint64_t b=0; b < N; ++b) 19 | { 20 | if(b % 3 == 2 || b < N/2) 21 | { 22 | ret += b; 23 | } 24 | } 25 | 26 | return ret; 27 | } 28 | 29 | 30 | uint64_t sum2() 31 | { 32 | TIME_IT; 33 | uint64_t ret = 0; 34 | for(uint64_t b=0; b < N; ++b) 35 | { 36 | if(b < N/2 || b % 3 == 2) 37 | { 38 | ret += b; 39 | } 40 | } 41 | 42 | return ret; 43 | } 44 | 45 | int main() 46 | { 47 | volatile uint64_t dummy = 0; 48 | for(int i = 0; i < 100; ++i) 49 | { 50 | dummy = sum1(); 51 | } 52 | 53 | for(int i = 0; i < 100; ++i) 54 | { 55 | dummy = sum2(); 56 | } 57 | 58 | Timer::dump(); 59 | } 60 | 61 | -------------------------------------------------------------------------------- /Lesson8/Exercise08/Timer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | #include "Timer.h" 7 | 8 | 9 | using std::map; 10 | using std::string; 11 | using std::cerr; 12 | using std::endl; 13 | 14 | 15 | // When constructed, save the name and current clock time 16 | Timer::Timer(string sName): m_sName(sName) 17 | { 18 | m_tmStart = std::chrono::high_resolution_clock::now(); 19 | } 20 | 21 | // When destroyed, add the time elapsed and also increment the count under this name 22 | Timer::~Timer() 23 | { 24 | auto tmNow = std::chrono::high_resolution_clock::now(); 25 | auto msElapsed = std::chrono::duration_cast(tmNow - m_tmStart); 26 | 27 | ms_Counts[m_sName]++; 28 | ms_Times[m_sName] += msElapsed.count(); 29 | } 30 | 31 | // Print out the stats for each measured block/function 32 | void Timer::dump() 33 | { 34 | cerr << "Name\t\t\tCount\t\t\tTime(ms)\t\tAverage(ms)\n"; 35 | cerr << "-----------------------------------------------------------------------------------\n"; 36 | for(const auto& it: ms_Times) 37 | { 38 | auto iCount = ms_Counts[it.first]; 39 | cerr << it.first << "\t\t\t" << iCount << "\t\t\t" << it.second << "\t\t\t" << it.second / iCount << "\n"; 40 | } 41 | } 42 | 43 | 44 | // Define static members 45 | map Timer::ms_Counts; 46 | map Timer::ms_Times; 47 | 48 | -------------------------------------------------------------------------------- /Lesson8/Exercise08/Timer.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | class Timer 7 | { 8 | static std::map ms_Counts; 9 | static std::map ms_Times; 10 | 11 | std::string m_sName; 12 | std::chrono::time_point m_tmStart; 13 | 14 | public: 15 | 16 | // When constructed, save the name and current clock time 17 | Timer(std::string sName); 18 | 19 | // When destroyed, add the time elapsed and also increment the count under this name 20 | ~Timer(); 21 | 22 | // Print out the stats for each measured block/function 23 | static void dump(); 24 | }; 25 | 26 | 27 | // Helper macro to time functions 28 | #define TIME_IT Timer t(__func__) 29 | 30 | -------------------------------------------------------------------------------- /Lesson8/Exercise09/Snippet7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "Timer.h" 7 | 8 | using std::vector; 9 | using std::cerr; 10 | using std::endl; 11 | 12 | const uint64_t N = 100000000; 13 | 14 | uint64_t sum1() 15 | { 16 | TIME_IT; 17 | uint64_t ret = 0; 18 | for(int i = 0; i < N; ++i) 19 | { 20 | for(int j = 0; j < 100; ++j) 21 | { 22 | ret += i ^ j; 23 | } 24 | } 25 | 26 | return ret; 27 | } 28 | 29 | 30 | uint64_t sum2() 31 | { 32 | TIME_IT; 33 | uint64_t ret = 0; 34 | for(int i = 0; i < 100; ++i) 35 | { 36 | for(int j = 0; j < N; ++j) 37 | { 38 | ret += i ^ j; 39 | } 40 | } 41 | 42 | return ret; 43 | } 44 | 45 | 46 | int main() 47 | { 48 | volatile uint64_t dummy; 49 | dummy = sum1(); 50 | dummy = sum2(); 51 | Timer::dump(); 52 | } 53 | 54 | -------------------------------------------------------------------------------- /Lesson8/Exercise09/Timer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | #include "Timer.h" 7 | 8 | 9 | using std::map; 10 | using std::string; 11 | using std::cerr; 12 | using std::endl; 13 | 14 | 15 | // When constructed, save the name and current clock time 16 | Timer::Timer(string sName): m_sName(sName) 17 | { 18 | m_tmStart = std::chrono::high_resolution_clock::now(); 19 | } 20 | 21 | // When destroyed, add the time elapsed and also increment the count under this name 22 | Timer::~Timer() 23 | { 24 | auto tmNow = std::chrono::high_resolution_clock::now(); 25 | auto msElapsed = std::chrono::duration_cast(tmNow - m_tmStart); 26 | 27 | ms_Counts[m_sName]++; 28 | ms_Times[m_sName] += msElapsed.count(); 29 | } 30 | 31 | // Print out the stats for each measured block/function 32 | void Timer::dump() 33 | { 34 | cerr << "Name\t\t\tCount\t\t\tTime(ms)\t\tAverage(ms)\n"; 35 | cerr << "-----------------------------------------------------------------------------------\n"; 36 | for(const auto& it: ms_Times) 37 | { 38 | auto iCount = ms_Counts[it.first]; 39 | cerr << it.first << "\t\t\t" << iCount << "\t\t\t" << it.second << "\t\t\t" << it.second / iCount << "\n"; 40 | } 41 | } 42 | 43 | 44 | // Define static members 45 | map Timer::ms_Counts; 46 | map Timer::ms_Times; 47 | 48 | -------------------------------------------------------------------------------- /Lesson8/Exercise09/Timer.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | class Timer 7 | { 8 | static std::map ms_Counts; 9 | static std::map ms_Times; 10 | 11 | std::string m_sName; 12 | std::chrono::time_point m_tmStart; 13 | 14 | public: 15 | 16 | // When constructed, save the name and current clock time 17 | Timer(std::string sName); 18 | 19 | // When destroyed, add the time elapsed and also increment the count under this name 20 | ~Timer(); 21 | 22 | // Print out the stats for each measured block/function 23 | static void dump(); 24 | }; 25 | 26 | 27 | // Helper macro to time functions 28 | #define TIME_IT Timer t(__func__) 29 | 30 | -------------------------------------------------------------------------------- /Lesson8/Exercise10/Timer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | #include "Timer.h" 7 | 8 | 9 | using std::map; 10 | using std::string; 11 | using std::cerr; 12 | using std::endl; 13 | 14 | 15 | // When constructed, save the name and current clock time 16 | Timer::Timer(string sName): m_sName(sName) 17 | { 18 | m_tmStart = std::chrono::high_resolution_clock::now(); 19 | } 20 | 21 | // When destroyed, add the time elapsed and also increment the count under this name 22 | Timer::~Timer() 23 | { 24 | auto tmNow = std::chrono::high_resolution_clock::now(); 25 | auto msElapsed = std::chrono::duration_cast(tmNow - m_tmStart); 26 | 27 | ms_Counts[m_sName]++; 28 | ms_Times[m_sName] += msElapsed.count(); 29 | } 30 | 31 | // Print out the stats for each measured block/function 32 | void Timer::dump() 33 | { 34 | cerr << "Name\t\t\tCount\t\t\tTime(ms)\t\tAverage(ms)\n"; 35 | cerr << "-----------------------------------------------------------------------------------\n"; 36 | for(const auto& it: ms_Times) 37 | { 38 | auto iCount = ms_Counts[it.first]; 39 | cerr << it.first << "\t\t\t" << iCount << "\t\t\t" << it.second << "\t\t\t" << it.second / iCount << "\n"; 40 | } 41 | } 42 | 43 | 44 | // Define static members 45 | map Timer::ms_Counts; 46 | map Timer::ms_Times; 47 | 48 | -------------------------------------------------------------------------------- /Lesson8/Exercise10/Timer.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | class Timer 7 | { 8 | static std::map ms_Counts; 9 | static std::map ms_Times; 10 | 11 | std::string m_sName; 12 | std::chrono::time_point m_tmStart; 13 | 14 | public: 15 | 16 | // When constructed, save the name and current clock time 17 | Timer(std::string sName); 18 | 19 | // When destroyed, add the time elapsed and also increment the count under this name 20 | ~Timer(); 21 | 22 | // Print out the stats for each measured block/function 23 | static void dump(); 24 | }; 25 | 26 | 27 | // Helper macro to time functions 28 | #define TIME_IT Timer t(__func__) 29 | 30 | -------------------------------------------------------------------------------- /Lesson8/Exercise11/Timer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | #include "Timer.h" 7 | 8 | 9 | using std::map; 10 | using std::string; 11 | using std::cerr; 12 | using std::endl; 13 | 14 | 15 | // When constructed, save the name and current clock time 16 | Timer::Timer(string sName): m_sName(sName) 17 | { 18 | m_tmStart = std::chrono::high_resolution_clock::now(); 19 | } 20 | 21 | // When destroyed, add the time elapsed and also increment the count under this name 22 | Timer::~Timer() 23 | { 24 | auto tmNow = std::chrono::high_resolution_clock::now(); 25 | auto msElapsed = std::chrono::duration_cast(tmNow - m_tmStart); 26 | 27 | ms_Counts[m_sName]++; 28 | ms_Times[m_sName] += msElapsed.count(); 29 | } 30 | 31 | // Print out the stats for each measured block/function 32 | void Timer::dump() 33 | { 34 | cerr << "Name\t\t\tCount\t\t\tTime(ms)\t\tAverage(ms)\n"; 35 | cerr << "-----------------------------------------------------------------------------------\n"; 36 | for(const auto& it: ms_Times) 37 | { 38 | auto iCount = ms_Counts[it.first]; 39 | cerr << it.first << "\t\t\t" << iCount << "\t\t\t" << it.second << "\t\t\t" << it.second / iCount << "\n"; 40 | } 41 | } 42 | 43 | 44 | // Define static members 45 | map Timer::ms_Counts; 46 | map Timer::ms_Times; 47 | 48 | -------------------------------------------------------------------------------- /Lesson8/Exercise11/Timer.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | class Timer 7 | { 8 | static std::map ms_Counts; 9 | static std::map ms_Times; 10 | 11 | std::string m_sName; 12 | std::chrono::time_point m_tmStart; 13 | 14 | public: 15 | 16 | // When constructed, save the name and current clock time 17 | Timer(std::string sName); 18 | 19 | // When destroyed, add the time elapsed and also increment the count under this name 20 | ~Timer(); 21 | 22 | // Print out the stats for each measured block/function 23 | static void dump(); 24 | }; 25 | 26 | 27 | // Helper macro to time functions 28 | #define TIME_IT Timer t(__func__) 29 | 30 | -------------------------------------------------------------------------------- /Lesson8/Exercise12/Snippet11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "Timer.h" 6 | 7 | using std::string; 8 | using std::cerr; 9 | using std::endl; 10 | 11 | // Size of the vector 12 | const int N = 10'000'000; 13 | 14 | const char* fruits[] = 15 | {"apple", "banana", "cherry", "durian", "guava", "jackfruit", "kumquat", "mango", "orange", "pear"}; 16 | 17 | 18 | uint64_t fun1() 19 | { 20 | TIME_IT; 21 | uint64_t sum = 0; 22 | string s1; 23 | for(uint64_t i = 0; i < N; ++i) 24 | { 25 | s1 = fruits[i % 10]; 26 | for(int k = 0; k < s1.size(); ++k) sum += s1[k]; 27 | } 28 | 29 | return sum; 30 | } 31 | 32 | uint64_t fun2() 33 | { 34 | TIME_IT; 35 | uint64_t sum = 0; 36 | char s1[32]; 37 | 38 | for(uint64_t i = 0; i < N; ++i) 39 | { 40 | char *ps1 = s1; 41 | const char *p1 = fruits[i % 10]; 42 | do { *ps1++ = *p1; } while(*p1++); 43 | for(ps1 = s1; *ps1; ++ps1) sum += *ps1; 44 | } 45 | 46 | return sum; 47 | } 48 | 49 | 50 | int main() 51 | { 52 | for(int i = 0; i < 10; ++i) 53 | { 54 | volatile uint64_t asum1 = fun1(); 55 | volatile uint64_t asum2 = fun2(); 56 | } 57 | 58 | Timer::dump(); 59 | 60 | } 61 | 62 | -------------------------------------------------------------------------------- /Lesson8/Exercise12/Timer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | #include "Timer.h" 7 | 8 | 9 | using std::map; 10 | using std::string; 11 | using std::cerr; 12 | using std::endl; 13 | 14 | 15 | // When constructed, save the name and current clock time 16 | Timer::Timer(string sName): m_sName(sName) 17 | { 18 | m_tmStart = std::chrono::high_resolution_clock::now(); 19 | } 20 | 21 | // When destroyed, add the time elapsed and also increment the count under this name 22 | Timer::~Timer() 23 | { 24 | auto tmNow = std::chrono::high_resolution_clock::now(); 25 | auto msElapsed = std::chrono::duration_cast(tmNow - m_tmStart); 26 | 27 | ms_Counts[m_sName]++; 28 | ms_Times[m_sName] += msElapsed.count(); 29 | } 30 | 31 | // Print out the stats for each measured block/function 32 | void Timer::dump() 33 | { 34 | cerr << "Name\t\t\tCount\t\t\tTime(ms)\t\tAverage(ms)\n"; 35 | cerr << "-----------------------------------------------------------------------------------\n"; 36 | for(const auto& it: ms_Times) 37 | { 38 | auto iCount = ms_Counts[it.first]; 39 | cerr << it.first << "\t\t\t" << iCount << "\t\t\t" << it.second << "\t\t\t" << it.second / iCount << "\n"; 40 | } 41 | } 42 | 43 | 44 | // Define static members 45 | map Timer::ms_Counts; 46 | map Timer::ms_Times; 47 | 48 | -------------------------------------------------------------------------------- /Lesson8/Exercise12/Timer.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | class Timer 7 | { 8 | static std::map ms_Counts; 9 | static std::map ms_Times; 10 | 11 | std::string m_sName; 12 | std::chrono::time_point m_tmStart; 13 | 14 | public: 15 | 16 | // When constructed, save the name and current clock time 17 | Timer(std::string sName); 18 | 19 | // When destroyed, add the time elapsed and also increment the count under this name 20 | ~Timer(); 21 | 22 | // Print out the stats for each measured block/function 23 | static void dump(); 24 | }; 25 | 26 | 27 | // Helper macro to time functions 28 | #define TIME_IT Timer t(__func__) 29 | 30 | -------------------------------------------------------------------------------- /Lesson8/Exercise13/Timer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | #include "Timer.h" 7 | 8 | 9 | using std::map; 10 | using std::string; 11 | using std::cerr; 12 | using std::endl; 13 | 14 | 15 | // When constructed, save the name and current clock time 16 | Timer::Timer(string sName): m_sName(sName) 17 | { 18 | m_tmStart = std::chrono::high_resolution_clock::now(); 19 | } 20 | 21 | // When destroyed, add the time elapsed and also increment the count under this name 22 | Timer::~Timer() 23 | { 24 | auto tmNow = std::chrono::high_resolution_clock::now(); 25 | auto msElapsed = std::chrono::duration_cast(tmNow - m_tmStart); 26 | 27 | ms_Counts[m_sName]++; 28 | ms_Times[m_sName] += msElapsed.count(); 29 | } 30 | 31 | // Print out the stats for each measured block/function 32 | void Timer::dump() 33 | { 34 | cerr << "Name\t\t\tCount\t\t\tTime(ms)\t\tAverage(ms)\n"; 35 | cerr << "-----------------------------------------------------------------------------------\n"; 36 | for(const auto& it: ms_Times) 37 | { 38 | auto iCount = ms_Counts[it.first]; 39 | cerr << it.first << "\t\t\t" << iCount << "\t\t\t" << it.second << "\t\t\t" << it.second / iCount << "\n"; 40 | } 41 | } 42 | 43 | 44 | // Define static members 45 | map Timer::ms_Counts; 46 | map Timer::ms_Times; 47 | 48 | -------------------------------------------------------------------------------- /Lesson8/Exercise13/Timer.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | class Timer 7 | { 8 | static std::map ms_Counts; 9 | static std::map ms_Times; 10 | 11 | std::string m_sName; 12 | std::chrono::time_point m_tmStart; 13 | 14 | public: 15 | 16 | // When constructed, save the name and current clock time 17 | Timer(std::string sName); 18 | 19 | // When destroyed, add the time elapsed and also increment the count under this name 20 | ~Timer(); 21 | 22 | // Print out the stats for each measured block/function 23 | static void dump(); 24 | }; 25 | 26 | 27 | // Helper macro to time functions 28 | #define TIME_IT Timer t(__func__) 29 | 30 | -------------------------------------------------------------------------------- /Lesson8/Exercise14/Snippet13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int wordCount(const std::string &s) 5 | { 6 | int count = 0; 7 | for(int i = 0, j = 1; i < s.size() - 1; ++i, ++j) 8 | { 9 | if(!isspace(s[i]) && isspace(s[j])) 10 | { 11 | ++count; 12 | } 13 | } 14 | 15 | return count; 16 | } 17 | -------------------------------------------------------------------------------- /Lesson8/Exercise14/Snippet14.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int wordCount(const std::string &s) 5 | { 6 | // Create a lookup table 7 | bool isSpace[256]; 8 | for(int i = 0; i < 256; ++i) 9 | { 10 | isSpace[i] = isspace((unsigned char)i); 11 | } 12 | 13 | int count = 0; 14 | int len = s.size() - 1; 15 | for(int i = 0, j = 1; i < len; ++i, ++j) 16 | { 17 | count += !isSpace[s[i]] & isSpace[s[j]]; 18 | } 19 | 20 | return count; 21 | } 22 | -------------------------------------------------------------------------------- /Lesson8/Exercise14/Snippet15.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int wordCount(const std::string &s) 6 | { 7 | // Create a lookup table for every pair of chars 8 | bool table[65536]; 9 | for(int i = 0; i < 256; ++i) 10 | { 11 | for(int j = 0; j < 256; ++j) 12 | { 13 | int idx = j + i * 256; 14 | table[idx] = !isspace(j) && isspace(i); 15 | } 16 | } 17 | 18 | int count = 0; 19 | for(int i = 0; i < s.size() - 1; ++i) 20 | { 21 | // grab the 2 bytes at s[i] as a 16 bit value 22 | unsigned short idx; 23 | memcpy(&idx, &s[i], 2); 24 | count += table[idx]; 25 | } 26 | 27 | return count; 28 | } 29 | -------------------------------------------------------------------------------- /Lesson8/Exercise14/Snippet16.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int wordCount(const std::string &s) 5 | { 6 | // Create a lookup table for every char 7 | bool table[256]; 8 | for(int i = 0; i < 256; ++i) 9 | { 10 | table[i] = isspace((unsigned char)i) ? 1 : 0; 11 | } 12 | 13 | auto isWordEnd = [&](char a, char b) 14 | { 15 | return !table[a] & table[b]; 16 | }; 17 | 18 | return std::inner_product(s.begin(), s.end()-1, s.begin()+1, 0, std::plus(), isWordEnd); 19 | } 20 | -------------------------------------------------------------------------------- /Lesson8/Exercise14/Snippet17.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int wordCount(const std::string &s) 6 | { 7 | // Create a lookup table for every pair of chars 8 | bool table[65536]; 9 | 10 | for(int i = 0; i < 256; ++i) 11 | { 12 | for(int j = 0; j < 256; ++j) 13 | { 14 | int idx = j + i * 256; 15 | table[idx] = !isspace(j) && isspace(i); 16 | } 17 | } 18 | 19 | auto isWordEnd = [&](char a, char b) 20 | { 21 | unsigned idx = (unsigned)a | (((unsigned)b) << 8); 22 | return table[idx]; 23 | }; 24 | 25 | return std::inner_product(s.begin(), s.end()-1, s.begin()+1, 0, std::plus(), isWordEnd); 26 | } 27 | -------------------------------------------------------------------------------- /Lesson8/Exercise14/Snippet18.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int wordCount(const std::string &s) 7 | { 8 | auto isWordEnd = [&](char a, char b) 9 | { 10 | return a > 32 & b < 33; 11 | }; 12 | 13 | return std::inner_product(s.begin(), s.end()-1, s.begin()+1, 0, std::plus(), isWordEnd); 14 | } 15 | -------------------------------------------------------------------------------- /Lesson8/Exercise14/Snippet19.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int wordCount(const std::string &s) 7 | { 8 | auto isWordStart = [&](char a, char b) 9 | { 10 | return a < 33 & b > 32; 11 | }; 12 | 13 | // Count the first word if any 14 | int count = s[0] > 32; 15 | 16 | // count the remaining 17 | return std::inner_product(s.begin(), s.end()-1, s.begin()+1, count, std::plus(), isWordStart); 18 | } 19 | -------------------------------------------------------------------------------- /Lesson8/Exercise14/Snippet20.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int wordCount(const std::string &s) 7 | { 8 | auto isWordEnd = [&](char a, char b) 9 | { 10 | return a > 32 & b < 33; 11 | }; 12 | 13 | int count = std::inner_product(s.begin(), s.end()-1, s.begin()+1, 0, std::plus(), isWordEnd); 14 | 15 | // count the last word if any 16 | if(s.back() > 32) 17 | { 18 | ++count; 19 | } 20 | 21 | return count; 22 | } 23 | -------------------------------------------------------------------------------- /Lesson8/Exercise14/Snippet21.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int wordCount(const std::string &s) 7 | { 8 | auto isWordEnd = [&](char a, char b) 9 | { 10 | return a > 32 & b < 33; 11 | }; 12 | 13 | const char *p = s.c_str(); 14 | return std::inner_product(p, p + s.size(), p+1, 0, std::plus(), isWordEnd); 15 | } 16 | -------------------------------------------------------------------------------- /Lesson8/Exercise14/Snippet22.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | #include "Timer.h" 6 | 7 | int wordCount(const std::string &s) 8 | { 9 | const char *p = &s[0]; 10 | int n = s.size() / 32; 11 | 12 | __m256i rspace = _mm256_set1_epi8(' '); 13 | uint64_t x, sum = 0; 14 | 15 | for(int i = 0; i < n; ++i) 16 | { 17 | __m256i r1 = _mm256_lddqu_si256((__m256i*)p); 18 | __m256i r2 = _mm256_lddqu_si256((__m256i*)(p+1)); 19 | __m256i r3 = _mm256_cmpgt_epi8(r1, rspace); 20 | __m256i r4 = _mm256_cmpgt_epi8(r2, rspace); 21 | __m256i r6 = _mm256_andnot_si256(r4, r3); 22 | 23 | for(int i = 0; i < 4; ++i) 24 | { 25 | x = _mm256_extract_epi64(r6, i); 26 | sum += _mm_popcnt_u64(x); 27 | } 28 | 29 | p += 32; 30 | } 31 | 32 | return sum / 8; 33 | } 34 | 35 | 36 | -------------------------------------------------------------------------------- /Lesson8/Exercise14/SnippetWC.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "Timer.h" 7 | 8 | using std::string; 9 | using std::cerr; 10 | using std::endl; 11 | using std::ifstream; 12 | using std::ostringstream; 13 | 14 | int wordCount(const string &s); 15 | 16 | int main(int argc, char **argv) 17 | { 18 | if(argc > 1) 19 | { 20 | Timer t("|Total Time|"); 21 | string sContent; 22 | ostringstream buf; 23 | ifstream ifs(argv[1]); 24 | 25 | { 26 | Timer t("File read"); 27 | buf << ifs.rdbuf(); 28 | sContent = buf.str(); 29 | sContent.push_back(' '); 30 | } 31 | 32 | int wc; 33 | { 34 | Timer t("Word count"); 35 | wc = wordCount(sContent); 36 | } 37 | 38 | cerr << wc << endl; 39 | } 40 | 41 | Timer::dump(); 42 | } 43 | -------------------------------------------------------------------------------- /Lesson8/Exercise14/SnippetWC1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "Timer.h" 7 | 8 | using std::string; 9 | using std::cerr; 10 | using std::endl; 11 | using std::ifstream; 12 | using std::ostringstream; 13 | 14 | int wordCount(const string &s); 15 | 16 | int main(int argc, char **argv) 17 | { 18 | if(argc > 1) 19 | { 20 | Timer t("|Total Time|"); 21 | string sContent; 22 | ostringstream buf; 23 | ifstream ifs(argv[1]); 24 | 25 | { 26 | Timer t("File read"); 27 | buf << ifs.rdbuf(); 28 | 29 | sContent.reserve(buf.str().size() + 1); 30 | sContent.resize(buf.str().size()); 31 | 32 | cerr << uint64_t(sContent.data()) << " before pb" << endl; 33 | sContent.push_back(' '); 34 | cerr << uint64_t(sContent.data()) << endl; 35 | 36 | 37 | } 38 | 39 | int wc; 40 | { 41 | Timer t("Word count"); 42 | wc = wordCount(sContent); 43 | } 44 | 45 | cerr << wc << endl; 46 | } 47 | 48 | Timer::dump(); 49 | } 50 | -------------------------------------------------------------------------------- /Lesson8/Exercise14/SnippetWC2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "Timer.h" 7 | 8 | using std::string; 9 | using std::cerr; 10 | using std::endl; 11 | using std::ifstream; 12 | using std::ostringstream; 13 | 14 | int wordCount(const string &s); 15 | 16 | int main(int argc, char **argv) 17 | { 18 | if(argc > 1) 19 | { 20 | Timer t("|Total Time|"); 21 | string sContent; 22 | ostringstream buf; 23 | ifstream ifs(argv[1]); 24 | 25 | { 26 | Timer t("File read"); 27 | buf << ifs.rdbuf(); 28 | } 29 | 30 | { 31 | Timer t("String copy"); 32 | sContent = buf.str(); 33 | } 34 | 35 | { 36 | Timer t("String push"); 37 | sContent.push_back(' '); 38 | } 39 | 40 | 41 | int wc; 42 | { 43 | Timer t("Word count"); 44 | wc = wordCount(sContent); 45 | } 46 | 47 | cerr << wc << endl; 48 | } 49 | 50 | Timer::dump(); 51 | } 52 | -------------------------------------------------------------------------------- /Lesson8/Exercise14/SnippetWC3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "Timer.h" 7 | 8 | using std::string; 9 | using std::cerr; 10 | using std::endl; 11 | using std::ifstream; 12 | using std::ostringstream; 13 | 14 | int wordCount(const string &s); 15 | 16 | int main(int argc, char **argv) 17 | { 18 | if(argc > 1) 19 | { 20 | Timer t("|Total Time|"); 21 | string sContent; 22 | ostringstream buf; 23 | ifstream ifs(argv[1]); 24 | 25 | { 26 | Timer t("File read"); 27 | buf << ifs.rdbuf(); 28 | } 29 | 30 | { 31 | Timer t("String copy"); 32 | sContent = buf.str(); 33 | } 34 | 35 | int wc; 36 | { 37 | Timer t("Word count"); 38 | wc = wordCount(sContent); 39 | } 40 | 41 | cerr << wc << endl; 42 | } 43 | 44 | Timer::dump(); 45 | } 46 | -------------------------------------------------------------------------------- /Lesson8/Exercise14/SnippetWC4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "Timer.h" 7 | 8 | using std::string; 9 | using std::cerr; 10 | using std::endl; 11 | using std::ifstream; 12 | using std::ostringstream; 13 | 14 | int wordCount(const string &s); 15 | 16 | int main(int argc, char **argv) 17 | { 18 | if(argc > 1) 19 | { 20 | Timer t("|Total Time|"); 21 | ifstream ifs(argv[1]); 22 | 23 | string sContent; 24 | 25 | { 26 | Timer t("String Alloc"); 27 | 28 | // Seek to end and reserve memory 29 | ifs.seekg(0, std::ios::end); 30 | sContent.resize(ifs.tellg()); 31 | } 32 | 33 | { 34 | Timer t("File read"); 35 | 36 | // Seek back to start and read data 37 | ifs.seekg(0, std::ios::beg); 38 | ifs.read(&sContent[0], sContent.size()); 39 | } 40 | 41 | int wc; 42 | { 43 | Timer t("Word count"); 44 | wc = wordCount(sContent); 45 | } 46 | 47 | cerr << wc << endl; 48 | } 49 | 50 | Timer::dump(); 51 | } 52 | -------------------------------------------------------------------------------- /Lesson8/Exercise14/SnippetWC5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "Timer.h" 7 | 8 | using std::string; 9 | using std::cerr; 10 | using std::endl; 11 | using std::ifstream; 12 | using std::ostringstream; 13 | 14 | int wordCount(const string &s); 15 | 16 | int main(int argc, char **argv) 17 | { 18 | if(argc > 1) 19 | { 20 | Timer t("|Total Time|"); 21 | ifstream ifs(argv[1]); 22 | 23 | string sContent; 24 | size_t len; 25 | { 26 | Timer t("String Alloc"); 27 | 28 | // Seek to end 29 | ifs.seekg(0, std::ios::end); 30 | 31 | // Get length and allow for 1 extra character at the end 32 | len = ifs.tellg(); 33 | ++len; 34 | 35 | // Pad to 32 byte boundary 36 | size_t rem = len % 32; 37 | if(rem) 38 | { 39 | len += (32 - rem); 40 | } 41 | sContent.resize(len); 42 | } 43 | 44 | { 45 | Timer t("File read"); 46 | 47 | // Seek back to start and read data 48 | ifs.seekg(0, std::ios::beg); 49 | ifs.read(&sContent[0], sContent.size()); 50 | } 51 | 52 | int wc; 53 | { 54 | Timer t("Word count"); 55 | wc = wordCount(sContent); 56 | } 57 | 58 | cerr << wc << endl; 59 | } 60 | 61 | Timer::dump(); 62 | } 63 | -------------------------------------------------------------------------------- /Lesson8/Exercise14/data.7z: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TrainingByPackt/Advanced-CPlusPlus/b2ba3fc955b1dc0c617b0b2543fc82ad36573ff2/Lesson8/Exercise14/data.7z -------------------------------------------------------------------------------- /Lesson8/readme.md: -------------------------------------------------------------------------------- 1 | # Advanced C++ 2 | Write robust C++ code so fast they will think you cheated --------------------------------------------------------------------------------