Skip to content

Commit 0271c82

Browse files
Merge pull request #2 from contour-terminal/improvement/clean_and_update
Update: github actions, project name, cmake CMP0135
2 parents 6100165 + 90a1d12 commit 0271c82

File tree

5 files changed

+61
-141
lines changed

5 files changed

+61
-141
lines changed

.github/workflows/build.yml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ jobs:
1313
strategy:
1414
fail-fast: false
1515
matrix:
16-
cxx: [11,14,17, 20]
16+
cxx: [17, 20]
1717
build_type: ["RelWithDebInfo"]
1818
compiler:
1919
[
20-
"g++-11",
20+
"g++-12",
2121
"clang++-14"
2222
]
2323
name: "Ubuntu 22.04 (${{ matrix.compiler }}, C++${{ matrix.cxx }}, ${{matrix.build_type}})"
@@ -34,17 +34,13 @@ jobs:
3434
- name: "update APT database"
3535
run: sudo apt -q update
3636

37-
- name: Install GCC 11
38-
if: ${{ startsWith(matrix.compiler, 'g++-11') }}
39-
run: sudo apt install -y g++-11
40-
- name: Install Clang 14
41-
if: ${{ startsWith(matrix.compiler, 'clang++-14') }}
42-
run: sudo apt install -y clang-14
37+
- name: Install Compilers
38+
run: sudo apt install -y g++-11 clang-14
4339

4440
- name: "Download dependencies"
4541
run: sudo apt install cmake ninja-build
4642
- name: "Cmake configure"
47-
run: cmake -S . -B build -G Ninja -D BOXED_CPP_TESTS=ON -DCMAKE_CXX_COMPILER=${{ matrix.compiler }} -DCMAKE_CXX_STANDARD=${{ matrix.cxx }} -DCMAKE_BUILD_TYPE=${{matrix.build_type}}
43+
run: cmake -S . -B build -G Ninja -D BOXED_CPP_TESTS=ON -D ENABLE_TIDY=ON -DPEDANTIC_COMPILER=ON -D CMAKE_CXX_FLAGS="-Wno-unknown-warning-option" -DCMAKE_CXX_COMPILER=${{ matrix.compiler }} -DCMAKE_CXX_STANDARD=${{ matrix.cxx }} -DCMAKE_BUILD_TYPE=${{matrix.build_type}}
4844
- name: "build "
4945
run: cmake --build build --parallel 3
5046

CMakeLists.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
cmake_minimum_required(VERSION 3.10)
2-
project(contour VERSION "0.1.0" LANGUAGES CXX)
2+
project(boxed-cpp VERSION "0.1.0" LANGUAGES CXX)
33
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
44

55
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
66

77
include(ThirdParties)
8-
ThirdPartiesAdd_Catch2()
9-
108
include(ClangTidy)
119
include(PedanticCompiler)
1210

