WPILibC++ 2024.3.2
MechanismObject2d.h
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/StringMap.h>
16
17#include "frc/Errors.h"
18
19namespace frc {
20
21/**
22 * Common base class for all Mechanism2d node types.
23 *
24 * To append another node, call Append with the type of node and its
25 * construction parameters. None of the node types are designed to be
26 * constructed directly, and are owned by their parent node/container - obtain
27 * pointers from the Append function or similar factory methods.
28 *
29 * @see Mechanism2d.
30 */
32 friend class Mechanism2d;
33
34 protected:
36
37 /**
38 * Update all entries with new ones from a new table.
39 *
40 * @param table the new table.
41 */
42 virtual void UpdateEntries(std::shared_ptr<nt::NetworkTable> table) = 0;
43
45
46 public:
47 virtual ~MechanismObject2d() = default;
48
49 /**
50 * Retrieve the object's name.
51 *
52 * @return the object's name relative to its parent.
53 */
54 const std::string& GetName() const;
55
56 /**
57 * Append a Mechanism object that is based on this one.
58 *
59 * @param name the name of the new object.
60 * @param args constructor arguments of the object type.
61 * @return the constructed and appended object, useful for variable
62 * assignments and call chaining.
63 * @throw if an object with the given name already exists.
64 */
65 template <typename T, typename... Args>
66 requires std::convertible_to<T*, MechanismObject2d*>
67 T* Append(std::string_view name, Args&&... args) {
68 std::scoped_lock lock(m_mutex);
69 auto& obj = m_objects[name];
70 if (obj) {
71 throw FRC_MakeError(
72 err::Error,
73 "MechanismObject names must be unique! `{}` was inserted twice!",
74 name);
75 }
76 obj = std::make_unique<T>(name, std::forward<Args>(args)...);
77 T* ex = static_cast<T*>(obj.get());
78 if (m_table) {
79 ex->Update(m_table->GetSubTable(name));
80 }
81 return ex;
82 }
83
84 private:
85 std::string m_name;
87 std::shared_ptr<nt::NetworkTable> m_table;
88 void Update(std::shared_ptr<nt::NetworkTable> table);
89};
90} // namespace frc
This file defines the StringMap class.
Visual 2D representation of arms, elevators, and general mechanisms through a node-based API.
Definition: Mechanism2d.h:43
Common base class for all Mechanism2d node types.
Definition: MechanismObject2d.h:31
virtual void UpdateEntries(std::shared_ptr< nt::NetworkTable > table)=0
Update all entries with new ones from a new table.
T * Append(std::string_view name, Args &&... args)
Append a Mechanism object that is based on this one.
Definition: MechanismObject2d.h:67
wpi::mutex m_mutex
Definition: MechanismObject2d.h:44
const std::string & GetName() const
Retrieve the object's name.
virtual ~MechanismObject2d()=default
MechanismObject2d(std::string_view name)
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:116
basic_string_view< char > string_view
Definition: core.h:501
Definition: AprilTagPoseEstimator.h:15
constexpr const char * name(const T &)
::std::mutex mutex
Definition: mutex.h:17
#define FRC_MakeError(status, format,...)
Makes a runtime error exception object.
Definition: Errors.h:153