Audacity 3.2.0
XMLAttributeValueView.h
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 XMLAttributeValueView.h
6
7 Dmitry Vedenko
8
9**********************************************************************/
10
11#pragma once
12
13#include <cstdint>
14#include <stdexcept>
15#include <string>
16#include <string_view>
17#include <limits>
18
19#include <wx/string.h>
20
43class XML_API XMLAttributeValueView final
44{
45public:
47 enum class Type
48 {
49 Null,
50 SignedInteger,
51 UnsignedInteger,
52 Float,
53 Double,
54 StringView
55 };
56
59
62
63 XMLAttributeValueView& operator = (const XMLAttributeValueView&) = default;
65
67 explicit XMLAttributeValueView(bool value) noexcept;
69 explicit XMLAttributeValueView(short value) noexcept;
71 explicit XMLAttributeValueView(unsigned short value) noexcept;
73 explicit XMLAttributeValueView(int value) noexcept;
75 explicit XMLAttributeValueView(unsigned int value) noexcept;
77 explicit XMLAttributeValueView(long value) noexcept;
79 explicit XMLAttributeValueView(unsigned long value) noexcept;
81 explicit XMLAttributeValueView(long long value) noexcept;
83 explicit XMLAttributeValueView(unsigned long long value) noexcept;
85 explicit XMLAttributeValueView(float value) noexcept;
87 explicit XMLAttributeValueView(double value) noexcept;
89 explicit XMLAttributeValueView(const std::string_view& value) noexcept;
90
92 Type GetType() const noexcept;
93
95 bool IsNull() const noexcept;
96
98 bool IsSignedInteger() const noexcept;
100 bool IsUnsignedInteger() const noexcept;
102 bool IsFloat() const noexcept;
104 bool IsDouble() const noexcept;
106 bool IsStringView() const noexcept;
107
109 bool TryGet(bool& value) const noexcept;
111 bool TryGet(short& value) const noexcept;
113 bool TryGet(unsigned short& value) const noexcept;
115 bool TryGet(int& value) const noexcept;
117 bool TryGet(unsigned int& value) const noexcept;
119 bool TryGet(long& value) const noexcept;
121 bool TryGet(unsigned long& value) const noexcept;
123 bool TryGet(long long& value) const noexcept;
125 bool TryGet(unsigned long long& value) const noexcept;
127 bool TryGet(float& value) const noexcept;
129 bool TryGet(double& value) const noexcept;
131 bool TryGet(std::string_view& value) const noexcept;
132
134 template<typename T>
135 T Get(T defaultValue = {}) 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 }
143 std::string ToString() const;
145 wxString ToWString() const;
146
147private:
148 template <typename ResultType>
149 bool TryGetInteger(ResultType& value) const noexcept;
150 // std::variant requires macOS 10.14, which is significantly higher than
151 // the current target
152 union
153 {
154 int64_t mInteger;
155 double mDouble;
156 float mFloat;
157
158 struct
159 {
160 const char* Data;
161 size_t Length;
162 } mStringView;
163 };
164
165 Type mType { Type::Null };
166};
A view into an attribute value. The class does not take the ownership of the data.
XMLAttributeValueView(const XMLAttributeValueView &)=default
XMLAttributeValueView()=default
Construct an uninitialized view of type Null.
Type
Type of the value represented by the XMLAttributeValueView.
XMLAttributeValueView(XMLAttributeValueView &&)=default
Services * Get()
Fetch the global instance, or nullptr if none is yet installed.
Definition: BasicUI.cpp:26
std::wstring ToWString(const std::string &str)
STL namespace.