Audacity 3.2.0
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
XMLAttributeValueView Class Referencefinal

A view into an attribute value. The class does not take the ownership of the data. More...

#include <XMLAttributeValueView.h>

Public Types

enum class  Type {
  Null , SignedInteger , UnsignedInteger , Float ,
  Double , StringView
}
 Type of the value represented by the XMLAttributeValueView. More...
 

Public Member Functions

 XMLAttributeValueView ()=default
 Construct an uninitialized view of type Null. More...
 
 XMLAttributeValueView (const XMLAttributeValueView &)=default
 
 XMLAttributeValueView (XMLAttributeValueView &&)=default
 
XMLAttributeValueViewoperator= (const XMLAttributeValueView &)=default
 
XMLAttributeValueViewoperator= (XMLAttributeValueView &&)=default
 
 XMLAttributeValueView (bool value) noexcept
 Construct a view of type UnsignedInteger from the value. More...
 
 XMLAttributeValueView (short value) noexcept
 Construct a view of type SignedInteger from the value. More...
 
 XMLAttributeValueView (unsigned short value) noexcept
 Construct a view of type UnsignedInteger from the value. More...
 
 XMLAttributeValueView (int value) noexcept
 Construct a view of type SignedInteger from the value. More...
 
 XMLAttributeValueView (unsigned int value) noexcept
 Construct a view of type UnsignedInteger from the value. More...
 
 XMLAttributeValueView (long value) noexcept
 Construct a view of type SignedInteger from the value. More...
 
 XMLAttributeValueView (unsigned long value) noexcept
 Construct a view of type UnsignedInteger from the value. More...
 
 XMLAttributeValueView (long long value) noexcept
 Construct a view of type SignedInteger from the value. More...
 
 XMLAttributeValueView (unsigned long long value) noexcept
 Construct a view of type UnsignedInteger from the value. More...
 
 XMLAttributeValueView (float value) noexcept
 Construct a view of type Float from the value. More...
 
 XMLAttributeValueView (double value) noexcept
 Construct a view of type Double from the value. More...
 
 XMLAttributeValueView (const std::string_view &value) noexcept
 Construct a view of type StringView from the value. More...
 
Type GetType () const noexcept
 Get the view type. More...
 
bool IsNull () const noexcept
 Check if view is Null. More...
 
bool IsSignedInteger () const noexcept
 Check if view has the SignedInteger type. More...
 
bool IsUnsignedInteger () const noexcept
 Check if view has the UnsignedInteger type. More...
 
bool IsFloat () const noexcept
 Check if view has the Float type. More...
 
bool IsDouble () const noexcept
 Check if view has the Double type. More...
 
bool IsStringView () const noexcept
 Check if view has the StringView type. More...
 
bool TryGet (bool &value) const noexcept
 Try to get a boolean value from the view. More...
 
bool TryGet (short &value) const noexcept
 Try to get a short value from the view. More...
 
bool TryGet (unsigned short &value) const noexcept
 Try to get an unsigned short value from the view. More...
 
bool TryGet (int &value) const noexcept
 Try to get an int value from the view. More...
 
bool TryGet (unsigned int &value) const noexcept
 Try to get an unsigned int value from the view. More...
 
bool TryGet (long &value) const noexcept
 Try to get a long value from the view. More...
 
bool TryGet (unsigned long &value) const noexcept
 Try to get an unsigned long value from the view. More...
 
bool TryGet (long long &value) const noexcept
 Try to get a long long value from the view. More...
 
bool TryGet (unsigned long long &value) const noexcept
 Try to get an unsigned long long value from the view. More...
 
bool TryGet (float &value) const noexcept
 Try to get a float value from the view. More...
 
bool TryGet (double &value) const noexcept
 Try to get a double value from the view. More...
 
bool TryGet (std::string_view &value) const noexcept
 Try to get a string_view value from the view. More...
 
template<typename T >
Get (T defaultValue={}) const noexcept
 Returns the value if there is a viable conversion, default value otherwise. More...
 
std::string ToString () const
 Convert the view value to an UTF8 string. More...
 
wxString ToWString () const
 Convert the view value to wxString. More...
 

Private Member Functions

template<typename ResultType >
bool TryGetInteger (ResultType &value) const noexcept
 

Private Attributes

union {
   int64_t   mInteger
 
   double   mDouble
 
   float   mFloat
 
   struct {
      const char *   Data
 
