WPILibC++ 2023.4.3
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 <memory>
8#include <stdexcept>
9#include <string>
10#include <string_view>
11#include <type_traits>
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 typename =
67 std::enable_if_t<std::is_convertible_v<T*, MechanismObject2d*>>>
68 T* Append(std::string_view name, Args&&... args) {
69 std::scoped_lock lock(m_mutex);
70 auto& obj = m_objects[name];
71 if (obj) {
72 throw FRC_MakeError(
73 err::Error,
74 "MechanismObject names must be unique! `{}` was inserted twice!",
75 name);
76 }
77 obj = std::make_unique<T>(name, std::forward<Args>(args)...);
78 T* ex = static_cast<T*>(obj.get());
79 if (m_table) {
80 ex->Update(m_table->GetSubTable(name));
81 }
82 return ex;
83 }
84
85 private:
86 std::string m_name;
88 std::shared_ptr<nt::NetworkTable> m_table;
89 void Update(std::shared_ptr<nt::NetworkTable> table);
90};
91} // 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:68
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:114
basic_string_view< char > string_view
Definition: core.h:520
Definition: AprilTagFieldLayout.h:22
::std::mutex mutex
Definition: mutex.h:17
#define FRC_MakeError(status, format,...)
Makes a runtime error exception object.
Definition: Errors.h:152