Skip to content

Commit

Permalink
refactor: op[U] for quantity and quantity_point replaced with `…
Browse files Browse the repository at this point in the history
….in(U)`

Resolves #469
  • Loading branch information
mpusz committed Aug 23, 2023
1 parent d8f646a commit ae92b49
Show file tree
Hide file tree
Showing 24 changed files with 113 additions and 113 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ int main()
constexpr auto v2 = 70 * mph;
constexpr auto v3 = avg_speed(220. * isq::distance[km], 2 * h);
constexpr auto v4 = avg_speed(isq::distance(140. * mi), 2 * h);
constexpr auto v5 = v3[m / s];
constexpr auto v5 = v3.in(m / s);
constexpr auto v6 = value_cast<m / s>(v4);
constexpr auto v7 = value_cast<int>(v6);
Expand All @@ -123,4 +123,4 @@ int main()
}
```

_Try it on the [Compiler Explorer](https://godbolt.org/z/zPnerKqzh)._
_Try it on the [Compiler Explorer](https://godbolt.org/z/E4rvPGa3n)._
6 changes: 3 additions & 3 deletions docs/getting_started/code_example.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ int main()
constexpr auto v2 = 70 * mph;
constexpr auto v3 = avg_speed(220. * isq::distance[km], 2 * h);
constexpr auto v4 = avg_speed(isq::distance(140. * mi), 2 * h);
constexpr auto v5 = v3[m / s];
constexpr auto v5 = v3.in(m / s);
constexpr auto v6 = value_cast<m / s>(v4);
constexpr auto v7 = value_cast<int>(v6);
Expand All @@ -76,8 +76,8 @@ int main()
}
```

!!! example "[Try it on Compiler Explorer](https://godbolt.org/z/zPnerKqzh)"
!!! example "[Try it on Compiler Explorer](https://godbolt.org/z/E4rvPGa3n)"

!!! note

You can find more code examples in the Examples chapter.
You can find more code examples in the [Examples](../users_guide/examples/tags_index.md) chapter.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ constexpr auto speed_of_light_in_vacuum = 1 * si::si2019::speed_of_light_in_vacu
QuantityOf<isq::permittivity_of_vacuum> auto q = 1 / (permeability_of_vacuum * pow<2>(speed_of_light_in_vacuum));
std::cout << "permittivity of vacuum = " << q << " = " << q[F / m] << "\n";
std::cout << "permittivity of vacuum = " << q << " = " << q.in(F / m) << "\n";
```

The above first prints the following:
Expand Down Expand Up @@ -99,18 +99,18 @@ std::cout << "in `GeV` and `c`:\n"
<< "m = " << m1 << "\n"
<< "E = " << E << "\n";
const auto p2 = p1[GeV / (m / s)];
const auto m2 = m1[GeV / pow<2>(m / s)];
const auto E2 = total_energy(p2, m2, c)[GeV];
const auto p2 = p1.in(GeV / (m / s));
const auto m2 = m1.in(GeV / pow<2>(m / s));
const auto E2 = total_energy(p2, m2, c).in(GeV);
std::cout << "\nin `GeV`:\n"
<< "p = " << p2 << "\n"
<< "m = " << m2 << "\n"
<< "E = " << E2 << "\n";
const auto p3 = p1[kg * m / s];
const auto m3 = m1[kg];
const auto E3 = total_energy(p3, m3, c)[J];
const auto p3 = p1.in(kg * m / s);
const auto m3 = m1.in(kg);
const auto E3 = total_energy(p3, m3, c).in(J);
std::cout << "\nin SI base units:\n"
<< "p = " << p3 << "\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ using namespace mp_units::si::unit_symbols;
auto speed = 60. * isq::speed[km / h];
auto duration = 8 * s;
auto acceleration = speed / duration;
std::cout << "acceleration: " << acceleration << " (" << acceleration[m / s2] << ")\n";
std::cout << "acceleration: " << acceleration << " (" << acceleration.in(m / s2) << ")\n";
```
The `acceleration`, being the result of the above code, has the following type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ int main()

