NanoOcp
Minimal AES70 / OCP.1 TCP client/server library for d&b Soundscape devices
Loading...
Searching...
No Matches
Variant.h
Go to the documentation of this file.
1/* Copyright (c) 2024, Bernardo Escalona
2 *
3 * This file is part of NanoOcp <https://github.com/ChristianAhrens/NanoOcp>
4 *
5 * This library is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License version 3.0 as published
7 * by the Free Software Foundation.
8 *
9 * This library is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 * details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19#pragma once
20
21#include <cstdint> //< USE std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t in GCC-13
22#include <variant> //< USE std::variant
23#include <array> //< USE std::array
24#include "Ocp1DataTypes.h" //< USE NanoOcp1::Ocp1DataType
25
26
27namespace NanoOcp1
28{
29
102{
103public:
105 Variant(bool v);
107 Variant(std::int32_t v);
109 Variant(std::uint8_t v);
111 Variant(std::uint16_t v);
113 Variant(std::uint32_t v);
115 Variant(std::uint64_t v);
117 Variant(std::float_t v);
119 Variant(std::double_t v);
121 Variant(const std::string& v);
123 Variant(const char* v);
132 Variant(std::float_t x, std::float_t y, std::float_t z);
133
137 Variant() = default;
138
146 Variant(const std::vector<std::uint8_t>& data, Ocp1DataType type = OCP1DATATYPE_BLOB);
147
148 virtual ~Variant() = default;
149
151 bool operator==(const Variant& other) const;
153 bool operator!=(const Variant& other) const;
154
161 bool IsValid() const;
162
169
177 std::vector<std::uint8_t> ToParamData(Ocp1DataType type = OCP1DATATYPE_NONE, bool* pOk = nullptr) const;
178
195 bool ToBool(bool* pOk = nullptr) const;
197 std::int32_t ToInt32(bool* pOk = nullptr) const;
199 std::uint8_t ToUInt8(bool* pOk = nullptr) const;
201 std::uint16_t ToUInt16(bool* pOk = nullptr) const;
203 std::uint32_t ToUInt32(bool* pOk = nullptr) const;
205 std::uint64_t ToUInt64(bool* pOk = nullptr) const;
207 std::float_t ToFloat(bool* pOk = nullptr) const;
209 std::double_t ToDouble(bool* pOk = nullptr) const;
215 std::string ToString(bool* pOk = nullptr) const;
216
226 std::array<std::float_t, 3> ToPosition(bool* pOk = nullptr) const;
227
234 std::string ToPositionString(bool* pOk = nullptr) const;
235
245 std::array<std::float_t, 6> ToAimingAndPosition(bool* pOk = nullptr) const;
246
247 [[deprecated("Use ToAimingAndPosition instead, this method will be removed in the future. "
248 "NOTE: The output of both methods is identical, but the new method has a more consistent name.")]]
249 std::array<std::float_t, 6> ToPositionAndRotation(bool* pOk = nullptr) const;
250
258 std::string ToAimingAndPositionString(bool* pOk = nullptr) const;
259
267 std::vector<bool> ToBoolVector(bool* pOk = nullptr) const;
268
276 std::vector<std::string> ToStringVector(bool* pOk = nullptr) const;
277
278
279protected:
286 std::vector<std::uint8_t> ToByteVector(bool* pOk = nullptr) const;
287
310
318 using VariantType = std::variant<std::monostate, // TypeNone
319 bool, // TypeBool
320 std::int32_t, // TypeInt32
321 std::uint8_t, // TypeUInt8
322 std::uint16_t, // TypeUInt16
323 std::uint32_t, // TypeUInt32
324 std::uint64_t, // TypeUInt64
325 std::float_t, // TypeFloat
326 std::double_t, // TypeDouble
327 std::string, // TypeString
328 std::vector<std::uint8_t>>; // TypeByteVector
329
330private:
331 VariantType m_value;
332};
333
334}
Type-erased OCA parameter value with built-in marshal/unmarshal support.
Definition Variant.h:102
std::array< std::float_t, 6 > ToAimingAndPosition(bool *pOk=nullptr) const
Definition Variant.cpp:675
TypeIndex
Compact index enum for the internal std::variant alternative types.
Definition Variant.h:297
@ TypeFloat
std::float_t (32-bit IEEE 754)
Definition Variant.h:305
@ TypeInt32
std::int32_t
Definition Variant.h:300
@ TypeByteVector
std::vector<uint8_t> — used for blobs and multi-float spatial types
Definition Variant.h:308
@ TypeUInt64
std::uint64_t
Definition Variant.h:304
@ TypeUInt8
std::uint8_t
Definition Variant.h:301
@ TypeDouble
std::double_t (64-bit IEEE 754)
Definition Variant.h:306
@ TypeString
std::string (OCA string: 2-byte length prefix + UTF-8 bytes)
Definition Variant.h:307
@ TypeNone
Default / unset state (std::monostate). IsValid() returns false.
Definition Variant.h:298
@ TypeUInt32
std::uint32_t
Definition Variant.h:303
@ TypeUInt16
std::uint16_t
Definition Variant.h:302
std::array< std::float_t, 6 > ToPositionAndRotation(bool *pOk=nullptr) const
Definition Variant.cpp:670
std::vector< bool > ToBoolVector(bool *pOk=nullptr) const
Definition Variant.cpp:735
bool IsValid() const
Definition Variant.cpp:135
bool ToBool(bool *pOk=nullptr) const
Returns the value as bool. Numeric types: non-zero = true.
Definition Variant.cpp:161
std::float_t ToFloat(bool *pOk=nullptr) const
Returns the value as float_t (32-bit). Conversion from double loses precision.
Definition Variant.cpp:455
std::string ToPositionString(bool *pOk=nullptr) const
Definition Variant.cpp:650
Ocp1DataType GetDataType() const
Definition Variant.cpp:140
std::vector< std::uint8_t > ToByteVector(bool *pOk=nullptr) const
Definition Variant.cpp:534
std::double_t ToDouble(bool *pOk=nullptr) const
Returns the value as double_t (64-bit).
Definition Variant.cpp:412
virtual ~Variant()=default
std::uint64_t ToUInt64(bool *pOk=nullptr) const
Returns the value as uint64_t.
Definition Variant.cpp:369
std::vector< std::uint8_t > ToParamData(Ocp1DataType type=OCP1DATATYPE_NONE, bool *pOk=nullptr) const
Definition Variant.cpp:570
std::uint8_t ToUInt8(bool *pOk=nullptr) const
Returns the value as uint8_t. Values outside [0, 255] are clamped/truncated.
Definition Variant.cpp:240
bool operator==(const Variant &other) const
Returns true if both Variants hold the same type and value.
Definition Variant.cpp:125
std::uint16_t ToUInt16(bool *pOk=nullptr) const
Returns the value as uint16_t.
Definition Variant.cpp:283
std::variant< std::monostate, bool, std::int32_t, std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t, std::float_t, std::double_t, std::string, std::vector< std::uint8_t > > VariantType
The underlying std::variant that holds the actual value.
Definition Variant.h:328
bool operator!=(const Variant &other) const
Returns true if the Variants differ in type or value.
Definition Variant.cpp:130
std::array< std::float_t, 3 > ToPosition(bool *pOk=nullptr) const
Definition Variant.cpp:619
std::uint32_t ToUInt32(bool *pOk=nullptr) const
Returns the value as uint32_t.
Definition Variant.cpp:326
std::string ToAimingAndPositionString(bool *pOk=nullptr) const
Definition Variant.cpp:714
std::int32_t ToInt32(bool *pOk=nullptr) const
Returns the value as int32_t. Numeric widening/narrowing applied as needed.
Definition Variant.cpp:197
std::string ToString(bool *pOk=nullptr) const
Returns the value as a std::string. Only succeeds if the internal type is TypeString; numeric types a...
Definition Variant.cpp:498
std::vector< std::string > ToStringVector(bool *pOk=nullptr) const
Definition Variant.cpp:775
Minimal AES70 / OCP.1 TCP client/server library built on JUCE.
Definition NanoOcp1.cpp:23
Ocp1DataType
OCA base data type codes, matching OcaBaseDataType in the AES70 specification.
@ OCP1DATATYPE_NONE
No type; used as "not set" sentinel.
@ OCP1DATATYPE_BLOB
Variable-length binary blob; layout is property-specific.