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

bblanchon / ArduinoJson / 4017712708

pending completion
4017712708

push

github

Benoit Blanchon
Deduce template argument of `pgm_read()`

15 of 15 new or added lines in 3 files covered. (100.0%)

3370 of 3386 relevant lines covered (99.53%)

6017.47 hits per line

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

98.75
/src/ArduinoJson/Document/BasicJsonDocument.hpp
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2022, Benoit BLANCHON
3
// MIT License
4

5
#pragma once
6

7
#include <ArduinoJson/Document/JsonDocument.hpp>
8

9
namespace ARDUINOJSON_NAMESPACE {
10

11
// Helper to implement the "base-from-member" idiom
12
// (we need to store the allocator before constructing JsonDocument)
13
template <typename TAllocator>
14
class AllocatorOwner {
15
 public:
16
  AllocatorOwner() {}
4✔
17
  AllocatorOwner(TAllocator a) : _allocator(a) {}
1,384✔
18

19
  void* allocate(size_t size) {
1,400✔
20
    return _allocator.allocate(size);
1,400✔
21
  }
22

23
  void deallocate(void* ptr) {
1,409✔
24
    if (ptr)
1,409✔
25
      _allocator.deallocate(ptr);
1,399✔
26
  }
1,409✔
27

28
  void* reallocate(void* ptr, size_t new_size) {
15✔
29
    return _allocator.reallocate(ptr, new_size);
15✔
30
  }
31

32
  TAllocator& allocator() {
1✔
33
    return _allocator;
1✔
34
  }
35

36
 private:
37
  TAllocator _allocator;
38
};
39

40
// A JsonDocument that uses the provided allocator to allocate its memory pool.
41
// https://arduinojson.org/v6/api/basicjsondocument/
42
template <typename TAllocator>
43
class BasicJsonDocument : AllocatorOwner<TAllocator>, public JsonDocument {
44
 public:
45
  explicit BasicJsonDocument(size_t capa, TAllocator alloc = TAllocator())
1,384✔
46
      : AllocatorOwner<TAllocator>(alloc), JsonDocument(allocPool(capa)) {}
1,384✔
47

48
  // Copy-constructor
49
  BasicJsonDocument(const BasicJsonDocument& src)
6✔
50
      : AllocatorOwner<TAllocator>(src), JsonDocument() {
6✔
51
    copyAssignFrom(src);
6✔
52
  }
6✔
53

54
  // Move-constructor
55
#if ARDUINOJSON_HAS_RVALUE_REFERENCES
56
  BasicJsonDocument(BasicJsonDocument&& src) : AllocatorOwner<TAllocator>(src) {
57
    moveAssignFrom(src);
58
  }
59
#endif
60

61
  BasicJsonDocument(const JsonDocument& src) {
1✔
62
    copyAssignFrom(src);
1✔
63
  }
1✔
64

65
  // Construct from variant, array, or object
66
  template <typename T>
67
  BasicJsonDocument(
2✔
68
      const T& src,
69
      typename enable_if<
70
          is_same<T, JsonVariant>::value ||
71
          is_same<T, JsonVariantConst>::value || is_same<T, JsonArray>::value ||
72
          is_same<T, JsonArrayConst>::value || is_same<T, JsonObject>::value ||
73
          is_same<T, JsonObjectConst>::value>::type* = 0)
74
      : JsonDocument(allocPool(src.memoryUsage())) {
4✔
75
    set(src);
2✔
76
  }
2✔
77

78
  // disambiguate
79
  BasicJsonDocument(JsonVariant src)
1✔
80
      : JsonDocument(allocPool(src.memoryUsage())) {
2✔
81
    set(src);
1✔
82
  }
1✔
83

84
  ~BasicJsonDocument() {
1,394✔
85
    freePool();
1,394✔
86
  }
1,394✔
87

88
  BasicJsonDocument& operator=(const BasicJsonDocument& src) {
7✔
89
    copyAssignFrom(src);
7✔
90
    return *this;
7✔
91
  }
92

93
#if ARDUINOJSON_HAS_RVALUE_REFERENCES
94
  BasicJsonDocument& operator=(BasicJsonDocument&& src) {
95
    moveAssignFrom(src);
96
    return *this;
97
  }
98
#endif
99

100
  template <typename T>
101
  BasicJsonDocument& operator=(const T& src) {
11✔
102
    size_t requiredSize = src.memoryUsage();
11✔
103
    if (requiredSize > capacity())
11✔
104
      reallocPool(requiredSize);
×
105
    set(src);
11✔
106
    return *this;
11✔
107
  }
108

109
  // Reduces the capacity of the memory pool to match the current usage.
110
  // https://arduinojson.org/v6/api/basicjsondocument/shrinktofit/
111
  void shrinkToFit() {
30✔
112
    ptrdiff_t bytes_reclaimed = _pool.squash();
30✔
113
    if (bytes_reclaimed == 0)
30✔
114
      return;
15✔
115

116
    void* old_ptr = _pool.buffer();
15✔
117
    void* new_ptr = this->reallocate(old_ptr, _pool.capacity());
15✔
118

119
    ptrdiff_t ptr_offset =
15✔
120
        static_cast<char*>(new_ptr) - static_cast<char*>(old_ptr);
121

122
    _pool.movePointers(ptr_offset);
15✔
123
    _data.movePointers(ptr_offset, ptr_offset - bytes_reclaimed);
15✔
124
  }
125

126
  // Reclaims the memory leaked when removing and replacing values.
127
  // https://arduinojson.org/v6/api/jsondocument/garbagecollect/
128
  bool garbageCollect() {
3✔
129
    // make a temporary clone and move assign
130
    BasicJsonDocument tmp(*this);
6✔
131
    if (!tmp.capacity())
3✔
132
      return false;
1✔
133
    tmp.set(*this);
2✔
134
    moveAssignFrom(tmp);
2✔
135
    return true;
2✔
136
  }
137

138
  using AllocatorOwner<TAllocator>::allocator;
139

140
 private:
141
  MemoryPool allocPool(size_t requiredSize) {
1,400✔
142
    size_t capa = addPadding(requiredSize);
1,400✔
143
    return MemoryPool(reinterpret_cast<char*>(this->allocate(capa)), capa);
1,400✔
144
  }
145

146
  void reallocPool(size_t requiredSize) {
14✔
147
    size_t capa = addPadding(requiredSize);
14✔
148
    if (capa == _pool.capacity())
14✔
149
      return;
1✔
150
    freePool();
13✔
151
    replacePool(allocPool(addPadding(requiredSize)));
13✔
152
  }
153

154
  void freePool() {
1,409✔
155
    this->deallocate(getPool()->buffer());
1,409✔
156
  }
1,409✔
157

158
  void copyAssignFrom(const JsonDocument& src) {
14✔
159
    reallocPool(src.capacity());
14✔
160
    set(src);
14✔
161
  }
14✔
162

163
  void moveAssignFrom(BasicJsonDocument& src) {
2✔
164
    freePool();
2✔
165
    _data = src._data;
2✔
166
    _pool = src._pool;
2✔
167
    src._data.setNull();
2✔
168
    src._pool = MemoryPool(0, 0);
2✔
169
  }
2✔
170
};
171

172
}  // namespace ARDUINOJSON_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