Skip to content

Commit 365f798

Browse files
committed
chore: refactor dom Object and JavaScript
1 parent 535aef1 commit 365f798

File tree

8 files changed

+580
-164
lines changed

8 files changed

+580
-164
lines changed

include/mrdox/Support/Dom.hpp

Lines changed: 393 additions & 63 deletions
Large diffs are not rendered by default.

include/mrdox/Support/String.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@ class StringLiteral
5050
return { data_, size_ };
5151
}
5252

53-
std::string_view const&
53+
std::string_view
5454
get() const noexcept
5555
{
56-
return { data_, size_ };
56+
return std::string_view(*this);
5757
}
5858

59-
std::string_view const&
59+
std::string_view
6060
operator*() const noexcept
6161
{
62-
return get();
62+
return std::string_view(*this);
6363
}
6464
};
6565

source/-adoc/AdocCorpus.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ class DomJavadoc : public dom::LazyObjectImpl
264264
dom::Object
265265
construct() const override
266266
{
267-
entries_type list;
267+
storage_type list;
268268
list.reserve(2);
269269

270270
// brief

source/-adoc/Builder.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,6 @@ createContext(
199199
SymbolID const& id)
200200
{
201201
return dom::Object({
202-
{ "document", "test" },
203-
{ "test", "===" },
204202
{ "symbol", domCorpus_.get(id) }
205203
});
206204
}

source/Metadata/DomMetadata.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ static
110110
dom::Object
111111
domCreate(SourceInfo const& I)
112112
{
113-
dom::Object::entries_type entries;
113+
dom::Object::storage_type entries;
114114
if(I.DefLoc)
115115
entries.emplace_back("def", domCreate(*I.DefLoc));
116116
if(! I.Loc.empty())
@@ -338,7 +338,7 @@ domCreate(
338338
{
339339
if(! I)
340340
return nullptr;
341-
dom::Object::entries_type entries = {
341+
dom::Object::storage_type entries = {
342342
{ "kind", toString(I->Kind) }
343343
};
344344
visit(*I, [&]<typename T>(const T& t)
@@ -623,7 +623,7 @@ requires std::derived_from<T, Info>
623623
dom::Object
624624
DomInfo<T>::construct() const
625625
{
626-
entries_type entries;
626+
storage_type entries;
627627
entries.insert(entries.end(), {
628628
{ "id", toBase16(I_.id) },
629629
{ "kind", toString(I_.Kind) },

source/Support/Dom.cpp

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@
1212
#include <mrdox/Support/Error.hpp>
1313
#include <mrdox/Support/RangeFor.hpp>
1414
#include <algorithm>
15+
#include <ranges>
1516

1617
namespace clang {
1718
namespace mrdox {
1819
namespace dom {
1920

21+
static_assert(std::random_access_iterator<Object::iterator>);
22+
static_assert(std::ranges::random_access_range<Object>);
23+
2024
//------------------------------------------------
2125
//
2226
// Array
@@ -119,12 +123,22 @@ Object(
119123

120124
Object::
121125
Object(
122-
entries_type list)
126+
storage_type list)
123127
: impl_(std::make_shared<
124128
DefaultObjectImpl>(std::move(list)))
125129
{
126130
}
127131

132+
auto
133+
Object::
134+
at(size_type i) const ->
135+
reference
136+
{
137+
if(i >= size())
138+
throw Error("out of range");
139+
return impl_->get(i);
140+
}
141+
128142
std::string
129143
toString(
130144
Object const& obj)
@@ -134,14 +148,14 @@ toString(
134148
std::string s = "{";
135149
{
136150
auto insert = std::back_inserter(s);
137-
for(auto const& kv : RangeFor(obj.entries()))
151+
for(auto const& kv : RangeFor(obj))
138152
{
139153
if(! kv.first)
140154
s.push_back(',');
141155
fmt::format_to(insert,
142156
" {} : {}",
143-
kv.value.first,
144-
toStringChild(kv.value.second));
157+
kv.value.key,
158+
toStringChild(kv.value.value));
145159
}
146160
}
147161
s += " }";
@@ -159,69 +173,57 @@ DefaultObjectImpl() noexcept = default;
159173

160174
DefaultObjectImpl::
161175
DefaultObjectImpl(
162-
entries_type entries) noexcept
176+
storage_type entries) noexcept
163177
: entries_(std::move(entries))
164178
{
165179
}
166180

167181
std::size_t
168182
DefaultObjectImpl::
169-
size() const noexcept
183+
size() const
170184
{
171185
return entries_.size();
172186
}
173187

174-
bool
188+
auto
175189
DefaultObjectImpl::
176-
exists(
177-
std::string_view key) const
190+
get(std::size_t i) const ->
191+
reference
178192
{
179-
return std::find_if(
180-
entries_.begin(), entries_.end(),
181-
[key](value_type const& kv)
182-
{
183-
return kv.first == key;
184-
}) != entries_.end();
193+
MRDOX_ASSERT(i < entries_.size());
194+
return entries_[i];
185195
}
186196

187197
Value
188198
DefaultObjectImpl::
189-
get(std::string_view key) const
199+
find(std::string_view key) const
190200
{
191201
auto it = std::find_if(
192202
entries_.begin(), entries_.end(),
193-
[key](value_type const& kv)
203+
[key](auto const& kv)
194204
{
195-
return kv.first == key;
205+
return kv.key == key;
196206
});
197-
if(it != entries_.end())
198-
return it->second;
199-
return nullptr;
207+
if(it == entries_.end())
208+
return nullptr;
209+
return it->value;
200210
}
201211

202212
void
203213
DefaultObjectImpl::
204214
set(std::string_view key, Value value)
205215
{
206-
auto it = std::find_if(
216+
auto it = std::find_if(
207217
entries_.begin(), entries_.end(),
208-
[key](value_type const& kv)
218+
[key](auto const& kv)
209219
{
210-
return kv.first == key;
220+
return kv.key == key;
211221
});
212222
if(it == entries_.end())
213223
entries_.emplace_back(
214224
key, std::move(value));
215225
else
216-
it->second = std::move(value);
217-
}
218-
219-
auto
220-
DefaultObjectImpl::
221-
entries() const ->
222-
entries_type
223-
{
224-
return entries_;
226+
it->value = std::move(value);
225227
}
226228

227229
//------------------------------------------------
@@ -246,24 +248,24 @@ obj() const
246248

247249
std::size_t
248250
LazyObjectImpl::
249-
size() const noexcept
251+
size() const
250252
{
251253
return obj().size();
252254
}
253255

254-
bool
256+
auto
255257
LazyObjectImpl::
256-
exists(
257-
std::string_view key) const
258+
get(std::size_t i) const ->
259+
reference
258260
{
259-
return obj().exists(key);
261+
return obj().get(i);
260262
}
261263

262264
Value
263265
LazyObjectImpl::
264-
get(std::string_view key) const
266+
find(std::string_view key) const
265267
{
266-
return obj().get(key);
268+
return obj().find(key);
267269
}
268270

269271
void
@@ -274,14 +276,6 @@ set(
274276
obj().set(key, value);
275277
}
276278

277-
auto
278-
LazyObjectImpl::
279-
entries() const ->
280-
entries_type
281-
{
282-
return obj().entries();
283-
}
284-
285279
//------------------------------------------------
286280
//
287281
// Value

0 commit comments

Comments
 (0)