Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 4 | |
| 5 | #include "wpimath/MathShared.h" |
| 6 | |
| 7 | #include <wpi/mutex.h> |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 8 | #include <wpi/timestamp.h> |
| 9 | |
| 10 | #include "units/time.h" |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 11 | |
| 12 | using namespace wpi::math; |
| 13 | |
| 14 | namespace { |
| 15 | class DefaultMathShared : public MathShared { |
| 16 | public: |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 17 | void ReportErrorV(fmt::string_view format, fmt::format_args args) override {} |
| 18 | void ReportWarningV(fmt::string_view format, fmt::format_args args) override { |
| 19 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 20 | void ReportUsage(MathUsageId id, int count) override {} |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 21 | units::second_t GetTimestamp() override { |
| 22 | return units::second_t{wpi::Now() * 1.0e-6}; |
| 23 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 24 | }; |
| 25 | } // namespace |
| 26 | |
| 27 | static std::unique_ptr<MathShared> mathShared; |
| 28 | static wpi::mutex setLock; |
| 29 | |
| 30 | MathShared& MathSharedStore::GetMathShared() { |
| 31 | std::scoped_lock lock(setLock); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 32 | if (!mathShared) { |
| 33 | mathShared = std::make_unique<DefaultMathShared>(); |
| 34 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 35 | return *mathShared; |
| 36 | } |
| 37 | |
| 38 | void MathSharedStore::SetMathShared(std::unique_ptr<MathShared> shared) { |
| 39 | std::scoped_lock lock(setLock); |
| 40 | mathShared = std::move(shared); |
| 41 | } |