      size_t   Length
 
   }   mStringView
 
}; 
 
Type mType { Type::Null }
 

Detailed Description

A view into an attribute value. The class does not take the ownership of the data.

Audacity internally uses regular XML and an internal binary XML-like format. Binary format extends the possible XML types to support integers and floating point values, not just the string values.

This class represents the attribute value for both text and binary XML formats.

Class has a number of Get overloads, that allow to read the value with the required data type.

Integer-based types can be read from the view if it was initialized by an integer value or with a string_view that is a textual representation of an integer value. To avoid runtime data loss it is not possible to read an integer from the floating point values. Boundary checks are always performed. If the type is not large enough to store the value, Get fails.

Floating point values can be read from the view, if it is initialized using floating point, integer or a compatible string value. Conversion from Float to Double is forbidden.

String values can be read only if the view was created using a string_view. However there are ToString() and ToWString() methods that allow conversion from any type to string. Null type is represented by an empty string.

Definition at line 43 of file XMLAttributeValueView.h.

Member Enumeration Documentation

◆ Type

enum class XMLAttributeValueView::Type
strong

Type of the value represented by the XMLAttributeValueView.

Enumerator
Null 

The value is not initialized.

SignedInteger 

The value is a signed integer.

UnsignedInteger 

The value is an unsigned integer.

Float 

The value is a single precision floating point value.

Double 

The value is a double precision floating point value.

StringView 

The value is a string_view.

Definition at line 47 of file XMLAttributeValueView.h.

48 {
49 Null,
50 SignedInteger,
51 UnsignedInteger,
52 Float,
53 Double,
54 StringView
55 };

Constructor & Destructor Documentation

◆ XMLAttributeValueView() [1/15]

XMLAttributeValueView::XMLAttributeValueView ( )
default

Construct an uninitialized view of type Null.

◆ XMLAttributeValueView() [2/15]

XMLAttributeValueView::XMLAttributeValueView ( const XMLAttributeValueView )
default

◆ XMLAttributeValueView() [3/15]

XMLAttributeValueView::XMLAttributeValueView ( XMLAttributeValueView &&  )
default

◆ XMLAttributeValueView() [4/15]

XMLAttributeValueView::XMLAttributeValueView ( bool  value)
explicitnoexcept

Construct a view of type UnsignedInteger from the value.

Definition at line 16 of file XMLAttributeValueView.cpp.

17 : mInteger(value)
19{
20}
@ UnsignedInteger
The value is an unsigned integer.

◆ XMLAttributeValueView() [5/15]

XMLAttributeValueView::XMLAttributeValueView ( short  value)
explicitnoexcept

Construct a view of type SignedInteger from the value.

Definition at line 22 of file XMLAttributeValueView.cpp.

23 : mInteger(value)
25{
26}
@ SignedInteger
The value is a signed integer.

◆ XMLAttributeValueView() [6/15]

XMLAttributeValueView::XMLAttributeValueView ( unsigned short  value)
explicitnoexcept

Construct a view of type UnsignedInteger from the value.

Definition at line 28 of file XMLAttributeValueView.cpp.

29 : mInteger(value)
31{
32}

◆ XMLAttributeValueView() [7/15]

XMLAttributeValueView::XMLAttributeValueView ( int  value)
explicitnoexcept

Construct a view of type SignedInteger from the value.

Definition at line 34 of file XMLAttributeValueView.cpp.

35 : mInteger(value)
37{
38}

◆ XMLAttributeValueView() [8/15]

XMLAttributeValueView::XMLAttributeValueView ( unsigned int  value)
explicitnoexcept

Construct a view of type UnsignedInteger from the value.

Definition at line 40 of file XMLAttributeValueView.cpp.

41 : mInteger(value)
43{
44}

◆ XMLAttributeValueView() [9/15]

XMLAttributeValueView::XMLAttributeValueView ( long  value)
explicitnoexcept

Construct a view of type SignedInteger from the value.

Definition at line 46 of file XMLAttributeValueView.cpp.

47 : mInteger(value)
49{
50}

◆ XMLAttributeValueView() [10/15]

XMLAttributeValueView::XMLAttributeValueView ( unsigned long  value)
explicitnoexcept

Construct a view of type UnsignedInteger from the value.

Definition at line 52 of file XMLAttributeValueView.cpp.

53 : mInteger(value)
55{
56}

◆ XMLAttributeValueView() [11/15]

