Skip to content

Commit

Permalink
feat: add module mp_units
Browse files Browse the repository at this point in the history
  • Loading branch information
JohelEGP committed Aug 31, 2023
1 parent fa38e4f commit f0cf994
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 24 deletions.
14 changes: 14 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ target_link_libraries(
add_library(mp-units::mp-units ALIAS mp-units)
install(TARGETS mp-units EXPORT mp-unitsTargets)

# C++ module
if(CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API)
add_library(mp-units-modules STATIC)
add_library(mp-units::modules ALIAS mp-units-modules)
target_sources(
mp-units-modules PUBLIC FILE_SET CXX_MODULES BASE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}" FILES "mp-units.cpp"
)
target_link_libraries(mp-units-modules PRIVATE mp-units::mp-units)
target_include_directories(
mp-units-modules ${unitsAsSystem} INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/core/include>
$<INSTALL_INTERFACE:include>
)
endif()

# local build
export(EXPORT mp-unitsTargets NAMESPACE mp-units::)
configure_file("mp-unitsConfig.cmake" "." COPYONLY)
Expand Down
2 changes: 1 addition & 1 deletion src/core-fmt/include/mp-units/bits/fmt_hacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ MP_UNITS_DIAGNOSTIC_POP

#else

#ifndef __cpp_lib_format
#if !defined(__cpp_lib_format) && !defined(__cpp_lib_format_ranges)
#error "std::formatting facility not supported"
#endif

Expand Down
1 change: 1 addition & 0 deletions src/core/include/mp-units/bits/external/type_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <mp-units/bits/external/hacks.h> // IWYU pragma: keep
#include <cstddef>
#include <type_traits>
#include <utility>

MP_UNITS_DIAGNOSTIC_PUSH
Expand Down
15 changes: 1 addition & 14 deletions src/core/include/mp-units/quantity_spec.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <mp-units/bits/reference_concepts.h>
#include <mp-units/bits/representation_concepts.h>
#include <mp-units/dimension.h>
#include <mp-units/quantity_spec_macro.h>
#include <tuple>

