Skip to content

Commit e92e988

Browse files
committed
feat: Array::emplace_back
1 parent 1eda403 commit e92e988

File tree

2 files changed

+105
-31
lines changed

2 files changed

+105
-31
lines changed

include/mrdox/Support/Dom.hpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ class MRDOX_DECL
124124
*/
125125
value_type at(size_type i) const;
126126

127+
/** Append an element to the end of the array.
128+
129+
If the array is read-only, an exception
130+
is thrown.
131+
*/
132+
void emplace_back(value_type value);
133+
127134
/** Return a diagnostic string.
128135
*/
129136
friend
@@ -162,6 +169,46 @@ class MRDOX_DECL
162169
/** Return the i-th element, without bounds checking.
163170
*/
164171
virtual value_type get(size_type i) const = 0;
172+
173+
/** Append an element to the end of the array.
174+
175+
The default implementation throws an exception,
176+
making the array effectively read-only.
177+
*/
178+
virtual void emplace_back(value_type value);
179+
};
180+
181+
//------------------------------------------------
182+
//
183+
// DefaultArrayImpl
184+
//
185+
//------------------------------------------------
186+
187+
/** The default array implementation.
188+
189+
This implementation is backed by a simple
190+
vector and allows appending.
191+
*/
192+
class MRDOX_DECL
193+
DefaultArrayImpl : public ArrayImpl
194+
{
195+
public:
196+
/// @copydoc Array::value_type
197+
using value_type = Array::value_type;
198+
199+
/// @copydoc Array::size_type
200+
using size_type = Array::size_type;
201+
202+
/** The type of storage used by the default implementation.
203+
*/
204+
using storage_type = std::vector<value_type>;
205+
206+
size_type size() const override;
207+
value_type get(size_type i) const override;
208+
void emplace_back(value_type value) override;
209+
210+
private:
211+
std::vector<value_type> elements_;
165212
};
166213

167214
/** Return a new array using a custom implementation.
@@ -923,6 +970,11 @@ inline auto Array::at(std::size_t i) const -> value_type
923970
Error("out of range").Throw();
924971
}
925972

973+
inline void Array::emplace_back(value_type value)
974+
{
975+
impl_->emplace_back(std::move(value));
976+
}
977+
926978
//------------------------------------------------
927979

928980
inline bool Object::empty() const

source/Support/Dom.cpp

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,6 @@ static_assert(std::ranges::random_access_range<Object>);
2727
//
2828
//------------------------------------------------
2929

30-
namespace {
31-
32-
class DefaultArrayImpl : public ArrayImpl
33-
{
34-
std::vector<Value> v_;
35-
36-
public:
37-
std::size_t
38-
size() const noexcept override
39-
{
40-
return v_.size();
41-
}
42-
43-
Value get(std::size_t i) const override
44-
{
45-
MRDOX_ASSERT(i < v_.size());
46-
return v_[i];
47-
}
48-
};
49-
50-
} // (anon)
51-
52-
//------------------------------------------------
53-
//
54-
// Array
55-
//
56-
//------------------------------------------------
57-
5830
Array::
5931
~Array() = default;
6032

@@ -97,19 +69,60 @@ toString(
9769
return s;
9870
}
9971

72+
//------------------------------------------------
73+
//
74+
// ArrayImpl
75+
//
10076
//------------------------------------------------
10177

10278
ArrayImpl::
10379
~ArrayImpl() = default;
10480

81+
void
82+
ArrayImpl::
83+
emplace_back(
84+
value_type value)
85+
{
86+
Error("Array is const").Throw();
87+
}
88+
10589
//------------------------------------------------
10690
//
107-
// Object
91+
// DefaultArrayImpl
10892
//
10993
//------------------------------------------------
11094

111-
ObjectImpl::
112-
~ObjectImpl() = default;
95+
auto
96+
DefaultArrayImpl::
97+
size() const ->
98+
size_type
99+
{
100+
return elements_.size();
101+
}
102+
103+
auto
104+
DefaultArrayImpl::
105+
get(
106+
size_type i) const ->
107+
value_type
108+
{
109+
MRDOX_ASSERT(i < elements_.size());
110+
return elements_[i];
111+
}
112+
113+
void
114+
DefaultArrayImpl::
115+
emplace_back(
116+
value_type value)
117+
{
118+
elements_.emplace_back(std::move(value));
119+
}
120+
121+
//------------------------------------------------
122+
//
123+
// Object
124+
//
125+
//------------------------------------------------
113126

114127
Object::
115128
~Object() = default;
@@ -169,6 +182,15 @@ toString(
169182
return s;
170183
}
171184

185+
//------------------------------------------------
186+
//
187+
// ObjectImpl
188+
//
189+
//------------------------------------------------
190+
191+
ObjectImpl::
192+
~ObjectImpl() = default;
193+
172194
//------------------------------------------------
173195
//
174196
// DefaultObjectImpl

0 commit comments

Comments
 (0)