-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR defines the mechanisms for symbols, using the new `SymbolFor` monovalue type, and the `symbol_for` utility function. We are starting out conservative here, and only supplying operations for raw numbers, quantity, and self-composition for now. It's easy to add more later if we want; easier than subtracting something we regret. `SymbolFor` supports prefixes, too. We can apply a prefix applier to an instance, which makes it easy to create a symbol called `nm` as `nano(m)` if we already have a symbol `m`. We also update the docs. For now, we just provide reference docs, update the how-to, and update the comparison chart. Since unit symbols are simply better than UDLs, we bump the UDL libraries down from "good" to "fair". mp-units gets bumped down from "best" to "good" because now there is no "best". Helps #43.
- Loading branch information
Showing
9 changed files
with
275 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2023 Aurora Operations, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#pragma once | ||
|
||
#include "au/wrapper_operations.hh" | ||
|
||
namespace au { | ||
|
||
// | ||
// A representation of the symbol for a unit. | ||
// | ||
// To use, create an instance variable templated on a unit, and make the instance variable's name | ||
// the symbol to represent. For example: | ||
// | ||
// constexpr auto m = SymbolFor<Meters>{}; | ||
// | ||
template <typename Unit> | ||
struct SymbolFor : detail::MakesQuantityFromNumber<SymbolFor, Unit>, | ||
detail::ScalesQuantity<SymbolFor, Unit>, | ||
detail::ComposesWith<SymbolFor, Unit, SymbolFor, SymbolFor> {}; | ||
|
||
// | ||
// Create a unit symbol using the more fluent APIs that unit slots make possible. For example: | ||
// | ||
// constexpr auto mps = symbol_for(meters / second); | ||
// | ||
// This is generally easier to work with and makes code that is easier to read, at the cost of being | ||
// (very slightly) slower to compile. | ||
// | ||
template <typename UnitSlot> | ||
constexpr auto symbol_for(UnitSlot) { | ||
return SymbolFor<AssociatedUnitT<UnitSlot>>{}; | ||
} | ||
|
||
// Support using symbols in unit slot APIs (e.g., `v.in(m / s)`). | ||
template <typename U> | ||
struct AssociatedUnit<SymbolFor<U>> : stdx::type_identity<U> {}; | ||
|
||
} // namespace au |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2023 Aurora Operations, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "au/unit_symbol.hh" | ||
|
||
#include <type_traits> | ||
|
||
#include "au/testing.hh" | ||
#include "au/units/meters.hh" | ||
#include "au/units/seconds.hh" | ||
#include "gtest/gtest.h" | ||
|
||
using ::testing::StaticAssertTypeEq; | ||
|
||
namespace au { | ||
namespace { | ||
constexpr auto m = symbol_for(meters); | ||
constexpr auto s = symbol_for(seconds); | ||
} // namespace | ||
|
||
TEST(SymbolFor, TakesUnitSlot) { | ||
StaticAssertTypeEq<std::decay_t<decltype(m)>, SymbolFor<Meters>>(); | ||
} | ||
|
||
TEST(SymbolFor, CreatesQuantityFromRawNumber) { | ||
EXPECT_THAT(3.5f * m, SameTypeAndValue(meters(3.5f))); | ||
} | ||
|
||
TEST(SymbolFor, ScalesUnitsOfExistingQuantity) { | ||
EXPECT_THAT(meters(25.4) / s, SameTypeAndValue((meters / second)(25.4))); | ||
} | ||
|
||
TEST(SymbolFor, CompatibleWithUnitSlot) { EXPECT_THAT(meters(35u).in(m), SameTypeAndValue(35u)); } | ||
|
||
} // namespace au |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters