CTRE Phoenix Pro C++ 23.0.12
ControlRequest.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) Cross The Road Electronics.  All rights reserved.
3 * License information can be found in CTRE_LICENSE.txt
4 * For support and suggestions contact support@ctr-electronics.com or file
5 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
6 */
7#pragma once
8
9#include <map>
10#include <memory>
11#include <sstream>
12#include <string>
13
14namespace ctre {
15namespace phoenixpro {
16
17namespace hardware {
18 class ParentDevice;
19}
20
21namespace controls {
22
23 /**
24 * \brief Information about a control request.
25 *
26 * This type contains a map that maps control parameter names to
27 * the corresponding applied value. Users can iterate through the map
28 * to look at the applied control parameters of the control request.
29 */
31 {
32 std::map<std::string, std::string> values{};
33
34 public:
35 /**
36 * \brief Constructs a new ControlInfo object for the control request of the given name.
37 *
38 * This is constructed by the ControlRequest class and can be accessed by calling ControlRequest#GetControlInfo.
39 *
40 * \param name Name of the control request
41 */
42 ControlInfo(std::string name)
43 {
44 values["name"] = std::move(name);
45 }
46
47 /**
48 * \brief Gets the map of control parameter names to the corresponding applied value.
49 *
50 * \returns Map of control parameter names and corresponding applied values
51 */
52 std::map<std::string, std::string> &GetNameValues() { return values; }
53 /**
54 * \brief Gets the map of control parameter names to the corresponding applied value.
55 *
56 * \returns Map of control parameter names and corresponding applied values
57 */
58 const std::map<std::string, std::string> &GetNameValues() const { return values; }
59 };
60
61 /**
62 * \brief Abstract Control Request class that other control requests extend for use.
63 */
65 {
67 virtual ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, bool cancelOtherRequests, std::shared_ptr<ControlRequest> &req) = 0;
68
69 protected:
71
72 /* don't allow copy and move of the base class, only derived classes */
73 ControlRequest(ControlRequest const &) = default;
77
78 public:
79 /**
80 * \brief Constructs a new Control Request with the given name.
81 *
82 * \param name Name of the control request
83 */
84 ControlRequest(std::string name) : requestReference{std::move(name)}
85 {
86 }
87
88 /**
89 * \brief Gets information about this control request.
90 *
91 * \returns An instance of ControlInfo containing information about this control request
92 */
94 {
95 return requestReference;
96 }
97 /**
98 * \brief Gets information about this control request.
99 *
100 * \returns An instance of ControlInfo containing information about this control request
101 */
103 {
104 return requestReference;
105 }
106
107 virtual ~ControlRequest() = default;
108 virtual std::string ToString() const = 0;
109 };
110
111 /**
112 * \brief Generic Empty Control class used to do nothing.
113 */
115 {
116 ctre::phoenix::StatusCode SendRequest(const char *network, uint32_t deviceHash, bool cancelOtherRequests, std::shared_ptr<ControlRequest> &req)
117 {
118 (void)network;
119 (void)deviceHash;
120 (void)cancelOtherRequests;
121
122 if (req.get() != this)
123 {
124 auto const reqCast = dynamic_cast<EmptyControl *>(req.get());
125 if (reqCast != nullptr)
126 {
127 *reqCast = *this;
128 }
129 else
130 {
131 req = std::make_shared<EmptyControl>(*this);
132 }
133 }
134
135 return 0;
136 }
137
138 public:
139 /**
140 * \brief Constructs an empty control request.
141 */
142 EmptyControl() : ControlRequest{"EmptyControl"}
143 {
144 }
145
146 std::string ToString() const
147 {
148 std::stringstream ss;
149 ss << "class: EmptyControl" << std::endl;
150 return ss.str();
151 }
152 };
153
154}
155
156}
157}
Information about a control request.
Definition: ControlRequest.hpp:31
ControlInfo(std::string name)
Constructs a new ControlInfo object for the control request of the given name.
Definition: ControlRequest.hpp:42
const std::map< std::string, std::string > & GetNameValues() const
Gets the map of control parameter names to the corresponding applied value.
Definition: ControlRequest.hpp:58
std::map< std::string, std::string > & GetNameValues()
Gets the map of control parameter names to the corresponding applied value.
Definition: ControlRequest.hpp:52
Abstract Control Request class that other control requests extend for use.
Definition: ControlRequest.hpp:65
ControlRequest(ControlRequest const &)=default
const ControlInfo & GetControlInfo() const
Gets information about this control request.
Definition: ControlRequest.hpp:102
ControlRequest & operator=(ControlRequest &&)=default
ControlRequest(std::string name)
Constructs a new Control Request with the given name.
Definition: ControlRequest.hpp:84
ControlRequest(ControlRequest &&)=default
ControlInfo & GetControlInfo()
Gets information about this control request.
Definition: ControlRequest.hpp:93
virtual std::string ToString() const =0
ControlInfo requestReference
Definition: ControlRequest.hpp:70
ControlRequest & operator=(ControlRequest const &)=default
Generic Empty Control class used to do nothing.
Definition: ControlRequest.hpp:115
EmptyControl()
Constructs an empty control request.
Definition: ControlRequest.hpp:142
std::string ToString() const
Definition: ControlRequest.hpp:146
Parent class for all devices.
Definition: ParentDevice.hpp:30
Definition: string_util.hpp:14