refactor: restructure project to build a library for each book chapter

This commit is contained in:
Michael Mandl 2023-10-21 11:43:01 +02:00
parent 5a3798a604
commit dfaee89d56
Signed by: mandlm
GPG key ID: 4AA25D647AA54CC7
11 changed files with 15 additions and 6 deletions

1
chapters/CMakeLists.txt Normal file
View file

@ -0,0 +1 @@
add_subdirectory(chapter_02)

View file

@ -0,0 +1,4 @@
add_library(chapter_02 src/initlist.cpp src/null.cpp src/constexp.cpp
src/ifswitch.cpp)
target_include_directories(chapter_02 PUBLIC include)

View file

@ -0,0 +1,5 @@
#pragma once
namespace constexp {
void test();
};

View file

@ -0,0 +1,7 @@
#pragma once
namespace ifswitch {
void test();
};

View file

@ -0,0 +1,5 @@
#pragma once
namespace initlist {
void test();
};

View file

@ -0,0 +1,5 @@
#pragma once
namespace null {
void test();
};

View file

@ -0,0 +1,27 @@
#include "constexp.h"
#include <iostream>
constexpr unsigned int fibonacci(const unsigned int n) {
if (n == 1 || n == 2) {
return 1;
}
return fibonacci(n - 2) + fibonacci(n - 1);
}
void constexp::test() {
std::cout << "Fibonacci(10): ";
for (const unsigned int n : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) {
std::cout << fibonacci(n) << " ";
}
std::cout << std::endl;
int arr_1[fibonacci(10)];
std::cout << "Array is " << sizeof(arr_1) / sizeof(int) << " items long"
<< std::endl;
constexpr unsigned int arr_2_len = 10;
int arr_2[arr_2_len];
std::cout << "Array is " << sizeof(arr_2) / sizeof(int) << " items long"
<< std::endl;
}

View file

@ -0,0 +1,25 @@
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <vector>
#include "ifswitch.h"
void ifswitch::test() {
std::vector<uint32_t> vec = {1, 2, 3, 4};
if (auto it = std::find(vec.begin(), vec.end(), 23); it != vec.end()) {
std::cout << "Found element 23 in vector" << std::endl;
} else {
std::cout << "No element 23 in vector" << std::endl;
}
switch (auto it = vec.rbegin(); *it) {
case 4:
std::cout << "Found 4 at the back" << std::endl;
break;
default:
std::cout << "No 4 at the back" << std::endl;
break;
}
}

View file

@ -0,0 +1,22 @@
#include "initlist.h"
#include <initializer_list>
#include <iostream>
#include <vector>
class InitList {
public:
std::vector<unsigned int> _list;
InitList(std::initializer_list<unsigned int> list) : _list(list) {}
};
void initlist::test() {
InitList initList{1, 2, 3, 4, 5};
std::cout << "InitList: ";
for (const auto &item : initList._list) {
std::cout << item << " ";
}
std::cout << std::endl;
}

View file

@ -0,0 +1,29 @@
#include <iostream>
#include <type_traits>
#include "null.h"
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() {
if (std::is_same<decltype(NULL), decltype(0)>::value) {
std::cout << "NULL == 0" << std::endl;
}
if (std::is_same<decltype(NULL), decltype((void *)0)>::value) {
std::cout << "NULL == (void *)0" << std::endl;
}
if (std::is_same<decltype(NULL), decltype(__null)>::value) {
std::cout << "NULL == __null" << std::endl;
}
if (std::is_same<decltype(NULL), std::nullptr_t>::value) {
std::cout << "NULL == nullptr" << std::endl;
}
call_test(0);
call_test(nullptr);
}