Skip to content

Commit 6a56b3a

Browse files
authored
feat(add): make numbers able to be included by other code (#15)
* adopt the package layout of Cargo * re-arrange tests * remove dependency of pthread * make numbers include-able
1 parent 6f7283d commit 6a56b3a

23 files changed

+159
-124
lines changed

CMakeLists.txt

+22-8
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,35 @@ set(BUILD_SHARED_LIBS OFF)
44
set(CMAKE_CXX_STANDARD 17)
55
set(CMAKE_CXX_STANDARD_REQUIRED ON)
66

7-
set(version 0.0.1)
7+
string(COMPARE EQUAL ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR} PROJECT_IS_TOP_LEVEL)
8+
9+
if (PROJECT_IS_TOP_LEVEL)
10+
message(STATUS "Project is top level")
11+
endif()
12+
13+
option(NUMBERS_TEST "Build and perform numbers tests" ${PROJECT_IS_TOP_LEVEL})
14+
15+
set(version 0.0.2)
816

917
project(numbers VERSION ${version} LANGUAGES CXX)
1018

1119
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
1220

1321
# Includes.
1422
set(SRC_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/src/include)
15-
set(THIRD_PARTY_INCLUDE_DIR
16-
${PROJECT_SOURCE_DIR}/third_party
17-
)
1823

19-
include_directories(${SRC_INCLUDE_DIR} ${THIRD_PARTY_INCLUDE_DIR})
24+
include_directories(${SRC_INCLUDE_DIR})
2025

2126
add_subdirectory(src)
22-
add_subdirectory(examples)
23-
add_subdirectory(third_party)
24-
add_subdirectory(test)
27+
28+
if (NUMBERS_TEST)
29+
message(STATUS "Building and running tests")
30+
set(THIRD_PARTY_INCLUDE_DIR
31+
${PROJECT_SOURCE_DIR}/third_party
32+
)
33+
include_directories(${THIRD_PARTY_INCLUDE_DIR})
34+
35+
add_subdirectory(examples)
36+
add_subdirectory(third_party)
37+
add_subdirectory(tests)
38+
endif()

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ numbers
1717

1818
- **Full Control** over handling integer overflow
1919

20-
- Same as **Plain Old Data** (WIP)
20+
- Same as **Primitive Types** (WIP)
2121

2222
- **Support Integers**: i8, i16, i32, i64, u8, u16, u32, u64, even i128 & u128 (not ready yet)
2323

examples/CMakeLists.txt

+26-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
cmake_minimum_required(VERSION 3.10)
22

3-
project(NumbersExample LANGUAGES CXX)
3+
file(GLOB_RECURSE EXAMPLE_SOURCES "${PROJECT_SOURCE_DIR}/examples/*.cc")
44

5-
add_executable(${PROJECT_NAME} EXCLUDE_FROM_ALL main.cc)
5+
add_custom_target(example)
66

7-
target_link_libraries(${PROJECT_NAME} PRIVATE numbers_obj)
7+
foreach (example_source ${EXAMPLE_SOURCES})
8+
get_filename_component(example_filename ${example_source} NAME)
9+
string(REPLACE ".cc" "" example_name ${example_filename})
10+
add_executable(${example_name} EXCLUDE_FROM_ALL ${example_source})
811

9-
add_custom_target(example
10-
COMMAND ${PROJECT_NAME}
11-
DEPENDS ${PROJECT_NAME}
12-
COMMENT "Running Example..."
13-
)
12+
add_custom_command(
13+
TARGET example
14+
COMMENT "Running example ${example_name}..."
15+
COMMAND $<TARGET_FILE:${example_name}>
16+
USES_TERMINAL
17+
)
18+
19+
target_link_libraries(${example_name} PRIVATE numbers_obj)
20+
21+
set_target_properties(${example_name}
22+
PROPERTIES
23+
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/examples"
24+
COMMAND ${example_name}
25+
)
26+
27+
add_custom_target(example-${example_name}
28+
COMMENT "Running example ${example_name}..."
29+
COMMAND ${example_name}
30+
)
31+
endforeach ()

examples/arithmetic.cc

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <iostream>
2+
#include "numbers.h"
3+
4+
int main(int argc, char const *argv[]) {
5+
std::cout << "==== arithmetic_example ==== \n";
6+
numbers::i8 a = 100;
7+
std::cout << a << '\n';
8+
try {
9+
a = a + a;
10+
std::cout << a << '\n';
11+
} catch (std::runtime_error &err) {
12+
std::cout << "Catch error: " << err.what() << '\n';
13+
}
14+
15+
numbers::i8 b = 127;
16+
numbers::i8 c = 0;
17+
try {
18+
numbers::i8 ret = c - b;
19+
std::cout << ret << '\n';
20+
c = -10;
21+
ret = c - b;
22+
std::cout << ret << '\n';
23+
} catch (std::runtime_error &err) {
24+
std::cout << "Catch error: " << err.what() << '\n';
25+
}
26+
27+
auto d = static_cast<numbers::i16>(b);
28+
try {
29+
d = d + d;
30+
std::cout << d << '\n';
31+
} catch (std::runtime_error &err) {
32+
std::cout << "Catch error: " << err.what() << '\n';
33+
}
34+
35+
return 0;
36+
}

