#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 JIS8 = 021, // 17 — 8-bit JIS (single-byte Japanese text) C2 = 022, // 18 — 2-byte Unicode code points (big-endian) 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 }; inline const char* format_name(Format f) { switch (f) { case Format::List: return "L"; case Format::Binary: return "B"; case Format::Boolean: return "BOOLEAN"; case Format::ASCII: return "A"; case Format::JIS8: return "J"; case Format::C2: return "C"; case Format::I8: return "I8"; case Format::I1: return "I1"; case Format::I2: return "I2"; case Format::I4: return "I4"; case Format::F8: return "F8"; case Format::F4: return "F4"; case Format::U8: return "U8"; case Format::U1: return "U1"; case Format::U2: return "U2"; case Format::U4: return "U4"; } return "?"; } // 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. inline std::size_t element_size(Format f) { switch (f) { case Format::List: return 0; case Format::ASCII: case Format::JIS8: case Format::Binary: case Format::Boolean: case Format::U1: case Format::I1: return 1; case Format::C2: case Format::U2: case Format::I2: return 2; case Format::U4: case Format::I4: case Format::F4: return 4; case Format::U8: case Format::I8: case Format::F8: return 8; } return 0; } // 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, JIS-8 std::vector, // Binary, Boolean, U1 std::vector, // I1 std::vector, // I2 std::vector, // I4 std::vector, // I8 std::vector, // U2, C2 (Unicode code points) 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 { return std::visit([](const auto& v) { return v.size(); }, data_); } // --- 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 jis8(std::string s) { return Item(Format::JIS8, std::move(s)); } static Item c2(std::vector code_points) { return Item(Format::C2, std::move(code_points)); } 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_); } // JIS-8 shares the std::string storage slot (it's a single-byte // encoding like ASCII); callers disambiguate via `format()`. const std::string& as_jis8() const { return std::get(data_); } // C2 (Unicode) shares the std::vector storage with U2. const std::vector& as_c2() 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