XMLAttributeValueView::XMLAttributeValueView ( long long  value)
explicitnoexcept

Construct a view of type SignedInteger from the value.

Definition at line 58 of file XMLAttributeValueView.cpp.

59 : mInteger(value)
61{
62}

◆ XMLAttributeValueView() [12/15]

XMLAttributeValueView::XMLAttributeValueView ( unsigned long long  value)
explicitnoexcept

Construct a view of type UnsignedInteger from the value.

Definition at line 64 of file XMLAttributeValueView.cpp.

65 : mInteger(value)
67{
68}

◆ XMLAttributeValueView() [13/15]

XMLAttributeValueView::XMLAttributeValueView ( float  value)
explicitnoexcept

Construct a view of type Float from the value.

Definition at line 70 of file XMLAttributeValueView.cpp.

71 : mFloat(value)
73{
74}
@ Float
The value is a single precision floating point value.

◆ XMLAttributeValueView() [14/15]

XMLAttributeValueView::XMLAttributeValueView ( double  value)
explicitnoexcept

Construct a view of type Double from the value.

Definition at line 76 of file XMLAttributeValueView.cpp.

77 : mDouble(value)
79{
80}
@ Double
The value is a double precision floating point value.

◆ XMLAttributeValueView() [15/15]

XMLAttributeValueView::XMLAttributeValueView ( const std::string_view &  value)
explicitnoexcept

Construct a view of type StringView from the value.

Definition at line 82 of file XMLAttributeValueView.cpp.

85{
86 mStringView.Data = value.data();
87 mStringView.Length = value.length();
88}
struct XMLAttributeValueView::@138::@140 mStringView
@ StringView
The value is a string_view.

Member Function Documentation

◆ Get()

template<typename T >
T XMLAttributeValueView::Get ( defaultValue = {}) const
inlinenoexcept

Returns the value if there is a viable conversion, default value otherwise.

Definition at line 135 of file XMLAttributeValueView.h.

135 {}) const noexcept
136 {
137 // TryGet only modifies the value if conversion is possible,
138 // so just reuse the defaultValue
139 (void) TryGet(defaultValue);
140 return defaultValue;
141 }
bool TryGet(bool &value) const noexcept
Try to get a boolean value from the view.

◆ GetType()

XMLAttributeValueView::Type XMLAttributeValueView::GetType ( ) const
noexcept

Get the view type.

Definition at line 90 of file XMLAttributeValueView.cpp.

91{
92 return mType;
93}

References mType.

◆ IsDouble()

bool XMLAttributeValueView::IsDouble ( ) const
noexcept

Check if view has the Double type.

Definition at line 115 of file XMLAttributeValueView.cpp.

116{
117 return mType == Type::Double;
118}

References Double, and mType.

◆ IsFloat()

bool XMLAttributeValueView::IsFloat ( ) const
noexcept

Check if view has the Float type.

Definition at line 110 of file XMLAttributeValueView.cpp.

111{
112 return mType == Type::Float;
113}

References Float, and mType.

◆ IsNull()

bool XMLAttributeValueView::IsNull ( ) const
noexcept

Check if view is Null.

Definition at line 95 of file XMLAttributeValueView.cpp.

96{
97 return mType == Type::Null;
98}
@ Null
The value is not initialized.

References mType, and Null.

◆ IsSignedInteger()

bool XMLAttributeValueView::IsSignedInteger ( ) const
noexcept

Check if view has the SignedInteger type.

Definition at line 100 of file XMLAttributeValueView.cpp.

101{
102 return mType == Type::SignedInteger;
103}

References mType, and SignedInteger.

◆ IsStringView()

bool XMLAttributeValueView::IsStringView ( ) const
noexcept

Check if view has the StringView type.

Definition at line 120 of file XMLAttributeValueView.cpp.

121{
122 return mType == Type::StringView;
123}

References mType, and StringView.

◆ IsUnsignedInteger()

bool XMLAttributeValueView::IsUnsignedInteger ( ) const
noexcept

Check if view has the UnsignedInteger type.

Definition at line 105 of file XMLAttributeValueView.cpp.

106{
108}

References mType, and UnsignedInteger.

◆ operator=() [1/2]

XMLAttributeValueView & XMLAttributeValueView::operator= ( const XMLAttributeValueView )
default

◆ operator=() [2/2]

