Skip to content

Commit e058a47

Browse files
committed
feat: natural units support is now opt-in
1 parent 3fdf2de commit e058a47

26 files changed

+222
-95
lines changed

conanfile.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class MPUnitsConan(ConanFile):
6161
"no_crtp": [True, False],
6262
"contracts": ["none", "gsl-lite", "ms-gsl"],
6363
"freestanding": [True, False],
64+
"natural_units": [True, False],
6465
}
6566
default_options = {
6667
# "cxx_modules" default set in config_options()
@@ -69,6 +70,7 @@ class MPUnitsConan(ConanFile):
6970
# "no_crtp" default set in config_options()
7071
"contracts": "gsl-lite",
7172
"freestanding": False,
73+
"natural_units": True,
7274
}
7375
implements = ["auto_header_only"]
7476
exports = "LICENSE.md"
@@ -281,6 +283,7 @@ def generate(self):
281283
tc.cache_variables["MP_UNITS_API_STD_FORMAT"] = opt.std_format
282284
tc.cache_variables["MP_UNITS_API_NO_CRTP"] = opt.no_crtp
283285
tc.cache_variables["MP_UNITS_API_CONTRACTS"] = str(opt.contracts).upper()
286+
tc.cache_variables["MP_UNITS_API_NATURAL_UNITS"] = opt.natural_units
284287

285288
tc.generate()
286289
deps = CMakeDeps(self)

docs/getting_started/installation_and_usage.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ dependencies by other means, some modifications to the library's CMake files mig
154154

155155
[conan freestanding]: https://github.com/mpusz/mp-units/releases/tag/v2.2.0
156156