examples/checked_sub.cc

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
#include "numbers.h"
3+
4+
int main(int argc, char const *argv[]) {
5+
std::cout << "==== checked_sub_example ==== \n";
6+
numbers::i8 a = numbers::i8::MIN;
7+
std::cout << a << '\n';
8+
std::optional<numbers::i8> ret = a.checked_sub(1);
9+
if (ret) {
10+
std::cout << ret.value() << '\n';
11+
} else {
12+
std::cout << "Overflow!\n";
13+
}
14+
return 0;
15+
}

examples/i128.cc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "numbers.h"
2+
3+
int main(int argc, char const *argv[]) {
4+
std::cout << "==== i128_example ==== \n";
5+
numbers::i128 a = 40;
6+
numbers::i128 max = numbers::i128::MAX;
7+
numbers::i128 min = numbers::i128::MIN;
8+
std::cout << "a= " << a << ", max= " << max << ", min= " << min << '\n';
9+
numbers::i128 ret = max - a;
10+
std::cout << "max - a = " << ret << '\n';
11+
return 0;
12+
}

examples/main.cc

-95
This file was deleted.

examples/overflowing_div.cc

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <iostream>
2+
#include <tuple>
3+
4+
#include "numbers.h"
5+
6+
int main(int argc, char const *argv[]) {
7+
std::cout << "==== overflowing_div_example ==== \n";
8+
numbers::i16 a = 40;
9+
numbers::i16 b = 2;
10+
auto [ret, overflowing] = a.overflowing_div(b);
11+
std::cout << "a= " << a << ", b= " << b << '\n';
12+
if (!overflowing) {
13+
std::cout << "Never Overflow!\tret = " << ret << '\n';
14+
} else {
15+
std::cout << "Overflow!\n";
16+
}
17+
return 0;
18+
}

examples/saturating_mul.cc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <iostream>
2+
#include "numbers.h"
3+
4+
int main(int argc, char const *argv[]) {
5+
std::cout << "==== saturating_mul_example ==== \n";
6+
numbers::i64 a = 40;
7+
numbers::i64 b = numbers::i64::MAX;
8+
std::cout << "a= " << a << ", b= " << b << '\n';
9+
numbers::i64 ret = a.saturating_mul(b);
10+
std::cout << ret << '\n';
11+
return 0;
12+
}

examples/u128.cc

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <iostream>
2+
#include "numbers.h"
3+
4+
int main(int argc, char const *argv[]) {
5+
std::cout << "==== u128_example ==== \n";
6+
numbers::u128 a = 40;
7+
numbers::u128 max = numbers::u128::MAX;
8+
numbers::u128 min = numbers::u128::MIN;
9+
std::cout << "a= " << a << ", max= " << max << ", min= " << min << '\n';
10+
numbers::u128 ret = max - a;
11+
std::cout << "max - a = " << ret << '\n';
12+
return 0;
13+
}

src/CMakeLists.txt

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
add_subdirectory(numbers)
22

3-
add_library(numbers_lib STATIC ${ALL_OBJECT_FILES})
3+
add_library(numbers STATIC ${ALL_OBJECT_FILES})
44

5-
find_package(Threads REQUIRED)
6-
7-
set(THIRDPARTY_LIBS
8-
Threads::Threads
9-
)
10-
11-
target_link_libraries(numbers_lib ${THIRDPARTY_LIBS})
12-
13-
target_include_directories(numbers_lib PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
5+
target_include_directories(numbers PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
146
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEIR}>
157
)

test/CMakeLists.txt tests/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ enable_testing()
22

33
include(GoogleTest)
44

5-
set(SRC_TEST_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/test/include)
5+
set(SRC_TEST_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/tests/include)
66
include_directories(${SRC_TEST_INCLUDE_DIR})
77

88
add_subdirectory(utils)
File renamed without changes.

test/integer/CMakeLists.txt tests/integer/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ set_target_properties(${PROJECT_NAME}
2727
list(APPEND TEST_TARGETS ${PROJECT_NAME} PARENT_SCOPE)
2828

2929
# Add the test targets
30-
add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME})
30+
add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME})
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)