Address.hpp
1 #pragma once
2 #include <score/serialization/DataStreamFwd.hpp>
3 #include <score/tools/std/Optional.hpp>
4 
5 #include <ossia/detail/destination_index.hpp>
6 #include <ossia/detail/optional.hpp>
7 
8 #include <QString>
9 #include <QStringList>
10 
11 #include <score_lib_state_export.h>
12 
13 #include <memory>
14 #include <verdigris>
15 namespace ossia
16 {
17 struct destination_qualifiers;
18 struct unit_t;
19 }
20 
21 namespace State
22 {
23 struct SCORE_LIB_STATE_EXPORT DestinationQualifiers
24 {
25  W_GADGET(DestinationQualifiers)
26 public:
27  DestinationQualifiers() noexcept;
30  DestinationQualifiers& operator=(const DestinationQualifiers& other);
31  DestinationQualifiers& operator=(DestinationQualifiers&& other) noexcept;
33 
34  DestinationQualifiers(const ossia::destination_qualifiers&);
35  DestinationQualifiers& operator=(const ossia::destination_qualifiers&);
36 
37  operator const ossia::destination_qualifiers&() const noexcept;
38  operator ossia::destination_qualifiers&() noexcept;
39 
40  bool operator==(const State::DestinationQualifiers& other) const noexcept;
41  bool operator!=(const State::DestinationQualifiers& other) const noexcept;
42 
43  const ossia::destination_qualifiers& get() const noexcept;
44  ossia::destination_qualifiers& get() noexcept;
45 
46 private:
47  std::unique_ptr<ossia::destination_qualifiers> qualifiers;
48 };
57 struct SCORE_LIB_STATE_EXPORT Address
58 {
59  Address() noexcept;
60  Address(const Address& other) noexcept;
61  Address(Address&&) noexcept;
62  Address& operator=(const Address& other) noexcept;
63  Address& operator=(Address&& other) noexcept;
64  Address(QString d, QStringList p) noexcept;
65 
66  // Data
67  QString device; // No device means that this is the invisible root node.
68 
69  QStringList path; // Note : path is empty if address is root: "device:/"
70  // In terms of Device::Node, this means that the node is the device node.
71 
72  // Check that the given string is a valid address
73  // Note: a "maybe" concept would help here.
74  static bool validateString(const QString& str);
75  static bool validateFragment(const QString& s);
76 
77  // Make an address from a valid address string
78  static std::optional<Address> fromString(const QString& str);
79 
84  QString toString() const;
85 
86  QString toString_unsafe() const;
87 
92  QString toShortString() const;
93 
97  bool isSet() const noexcept;
98 
99  bool operator==(const Address& a) const;
100  bool operator!=(const Address& a) const;
101 };
102 
103 using AccessorVector = ossia::destination_index;
104 SCORE_LIB_STATE_EXPORT
105 QString toString(const ossia::destination_qualifiers& d);
106 
107 struct SCORE_LIB_STATE_EXPORT AddressAccessor
108 {
109  AddressAccessor() noexcept;
110  AddressAccessor(const AddressAccessor& other) noexcept;
111  AddressAccessor(AddressAccessor&& other) noexcept;
112  AddressAccessor& operator=(const AddressAccessor& other) noexcept;
113  AddressAccessor& operator=(AddressAccessor&& other) noexcept;
114 
115  explicit AddressAccessor(State::Address a) noexcept;
116  AddressAccessor(State::Address a, const AccessorVector& v) noexcept;
118  State::Address a, const AccessorVector& v, const ossia::unit_t&) noexcept;
119 
120  AddressAccessor& operator=(const Address& a);
121  AddressAccessor& operator=(Address&& a);
122 
123  State::Address address;
124  DestinationQualifiers qualifiers;
125 
126  // Utility
127  QString toString_unsafe() const;
128  QString toString() const;
129  QString toShortString() const;
130 
134  bool isSet() const noexcept;
135 
136  bool operator==(const AddressAccessor& other) const;
137  bool operator!=(const AddressAccessor& a) const;
138 };
139 
146 struct SCORE_LIB_STATE_EXPORT AddressAccessorHead
147 {
148  QString name;
149  DestinationQualifiers qualifiers;
150 
151  QString toString() const;
152 };
153 
154 SCORE_LIB_STATE_EXPORT
155 bool addressIsChildOf(
156  const State::Address& root, const State::Address& maybe_child) noexcept;
157 
158 SCORE_LIB_STATE_EXPORT
159 State::Address replaceAddressPart(
160  const State::Address& old, const State::Address& replacement) noexcept;
161 
162 SCORE_LIB_STATE_EXPORT
163 State::AddressAccessor replaceAddressPart(
164  const State::AddressAccessor& old, const State::Address& replacement) noexcept;
165 
166 SCORE_LIB_STATE_EXPORT
167 void rerootAddress(
168  State::Address& current, const State::Address& oldRoot,
169  const State::Address& newRoot);
170 
171 SCORE_LIB_STATE_EXPORT std::optional<State::Address> parseAddress(const QString& str);
172 SCORE_LIB_STATE_EXPORT std::optional<State::AddressAccessor>
173 parseAddressAccessor(const QString& str);
174 
175 SCORE_LIB_STATE_EXPORT QDebug operator<<(QDebug d, const State::Address& a);
176 SCORE_LIB_STATE_EXPORT QDebug operator<<(QDebug d, const DestinationQualifiers& a);
177 SCORE_LIB_STATE_EXPORT QDebug operator<<(QDebug d, const State::AccessorVector& a);
178 SCORE_LIB_STATE_EXPORT QDebug operator<<(QDebug d, const State::AddressAccessorHead& a);
179 SCORE_LIB_STATE_EXPORT QDebug operator<<(QDebug d, const State::AddressAccessor& a);
180 SCORE_LIB_STATE_EXPORT QStringList stringList(const State::Address& addr);
181 }
182 
183 namespace std
184 {
185 template <>
186 struct SCORE_LIB_STATE_EXPORT hash<State::Address>
187 {
188  std::size_t operator()(const State::Address& k) const;
189 };
190 
191 template <>
192 struct SCORE_LIB_STATE_EXPORT hash<State::AddressAccessor>
193 {
194  std::size_t operator()(const State::AddressAccessor& k) const;
195 };
196 }
197 
198 SCORE_SERIALIZE_DATASTREAM_DECLARE(SCORE_LIB_STATE_EXPORT, State::DestinationQualifiers)
199 SCORE_SERIALIZE_DATASTREAM_DECLARE(SCORE_LIB_STATE_EXPORT, State::Address)
200 SCORE_SERIALIZE_DATASTREAM_DECLARE(SCORE_LIB_STATE_EXPORT, State::AddressAccessor)
201 
202 Q_DECLARE_METATYPE(State::DestinationQualifiers)
203 Q_DECLARE_METATYPE(State::Address)
204 Q_DECLARE_METATYPE(State::AddressAccessor)
205 
206 W_REGISTER_ARGTYPE(State::DestinationQualifiers)
207 W_REGISTER_ARGTYPE(State::Address)
208 W_REGISTER_ARGTYPE(State::AddressAccessor)
Utilities for OSSIA data structures.
Definition: DeviceInterface.hpp:33
The AddressAccessorHead struct.
Definition: Address.hpp:147
Definition: Address.hpp:108
The Address struct.
Definition: Address.hpp:58
Definition: Address.hpp:24