Skip to content

Commit 31c99f4

Browse files
committed
chore: Dom tidy
1 parent efe3d17 commit 31c99f4

File tree

3 files changed

+171
-24
lines changed

3 files changed

+171
-24
lines changed

include/mrdox/Support/Dom.hpp

Lines changed: 153 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include <string_view>
2020
#include <type_traits>
2121
#include <utility>
22-
2322
#include <vector>
2423

2524
namespace clang {
@@ -48,32 +47,51 @@ enum class Kind
4847
//
4948
//------------------------------------------------
5049

51-
/** A type-erased object or array implementation.
50+
/** Reference-counting base class for dynamic types.
5251
*/
5352
class MRDOX_DECL
5453
Any
5554
{
5655
std::atomic<std::size_t> mutable refs_ = 1;
5756

5857
protected:
58+
/** Destructor.
59+
*/
60+
virtual ~Any() = 0;
61+
62+
/** Constructor.
63+
*/
5964
Any() noexcept;
65+
66+
/** Constructor.
67+
68+
The caller will have exclusive ownership
69+
immediately after construction is complete.
70+
*/
6071
Any(Any const&) noexcept;
6172

6273
public:
63-
virtual ~Any() = 0;
64-
74+
/** Return a pointer with shared ownership.
75+
*/
6576
Any* addref() noexcept
6677
{
6778
++refs_;
6879
return this;
6980
}
7081

82+
/** Return a pointer with shared ownership.
83+
*/
7184
Any const* addref() const noexcept
7285
{
7386
++refs_;
7487
return this;
7588
}
7689

90+
/** Release shared ownership.
91+
92+
If this is the last remaining owner,
93+
then this will be deleted.
94+
*/
7795
void release() const noexcept
7896
{
7997
if(--refs_ > 0)
@@ -92,16 +110,16 @@ auto create(Args&&... args);
92110
//
93111
//------------------------------------------------
94112

95-
/** A pointer container for object or array.
113+
/** A pointer container for dynamic objects.
96114
*/
97-
template<class T = Any>
98-
requires std::convertible_to<T*, Any*>
115+
template<class T>
99116
class Pointer
100117
{
101118
Any* any_;
102119

120+
static_assert(std::derived_from<T, Any>);
121+
103122
template<class U>
104-
requires std::convertible_to<U*, Any*>
105123
friend class Pointer;
106124

107125
explicit
@@ -217,27 +235,102 @@ using ArrayPtr = Pointer<Array>;
217235
class MRDOX_DECL
218236
Object : public Any
219237
{
220-
Object(Object const&);
238+
protected:
239+
/** Constructor.
240+
241+
The newly constructed object will retain
242+
a copy of the list of values in `other`.
243+
*/
244+
Object(Object const& other);
221245

222246
public:
247+
/** The type of an element in this container.
248+
*/
223249
using value_type = std::pair<std::string, Value>;
250+
251+
/** The underlying, iterable range used by this container.
252+
*/
224253
using list_type = std::vector<value_type>;
225254

255+
/** Constructor.
256+
257+
Default-constructed objects are empty.
258+
*/
226259
Object() noexcept;
260+
261+
/** Constructor.
262+
263+
Upon construction, the object will retain
264+
ownership of a shallow copy of the specified
265+
list. In particular, dynamic objects will
266+
be acquired with shared ownership.
267+
268+
@param list The initial list of values.
269+
*/
227270
explicit Object(list_type list);
271+
272+
/** Return an iterable range with the contents.
273+
*/
228274
list_type const& list() const noexcept;
275+
276+
/** Add elements to the container.
277+
278+
No checking is performed to prevent
279+
the insertion of duplicate keys.
280+
*/
229281
void append(std::string_view key, Value value);
282+
283+
/** Add elements to the container.
284+
285+
No checking is performed to prevent
286+
the insertion of duplicate keys.
287+
*/
230288
void append(std::initializer_list<value_type>);
289+
290+
/** Return a deep copy of the container.
291+
292+
In particular, dynamic objects will be
293+
deeply copied; changes to the copy are
294+
not reflected in the original.
295+
*/
231296
virtual Value copy() const;
297+
298+
/** Return true if the container is empty.
299+
*/
232300
virtual bool empty() const noexcept;
301+
302+
/** Return the value for a given key.
303+
304+
If the key does not exist, a null value
305+
is returned.
306+
307+
@return The value, or null.
308+
309+
@param key The key.
310+
*/
233311
virtual Value get(std::string_view key) const;
312+
313+
/** Set or replace the value for a given key.
314+
315+
This function inserts a new key or changes
316+
the value for the existing key if it is
317+
already present.
318+
319+
@param key The key.
320+
321+
@param value The value to set.
322+
*/
234323
virtual void set(std::string_view key, Value value);
324+
325+
// VFALCO DEPRECATED (for duktape)
235326
virtual std::vector<std::string_view> props() const;
236327

237328
private:
238329
list_type list_;
239330
};
240331

332+
/** Alias for a pointer to an Object.
333+
*/
241334
using ObjectPtr = Pointer<Object>;
242335

243336
/** Return a new object with a given list of properties.
@@ -260,14 +353,26 @@ class MRDOX_DECL
260353
{
261354
std::atomic<Object*> mutable p_ = nullptr;
262355

356+
/** Return the object.
357+
*/
263358
virtual ObjectPtr construct() const = 0;
264359

265360
public:
266-
LazyObject() noexcept;
361+
/** Destructor.
362+
*/
267363
~LazyObject() noexcept;
364+
365+
/** Constructor.
366+
*/
367+
LazyObject() noexcept;
368+
369+
/** Return the object.
370+
*/
268371
ObjectPtr get() const;
269372
};
270373

374+
/** Alias for a pointer to a LazyObject.
375+
*/
271376
using LazyObjectPtr = Pointer<LazyObject>;
272377

273378
//------------------------------------------------
@@ -368,12 +473,32 @@ class MRDOX_DECL
368473
*/
369474
Value copy() const;
370475

476+
/** Return the type of value contained.|
477+
*/
371478
dom::Kind kind() const noexcept;
479+
480+
/** Return true if this is null.
481+
*/
372482
bool isNull() const noexcept { return kind_ == Kind::Null; }
483+
484+
/** Return true if this is a boolean.
485+
*/
373486
bool isBool() const noexcept { return kind_ == Kind::Boolean; }
487+
488+
/** Return true if this is an integer.
489+
*/
374490
bool isInteger() const noexcept { return kind_ == Kind::Integer; }
491+
492+
/** Return true if this is a string.
493+
*/
375494
bool isString() const noexcept { return kind_ == Kind::String; }
495+
496+
/** Return true if this is an array.
497+
*/
376498
bool isArray() const noexcept { return kind_ == Kind::Array; }
499+
500+
/** Return true if this is an object.
501+
*/
377502
bool isObject() const noexcept
378503
{
379504
return
@@ -408,19 +533,33 @@ class MRDOX_DECL
408533
return arr_;
409534
}
410535

411-
ObjectPtr getObject() const noexcept;
536+
/** Return the object if this is an object.
412537
413-
void swap(Value& other) noexcept;
538+
@throw Error `! isObject()`
539+
*/
540+
ObjectPtr
541+
getObject() const;
542+
543+
/** Swap two values.
544+
*/
545+
void
546+
swap(Value& other) noexcept;
414547

415-
friend void swap(Value& v0, Value& v1) noexcept
548+
/** Swap two values.
549+
*/
550+
friend
551+
void
552+
swap(Value& v0, Value& v1) noexcept
416553
{
417554
v0.swap(v1);
418555
}
419556
};
420557

558+
/** Return a non-empty string, or a null.
559+
*/
421560
inline
422561
Value
423-
nonEmptyString(
562+
stringOrNull(
424563
std::string_view s)
425564
{
426565
if(! s.empty())

source/Metadata/DomMetadata.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ class DomParam : public dom::Object
262262
Param const& I,
263263
Corpus const& corpus) noexcept
264264
: Object({
265-
{"name", dom::nonEmptyString(I.Name)},
265+
{"name", dom::stringOrNull(I.Name)},
266266
{"type", dom::create<DomTypeInfo>(I.Type, corpus)},
267-
{"default", dom::nonEmptyString(I.Default)}
267+
{"default", dom::stringOrNull(I.Default)}
268268
})
269269
{
270270
}
@@ -321,7 +321,7 @@ class DomTArg : public dom::Object
321321
TArg const& I,
322322
Corpus const& corpus)
323323
: Object({
324-
{"value", dom::nonEmptyString(I.Value)}
324+
{"value", dom::stringOrNull(I.Value)}
325325
})
326326
{
327327
}
@@ -448,7 +448,7 @@ DomTParam(
448448
Corpus const& corpus)
449449
: Object({
450450
{"kind", toString(I.Kind)},
451-
{"name", dom::nonEmptyString(I.Name)},
451+
{"name", dom::stringOrNull(I.Name)},
452452
{"is-pack", I.IsParameterPack},
453453
{"type", I.Kind == TParamKind::NonType ?
454454
dom::create<DomTypeInfo>(
@@ -837,7 +837,7 @@ construct() const
837837
{
838838
list.insert(list.end(), {
839839
{ "type", dom::create<DomTypeInfo>(I_.Type, corpus_) },
840-
{ "default", dom::nonEmptyString(I_.Default) },
840+
{ "default", dom::stringOrNull(I_.Default) },
841841
{ "isNodiscard", I_.specs.isNodiscard.get() },
842842
{ "isDeprecated", I_.specs.isDeprecated.get() },
843843
{ "hasNoUniqueAddress", I_.specs.hasNoUniqueAddress.get() }

source/Support/Dom.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//
1010

1111
#include <mrdox/Support/Dom.hpp>
12+
#include <mrdox/Support/Error.hpp>
1213

1314
namespace clang {
1415
namespace mrdox {
@@ -46,7 +47,14 @@ get(std::size_t) const
4647

4748
Object::
4849
Object(
49-
Object const&) = default;
50+
Object const& other)
51+
{
52+
// deep-copy the list
53+
list_.reserve(other.list_.size());
54+
for(auto const& kv : other.list_)
55+
list_.emplace_back(
56+
kv.first, kv.second.copy());
57+
}
5058

5159
Object::
5260
Object() noexcept = default;
@@ -369,8 +377,9 @@ copy() const
369377
// we can just give the caller shared ownership.
370378
return *this;
371379
case Kind::Object:
372-
case Kind::LazyObject:
373380
return obj_->copy();
381+
case Kind::LazyObject:
382+
return lazy_obj_->get()->copy();
374383
default:
375384
MRDOX_UNREACHABLE();
376385
}
@@ -413,14 +422,13 @@ isTruthy() const noexcept
413422

414423
ObjectPtr
415424
Value::
416-
getObject() const noexcept
425+
getObject() const
417426
{
418427
if(kind_ == Kind::Object)
419428
return obj_;
420429
if(kind_ == Kind::LazyObject)
421430
return lazy_obj_->get();
422-
MRDOX_ASSERT(kind_ == Kind::Object);
423-
return obj_;
431+
throw Error("not an Object");
424432
}
425433

426434
void

0 commit comments

Comments
 (0)