std::cout << "A car driving " << distance << " in " << duration
<< " has an average speed of " << speed
<< " (" << speed[km / h] << ")\n";
<< " (" << speed.in(km / h) << ")\n";
}
```
Expand All @@ -82,7 +82,7 @@ The code above prints:
A car driving 110 km in 2 h has an average speed of 15.2778 m/s (55 km/h)
```

!!! example "[Try it on Compiler Explorer](https://godbolt.org/z/4zecYqn5z)"
!!! example "[Try it on Compiler Explorer](https://godbolt.org/z/zWe8ecf93)"


### Easy to understand compilation error messages
Expand Down Expand Up @@ -146,15 +146,15 @@ int main()

std::cout << "A car driving " << distance << " in " << duration
<< " has an average speed of " << speed
<< " (" << speed[km / h] << ")\n";
<< " (" << speed.in(km / h) << ")\n";
}
```

```text
A car driving 110 km in 2 h has an average speed of 15.2778 m/s (55 km/h)
```

!!! example "[Try it on Compiler Explorer](https://godbolt.org/z/jhfWjGadz)"
!!! example "[Try it on Compiler Explorer](https://godbolt.org/z/q3PzMzqsh)"

In case we will accidentally make the same calculation error as before, this time, we will
get a bit longer error message also containing information about the quantity type:
Expand Down
6 changes: 3 additions & 3 deletions docs/users_guide/framework_basics/systems_of_units.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ the following:
```cpp
auto q1 = 42 * W;
std::cout << q1 << "\n";
std::cout << q1[J / s] << "\n";
std::cout << q1[N * m / s] << "\n";
std::cout << q1[kg * m2 / s3] << "\n";
std::cout << q1.in(J / s) << "\n";
std::cout << q1.in(N * m / s) << "\n";
std::cout << q1.in(kg * m2 / s3) << "\n";
```

prints:
Expand Down
2 changes: 1 addition & 1 deletion docs/users_guide/framework_basics/text_output.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ associated with this quantity.
before passing it to the text output:
```cpp
std::cout << v1[km / h] << '\n'; // 110 km/h
std::cout << v1.in(km / h) << '\n'; // 110 km/h
std::cout << value_cast<m / s>(v1) << '\n'; // 30.5556 m/s
```

Expand Down
6 changes: 3 additions & 3 deletions docs/users_guide/framework_basics/value_conversions.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

```cpp
auto q1 = 5 * km;
std::cout << q1[m] << '\n';
std::cout << q1.in(m) << '\n';
quantity<si::metre, int> q2 = q1;
```

Expand All @@ -23,7 +23,7 @@ In case a user would like to perform an opposite transformation:

```cpp
auto q1 = 5 * m;
std::cout << q1[km] << '\n';
std::cout << q1.in(km) << '\n';
quantity<si::kilo<si::metre>, int> q2 = q1;
```

Expand All @@ -34,7 +34,7 @@ representation type:

```cpp
auto q1 = 5. * m;
std::cout << q1[km] << '\n';
std::cout << q1.in(km) << '\n';
quantity<si::kilo<si::metre>> q2 = q1;
```

Expand Down
10 changes: 5 additions & 5 deletions example/capacitor_time_curve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ int main()
std::cout << "at " << tt << " voltage is ";

if (Vt >= 1 * V)
std::cout << Vt[V];
std::cout << Vt.in(V);
else if (Vt >= 1 * mV)
std::cout << Vt[mV];
std::cout << Vt.in(mV);
else if (Vt >= 1 * uV)
std::cout << Vt[uV];
std::cout << Vt.in(uV);
else if (Vt >= 1 * nV)
std::cout << Vt[nV];
std::cout << Vt.in(nV);
else
std::cout << Vt[pV];
std::cout << Vt.in(pV);
std::cout << "\n";
}
}
40 changes: 20 additions & 20 deletions example/clcpp_response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,24 @@ void quantities_with_typed_units()

std::cout << meter << '\n';

std::cout << " = " << meter[astronomical_unit] << '\n';
std::cout << " = " << meter[iau::angstrom] << '\n';
std::cout << " = " << meter[imperial::chain] << '\n';
std::cout << " = " << meter[imperial::fathom] << '\n';
std::cout << " = " << meter[usc::fathom] << '\n';
std::cout << " = " << meter[international::foot] << '\n';
std::cout << " = " << meter[usc::survey1893::us_survey_foot] << '\n';
std::cout << " = " << meter[international::inch] << '\n';
std::cout << " = " << meter[iau::light_year] << '\n';
std::cout << " = " << meter[international::mile] << '\n';
std::cout << " = " << meter[international::nautical_mile] << '\n';
std::cout << " = " << meter[iau::parsec] << '\n';
std::cout << " = " << meter[typographic::pica_dtp] << '\n';
std::cout << " = " << meter[typographic::pica_us] << '\n';
std::cout << " = " << meter[typographic::point_dtp] << '\n';
std::cout << " = " << meter[typographic::point_us] << '\n';
std::cout << " = " << meter[imperial::rod] << '\n';
std::cout << " = " << meter[international::yard] << '\n';
std::cout << " = " << meter.in(astronomical_unit) << '\n';
std::cout << " = " << meter.in(iau::angstrom) << '\n';
std::cout << " = " << meter.in(imperial::chain) << '\n';
std::cout << " = " << meter.in(imperial::fathom) << '\n';
std::cout << " = " << meter.in(usc::fathom) << '\n';
std::cout << " = " << meter.in(international::foot) << '\n';
std::cout << " = " << meter.in(usc::survey1893::us_survey_foot) << '\n';
std::cout << " = " << meter.in(international::inch) << '\n';
std::cout << " = " << meter.in(iau::light_year) << '\n';
std::cout << " = " << meter.in(international::mile) << '\n';
std::cout << " = " << meter.in(international::nautical_mile) << '\n';
std::cout << " = " << meter.in(iau::parsec) << '\n';
std::cout << " = " << meter.in(typographic::pica_dtp) << '\n';
std::cout << " = " << meter.in(typographic::pica_us) << '\n';
std::cout << " = " << meter.in(typographic::point_dtp) << '\n';
std::cout << " = " << meter.in(typographic::point_us) << '\n';
std::cout << " = " << meter.in(imperial::rod) << '\n';
std::cout << " = " << meter.in(international::yard) << '\n';
}

void calcs_comparison()
Expand All @@ -119,8 +119,8 @@ void calcs_comparison()
"or small values in other units to the base unit.\n"
"This is both inefficient and inaccurate\n\n";

const auto L1B = L1A[m];
const auto L2B = L2A[m];
const auto L1B = L1A.in(m);
const auto L2B = L2A.in(m);
const auto LrB = L1B + L2B;
std::cout << MP_UNITS_STD_FMT::format("{:%.30eQ %q}\n + {:%.30eQ %q}\n = {:%.30eQ %q}\n\n", L1B, L2B, LrB);

Expand Down
2 changes: 1 addition & 1 deletion example/conversion_factor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int main()
std::cout << "conversion factor in mp-units...\n\n";

constexpr auto lengthA = 2.0 * m;
constexpr auto lengthB = lengthA[mm];
constexpr auto lengthB = lengthA.in(mm);

std::cout << MP_UNITS_STD_FMT::format("lengthA( {} ) and lengthB( {} )\n", lengthA, lengthB)
<< "represent the same length in different units.\n\n";
Expand Down
2 changes: 1 addition & 1 deletion example/hello_units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int main()
constexpr auto v2 = 70 * mph;
constexpr auto v3 = avg_speed(220. * km, 2 * h);
constexpr auto v4 = avg_speed(isq::distance(140. * mi), 2 * isq::duration[h]);
constexpr auto v5 = v3[m / s];
constexpr auto v5 = v3.in(m / s);
constexpr auto v6 = value_cast<m / s>(v4);
constexpr auto v7 = value_cast<int>(v6);

Expand Down
2 changes: 1 addition & 1 deletion example/measurement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void example()
const auto t = measurement{1.2, 0.1} * s;

const QuantityOf<isq::velocity> auto v = a * t;
std::cout << a << " * " << t << " = " << v << " = " << v[km / h] << '\n';
std::cout << a << " * " << t << " = " << v << " = " << v.in(km / h) << '\n';

const auto length = measurement{123., 1.} * m;
std::cout << "10 * " << length << " = " << 10 * length << '\n';
Expand Down
20 changes: 12 additions & 8 deletions example/si_constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!! Before you commit any changes to this file please make sure to check if it !!!
// !!! renders correctly in the documentation "Examples" section. !!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

#include <mp-units/format.h>
#include <mp-units/systems/si/constants.h>
#include <mp-units/systems/si/unit_symbols.h>
#include <mp-units/systems/si/si.h>
#include <iostream>

template<class T>
Expand All @@ -37,18 +41,18 @@ int main()
std::cout << "The seven defining constants of the SI and the seven corresponding units they define:\n";
std::cout << MP_UNITS_STD_FMT::format("- hyperfine transition frequency of Cs: {} = {:%.0Q %q}\n",
1. * si2019::hyperfine_structure_transition_frequency_of_cs,
(1. * si2019::hyperfine_structure_transition_frequency_of_cs)[Hz]);
(1. * si2019::hyperfine_structure_transition_frequency_of_cs).in(Hz));
std::cout << MP_UNITS_STD_FMT::format("- speed of light in vacuum: {} = {:%.0Q %q}\n",
1. * si2019::speed_of_light_in_vacuum,
(1. * si2019::speed_of_light_in_vacuum)[m / s]);
(1. * si2019::speed_of_light_in_vacuum).in(m / s));
std::cout << MP_UNITS_STD_FMT::format("- Planck constant: {} = {:%.8eQ %q}\n",
1. * si2019::planck_constant, (1. * si2019::planck_constant)[J * s]);
1. * si2019::planck_constant, (1. * si2019::planck_constant).in(J * s));
std::cout << MP_UNITS_STD_FMT::format("- elementary charge: {} = {:%.9eQ %q}\n",
1. * si2019::elementary_charge, (1. * si2019::elementary_charge)[C]);
1. * si2019::elementary_charge, (1. * si2019::elementary_charge).in(C));
std::cout << MP_UNITS_STD_FMT::format("- Boltzmann constant: {} = {:%.6eQ %q}\n",
1. * si2019::boltzmann_constant, (1. * si2019::boltzmann_constant)[J / K]);
1. * si2019::boltzmann_constant, (1. * si2019::boltzmann_constant).in(J / K));
std::cout << MP_UNITS_STD_FMT::format("- Avogadro constant: {} = {:%.8eQ %q}\n",
1. * si2019::avogadro_constant, (1. * si2019::avogadro_constant)[1 / mol]);
1. * si2019::avogadro_constant, (1. * si2019::avogadro_constant).in(1 / mol));
// TODO uncomment the below when ISQ is done
// std::cout << MP_UNITS_STD_FMT::format("- luminous efficacy: {} = {}\n",
// si2019::luminous_efficacy(1.),
Expand Down
5 changes: 3 additions & 2 deletions example/spectroscopy_units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ template<QuantityOf<isq::energy> T1, QuantityOf<isq::wavenumber> T2, QuantityOf<
QuantityOf<isq::thermodynamic_temperature> T4, QuantityOf<isq::wavelength> T5>
void print_line_si(const std::tuple<T1, T2, T3, T4, T5>& t)
{
MP_UNITS_STD_FMT::println("| {:<15} | {:<15} | {:<15} | {:<15} | {:<15} |", std::get<0>(t)[eV],
std::get<1>(t)[1 / cm], std::get<2>(t)[THz], std::get<3>(t)[K], std::get<4>(t)[um]);
MP_UNITS_STD_FMT::println("| {:<15} | {:<15} | {:<15} | {:<15} | {:<15} |", std::get<0>(t).in(eV),
std::get<1>(t).in(1 / cm), std::get<2>(t).in(THz), std::get<3>(t).in(K),
std::get<4>(t).in(um));
}

int main()
Expand Down
6 changes: 3 additions & 3 deletions example/storage_tank.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ int main()
const auto fill_ratio = fill_level / height;

std::cout << MP_UNITS_STD_FMT::format("fill height at {} = {} ({} full)\n", fill_time, fill_level,
fill_ratio[percent]);
std::cout << MP_UNITS_STD_FMT::format("fill weight at {} = {} ({})\n", fill_time, filled_weight, filled_weight[N]);
fill_ratio.in(percent));
std::cout << MP_UNITS_STD_FMT::format("fill weight at {} = {} ({})\n", fill_time, filled_weight, filled_weight.in(N));
std::cout << MP_UNITS_STD_FMT::format("spare capacity at {} = {}\n", fill_time, spare_capacity);
std::cout << MP_UNITS_STD_FMT::format("input flow rate = {}\n", input_flow_rate);
std::cout << MP_UNITS_STD_FMT::format("float rise rate = {}\n", float_rise_rate);
std::cout << MP_UNITS_STD_FMT::format("tank full E.T.A. at current flow rate = {}\n", fill_time_left[s]);
std::cout << MP_UNITS_STD_FMT::format("tank full E.T.A. at current flow rate = {}\n", fill_time_left.in(s));
}
2 changes: 1 addition & 1 deletion example/strong_angular_quantities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ int main()
const auto torque = isq_angle::torque(lever * force * angular::sin(angle) / (1 * isq_angle::cotes_angle));

std::cout << "Applying a perpendicular force of " << force << " to a " << lever << " long lever results in "
<< torque[N * m / rad] << " of torque.\n";
<< torque.in(N * m / rad) << " of torque.\n";
}
16 changes: 8 additions & 8 deletions example/total_energy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,31 @@ void si_example()
using namespace mp_units::si::unit_symbols;
constexpr auto GeV = si::giga<si::electronvolt>;
constexpr QuantityOf<isq::speed> auto c = 1. * si::si2019::speed_of_light_in_vacuum;
auto c2 = pow<2>(c);
constexpr auto c2 = pow<2>(c);

const auto p1 = isq::momentum(4. * GeV / c);
const QuantityOf<isq::mass> auto m1 = 3. * GeV / c2;
const auto E = total_energy(p1, m1, c);

std::cout << "\n*** SI units (c = " << c << " = " << c[si::metre / s] << ") ***\n";
std::cout << "\n*** SI units (c = " << c << " = " << c.in(si::metre / s) << ") ***\n";

std::cout << "\n[in `GeV` and `c`]\n"
<< "p = " << p1 << "\n"
<< "m = " << m1 << "\n"
<< "E = " << E << "\n";

const auto p2 = p1[GeV / (m / s)];
const auto m2 = m1[GeV / pow<2>(m / s)];
const auto E2 = total_energy(p2, m2, c)[GeV];
const auto p2 = p1.in(GeV / (m / s));
const auto m2 = m1.in(GeV / pow<2>(m / s));
const auto E2 = total_energy(p2, m2, c).in(GeV);

std::cout << "\n[in `GeV`]\n"
<< "p = " << p2 << "\n"
<< "m = " << m2 << "\n"
<< "E = " << E2 << "\n";

const auto p3 = p1[kg * m / s];
const auto m3 = m1[kg];
const auto E3 = total_energy(p3, m3, c)[J];
const auto p3 = p1.in(kg * m / s);
const auto m3 = m1.in(kg);
const auto E3 = total_energy(p3, m3, c).in(J);

std::cout << "\n[in SI base units]\n"
<< "p = " << p3 << "\n"
Expand Down
6 changes: 3 additions & 3 deletions src/core/include/mp-units/quantity.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,15 @@ class quantity {
#endif

template<Unit U>
requires requires(quantity q) { q[U{}]; }
requires requires(quantity q) { q.in(U{}); }
[[nodiscard]] constexpr rep number_in(U) const noexcept
{
return (*this)[U{}].number();
return (*this).in(U{}).number();
}

template<Unit U>
requires detail::QuantityConvertibleTo<quantity, quantity<::mp_units::reference<quantity_spec, U{}>{}, Rep>>
[[nodiscard]] constexpr quantity<::mp_units::reference<quantity_spec, U{}>{}, Rep> operator[](U) const
[[nodiscard]] constexpr quantity<::mp_units::reference<quantity_spec, U{}>{}, Rep> in(U) const
{
return quantity<quantity_spec[U{}], Rep>{*this};
}
Expand Down
Loading

0 comments on commit ae92b49

Please sign in to comment.