XMLAttributeValueView & XMLAttributeValueView::operator= ( XMLAttributeValueView &&  )
default

◆ ToString()

std::string XMLAttributeValueView::ToString ( ) const

Convert the view value to an UTF8 string.

Definition at line 245 of file XMLAttributeValueView.cpp.

246{
247 switch (mType)
248 {
250 return {};
252 return std::to_string(mInteger);
254 return std::to_string(static_cast<uint64_t>(mInteger));
256 return std::to_string(mFloat);
258 return std::to_string(mDouble);
260 return std::string(mStringView.Data, mStringView.Length);
261 }
262
263 return {};
264}

References Double, Float, mDouble, mFloat, mInteger, mStringView, mType, Null, SignedInteger, StringView, and UnsignedInteger.

◆ ToWString()

wxString XMLAttributeValueView::ToWString ( ) const

Convert the view value to wxString.

Definition at line 266 of file XMLAttributeValueView.cpp.

267{
268 switch (mType)
269 {
271 return {};
273 return wxString() << mInteger;
275 return wxString() << static_cast<uint64_t>(mInteger);
277 return wxString() << mFloat;
279 return wxString() << mDouble;
281 return wxString::FromUTF8(mStringView.Data, mStringView.Length);
282 }
283
284 return {};
285}

References Double, Float, mDouble, mFloat, mInteger, mStringView, mType, Null, SignedInteger, StringView, and UnsignedInteger.

Referenced by Track::HandleCommonXMLAttribute().

Here is the caller graph for this function:

◆ TryGet() [1/12]

bool XMLAttributeValueView::TryGet ( bool &  value) const
noexcept

Try to get a boolean value from the view.

Definition at line 125 of file XMLAttributeValueView.cpp.

126{
127 return TryGetInteger(value);
128}
bool TryGetInteger(ResultType &value) const noexcept

Referenced by Track::HandleCommonXMLAttribute(), PlayableTrack::HandleXMLAttribute(), SelectedRegion::HandleXMLAttribute(), WaveformAppearance::HandleXMLAttribute(), WaveColorAttachment::HandleXMLAttribute(), and ChannelView::HandleXMLAttribute().

Here is the caller graph for this function:

◆ TryGet() [2/12]

bool XMLAttributeValueView::TryGet ( double &  value) const
noexcept

Try to get a double value from the view.

Definition at line 200 of file XMLAttributeValueView.cpp.

201{
202 if (mType == Type::Float)
203 {
204 value = mFloat;
205 return true;
206 }
207 else if (mType == Type::Double)
208 {
209 value = mDouble;
210 return true;
211 }
213 {
214 value = mInteger;
215 return true;
216 }
217 else if (mType == Type::StringView)
218 {
219 const char* end = mStringView.Data + mStringView.Length;
220
221 double tempValue = {};
222
223 const auto result = FromChars(mStringView.Data, end, tempValue);
224
225 if (result.ec == std::errc() && result.ptr == end)
226 {
227 value = tempValue;
228 return true;
229 }
230 }
231
232 return false;
233}
FromCharsResult FromChars(const char *buffer, const char *last, float &value) noexcept
Parse a string into a single precision floating point value, always uses the dot as decimal.
Definition: FromChars.cpp:153
const char * end(const char *str) noexcept
Definition: StringUtils.h:106

References details::end(), and FromChars().

Here is the call graph for this function:

◆ TryGet() [3/12]

bool XMLAttributeValueView::TryGet ( float &  value) const
noexcept

Try to get a float value from the view.

Definition at line 170 of file XMLAttributeValueView.cpp.

171{
172 if (mType == Type::Float)
173 {
174 value = mFloat;
175 return true;
176 }
178 {
179 value = mInteger;
180 return true;
181 }
182 else if (mType == Type::StringView)
183 {
184 const char* end = mStringView.Data + mStringView.Length;
185
186 float tempValue = {};
187
188 const auto result = FromChars(mStringView.Data, end, tempValue);
189
190 if (result.ec == std::errc() && result.ptr == end)
191 {
192 value = tempValue;
193 return true;
194 }
195 }
196
197 return false;
198}

References details::end(), and FromChars().

Here is the call graph for this function:

◆ TryGet() [4/12]

bool XMLAttributeValueView::TryGet ( int &  value) const
noexcept

Try to get an int value from the view.

Definition at line 140 of file XMLAttributeValueView.cpp.

141{
142 return TryGetInteger(value);
143}

