Skip to content

Commit

Permalink
Lib is findable with find_package(io1 COMPONENTS money) and no longer…
Browse files Browse the repository at this point in the history
… with find_package(io1-money)
  • Loading branch information
yobeonline committed Feb 14, 2024
1 parent caec244 commit 6e880c5
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
-S test
-B install_build
-D CMAKE_BUILD_TYPE=${{ matrix.config }}
-D "io1-money_DIR=../install/share/io1-money/cmake"
-D "io1_DIR=../install/share/io1/cmake"
RESULT_VARIABLE result
)
if (NOT result EQUAL 0)
Expand Down
31 changes: 18 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ if(IO1_WITH_TESTS)
list(APPEND VCPKG_MANIFEST_FEATURES "tests")
endif()

set(IO1_PROJECT_NAME "money")

project(
io1-money
io1-${IO1_PROJECT_NAME}
VERSION 0.1.0
DESCRIPTION
"A class to store money amounts for personal accounting applications."
Expand All @@ -24,31 +26,34 @@ target_include_directories(
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_20)

add_library(io1::money ALIAS ${PROJECT_NAME})
set_target_properties(${PROJECT_NAME} PROPERTIES EXPORT_NAME
${IO1_PROJECT_NAME})

add_library(io1::${IO1_PROJECT_NAME} ALIAS ${PROJECT_NAME})

install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME})

install(
EXPORT ${PROJECT_NAME}
NAMESPACE io1::
DESTINATION ${CMAKE_INSTALL_DATADIR}/io1/cmake)

install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/io1
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${PROJECT_NAME}ConfigVersion.cmake"
"io1ConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion ARCH_INDEPENDENT)

configure_package_config_file(
"${PROJECT_SOURCE_DIR}/cmake/config.cmake.in" "${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake)

install(
EXPORT ${PROJECT_NAME}
NAMESPACE io1::
DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake)
"${PROJECT_SOURCE_DIR}/cmake/config.cmake.in" "io1Config.cmake"
INSTALL_DESTINATION ${CMAKE_INSTALL_DATADIR}/io1/cmake)

install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake)
install(FILES "${PROJECT_BINARY_DIR}/io1ConfigVersion.cmake"
"${PROJECT_BINARY_DIR}/io1Config.cmake"
DESTINATION ${CMAKE_INSTALL_DATADIR}/io1/cmake)

if(IO1_WITH_TESTS)
include(CTest)
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@

`io1::money` is a header-only c++ 20 class that holds money amounts in the range -9,223,372,036,854,775,808 +9,223,372,036,854,775,807. Amounts are intended to be expressed in the lowest subdivision allowed by a currency (eg. cents for USD), hence the integer base type. Localized formatting is supported by the standard `moneypunct`facet. Arithmetic operations involving `io1::money` instances and integers are exact (yet subject to overflow). Arithmetic operations with floats are rounded towards the nearest even integer (aka banker’s rounding).

# Install

Configure and install with `cmake`:

```shell
cmake -B build WITH_TESTS=OFF
cmake --install build --prefix <install_dir>
```

Call `find_package` from your `CMakeLists.txt` file:

```cmake
find_package(io1 REQUIRED COMPONENTS money)
target_link_libraries(<target> PRIVATE io1::money)
```

# Rationale

An amount like \$0.10 cannot be represented by a float because of the involved loss of precision that would make the following operation unbalanced: \$1.00≠\$0.10+\$0.10… (ten times). Besides, not all currencies have a 1/100 subdivision[^dinar] or even a decimal subdivision[^ougiya]. As a result, money amounts are stored as plain integer values and formatting them with the correct currency / sub-currency format is left to the user of the class. For example, the value 12550 stored in a `io1::money` instance may be formatted as $125.50, 12.550 DT or 2510 UM depending on the locale context. The `io1` namespace provides overloads to `std::put_money` and `std::get_money` into order to format `io1::money` instances with the currently imbued `std::moneypunct` facet.
Expand Down
14 changes: 8 additions & 6 deletions cmake/config.cmake.in
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")
check_required_components("@PROJECT_NAME@")
if (TARGET io1::@PROJECT_NAME@)
string(REGEX REPLACE "^io1-" "" ALIAS_TARGET_NAME @PROJECT_NAME@)
add_library(io1::${ALIAS_TARGET_NAME} ALIAS io1::@PROJECT_NAME@)
endif()
foreach(component ${io1_FIND_COMPONENTS})
include(${CMAKE_CURRENT_LIST_DIR}/io1-${component}.cmake)
if(TARGET io1::${component})
set(io1_${component}_FOUND TRUE)
endif()
endforeach()

check_required_components(io1)
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.25)

project(test_io1-money_install LANGUAGES CXX)

find_package(io1-money REQUIRED)
find_package(io1 REQUIRED COMPONENTS money)

add_library(${PROJECT_NAME} test_install.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE io1::money)

0 comments on commit 6e880c5

Please sign in to comment.