-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add machinery for built-in constants, such as
c
(#335)
For Au's built-in constants, we follow the exact same policies as for units, including: - A new include folder, `"au/constants/..."` - A new target, `"//au:constants"`, which globs headers from that folder - Corresponding unit tests - Inclusion in the single-file script by individual names - An `--all-constants` option for the single-file script Oh, and while I was updating the single-file script, I noticed a slight "bug": ever since we started providing `_fwd.hh` files for the units, the single file script was treating those files as their own units. This doesn't _hurt_ anything, but it's just a little silly (see image). This PR fixes that bug as well. ![image](https://github.com/user-attachments/assets/22381e3e-4785-4c69-8ae9-94e4ff7c7c41) We _don't_ provide `_fwd.hh` files for _constants_, because there's nothing we could really forward declare. Constant objects are defined with spelled-out names in `ALL_CAPS` format. The corresponding file is the snake-case version. This keeps the constant itself unambiguous. We expect end users to actually use them in the following manner: ```cpp constexpr auto c = au::SPEED_OF_LIGHT; ``` Finally, we now mention new constants in the release notes. Helps #90. Remaining work includes adding more constants, and adding documentation.
- Loading branch information
Showing
8 changed files
with
166 additions
and
20 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
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,38 @@ | ||
// Copyright 2024 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/constant.hh" | ||
#include "au/units/meters.hh" | ||
#include "au/units/seconds.hh" | ||
|
||
namespace au { | ||
|
||
namespace detail { | ||
// DO NOT follow this pattern to define your own units. This is for library-defined units. | ||
// Instead, follow instructions at (https://aurora-opensource.github.io/au/main/howto/new-units/). | ||
template <typename T> | ||
struct SpeedOfLightLabel { | ||
static constexpr const char label[] = "c"; | ||
}; | ||
template <typename T> | ||
constexpr const char SpeedOfLightLabel<T>::label[]; | ||
struct SpeedOfLightUnit : decltype(Meters{} / Seconds{} * mag<299'792'458>()), | ||
SpeedOfLightLabel<void> { | ||
using SpeedOfLightLabel<void>::label; | ||
}; | ||
} // namespace detail | ||
|
||
constexpr auto SPEED_OF_LIGHT = make_constant(detail::SpeedOfLightUnit{}); | ||
|
||
} // 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,36 @@ | ||
// Copyright 2024 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/constants/speed_of_light.hh" | ||
|
||
#include "au/testing.hh" | ||
#include "au/units/meters.hh" | ||
#include "au/units/seconds.hh" | ||
#include "gtest/gtest.h" | ||
|
||
namespace au { | ||
namespace { | ||
|
||
using symbols::m; | ||
using symbols::s; | ||
using ::testing::StrEq; | ||
|
||
TEST(SpeedOfLight, HasExpectedValue) { | ||
EXPECT_THAT(SPEED_OF_LIGHT.as<int>(m / s), SameTypeAndValue(299'792'458 * m / s)); | ||
} | ||
|
||
TEST(SpeedOfLight, HasExpectedLabel) { EXPECT_THAT(unit_label(SPEED_OF_LIGHT), StrEq("c")); } | ||
|
||
} // namespace | ||
} // 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