Skip to content

Commit efe3d17

Browse files
committed
chore: Value::copy
1 parent 2e9e713 commit efe3d17

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

include/mrdox/Support/Dom.hpp

+11
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ class MRDOX_DECL
5555
{
5656
std::atomic<std::size_t> mutable refs_ = 1;
5757

58+
protected:
59+
Any() noexcept;
60+
Any(Any const&) noexcept;
61+
5862
public:
5963
virtual ~Any() = 0;
6064

@@ -213,6 +217,8 @@ using ArrayPtr = Pointer<Array>;
213217
class MRDOX_DECL
214218
Object : public Any
215219
{
220+
Object(Object const&);
221+
216222
public:
217223
using value_type = std::pair<std::string, Value>;
218224
using list_type = std::vector<value_type>;
@@ -222,6 +228,7 @@ class MRDOX_DECL
222228
list_type const& list() const noexcept;
223229
void append(std::string_view key, Value value);
224230
void append(std::initializer_list<value_type>);
231+
virtual Value copy() const;
225232
virtual bool empty() const noexcept;
226233
virtual Value get(std::string_view key) const;
227234
virtual void set(std::string_view key, Value value);
@@ -357,6 +364,10 @@ class MRDOX_DECL
357364
{
358365
}
359366

367+
/** Return a copy.
368+
*/
369+
Value copy() const;
370+
360371
dom::Kind kind() const noexcept;
361372
bool isNull() const noexcept { return kind_ == Kind::Null; }
362373
bool isBool() const noexcept { return kind_ == Kind::Boolean; }

source/Support/Dom.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ namespace clang {
1414
namespace mrdox {
1515
namespace dom {
1616

17+
Any::
18+
Any() noexcept = default;
19+
20+
Any::
21+
Any(Any const&) noexcept
22+
{
23+
MRDOX_ASSERT(refs_ == 1);
24+
}
25+
1726
Any::
1827
~Any() = default;
1928

@@ -35,6 +44,10 @@ get(std::size_t) const
3544

3645
//------------------------------------------------
3746

47+
Object::
48+
Object(
49+
Object const&) = default;
50+
3851
Object::
3952
Object() noexcept = default;
4053

@@ -70,6 +83,13 @@ append(
7083
list_.insert(list_.end(), init);
7184
}
7285

86+
Value
87+
Object::
88+
copy() const
89+
{
90+
return create<Object>(list_);
91+
}
92+
7393
bool
7494
Object::
7595
empty() const noexcept
@@ -333,6 +353,29 @@ operator=(
333353
return *this;
334354
}
335355

356+
Value
357+
Value::
358+
copy() const
359+
{
360+
switch(kind_)
361+
{
362+
case Kind::Null:
363+
case Kind::Boolean:
364+
case Kind::Integer:
365+
case Kind::String:
366+
return *this;
367+
case Kind::Array:
368+
// VFALCO currently, arrays are immutable so
369+
// we can just give the caller shared ownership.
370+
return *this;
371+
case Kind::Object:
372+
case Kind::LazyObject:
373+
return obj_->copy();
374+
default:
375+
MRDOX_UNREACHABLE();
376+
}
377+
}
378+
336379
dom::Kind
337380
Value::
338381
kind() const noexcept

0 commit comments

Comments
 (0)