namespace mp_units {
Expand Down Expand Up @@ -359,20 +360,6 @@ struct quantity_spec<Self, QS, Eq, Args...> : quantity_spec<Self, QS, Args...> {
static constexpr quantity_character character = detail::quantity_character_init<Args...>(Eq.character);
};

#ifdef __cpp_explicit_this_parameter

#define QUANTITY_SPEC(name, ...) \
inline constexpr struct name : ::mp_units::quantity_spec<__VA_ARGS__> { \
} name

#else

#define QUANTITY_SPEC(name, ...) \
inline constexpr struct name : ::mp_units::quantity_spec<name, __VA_ARGS__> { \
} name

#endif


/**
* @brief A specification of a derived quantity
Expand Down
13 changes: 13 additions & 0 deletions src/core/include/mp-units/quantity_spec_macro.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifdef __cpp_explicit_this_parameter

#define QUANTITY_SPEC(name, ...) \
inline constexpr struct name : ::mp_units::quantity_spec<__VA_ARGS__> { \
} name

#else

#define QUANTITY_SPEC(name, ...) \
inline constexpr struct name : ::mp_units::quantity_spec<name, __VA_ARGS__> { \
} name

#endif
58 changes: 58 additions & 0 deletions src/mp-units.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module;

// core
#include <gsl/gsl-lite.hpp>
#include <array>
#include <compare>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <initializer_list>
#include <iterator>
#include <limits>
#include <numbers>
#include <numeric>
#include <optional>
#include <ranges>
#include <string>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <utility>

// core-io
#include <sstream>

// core-fmt
#include <format>

// utility
#include <chrono>
#include <cmath>
#include <random>

export module mp_units;

export
{
// core
#include <mp-units/core.h>

// core-io
#include <mp-units/ostream.h>

// core-fmt
#include <mp-units/format.h>

// systems
#include <mp-units/systems/angular/angular.h>
#include <mp-units/systems/iec80000/iec80000.h>
#include <mp-units/systems/isq/isq.h>
#include <mp-units/systems/si/si.h>

// utility
#include <mp-units/chrono.h>
#include <mp-units/math.h>
#include <mp-units/random.h>
}
8 changes: 4 additions & 4 deletions src/utility/include/mp-units/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace mp_units {

namespace detail {
template<Quantity Q, typename InputIt>
static std::vector<typename Q::rep> i_qty_to_rep(InputIt first, InputIt last)
std::vector<typename Q::rep> i_qty_to_rep(InputIt first, InputIt last)
{
std::vector<typename Q::rep> intervals_rep;
intervals_rep.reserve(static_cast<size_t>(std::distance(first, last)));
Expand All @@ -41,7 +41,7 @@ static std::vector<typename Q::rep> i_qty_to_rep(InputIt first, InputIt last)
}

template<Quantity Q>
static std::vector<typename Q::rep> bl_qty_to_rep(std::initializer_list<Q>& bl)
std::vector<typename Q::rep> bl_qty_to_rep(std::initializer_list<Q>& bl)
{
std::vector<typename Q::rep> bl_rep;
bl_rep.reserve(bl.size());
Expand All @@ -52,7 +52,7 @@ static std::vector<typename Q::rep> bl_qty_to_rep(std::initializer_list<Q>& bl)
}

template<Quantity Q, typename UnaryOperation>
inline static std::vector<typename Q::rep> fw_bl_pwc(std::initializer_list<Q>& bl, UnaryOperation fw)
std::vector<typename Q::rep> fw_bl_pwc(std::initializer_list<Q>& bl, UnaryOperation fw)
{
using rep = MP_UNITS_TYPENAME Q::rep;
std::vector<rep> w_bl;
Expand All @@ -70,7 +70,7 @@ inline static std::vector<typename Q::rep> fw_bl_pwc(std::initializer_list<Q>& b
}

template<Quantity Q, typename UnaryOperation>
static std::vector<typename Q::rep> fw_bl_pwl(std::initializer_list<Q>& bl, UnaryOperation fw)
std::vector<typename Q::rep> fw_bl_pwl(std::initializer_list<Q>& bl, UnaryOperation fw)
{
std::vector<typename Q::rep> weights;
weights.reserve(bl.size());
Expand Down
10 changes: 7 additions & 3 deletions test/unit_test/static/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
cmake_minimum_required(VERSION 3.2)

add_library(unit_tests_static_truncating quantity_test.cpp)
target_link_libraries(unit_tests_static_truncating PRIVATE mp-units::mp-units)
target_compile_options(
unit_tests_static_truncating PRIVATE $<IF:$<CXX_COMPILER_ID:MSVC>,/wd4242 /wd4244,-Wno-conversion>
)
Expand Down Expand Up @@ -63,5 +62,10 @@ add_library(
usc_test.cpp
)

target_link_libraries(unit_tests_static PRIVATE mp-units::mp-units)
target_link_libraries(unit_tests_static PRIVATE unit_tests_static_truncating mp-units::mp-units)
target_link_libraries(unit_tests_static PRIVATE unit_tests_static_truncating)
foreach(target unit_tests_static_truncating unit_tests_static)
target_link_libraries(
"${target}" PRIVATE mp-units::mp-units $<$<TARGET_EXISTS:mp-units::modules>:mp-units::modules>
)
target_compile_definitions("${target}" PRIVATE $<$<TARGET_EXISTS:mp-units::modules>:MP_UNITS_MODULES>)
endforeach()
9 changes: 7 additions & 2 deletions test/unit_test/static/quantity_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@
// SOFTWARE.

#include "test_tools.h"
#include <limits>
#include <utility>
#ifdef MP_UNITS_MODULES
#include <mp-units/quantity_spec_macro.h>
import mp_units;
#else
#include <mp-units/chrono.h>
#include <mp-units/systems/isq/mechanics.h>
#include <mp-units/systems/isq/space_and_time.h>
#include <mp-units/systems/si/si.h>
#include <limits>
#include <utility>
#endif

template<>
inline constexpr bool mp_units::is_vector<int> = true;
Expand Down

0 comments on commit f0cf994

Please sign in to comment.