@@ -26,6 +24,7 @@ target_include_directories(boxed-cpp INTERFACE
2624

2725
option(BOXED_CPP_TESTS "Enables building of unittests for boxed-cpp [default: ON]" OFF)
2826
if(BOXED_CPP_TESTS)
27+
ThirdPartiesAdd_Catch2()
2928
enable_testing()
3029
add_executable(test-boxed-cpp
3130
test-boxed-cpp.cpp

README.md

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# C++ primitive type boxing
22

33
This is a small header-only library for easing primitive type boxing in C++.
4+
Primary goal of the library is to make it easy to avoid code with easily swappable parameters [clang-tidy:bugprone-easily-swappable-parameters](https://clang.llvm.org/extra/clang-tidy/checks/bugprone/easily-swappable-parameters.html).
5+
6+
Overview on the topic: [C++ Weekly With Jason Turner](https://www.youtube.com/watch?v=Zq4yYPG7Erc)
47

58
library is created to aid code health in [Contour Terminal Emulator](https://github.com/christianparpart/contour/).
69

@@ -15,9 +18,9 @@ This header can be simply copied into a project or used via CMake builtin functi
1518
// Create unique structures
1619
namespace tags { struct Speed{}; struct Permittivity{}; struct Permeability{}; }
1720

18-
using Speed = boxed::boxed<double,tags::Speed>;
19-
using Permittivity = boxed::boxed<double,tags::Permittivity>;
20-
using Permeability = boxed::boxed<double,tags::Permeability>;
21+
using Speed = boxed::boxed<double, tags::Speed>;
22+
using Permittivity = boxed::boxed<double, tags::Permittivity>;
23+
using Permeability = boxed::boxed<double, tags::Permeability>;
2124

2225

2326
int main()
@@ -37,6 +40,50 @@ int main()
3740
```
3841

3942
`
43+
# More advanced usage
44+
You can forget about the order of parameters in your code
45+
46+
``` c++
47+
#include <boxed-cpp/boxed.hpp>
48+
49+
// Create unique structures
50+
namespace tags { struct Speed{}; struct Permittivity{}; struct Permeability{}; }
51+
52+
using Speed = boxed::boxed<double, tags::Speed>;
53+
using Permittivity = boxed::boxed<double, tags::Permittivity>;
54+
using Permeability = boxed::boxed<double, tags::Permeability>;
55+
56+
Speed wave_speed_inside(Permittivity epsilon, Permeability mu)
57+
{
58+
return Speed(1.0 / std::sqrt(unbox(epsilon) * unbox(mu)));
59+
};
60+
61+
template <typename T, typename S>
62+
Speed wave_speed(T t, S s) // recursive variadic function
63+
{
64+
if constexpr (std::is_same_v<T, Permittivity>)
65+
{
66+
return wave_speed_inside(t, s);
67+
}
68+
else
69+
{
70+
return wave_speed_inside(s, t);
71+
}
72+
}
73+
74+
75+
int main()
76+
{
77+
constexpr auto vacuum_permittivity = Permittivity(8.85418781762039e-12);
78+
constexpr auto pi = 3.14159265358979323846;
79+
constexpr auto vacuum_permeability = Permeability(4 * pi * 1e-7);
80+
81+
auto speed_one = wave_speed(vacuum_permittivity, vacuum_permeability);
82+
auto speed_two = wave_speed(vacuum_permeability, vacuum_permittivity);
83+
// speed_one == speed_two
84+
}
85+
```
86+
4087
4188
4289
### License

cmake/ThirdParties.cmake

Lines changed: 2 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ macro(ThirdPartiesAdd_Catch2)
6464
DOWNLOAD_DIR "${3rdparty_DOWNLOAD_DIR}"
6565
DOWNLOAD_NAME "catch2-${3rdparty_Catch2_VERSION}.tar.gz"
6666
EXCLUDE_FROM_ALL
67+
DOWNLOAD_EXTRACT_TIMESTAMP 1
6768
)
6869
FetchContent_MakeAvailable(Catch2)
6970
else()
@@ -75,6 +76,7 @@ macro(ThirdPartiesAdd_Catch2)
7576
DOWNLOAD_DIR "${3rdparty_DOWNLOAD_DIR}"
7677
DOWNLOAD_NAME "catch2-${3rdparty_Catch2_VERSION}.tar.gz"
7778
EXCLUDE_FROM_ALL
79+
DOWNLOAD_EXTRACT_TIMESTAMP 1
7880
)
7981
endif()
8082
endmacro()
@@ -108,128 +110,3 @@ macro(ThirdPartiesAdd_range_v3)
108110
)
109111
endif()
110112
endmacro()
111-
112-
# {{{ yaml-cpp
113-
macro(ThirdPartiesAdd_yaml_cpp)
114-
set(3rdparty_yaml_cpp_VERSION "0.6.3" CACHE STRING "Embedded yaml-cpp version")
115-
set(3rdparty_yaml_cpp_CHECKSUM "MD5=b45bf1089a382e81f6b661062c10d0c2" CACHE STRING "Embedded yaml-cpp checksum")
116-
#set(yaml_cpp_patch "${CMAKE_CURRENT_SOURCE_DIR}/cmake/yaml-cpp.patch")
117-
set(yaml_cpp_patch "${CMAKE_CURRENT_BINARY_DIR}/patches/yaml-cpp.patch")
118-
if(NOT EXISTS "${yaml_cpp_patch}")
119-
file(WRITE "${yaml_cpp_patch}"
120-
[[--- CMakeLists.txt 2021-03-03 08:33:57.271688830 +0100
121-
+++ CMakeLists.txt.new 2021-03-03 09:32:34.817113397 +0100
122-
@@ -15,8 +15,8 @@
123-
### Project options
124-
###
125-
## Project stuff
126-
-option(YAML_CPP_BUILD_TESTS "Enable testing" ON)
127-
-option(YAML_CPP_BUILD_TOOLS "Enable parse tools" ON)
128-
+option(YAML_CPP_BUILD_TESTS "Enable testing" OFF)
129-
+option(YAML_CPP_BUILD_TOOLS "Enable parse tools" OFF)
130-
option(YAML_CPP_BUILD_CONTRIB "Enable contrib stuff in library" ON)
131-
option(YAML_CPP_INSTALL "Enable generation of install target" ON)
132-
133-
@@ -259,7 +259,7 @@
134-
endif()
135-
136-
if (NOT CMAKE_VERSION VERSION_LESS 2.8.12)
137-
- target_include_directories(yaml-cpp
138-
+ target_include_directories(yaml-cpp SYSTEM
139-
PUBLIC $<BUILD_INTERFACE:${YAML_CPP_SOURCE_DIR}/include>
140-
$<INSTALL_INTERFACE:${INCLUDE_INSTALL_ROOT_DIR}>
141-
PRIVATE $<BUILD_INTERFACE:${YAML_CPP_SOURCE_DIR}/src>)
142-
]])
143-
endif()
144-
set(YAML_CPP_BUILD_CONTRIB OFF CACHE INTERNAL "")
145-
set(YAML_CPP_BUILD_TESTS OFF CACHE INTERNAL "")
146-
set(YAML_CPP_BUILD_TOOLS OFF CACHE INTERNAL "")
147-
set(YAML_CPP_INSTALL OFF CACHE INTERNAL "")
148-
if(THIRDPARTIES_HAS_FETCHCONTENT)
149-
FetchContent_Declare(
150-
yaml-cpp
151-
URL "http://github.com/jbeder/yaml-cpp/archive/yaml-cpp-${3rdparty_yaml_cpp_VERSION}.tar.gz"
152-
URL_HASH "${3rdparty_yaml_cpp_CHECKSUM}"
153-
DOWNLOAD_DIR "${3rdparty_DOWNLOAD_DIR}"
154-
DOWNLOAD_NAME "yaml-cpp-${3rdparty_yaml_cpp_VERSION}.tar.gz"
155-
EXCLUDE_FROM_ALL
156-
# PATCH_COMMAND breaks on GitHub CI for Windows (but not on local Windows machine, what?)
157-
# PATCH_COMMAND patch "${yaml_cpp_patch}"
158-
)
159-
FetchContent_MakeAvailable(yaml-cpp)
160-
else()
161-
download_project(
162-
PROJ yaml-cpp
163-
URL "http://github.com/jbeder/yaml-cpp/archive/yaml-cpp-${3rdparty_yaml_cpp_VERSION}.tar.gz"
164-
URL_HASH "${3rdparty_yaml_cpp_CHECKSUM}"
165-
DOWNLOAD_DIR "${3rdparty_DOWNLOAD_DIR}"
166-
DOWNLOAD_NAME "yaml-cpp-${3rdparty_yaml_cpp_VERSION}.tar.gz"
167-
EXCLUDE_FROM_ALL
168-
PREFIX "${FETCHCONTENT_BASE_DIR}/yaml-cpp-${3rdparty_range_v3_VERSION}"
169-
# PATCH_COMMAND breaks on GitHub CI for Windows (but not on local Windows machine, what?)
170-
# PATCH_COMMAND patch "${yaml_cpp_patch}"
171-
)
172-
endif()
173-
endmacro()
174-
# }}}
175-
176-
# {{{ libunicode
177-
macro(ThirdPartiesAdd_libunicode)
178-
set(3rdparty_libunicode_VERSION "8ae215269247a74e10b2ed90de023343cd447be5" CACHE STRING "libunicode: commit hash")
179-
set(3rdparty_libunicode_CHECKSUM "SHA256=256a22b22ee724cc6e61049e8649468697a2e2d83a8c7a6b2714ec36acdb59b2" CACHE STRING "libunicode: download checksum")
180-
# XXX: temporary patch until libunicode gets rid of sumbodules.
181-
set(libunicode_patch "${CMAKE_CURRENT_BINARY_DIR}/patches/libunicode.patch")
182-
if(NOT EXISTS "${libunicode_patch}")
183-
file(WRITE "${libunicode_patch}" [[
184-
--- CMakeLists.txt 2021-03-03 08:40:11.184332244 +0100
185-
+++ CMakeLists.txt.new 2021-03-03 08:49:06.336734764 +0100
186-
@@ -48,18 +48,6 @@
187-
# ----------------------------------------------------------------------------
188-
# 3rdparty dependencies
189-
190-
-if(LIBUNICODE_EMBEDDED_FMTLIB)
191-
- add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/fmt" EXCLUDE_FROM_ALL)
192-
- add_definitions(-DFMT_USE_WINDOWS_H=0)
193-
-else()
194-
- # master project must provide its own fmtlib
195-
-endif()
196-
-
197-
-if(LIBUNICODE_TESTING AND LIBUNICODE_EMBEDDED_CATCH2)
198-
- add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/catch2")
199-
-else()
200-
- # master project must provide its own fmtlib
201-
-endif()
202-
203-
add_subdirectory(src/unicode)
204-
add_subdirectory(src/tools)
205-
]])
206-
endif()
207-
if(THIRDPARTIES_HAS_FETCHCONTENT)
208-
FetchContent_Declare(
209-
libunicode
210-
URL "https://github.com/christianparpart/libunicode/archive/${3rdparty_libunicode_VERSION}.tar.gz"
211-
URL_HASH "${3rdparty_libunicode_CHECKSUM}"
212-
DOWNLOAD_DIR "${3rdparty_DOWNLOAD_DIR}"
213-
DOWNLOAD_NAME "libunicode-${3rdparty_libunicode_VERSION}.tar.gz"
214-
UPDATE_DISCONNECTED 0
215-
EXCLUDE_FROM_ALL
216-
# same here
217-
#PATCH_COMMAND patch "${libunicode_patch}"
218-
)
219-
FetchContent_MakeAvailable(libunicode)
220-
else()
221-
download_project(
222-
PROJ libunicode
223-
URL "https://github.com/christianparpart/libunicode/archive/${3rdparty_libunicode_VERSION}.zip"
224-
URL_HASH "${3rdparty_libunicode_CHECKSUM}"
225-
DOWNLOAD_DIR "${3rdparty_DOWNLOAD_DIR}"
226-
DOWNLOAD_NAME "libunicode-${3rdparty_libunicode_VERSION}.tar.gz"
227-
EXCLUDE_FROM_ALL
228-
PREFIX "${FETCHCONTENT_BASE_DIR}/libunicode-${3rdparty_libunicode_VERSION}"
229-
# same here
230-
#PATCH_COMMAND patch "${libunicode_patch}"
231-
)
232-
endif()
233-
endmacro()
234-
# }}}
235-

test-boxed-cpp.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ TEST_CASE("boxed_cast with different inner types")
5353
{
5454
auto constexpr a = N{3};
5555
auto constexpr b = boxed_cast<Z>(a);
56-
56+
#ifndef __GNUG__
5757
static_assert(*a == *b);
58+
#endif
5859
static_assert(std::is_same_v<decltype(b), Z const>);
5960
}
6061

0 commit comments

Comments
 (0)