◆ TryGet() [5/12]

bool XMLAttributeValueView::TryGet ( long &  value) const
noexcept

Try to get a long value from the view.

Definition at line 150 of file XMLAttributeValueView.cpp.

151{
152 return TryGetInteger(value);
153}

◆ TryGet() [6/12]

bool XMLAttributeValueView::TryGet ( long long &  value) const
noexcept

Try to get a long long value from the view.

Definition at line 160 of file XMLAttributeValueView.cpp.

161{
162 return TryGetInteger(value);
163}

◆ TryGet() [7/12]

bool XMLAttributeValueView::TryGet ( short &  value) const
noexcept

Try to get a short value from the view.

Definition at line 130 of file XMLAttributeValueView.cpp.

131{
132 return TryGetInteger(value);
133}

◆ TryGet() [8/12]

bool XMLAttributeValueView::TryGet ( std::string_view &  value) const
noexcept

Try to get a string_view value from the view.

Definition at line 235 of file XMLAttributeValueView.cpp.

236{
237 if (mType != Type::StringView)
238 return false;
239
240 value = std::string_view(mStringView.Data, mStringView.Length);
241
242 return true;
243}

◆ TryGet() [9/12]

bool XMLAttributeValueView::TryGet ( unsigned int &  value) const
noexcept

Try to get an unsigned int value from the view.

Definition at line 145 of file XMLAttributeValueView.cpp.

146{
147 return TryGetInteger(value);
148}

◆ TryGet() [10/12]

bool XMLAttributeValueView::TryGet ( unsigned long &  value) const
noexcept

Try to get an unsigned long value from the view.

Definition at line 155 of file XMLAttributeValueView.cpp.

156{
157 return TryGetInteger(value);
158}

◆ TryGet() [11/12]

bool XMLAttributeValueView::TryGet ( unsigned long long &  value) const
noexcept

Try to get an unsigned long long value from the view.

Definition at line 165 of file XMLAttributeValueView.cpp.

166{
167 return TryGetInteger(value);
168}

◆ TryGet() [12/12]

bool XMLAttributeValueView::TryGet ( unsigned short &  value) const
noexcept

Try to get an unsigned short value from the view.

Definition at line 135 of file XMLAttributeValueView.cpp.

136{
137 return TryGetInteger(value);
138}

◆ TryGetInteger()

template<typename ResultType >
bool XMLAttributeValueView::TryGetInteger ( ResultType &  value) const
privatenoexcept

Definition at line 318 of file XMLAttributeValueView.cpp.

319{
320 static_assert(std::is_integral_v<ResultType>);
321
323 {
324 return CheckInteger(value, mInteger);
325 }
326 else if (mType == Type::UnsignedInteger)
327 {
328 return CheckInteger(value, static_cast<uint64_t>(mInteger));
329 }
330 else if (mType == Type::StringView)
331 {
332 const char* end = mStringView.Data + mStringView.Length;
333
334 ResultType tempValue = {};
335
336 const auto result = FromChars(mStringView.Data, end, tempValue);
337
338 if (result.ec == std::errc() && result.ptr == end)
339 {
340 value = tempValue;
341 return true;
342 }
343 }
344
345 return false;
346}
bool CheckInteger(ResultType &output, int64_t value) noexcept

References CheckInteger(), details::end(), and FromChars().

Here is the call graph for this function:

Member Data Documentation

◆ 

union { ... } XMLAttributeValueView::@139

◆ Data

const char* XMLAttributeValueView::Data

Definition at line 160 of file XMLAttributeValueView.h.

◆ Length

size_t XMLAttributeValueView::Length

Definition at line 161 of file XMLAttributeValueView.h.

◆ mDouble

double XMLAttributeValueView::mDouble

Definition at line 155 of file XMLAttributeValueView.h.

Referenced by ToString(), and ToWString().

◆ mFloat

float XMLAttributeValueView::mFloat

Definition at line 156 of file XMLAttributeValueView.h.

Referenced by ToString(), and ToWString().

◆ mInteger

int64_t XMLAttributeValueView::mInteger

Definition at line 154 of file XMLAttributeValueView.h.

Referenced by ToString(), and ToWString().

◆ 

struct { ... } XMLAttributeValueView::mStringView

Referenced by ToString(), and ToWString().

◆ mType

Type XMLAttributeValueView::mType { Type::Null }
private

The documentation for this class was generated from the following files: