• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bblanchon / ArduinoJson / 5082307777

pending completion
5082307777

push

github

Benoit Blanchon
Move all functions from `VariantFunctions.hpp` to ``VariantData.hpp`

76 of 76 new or added lines in 1 file covered. (100.0%)

3317 of 3336 relevant lines covered (99.43%)

6264.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.18
/src/ArduinoJson/Collection/CollectionFunctions.hpp
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2023, Benoit BLANCHON
3
// MIT License
4

5
#pragma once
6

7
#include <ArduinoJson/Collection/CollectionData.hpp>
8

9
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
10

11
inline VariantData* collectionAddElement(CollectionData* array,
12
                                         MemoryPool* pool) {
13
  if (!array)
14
    return nullptr;
15
  auto slot = new (pool) VariantSlot();
16
  if (!slot)
17
    return nullptr;
18
  array->add(slot);
19
  return slot->data();
20
}
21

22
template <typename TAdaptedString>
23
inline VariantData* collectionAddMember(CollectionData* obj, TAdaptedString key,
24
                                        MemoryPool* pool) {
25
  ARDUINOJSON_ASSERT(!key.isNull());
26
  ARDUINOJSON_ASSERT(obj != nullptr);
27
  auto slot = new (pool) VariantSlot();
28
  if (!slot)
29
    return nullptr;
30
  if (key.isLinked())
31
    slot->setKey(key.data());
32
  else {
33
    auto storedKey = pool->saveString(key);
34
    if (!storedKey)
35
      return nullptr;
36
    slot->setKey(storedKey);
37
  }
38
  obj->add(slot);
39
  return slot->data();
40
}
41

42
inline void collectionClear(CollectionData* c, MemoryPool* pool) {
43
  if (!c)
44
    return;
45
  for (auto slot = c->head(); slot; slot = slot->next())
46
    slotRelease(slot, pool);
47
  c->clear();
48
}
49

50
inline bool collectionCopy(CollectionData* dst, const CollectionData* src,
51
                           MemoryPool* pool) {
52
  if (!dst || !src)
53
    return false;
54

55
  collectionClear(dst, pool);
56

57
  for (VariantSlot* s = src->head(); s; s = s->next()) {
58
    VariantData* var;
59
    if (s->key() != 0) {
60
      JsonString key(s->key(),
61
                     s->ownsKey() ? JsonString::Copied : JsonString::Linked);
62
      var = collectionAddMember(dst, adaptString(key), pool);
63
    } else {
64
      var = collectionAddElement(dst, pool);
65
    }
66
    if (!variantCopyFrom(var, s->data(), pool))
67
      return false;
68
  }
69
  return true;
70
}
71

72
template <typename TAdaptedString>
73
inline VariantData* collectionGetMember(const CollectionData* obj,
74
                                        TAdaptedString key) {
75
  if (!obj)
76
    return nullptr;
77
  return slotData(obj->get(key));
78
}
79

80
inline void collectionRemove(CollectionData* data, VariantSlot* slot,
81
                             MemoryPool* pool) {
82
  if (!data || !slot)
83
    return;
84
  data->remove(slot);
85
  slotRelease(slot, pool);
86
}
87

88
inline void collectionRemoveElement(CollectionData* array, size_t index,
89
                                    MemoryPool* pool) {
90
  if (!array)
91
    return;
92
  collectionRemove(array, array->get(index), pool);
93
}
94

95
template <typename TAdaptedString>
96
inline void collectionRemoveMember(CollectionData* obj, TAdaptedString key,
97
                                   MemoryPool* pool) {
98
  if (!obj)
99
    return;
100
  collectionRemove(obj, obj->get(key), pool);
101
}
102

103
ARDUINOJSON_END_PRIVATE_NAMESPACE
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc