refactor: restructure project to build a library for each book chapter
This commit is contained in:
parent
5a3798a604
commit
dfaee89d56
11 changed files with 15 additions and 6 deletions
1
chapters/CMakeLists.txt
Normal file
1
chapters/CMakeLists.txt
Normal file
|
@ -0,0 +1 @@
|
|||
add_subdirectory(chapter_02)
|
4
chapters/chapter_02/CMakeLists.txt
Normal file
4
chapters/chapter_02/CMakeLists.txt
Normal 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)
|
5
chapters/chapter_02/include/constexp.h
Normal file
5
chapters/chapter_02/include/constexp.h
Normal file
|
@ -0,0 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
namespace constexp {
|
||||
void test();
|
||||
};
|
7
chapters/chapter_02/include/ifswitch.h
Normal file
7
chapters/chapter_02/include/ifswitch.h
Normal file
|
@ -0,0 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
namespace ifswitch {
|
||||
|
||||
void test();
|
||||
|
||||
};
|
5
chapters/chapter_02/include/initlist.h
Normal file
5
chapters/chapter_02/include/initlist.h
Normal file
|
@ -0,0 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
namespace initlist {
|
||||
void test();
|
||||
};
|
5
chapters/chapter_02/include/null.h
Normal file
5
chapters/chapter_02/include/null.h
Normal file
|
@ -0,0 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
namespace null {
|
||||
void test();
|
||||
};
|
27
chapters/chapter_02/src/constexp.cpp
Normal file
27
chapters/chapter_02/src/constexp.cpp
Normal 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;
|
||||
}
|
25
chapters/chapter_02/src/ifswitch.cpp
Normal file
25
chapters/chapter_02/src/ifswitch.cpp
Normal 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;
|
||||
}
|
||||
}
|
22
chapters/chapter_02/src/initlist.cpp
Normal file
22
chapters/chapter_02/src/initlist.cpp
Normal 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;
|
||||
}
|
29
chapters/chapter_02/src/null.cpp
Normal file
29
chapters/chapter_02/src/null.cpp
Normal 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);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue