plcopen 0.22.0
载入中...
搜索中...
未找到
binding_manifest.h
浏览该文件的文档.
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <cstdint>
6#include <string>
7#include <string_view>
8
9#include "st/binding.h"
10#include "st/pins.h"
11#include "st/type_desc.h"
12
13namespace plcopen::core::st
14{
15
16// Stable public identity for an ST binding. Values are grouped by authority
17// set and follow that set's canonical order; they do not encode FbType or a
18// native C++ block type. In particular MC_CamOut and MC_GearOut remain
19// distinct even though the native facade currently aliases their block type.
20enum class BindingFbId : std::uint32_t
21{
23 r_trig = 0x00010001U,
24 f_trig = 0x00010002U,
25 sr = 0x00010003U,
26 rs = 0x00010004U,
27 ton = 0x00010005U,
28 tof = 0x00010006U,
29 tp = 0x00010007U,
30 ctu = 0x00010008U,
31 ctd = 0x00010009U,
32 ctud = 0x0001000aU,
33 mc_power = 0x00020001U,
34 mc_home = 0x00020002U,
35 mc_stop = 0x00020003U,
36 mc_halt = 0x00020004U,
37 mc_move_absolute = 0x00020005U,
38 mc_move_relative = 0x00020006U,
39 mc_move_additive = 0x00020007U,
40 mc_move_velocity = 0x0002000aU,
41 mc_set_override = 0x00020012U,
42 mc_reset = 0x00020021U,
43 mc_cam_out = 0x00020027U,
44 mc_gear_out = 0x00020029U,
45 mc_add_axis_to_group = 0x00030001U,
47 mc_ungroup_all_axes = 0x00030003U,
48 mc_group_read_status = 0x0003001eU,
49};
50
58
59enum class PinDirection : std::uint8_t
60{
61 input = 0,
64};
65
66using BindingPinId = std::uint16_t;
67
69{
71 std::string_view lower_name;
74 bool declared = false;
75 bool generated = false;
76 bool excluded = false;
77 std::string_view scope_reason;
78 std::string_view rejection_test;
79};
80
82{
85 std::string_view lower_name;
86 std::string_view source;
87 bool declared = false;
88 bool generated = false;
89 bool excluded = false;
90 std::string_view scope_reason;
91 std::string_view rejection_test;
92 const BindingPinDesc *pins = nullptr;
93 std::size_t pin_count = 0;
94};
95
102
104{
105
119
135
136constexpr TypeId pin_type_id(std::string_view name) noexcept
137{
138 if(name == "BOOL") return builtin::bool_;
139 if(name == "SINT") return builtin::sint;
140 if(name == "INT") return builtin::int_;
141 if(name == "DINT") return builtin::dint;
142 if(name == "LINT") return builtin::lint;
143 if(name == "USINT") return builtin::usint;
144 if(name == "UINT") return builtin::uint_;
145 if(name == "REAL") return builtin::real;
146 if(name == "LREAL") return builtin::lreal;
147 if(name == "TIME") return builtin::time;
148 if(name == "UDINT") return builtin::udint;
149 if(name == "ULINT") return builtin::ulint;
150 if(name == "BYTE") return builtin::byte_;
151 if(name == "WORD") return builtin::word;
152 if(name == "DWORD") return builtin::dword;
153 if(name == "LWORD") return builtin::lword;
154 if(name == "DATE") return builtin::date;
155 if(name == "TOD") return builtin::tod;
156 if(name == "DT") return builtin::dt;
157 for(const generated::StBindingTypeMetadata &metadata :
159 if(metadata.name == name) return metadata.type;
160 }
161 return invalid_type_id;
162}
163
164inline void append_unsigned(std::string &out, std::uint64_t value)
165{
166 char digits[20]{};
167 std::size_t count = 0;
168 do {
169 digits[count++] = static_cast<char>('0' + value % 10U);
170 value /= 10U;
171 } while(value != 0U);
172 while(count != 0U) out.push_back(digits[--count]);
173}
174
175} // namespace binding_manifest_detail
176
177// Load-domain view generated from the complete checked-in authority catalog.
178class BindingManifest
179{
180 public:
181 std::size_t fb_count() const noexcept { return fbs_.size(); }
182
183 const BindingFbDesc &fb(std::size_t index) const noexcept
184 {
185 return fbs_[index];
186 }
187
188 const BindingFbDesc *find(BindingFbId id) const noexcept
189 {
190 for(const BindingFbDesc &desc : fbs_) {
191 if(desc.id == id) return &desc;
192 }
193 return nullptr;
194 }
195
196 const TypeTable &type_table() const noexcept { return types_; }
197
198 std::size_t target_count() const noexcept { return targets_.size(); }
199
200 const BindingTargetDesc &target(std::size_t index) const noexcept
201 {
202 return targets_[index];
203 }
204
205 std::string canonical_dump() const
206 {
207 std::string out;
208 for(const BindingFbDesc &fb_desc : fbs_) {
209 out += "fb ";
211 out, static_cast<std::uint32_t>(fb_desc.id));
212 out += " set=";
214 out, static_cast<std::uint8_t>(fb_desc.set));
215 out += " name=";
216 out.append(fb_desc.lower_name.data(), fb_desc.lower_name.size());
217 out += " source=";
218 out.append(fb_desc.source.data(), fb_desc.source.size());
219 out += " declared=";
220 out += fb_desc.declared ? '1' : '0';
221 out += " generated=";
222 out += fb_desc.generated ? '1' : '0';
223 out += " excluded=";
224 out += fb_desc.excluded ? '1' : '0';
225 out += " scope=";
226 out.append(fb_desc.scope_reason.data(), fb_desc.scope_reason.size());
227 out += " rejection=";
228 out.append(fb_desc.rejection_test.data(),
229 fb_desc.rejection_test.size());
230 out += '\n';
231 for(std::size_t index = 0; index < fb_desc.pin_count; ++index) {
232 const BindingPinDesc &pin = fb_desc.pins[index];
233 out += "pin ";
235 out += " name=";
236 out.append(pin.lower_name.data(), pin.lower_name.size());
237 out += " direction=";
239 out, static_cast<std::uint8_t>(pin.direction));
240 out += " type=";
242 out += " declared=";
243 out += pin.declared ? '1' : '0';
244 out += " generated=";
245 out += pin.generated ? '1' : '0';
246 out += " excluded=";
247 out += pin.excluded ? '1' : '0';
248 out += " scope=";
249 out.append(pin.scope_reason.data(), pin.scope_reason.size());
250 out += " rejection=";
251 out.append(pin.rejection_test.data(),
252 pin.rejection_test.size());
253 out += '\n';
254 }
255 }
256 for(const BindingTargetDesc &target_desc : targets_) {
257 out += "target ";
259 out, static_cast<std::uint8_t>(target_desc.kind));
260 out += " type=";
261 binding_manifest_detail::append_unsigned(out, target_desc.type_id);
262 out += " bindable=";
263 out += target_desc.bindable ? '1' : '0';
264 out += '\n';
265 }
266 return out;
267 }
268
269 private:
270 friend const BindingManifest &binding_manifest();
271
272 static constexpr std::size_t kMaxPins = 32;
273
275 {
276 (void)install_binding_types(types_);
277 for(std::size_t fb_index = 0;
278 fb_index < generated::kStBindingFbs.size(); ++fb_index) {
279 const generated::StBindingFbMetadata &source =
280 generated::kStBindingFbs[fb_index];
281 for(std::size_t pin_index = 0; pin_index < source.pin_count;
282 ++pin_index) {
284 generated::kStBindingPins[source.first_pin + pin_index];
285 pins_[fb_index][pin_index] = {
286 pin.stable_id, pin.name,
289 true, true, false, {}, {}};
290 }
291 fbs_[fb_index] = {
292 static_cast<BindingFbId>(source.stable_id),
294 source.parser_key, source.source, true, true, false, {}, {},
295 pins_[fb_index].data(), source.pin_count};
296 }
297 }
298
299 TypeTable types_;
300 std::array<std::array<BindingPinDesc, kMaxPins>,
301 generated::kStBindingFbs.size()> pins_{};
302 std::array<BindingFbDesc, generated::kStBindingFbs.size()> fbs_{};
303 std::array<BindingTargetDesc, 2> targets_{{
306 }};
307};
308
309inline const BindingManifest &binding_manifest()
310{
311 static const BindingManifest manifest;
312 return manifest;
313}
314
316 "aliased native FBs require distinct binding identities");
317
318} // namespace plcopen::core::st
std::size_t target_count() const noexcept
std::size_t fb_count() const noexcept
const BindingFbDesc * find(BindingFbId id) const noexcept
friend const BindingManifest & binding_manifest()
const BindingFbDesc & fb(std::size_t index) const noexcept
const BindingTargetDesc & target(std::size_t index) const noexcept
const TypeTable & type_table() const noexcept
constexpr BindingSet binding_set(generated::StBindingSet set) noexcept
constexpr PinDirection pin_direction(generated::StBindingPinDirection direction) noexcept
constexpr TypeId pin_type_id(std::string_view name) noexcept
void append_unsigned(std::string &out, std::uint64_t value)
constexpr std::array< StBindingTypeMetadata, 49 > kStBindingTypes
constexpr std::array< StBindingPinMetadata, 1476 > kStBindingPins
constexpr std::array< StBindingFbMetadata, 134 > kStBindingFbs
std::uint32_t TypeId
TypeError install_binding_types(TypeTable &types)
const BindingManifest & binding_manifest()
constexpr TypeId invalid_type_id