157+
[`natural_units`](#natural_units){ #natural_units }
158+
159+
: [:octicons-tag-24: 2.5.0][conan natural units] · :octicons-milestone-24: `ON`/`OFF` (Default: `ON`)
160+
161+
Enables experimental natural units systems support.
162+
163+
[conan natural units]: https://github.com/mpusz/mp-units/releases/tag/v2.5.0
164+
157165
??? info "CMake options to set when Conan is not being used"
158166

159167
### CMake options
@@ -215,6 +223,14 @@ dependencies by other means, some modifications to the library's CMake files mig
215223

216224
[cmake freestanding]: https://github.com/mpusz/mp-units/releases/tag/v2.2.0
217225

226+
[`MP_UNITS_API_NATURAL_UNITS`](#MP_UNITS_API_NATURAL_UNITS){ #MP_UNITS_API_NATURAL_UNITS }
227+
228+
: [:octicons-tag-24: 2.5.0][cmake natural units] · :octicons-milestone-24: `ON`/`OFF` (Default: `ON`)
229+
230+
Enables experimental natural units systems support.
231+
232+
[cmake natural units]: https://github.com/mpusz/mp-units/releases/tag/v2.5.0
233+
218234

219235
## Installation and reuse
220236

example/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ add_example(si_constants)
6969
add_example(spectroscopy_units)
7070
add_example(storage_tank)
7171
add_example(strong_angular_quantities)
72-
add_example(total_energy)
72+
if(${projectPrefix}API_NATURAL_UNITS)
73+
add_example(total_energy)
74+
endif()
7375
add_example(unmanned_aerial_vehicle example_utils)
7476

7577
add_subdirectory(glide_computer_lib)

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,14 @@ option(${projectPrefix}API_THROWING_CONSTRAINTS "Enable throwing constraints"
8787
option(${projectPrefix}API_FREESTANDING "Builds only freestanding part of the library" OFF)
8888
set(${projectPrefix}API_CONTRACTS GSL-LITE CACHE STRING "Enable contract checking")
8989
check_cache_var_values(API_CONTRACTS NONE GSL-LITE MS-GSL)
90+
option(${projectPrefix}API_NATURAL_UNITS "Enables natural units support" ON)
9091

9192
message(STATUS "${projectPrefix}API_STD_FORMAT: ${${projectPrefix}API_STD_FORMAT}")
9293
message(STATUS "${projectPrefix}API_NO_CRTP: ${${projectPrefix}API_NO_CRTP}")
9394
message(STATUS "${projectPrefix}API_THROWING_CONSTRAINTS: ${${projectPrefix}API_THROWING_CONSTRAINTS}")
9495
message(STATUS "${projectPrefix}API_FREESTANDING: ${${projectPrefix}API_FREESTANDING}")
9596
message(STATUS "${projectPrefix}API_CONTRACTS: ${${projectPrefix}API_CONTRACTS}")
97+
message(STATUS "${projectPrefix}API_NATURAL_UNITS: ${${projectPrefix}API_NATURAL_UNITS}")
9698

9799
# validate options
98100
if(${projectPrefix}API_FREESTANDING AND NOT ${projectPrefix}API_CONTRACTS STREQUAL "NONE")

src/core/CMakeLists.txt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ add_mp_units_module(
6767
include/mp-units/framework/representation_concepts.h
6868
include/mp-units/framework/symbol_text.h
6969
include/mp-units/framework/symbolic_expression.h
70-
include/mp-units/framework/system_reference.h
7170
include/mp-units/framework/unit.h
7271
include/mp-units/framework/unit_concepts.h
7372
include/mp-units/framework/unit_magnitude.h
@@ -81,6 +80,13 @@ add_mp_units_module(
8180
MODULE_INTERFACE_UNIT mp-units-core.cpp
8281
)
8382

83+
if(${${projectPrefix}API_NATURAL_UNITS})
84+
target_sources(
85+
mp-units-core PUBLIC FILE_SET HEADERS BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/include FILES
86+
include/mp-units/framework/system_reference.h
87+
)
88+
endif()
89+
8490
if(NOT ${projectPrefix}API_FREESTANDING)
8591
target_sources(
8692
mp-units-core
@@ -171,6 +177,14 @@ if(${projectPrefix}API_THROWING_CONSTRAINTS)
171177
)
172178
endif()
173179

180+
# Natural units support
181+
if(${${projectPrefix}API_NATURAL_UNITS})
182+
target_compile_definitions(
183+
mp-units-core ${${projectPrefix}TARGET_SCOPE}
184+
${projectPrefix}API_NATURAL_UNITS=$<BOOL:${${projectPrefix}API_NATURAL_UNITS}>
185+
)
186+
endif()
187+
174188
# https://github.com/llvm/llvm-project/issues/131410
175189
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 20
176190
AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 20.2

src/core/include/mp-units/bits/get_associated_quantity.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#pragma once
2424

25+
#include <mp-units/compat_macros.h>
2526
#include <mp-units/framework/quantity_spec.h>
2627
#include <mp-units/framework/symbolic_expression.h>
2728
#include <mp-units/framework/unit_concepts.h>
@@ -33,7 +34,7 @@ struct common_unit;
3334

3435
namespace detail {
3536

36-
template<AssociatedUnit U>
37+
template<MP_UNITS_ASSOCIATED_UNIT U>
3738
[[nodiscard]] consteval auto get_associated_quantity(U u);
3839

3940
template<typename... Us>
@@ -42,10 +43,10 @@ template<typename... Us>
4243
return get_common_quantity_spec(get_associated_quantity(Us{})...);
4344
}
4445

45-
template<AssociatedUnit U>
46+
template<MP_UNITS_ASSOCIATED_UNIT U>
4647
using to_quantity_spec = decltype(get_associated_quantity(U{}));
4748

48-
template<AssociatedUnit U>
49+
template<MP_UNITS_ASSOCIATED_UNIT U>
4950
[[nodiscard]] consteval auto get_associated_quantity_impl(U u)
5051
{
5152
if constexpr (requires { U::_quantity_spec_; })
@@ -56,10 +57,10 @@ template<AssociatedUnit U>
5657
return expr_map<to_quantity_spec, derived_quantity_spec, struct dimensionless>(u);
5758
}
5859

59-
template<AssociatedUnit U>
60+
template<MP_UNITS_ASSOCIATED_UNIT U>
6061
constexpr auto get_associated_quantity_result = get_associated_quantity_impl(U{});
6162

62-
template<AssociatedUnit U>
63+
template<MP_UNITS_ASSOCIATED_UNIT U>
6364
[[nodiscard]] consteval auto get_associated_quantity(U)
6465
{
6566
return get_associated_quantity_result<U>;

src/core/include/mp-units/bits/hacks.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ MP_UNITS_DIAGNOSTIC_POP
158158

159159
#endif
160160

161+
#if !defined MP_UNITS_API_NATURAL_UNITS
162+
163+
#define MP_UNITS_API_NATURAL_UNITS 1
164+
165+
#endif
166+
167+
161168
#if defined(__clang__) && defined(__apple_build_version__) && __apple_build_version__ < 16000026
162169
#define MP_UNITS_XCODE15_HACKS
163170
#endif

src/core/include/mp-units/compat_macros.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@
4040

4141
#endif
4242

43+
#if MP_UNITS_API_NATURAL_UNITS
44+
45+
#define MP_UNITS_ASSOCIATED_UNIT AssociatedUnit
46+
#define MP_UNITS_ASSOCIATED_UNIT_T(U) AssociatedUnit<U>
47+
#define MP_UNITS_WEAK_UNIT_OF(...) detail::WeakUnitOf<__VA_ARGS__>
48+
49+
#else
50+
51+
#define MP_UNITS_ASSOCIATED_UNIT Unit
52+
#define MP_UNITS_ASSOCIATED_UNIT_T(U) Unit<U>
53+
#define MP_UNITS_WEAK_UNIT_OF(...) UnitOf<__VA_ARGS__>
54+
55+
#endif
56+
4357
#if MP_UNITS_HOSTED
4458
#define MP_UNITS_THROW(expr) throw expr
4559
#else

src/core/include/mp-units/framework.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
#include <mp-units/framework/representation_concepts.h>
4040
#include <mp-units/framework/symbol_text.h>
4141
#include <mp-units/framework/symbolic_expression.h>
42+
#if MP_UNITS_API_NATURAL_UNITS
4243
#include <mp-units/framework/system_reference.h>
44+
#endif
4345
#include <mp-units/framework/unit.h>
4446
#include <mp-units/framework/unit_concepts.h>
4547
#include <mp-units/framework/unit_magnitude.h>

src/core/include/mp-units/framework/quantity.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class quantity {
244244
return *this;
245245
}
246246

247-
template<detail::WeakUnitOf<quantity_spec> ToU>
247+
template<MP_UNITS_WEAK_UNIT_OF(quantity_spec) ToU>
248248
requires detail::ValuePreservingScaling<unit, ToU{}, rep>
249249
[[nodiscard]] constexpr QuantityOf<quantity_spec> auto in(ToU) const
250250
{
@@ -258,15 +258,15 @@ class quantity {
258258
return quantity<reference, ToRep>{*this};
259259
}
260260

261-
template<RepresentationOf<quantity_spec> ToRep, detail::WeakUnitOf<quantity_spec> ToU>
261+
template<RepresentationOf<quantity_spec> ToRep, MP_UNITS_WEAK_UNIT_OF(quantity_spec) ToU>
262262
requires detail::ValuePreservingConstruction<ToRep, rep> &&
263263
detail::ValuePreservingConversion<unit, rep, ToU{}, ToRep>
264264
[[nodiscard]] constexpr QuantityOf<quantity_spec> auto in(ToU) const
265265
{
266266
return quantity<detail::make_reference(quantity_spec, ToU{}), ToRep>{*this};
267267
}
268268

269-
template<detail::WeakUnitOf<quantity_spec> ToU>
269+
template<MP_UNITS_WEAK_UNIT_OF(quantity_spec) ToU>
270270
requires detail::SaneScaling<unit, ToU{}, rep>
271271
[[nodiscard]] constexpr QuantityOf<quantity_spec> auto force_in(ToU) const
272272
{
@@ -280,7 +280,7 @@ class quantity {
280280
return value_cast<ToRep>(*this);
281281
}
282282

283-
template<RepresentationOf<quantity_spec> ToRep, detail::WeakUnitOf<quantity_spec> ToU>
283+
template<RepresentationOf<quantity_spec> ToRep, MP_UNITS_WEAK_UNIT_OF(quantity_spec) ToU>
284284
requires std::constructible_from<ToRep, rep> && detail::SaneScaling<unit, ToU{}, rep>
285285
[[nodiscard]] constexpr QuantityOf<quantity_spec> auto force_in(ToU) const
286286
{
@@ -311,14 +311,14 @@ class quantity {
311311
= delete;
312312
#endif
313313

314-
template<detail::WeakUnitOf<quantity_spec> U>
314+
template<MP_UNITS_WEAK_UNIT_OF(quantity_spec) U>
315315
requires detail::ValuePreservingScaling<unit, U{}, rep>
316316
[[nodiscard]] constexpr rep numerical_value_in(U) const noexcept
317317
{
318318
return in(U{}).numerical_value_is_an_implementation_detail_;
319319
}
320320

321-
template<detail::WeakUnitOf<quantity_spec> U>
321+
template<MP_UNITS_WEAK_UNIT_OF(quantity_spec) U>
322322
requires detail::SaneScaling<unit, U{}, rep>
323323
[[nodiscard]] constexpr rep force_numerical_value_in(U) const noexcept
324324
{

0 commit comments

Comments
 (0)