ORE Studio 0.0.4
Loading...
Searching...
No Matches
domain_xsd.hpp
1
2#pragma once
3
4#ifndef XSDCPP_H
5#define XSDCPP_H
6
7#include <cstdint>
8#include <string>
9#include <vector>
10
11namespace xsd {
12
13typedef std::string string;
14
15// Wrapper for vector to allow specialization for bool
16// std::vector<bool> is specialized and .back() returns a proxy, not a real reference
17template <typename T>
18class vector : public std::vector<T>
19{
20public:
21 using std::vector<T>::vector;
22 using std::vector<T>::operator=;
23};
24
25// Specialization for vector<bool> using char storage to avoid proxy issues
26template <>
27class vector<bool> : public std::vector<char>
28{
29public:
30 using std::vector<char>::vector;
31 void push_back(bool value) { std::vector<char>::push_back(value ? 1 : 0); }
32 void emplace_back() { std::vector<char>::emplace_back(0); }
33 bool& back() { return reinterpret_cast<bool&>(std::vector<char>::back()); }
34 const bool& back() const { return reinterpret_cast<const bool&>(std::vector<char>::back()); }
35};
36
37template <typename T>
38class optional
39{
40public:
41 optional()
42 : _data(nullptr)
43 {
44 }
45 optional(const optional& other)
46 : _data(other._data ? new T(*other._data) : nullptr)
47 {
48 }
49
50 optional(optional&& other)
51 {
52 _data = other._data;
53 other._data = nullptr;
54 }
55
56 optional(const T& other)
57 : _data(new T(other))
58 {
59 }
60
61 optional(T&& other)
62 : _data(new T(std::move(other)))
63 {
64 }
65
66 ~optional()
67 {
68 delete _data;
69 }
70
71 optional& operator=(const optional& other)
72 {
73 if (other._data)
74 {
75 if (_data)
76 *_data = *other._data;
77 else
78 _data = new T(*other._data);
79 }
80 else
81 {
82 delete _data;
83 _data = nullptr;
84 }
85 return *this;
86 }
87
88 optional& operator=(optional&& other)
89 {
90 if (_data)
91 delete _data;
92 _data = other._data;
93 other._data = nullptr;
94 return *this;
95 }
96
97 optional& operator=(const T& other)
98 {
99 if (_data)
100 *_data = other;
101 else
102 _data = new T(other);
103 return *this;
104 }
105
106 optional& operator=(T&& other)
107 {
108 if (_data)
109 *_data = std::move(other);
110 else
111 _data = new T(std::move(other));
112 return *this;
113 }
114
115 operator bool() const { return _data != nullptr; }
116
117 T& operator*() { return *_data; }
118 const T& operator*() const { return *_data; }
119 T* operator->() { return _data; }
120 const T* operator->() const { return _data; }
121
122 friend bool operator==(const optional& lh, const T& rh) { return lh._data && *lh._data == rh; }
123 friend bool operator!=(const optional& lh, const T& rh) { return !lh._data || *lh._data != rh; }
124 friend bool operator==(const T& lh, const optional& rh) { return rh._data && lh == rh._data; }
125 friend bool operator!=(const T& lh, const optional& rh) { return !rh._data || lh != rh._data; }
126 friend bool operator==(const optional& lh, const optional& rh) { return lh._data == rh._data || (lh._data && rh._data && *lh._data == *rh._data); }
127 friend bool operator!=(const optional& lh, const optional& rh) { return lh._data != rh._data && (!lh._data || !rh._data || *lh._data != *rh._data); }
128
129private:
130 T* _data;
131};
132
133template <typename T>
134class base
135{
136public:
137 base()
138 : _value()
139 {
140 }
141 base(T value)
142 : _value(value)
143 {
144 }
145
146 operator T() const { return _value; }
147 operator T&() { return _value; }
148
149 base& operator=(T value)
150 {
151 _value = value;
152 return *this;
153 }
154
155private:
156 T _value;
157};
158
159struct any_attribute
160{
161 std::string name;
162 xsd::string value;
163};
164
165}
166
167#endif
STL namespace.