diff --git a/chapters/CMakeLists.txt b/chapters/CMakeLists.txt index 5c13caf..73f2f7f 100644 --- a/chapters/CMakeLists.txt +++ b/chapters/CMakeLists.txt @@ -1 +1,2 @@ +add_subdirectory(interface) add_subdirectory(chapter_02) diff --git a/chapters/chapter_02/CMakeLists.txt b/chapters/chapter_02/CMakeLists.txt index 51d62aa..7be36fd 100644 --- a/chapters/chapter_02/CMakeLists.txt +++ b/chapters/chapter_02/CMakeLists.txt @@ -1,4 +1,8 @@ add_library(chapter_02 src/initlist.cpp src/null.cpp src/constexp.cpp src/ifswitch.cpp) +target_compile_features(chapter_02 PUBLIC cxx_std_20) + target_include_directories(chapter_02 PUBLIC include) + +target_link_libraries(chapter_02 PUBLIC chapter_interface) diff --git a/chapters/chapter_02/include/constexp.h b/chapters/chapter_02/include/constexp.h index 362b4ff..1e9d6ef 100644 --- a/chapters/chapter_02/include/constexp.h +++ b/chapters/chapter_02/include/constexp.h @@ -1,5 +1,8 @@ #pragma once -namespace constexp { -void test(); +#include "chapter_interface.h" + +class ConstExpTest : public ChapterTest { +public: + virtual void test() override; }; diff --git a/chapters/chapter_02/include/ifswitch.h b/chapters/chapter_02/include/ifswitch.h index 405756b..dfd4a6d 100644 --- a/chapters/chapter_02/include/ifswitch.h +++ b/chapters/chapter_02/include/ifswitch.h @@ -1,7 +1,8 @@ #pragma once -namespace ifswitch { - -void test(); +#include "chapter_interface.h" +class IfswitchTest : public ChapterTest { +public: + virtual void test() override; }; diff --git a/chapters/chapter_02/include/initlist.h b/chapters/chapter_02/include/initlist.h index aa02a20..b92f891 100644 --- a/chapters/chapter_02/include/initlist.h +++ b/chapters/chapter_02/include/initlist.h @@ -1,5 +1,8 @@ #pragma once -namespace initlist { -void test(); +#include "chapter_interface.h" + +class InitListTest : public ChapterTest { +public: + virtual void test() override; }; diff --git a/chapters/chapter_02/include/null.h b/chapters/chapter_02/include/null.h index e8434f7..f72f834 100644 --- a/chapters/chapter_02/include/null.h +++ b/chapters/chapter_02/include/null.h @@ -1,5 +1,8 @@ #pragma once -namespace null { -void test(); +#include "chapter_interface.h" + +class NullTest : public ChapterTest { +public: + virtual void test() override; }; diff --git a/chapters/chapter_02/src/constexp.cpp b/chapters/chapter_02/src/constexp.cpp index f0a5d73..3d3479a 100644 --- a/chapters/chapter_02/src/constexp.cpp +++ b/chapters/chapter_02/src/constexp.cpp @@ -9,7 +9,7 @@ constexpr unsigned int fibonacci(const unsigned int n) { return fibonacci(n - 2) + fibonacci(n - 1); } -void constexp::test() { +void ConstExpTest::test() { std::cout << "Fibonacci(10): "; for (const unsigned int n : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) { std::cout << fibonacci(n) << " "; diff --git a/chapters/chapter_02/src/ifswitch.cpp b/chapters/chapter_02/src/ifswitch.cpp index 995ff1a..54ce430 100644 --- a/chapters/chapter_02/src/ifswitch.cpp +++ b/chapters/chapter_02/src/ifswitch.cpp @@ -5,7 +5,7 @@ #include "ifswitch.h" -void ifswitch::test() { +void IfswitchTest::test() { std::vector vec = {1, 2, 3, 4}; if (auto it = std::find(vec.begin(), vec.end(), 23); it != vec.end()) { diff --git a/chapters/chapter_02/src/initlist.cpp b/chapters/chapter_02/src/initlist.cpp index 01c0c02..3eb17c8 100644 --- a/chapters/chapter_02/src/initlist.cpp +++ b/chapters/chapter_02/src/initlist.cpp @@ -10,7 +10,7 @@ public: InitList(std::initializer_list list) : _list(list) {} }; -void initlist::test() { +void InitListTest::test() { InitList initList{1, 2, 3, 4, 5}; diff --git a/chapters/chapter_02/src/null.cpp b/chapters/chapter_02/src/null.cpp index 066e95f..dc71324 100644 --- a/chapters/chapter_02/src/null.cpp +++ b/chapters/chapter_02/src/null.cpp @@ -7,7 +7,7 @@ void call_test(char *) { std::cout << "call_test(char *) called" << std::endl; } void call_test(int) { std::cout << "call_test(int) called" << std::endl; } -void null::test() { +void NullTest::test() { if (std::is_same::value) { std::cout << "NULL == 0" << std::endl; } diff --git a/chapters/interface/CMakeLists.txt b/chapters/interface/CMakeLists.txt new file mode 100644 index 0000000..c455881 --- /dev/null +++ b/chapters/interface/CMakeLists.txt @@ -0,0 +1,3 @@ +add_library(chapter_interface INTERFACE) +target_compile_features(chapter_interface INTERFACE cxx_std_20) +target_include_directories(chapter_interface INTERFACE include) diff --git a/chapters/interface/include/chapter_interface.h b/chapters/interface/include/chapter_interface.h new file mode 100644 index 0000000..d45b9ad --- /dev/null +++ b/chapters/interface/include/chapter_interface.h @@ -0,0 +1,7 @@ +#pragma once + +class ChapterTest { +public: + virtual ~ChapterTest() = default; + virtual void test() = 0; +}; diff --git a/src/main.cpp b/src/main.cpp index 8dd124e..0d3f429 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,8 @@ #include #include +#include +#include +#include #include "constexp.h" #include "ifswitch.h" @@ -12,20 +15,18 @@ int main(int argc, char **argv) { std::cout << "Running " << PROJECT_NAME << ", version " << VERSION << "\n" << std::endl; - std::cout << "Null test:" << std::endl; - null::test(); + std::vector>> + chapterTests; + chapterTests.emplace_back("Null test", std::make_unique()); + chapterTests.emplace_back("If/Switch test", std::make_unique()); + chapterTests.emplace_back("Constexpr test", std::make_unique()); + chapterTests.emplace_back("Initializer list test", + std::make_unique()); - std::cout << "\n" - << "Constexpr test:" << std::endl; - constexp::test(); - - std::cout << "\n" - << "Ifswitch test:" << std::endl; - ifswitch::test(); - - std::cout << "\n" - << "Initializer list test:" << std::endl; - initlist::test(); + for (auto &[name, test] : chapterTests) { + std::cout << "\n" << name << std::endl; + test->test(); + } std::cout << "\nExiting" << std::endl;