From d918565cbcb8ed8863bd30cc4fe868adf3703faa Mon Sep 17 00:00:00 2001 From: Chip Hogg Date: Sat, 18 Feb 2023 19:24:39 -0500 Subject: [PATCH] Add compatibility utility for nholthaus units This creates a file, `"compatibility/nholthaus_units.hh"`, which makes it easy to set up a correspondence between each Au Quantity type, and the corresponding nholthaus units type. This "correspondence" includes bidirectional implicit conversions: so, you can pass `::au::QuantityD` to an API expecting `::units::length::meter_t`, and vice versa. This correspondence is purely opt-in. The file also needs to be included _after_ both of the other two libraries are included. Since this is fragile in general, we give instructions and an example for how to make it _not_ fragile, in `nholthaus_units_example_usage.hh`. We include a variety of unit tests to show that the quantities are indeed equivalent between the libraries. (Actually, this isn't quite always the case. These tests uncovered a bug in the nholthaus library, nholthaus/units#289.) We reserve the documentation of the corresponding-quantity feature in general, as well as its application to the nholthaus library, for a future PR. --- WORKSPACE | 9 + compatibility/BUILD | 35 +++ compatibility/nholthaus_units.hh | 252 ++++++++++++++++++ .../nholthaus_units_example_usage.hh | 44 +++ compatibility/nholthaus_units_test.cc | 161 +++++++++++ third_party/nholthaus_units/BUILD | 19 ++ 6 files changed, 520 insertions(+) create mode 100644 compatibility/BUILD create mode 100644 compatibility/nholthaus_units.hh create mode 100644 compatibility/nholthaus_units_example_usage.hh create mode 100644 compatibility/nholthaus_units_test.cc create mode 100644 third_party/nholthaus_units/BUILD diff --git a/WORKSPACE b/WORKSPACE index 3a19d366..c68a6f2d 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -116,6 +116,15 @@ http_archive( url = "https://github.com/bazelbuild/rules_python/archive/refs/tags/0.14.0.tar.gz", ) +http_archive( + name = "nholthaus_units", + add_prefix = "nholthaus_units", + build_file = "@//third_party/nholthaus_units:BUILD", + sha256 = "b1f3c1dd11afa2710a179563845ce79f13ebf0c8c090d6aa68465b18bd8bd5fc", + strip_prefix = "units-2.3.3/include/", + url = "https://github.com/nholthaus/units/archive/refs/tags/v2.3.3.tar.gz", +) + load("@rules_python//python:repositories.bzl", "python_register_toolchains") python_register_toolchains( diff --git a/compatibility/BUILD b/compatibility/BUILD new file mode 100644 index 00000000..b411863a --- /dev/null +++ b/compatibility/BUILD @@ -0,0 +1,35 @@ +# 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. + +cc_library( + name = "nholthaus_units", + hdrs = ["nholthaus_units.hh"], + visibility = ["//visibility:public"], +) + +cc_test( + name = "nholthaus_units_test", + size = "small", + srcs = [ + "nholthaus_units_example_usage.hh", + "nholthaus_units_test.cc", + ], + deps = [ + ":nholthaus_units", + "//au", + "//au:testing", + "@com_google_googletest//:gtest_main", + "@nholthaus_units", + ], +) diff --git a/compatibility/nholthaus_units.hh b/compatibility/nholthaus_units.hh new file mode 100644 index 00000000..f79391be --- /dev/null +++ b/compatibility/nholthaus_units.hh @@ -0,0 +1,252 @@ +// 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 + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// HOW TO USE THIS FILE +// +// This file does all of the "heavy lifting" in establishing correspondence between equivalent types +// in Au and in the nholthaus units library. HOWEVER, it does NOT include either of those libraries +// directly. The reason is simple: there is a wide diversity in the ways people include each of the +// libraries. Thus, if we included either directly here, it would fail to compile for many people. +// +// What you should do is to make a _new_ file, in your project, which does these things: +// +// 1. Include Au (in whatever manner is appropriate for your project). +// 2. Include the nholthaus units library (in whatever manner is appropriate for your project). +// 3. Include this present file, AFTER every other file. +// +// Relying on the order of includes is a fragile strategy in general. However, if you confine it to +// a single file, and then only ever use that new file, then the strategy can work. +//////////////////////////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include + +namespace au { + +namespace detail { + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// This utility lets us extract a single template parameter. + +template +struct SoleTemplateParameter; +template +using SoleTemplateParameterT = typename SoleTemplateParameter::type; +template