#pragma once #include #include #include #include #include namespace secsgem::secs2 { // SECS-II data item format codes (the 6-bit value, before being shifted left // two bits to make the format byte). Values are the canonical octal codes from // SEMI E5. enum class Format : uint8_t { List = 000, // 0 Binary = 010, // 8 Boolean = 011, // 9 ASCII = 020, // 16 I8 = 030, // 24 I1 = 031, // 25 I2 = 032, // 26 I4 = 034, // 28 F8 = 040, // 32 F4 = 044, // 36 U8 = 050, // 40 U1 = 051, // 41 U2 = 052, // 42 U4 = 054, // 44 }; const char* format_name(Format f); // Number of bytes one element of the given format occupies on the wire. // Lists are special (their length is an element count, not a byte count) and // return 0 here. std::size_t element_size(Format f); // A SECS-II data item: a typed, possibly nested value. Lists hold child items; // every other format holds a homogeneous array of scalars (a single scalar is // just an array of length one). The active variant alternative is kept in sync // with `format_`; several formats (Binary, Boolean, U1) share the same C++ // storage type and are disambiguated by `format_`. class Item { public: using List = std::vector; using Storage = std::variant< List, // List std::string, // ASCII std::vector, // Binary, Boolean, U1 std::vector, // I1 std::vector, // I2 std::vector, // I4 std::vector, // I8 std::vector, // U2 std::vector, // U4 std::vector, // U8 std::vector, // F4 std::vector>; // F8 Item() : format_(Format::List), data_(List{}) {} Format format() const { return format_; } bool is_list() const { return format_ == Format::List; } // Number of elements: child count for lists, character count for ASCII, // array length for numeric/binary formats. std::size_t size() const; // --- Factory functions ------------------------------------------------- static Item list(List items) { return Item(Format::List, std::move(items)); } static Item ascii(std::string s) { return Item(Format::ASCII, std::move(s)); } static Item binary(std::vector b) { return Item(Format::Binary, std::move(b)); } static Item boolean(std::vector b) { return Item(Format::Boolean, std::move(b)); } static Item boolean(bool b) { return Item(Format::Boolean, std::vector{static_cast(b ? 1 : 0)}); } static Item u1(std::vector v) { return Item(Format::U1, std::move(v)); } static Item u2(std::vector v) { return Item(Format::U2, std::move(v)); } static Item u4(std::vector v) { return Item(Format::U4, std::move(v)); } static Item u8(std::vector v) { return Item(Format::U8, std::move(v)); } static Item i1(std::vector v) { return Item(Format::I1, std::move(v)); } static Item i2(std::vector v) { return Item(Format::I2, std::move(v)); } static Item i4(std::vector v) { return Item(Format::I4, std::move(v)); } static Item i8(std::vector v) { return Item(Format::I8, std::move(v)); } static Item f4(std::vector v) { return Item(Format::F4, std::move(v)); } static Item f8(std::vector v) { return Item(Format::F8, std::move(v)); } // Scalar convenience overloads. static Item u1(uint8_t v) { return u1(std::vector{v}); } static Item u2(uint16_t v) { return u2(std::vector{v}); } static Item u4(uint32_t v) { return u4(std::vector{v}); } static Item u8(uint64_t v) { return u8(std::vector{v}); } static Item i1(int8_t v) { return i1(std::vector{v}); } static Item i2(int16_t v) { return i2(std::vector{v}); } static Item i4(int32_t v) { return i4(std::vector{v}); } static Item i8(int64_t v) { return i8(std::vector{v}); } static Item f4(float v) { return f4(std::vector{v}); } static Item f8(double v) { return f8(std::vector{v}); } // Construct directly from a format and matching storage (used by the decoder). static Item raw(Format f, Storage s) { return Item(f, std::move(s)); } // --- Typed accessors (throw std::bad_variant_access on mismatch) -------- const List& as_list() const { return std::get(data_); } List& as_list() { return std::get(data_); } const std::string& as_ascii() const { return std::get(data_); } const std::vector& as_bytes() const { return std::get>(data_); } const Storage& storage() const { return data_; } bool operator==(const Item&) const = default; private: Item(Format f, Storage s) : format_(f), data_(std::move(s)) {} Format format_; Storage data_; }; } // namespace secsgem::secs2