WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
MechanismObject2d.hpp
Go to the documentation of this file.
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.
4
5#pragma once
6
7#include <concepts>
8#include <memory>
9#include <stdexcept>
10#include <string>
11#include <string_view>
12#include <utility>
13
15#include "wpi/system/Errors.hpp"
17
18namespace wpi {
19
20/**
21 * Common base class for all Mechanism2d node types.
22 *
23 * To append another node, call Append with the type of node and its
24 * construction parameters. None of the node types are designed to be
25 * constructed directly, and are owned by their parent node/container - obtain
26 * pointers from the Append function or similar factory methods.
27 *
28 * @see Mechanism2d.
29 */
31 friend class Mechanism2d;
32
33 protected:
34 explicit MechanismObject2d(std::string_view name);
35
36 /**
37 * Update all entries with new ones from a new table.
38 *
39 * @param table the new table.
40 */
41 virtual void UpdateEntries(std::shared_ptr<wpi::nt::NetworkTable> table) = 0;
42
44
45 public:
46 virtual ~MechanismObject2d() = default;
47
48 /**
49 * Retrieve the object's name.
50 *
51 * @return the object's name relative to its parent.
52 */
53 const std::string& GetName() const;
54
55 /**
56 * Append a Mechanism object that is based on this one.
57 *
58 * @param name the name of the new object.
59 * @param args constructor arguments of the object type.
60 * @return the constructed and appended object, useful for variable
61 * assignments and call chaining.
62 * @throw if an object with the given name already exists.
63 */
64 template <typename T, typename... Args>
65 requires std::convertible_to<T*, MechanismObject2d*>
66 T* Append(std::string_view name, Args&&... args) {
67 std::scoped_lock lock(m_mutex);
68 auto& obj = m_objects[name];
69 if (obj) {
70 throw WPILIB_MakeError(
71 err::Error,
72 "MechanismObject names must be unique! `{}` was inserted twice!",
73 name);
74 }
75 obj = std::make_unique<T>(name, std::forward<Args>(args)...);
76 T* ex = static_cast<T*>(obj.get());
77 if (m_table) {
78 ex->Update(m_table->GetSubTable(name));
79 }
80 return ex;
81 }
82
83 private:
84 std::string m_name;
86 std::shared_ptr<wpi::nt::NetworkTable> m_table;
87 void Update(std::shared_ptr<wpi::nt::NetworkTable> table);
88};
89} // namespace wpi
#define WPILIB_MakeError(status, format,...)
Makes a runtime error exception object.
Definition Errors.hpp:164
@ name
Definition base.h:690
wpi::util::mutex m_mutex
Definition MechanismObject2d.hpp:43
virtual void UpdateEntries(std::shared_ptr< wpi::nt::NetworkTable > table)=0
Update all entries with new ones from a new table.
virtual ~MechanismObject2d()=default
MechanismObject2d(std::string_view name)
const std::string & GetName() const
Retrieve the object's name.
T * Append(std::string_view name, Args &&... args)
Append a Mechanism object that is based on this one.
Definition MechanismObject2d.hpp:66
friend class Mechanism2d
Definition MechanismObject2d.hpp:31
StringMap is a sorted associative container that contains key-value pairs with unique string keys.
Definition StringMap.hpp:26
::std::mutex mutex
Definition mutex.hpp:17
Definition CvSource.hpp:15