blob: b53d080ac06cd2eb126dd6c0189afc6d8225c1cd [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// 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 Schuh1e69f942020-11-14 15:06:14 -08004
5#include "wpimath/MathShared.h"
6
7#include <wpi/mutex.h>
James Kuszmaulb13e13f2023-11-22 20:44:04 -08008#include <wpi/timestamp.h>
9
10#include "units/time.h"
Austin Schuh1e69f942020-11-14 15:06:14 -080011
12using namespace wpi::math;
13
14namespace {
15class DefaultMathShared : public MathShared {
16 public:
Austin Schuh812d0d12021-11-04 20:16:48 -070017 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 Schuh1e69f942020-11-14 15:06:14 -080020 void ReportUsage(MathUsageId id, int count) override {}
James Kuszmaulb13e13f2023-11-22 20:44:04 -080021 units::second_t GetTimestamp() override {
22 return units::second_t{wpi::Now() * 1.0e-6};
23 }
Austin Schuh1e69f942020-11-14 15:06:14 -080024};
25} // namespace
26
27static std::unique_ptr<MathShared> mathShared;
28static wpi::mutex setLock;
29
30MathShared& MathSharedStore::GetMathShared() {
31 std::scoped_lock lock(setLock);
Austin Schuh812d0d12021-11-04 20:16:48 -070032 if (!mathShared) {
33 mathShared = std::make_unique<DefaultMathShared>();
34 }
Austin Schuh1e69f942020-11-14 15:06:14 -080035 return *mathShared;
36}
37
38void MathSharedStore::SetMathShared(std::unique_ptr<MathShared> shared) {
39 std::scoped_lock lock(setLock);
40 mathShared = std::move(shared);
41}