Skip to content

Commit

Permalink
make sure we do not override the rule of 5
Browse files Browse the repository at this point in the history
  • Loading branch information
anders-wind committed Jun 18, 2024
1 parent 981e416 commit 0713a09
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 16 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ auto main() -> int
On top of providing strong type utilities, `stronk` also enables unit-like behavior:
```cpp :file=./examples/unit_energy_example.cpp:line_start=0:line_end=23
```cpp :file=./examples/unit_energy_example.cpp:line_start=0:line_end=24
#include <ratio>
#include <stronk/prefabs.h>
#include <stronk/stronk.h>
#include <stronk/unit.h>
// We introduce a unit type with a default set of skills with the `stronk_default_unit` prefab
Expand All @@ -85,7 +86,7 @@ void watts_and_identity_units()

Different units can be combined by multiplying or dividing them:

```cpp :file=./examples/unit_energy_example.cpp:line_start=24:line_end=45
```cpp :file=./examples/unit_energy_example.cpp:line_start=25:line_end=46
// Lets introduce hours as a new unit_like type
struct Hours : twig::stronk<Hours, double, twig::unit>
{
Expand All @@ -111,7 +112,7 @@ void watt_hours_and_generating_new_units()

These new generated types are also units which can be used to generate new units:

```cpp :file=./examples/unit_energy_example.cpp:line_start=46:line_end=65
```cpp :file=./examples/unit_energy_example.cpp:line_start=47:line_end=66
// Lets introduce a type for euros, and start combining more types.
struct Euro : twig::stronk<Euro, double, twig::unit>
{
Expand Down Expand Up @@ -191,8 +192,12 @@ In case you want to specialize the resulting type of unit multiplication and div

By default the units are generated with the `stronk_default_prefab` type.

```cpp :file=./examples/specializers_example.cpp:line_end=29
#include <stronk/specializers.h>
```cpp :file=./examples/specializers_example.cpp:line_end=33
#include <cstdint>
#include <type_traits>

#include <stronk/stronk.h>
#include <stronk/unit.h>

// Lets consider the following units:
struct Distance : twig::stronk<Distance, double, twig::unit>
Expand Down
8 changes: 6 additions & 2 deletions examples/specializers_example.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#include <stronk/specializers.h>
#include <cstdint>
#include <type_traits>

#include <stronk/stronk.h>
#include <stronk/unit.h>

// Lets consider the following units:
struct Distance : twig::stronk<Distance, double, twig::unit>
Expand Down Expand Up @@ -48,4 +52,4 @@ auto main() -> int
using SpeedDeduced = decltype(Distance {} / Time {});
static_assert(std::is_same_v<SpeedDeduced, Speed>);
}
static_assert(__LINE__ == 51UL, "update readme if this changes");
static_assert(__LINE__ == 55UL, "update readme if this changes");
3 changes: 2 additions & 1 deletion examples/unit_energy_example.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <ratio>

#include <stronk/prefabs.h>
#include <stronk/stronk.h>
#include <stronk/unit.h>

// We introduce a unit type with a default set of skills with the `stronk_default_unit` prefab
Expand Down Expand Up @@ -70,4 +71,4 @@ auto main() -> int
watt_hours_and_generating_new_units();
introducing_another_type();
}
static_assert(__LINE__ == 73UL, "update readme if this changes");
static_assert(__LINE__ == 74UL, "update readme if this changes");
12 changes: 10 additions & 2 deletions include/stronk/stronk.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,18 @@ struct stronk : public Skills<Tag>...

constexpr stronk() noexcept(std::is_nothrow_default_constructible_v<T>) = default;

template<typename ConvertConstructibleT>
requires(std::constructible_from<underlying_type, ConvertConstructibleT>
&& !std::same_as<ConvertConstructibleT, self_t>)
STRONK_FORCEINLINE constexpr explicit stronk(ConvertConstructibleT&& value)
: _you_should_not_be_using_this_but_rather_unwrap(std::forward<ConvertConstructibleT>(value))
{
}

template<typename... ConvertConstructibleTs>
requires(std::constructible_from<underlying_type, ConvertConstructibleTs...>
&& sizeof...(ConvertConstructibleTs) >= 1)
STRONK_FORCEINLINE constexpr explicit stronk(ConvertConstructibleTs&&... values)
&& sizeof...(ConvertConstructibleTs) >= 2)
STRONK_FORCEINLINE constexpr stronk(ConvertConstructibleTs&&... values)
: _you_should_not_be_using_this_but_rather_unwrap(std::forward<ConvertConstructibleTs>(values)...)
{
}
Expand Down
14 changes: 8 additions & 6 deletions include/stronk/utilities/equality.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@ namespace twig::stronk_details
{

template<std::floating_point T>
inline constexpr auto default_abs_tol = []() -> T {
inline constexpr auto default_abs_tol = []() -> T
{
if constexpr (std::is_same_v<T, float>) {
return T(1e-5);
return T {1e-5F};
} else if constexpr (std::is_same_v<T, double>) {
return T(1e-8);
return T {1e-8};
} else {
static_assert(not_implemented_type<T>());
}
};

template<std::floating_point T>
inline constexpr auto default_rel_tol = []() -> T {
inline constexpr auto default_rel_tol = []() -> T
{
if constexpr (std::is_same_v<T, float>) {
return T(1e-3);
return T {1e-3F};
} else if constexpr (std::is_same_v<T, double>) {
return T(1e-5);
return T {1e-5};
} else {
static_assert(not_implemented_type<T>());
}
Expand Down
2 changes: 2 additions & 0 deletions tests/src/extensions_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <sstream>
#include <string>

#include <absl/hash/hash.h>
#include <fmt/core.h>
#include <gtest/gtest.h>
#include <nlohmann/json.hpp>
#include <nlohmann/json_fwd.hpp>
#include <stronk/extensions/absl.h>
#include <stronk/extensions/fmt.h>
#include <stronk/extensions/gtest.h>
Expand Down
4 changes: 4 additions & 0 deletions tests/src/specializers_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include <cstdint>
#include <type_traits>

#include <gtest/gtest.h>
#include <stronk/can_stream.h>
#include <stronk/specializers.h>
#include <stronk/stronk.h>
#include <stronk/unit.h>

namespace twig::tests
Expand Down
3 changes: 3 additions & 0 deletions tests/src/unit_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#include <cstdint>
#include <ratio>
#include <type_traits>

#include <gtest/gtest.h>
#include <stronk/extensions/gtest.h>
#include <stronk/prefabs.h>
#include <stronk/specializers.h>
#include <stronk/stronk.h>
#include <stronk/unit.h>
#include <stronk/utilities/type_list.h>

Expand Down

0 comments on commit 0713a09

Please sign in to comment.