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

bblanchon / ArduinoJson / 10572597112

27 Aug 2024 06:03AM UTC coverage: 99.194% (-0.3%) from 99.512%
10572597112

push

github

bblanchon
Merge conf_test for linux and windows

This reverts commit 83516e174.

3940 of 3972 relevant lines covered (99.19%)

10537.46 hits per line

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

98.36
/src/ArduinoJson/Variant/VariantImpl.hpp
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2024, Benoit BLANCHON
3
// MIT License
4

5
#pragma once
6

7
#include <ArduinoJson/Memory/ResourceManager.hpp>
8
#include <ArduinoJson/Variant/VariantData.hpp>
9

10
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
11

12
template <typename T>
13
inline void VariantData::setRawString(SerializedValue<T> value,
34✔
14
                                      ResourceManager* resources) {
15
  ARDUINOJSON_ASSERT(type_ == VALUE_IS_NULL);  // must call clear() first
16
  auto dup = resources->saveString(adaptString(value.data(), value.size()));
34✔
17
  if (dup)
34✔
18
    setRawString(dup);
30✔
19
}
34✔
20

21
template <typename TAdaptedString>
22
inline bool VariantData::setString(TAdaptedString value,
66,936✔
23
                                   ResourceManager* resources) {
24
  ARDUINOJSON_ASSERT(type_ == VALUE_IS_NULL);  // must call clear() first
25

26
  if (value.isNull())
66,936✔
27
    return false;
65,553✔
28

29
  if (value.isLinked()) {
1,383✔
30
    setLinkedString(value.data());
1,152✔
31
    return true;
1,152✔
32
  }
33

34
  auto dup = resources->saveString(value);
231✔
35
  if (dup) {
231✔
36
    setOwnedString(dup);
219✔
37
    return true;
219✔
38
  }
39

40
  return false;
12✔
41
}
42

43
inline void VariantData::clear(ResourceManager* resources) {
69,278✔
44
  if (type_ & OWNED_VALUE_BIT)
69,278✔
45
    resources->dereferenceString(content_.asOwnedString->data);
25✔
46

47
#if ARDUINOJSON_USE_EXTENSIONS
48
  if (type_ & EXTENSION_BIT)
69,278✔
49
    resources->freeExtension(content_.asSlotId);
×
50
#endif
51

52
  auto collection = asCollection();
69,278✔
53
  if (collection)
69,278✔
54
    collection->clear(resources);
17✔
55

56
  type_ = VALUE_IS_NULL;
69,278✔
57
}
69,278✔
58

59
#if ARDUINOJSON_USE_EXTENSIONS
60
inline const VariantExtension* VariantData::getExtension(
137✔
61
    const ResourceManager* resources) const {
62
  ARDUINOJSON_ASSERT(type_ & EXTENSION_BIT);
63
  return resources->getExtension(content_.asSlotId);
137✔
64
}
65
#endif
66

67
template <typename T>
68
typename enable_if<sizeof(T) == 8>::type VariantData::setFloat(
139✔
69
    T value, ResourceManager* resources) {
70
  ARDUINOJSON_ASSERT(type_ == VALUE_IS_NULL);  // must call clear() first
71
  (void)resources;                             // silence warning
72

73
  float valueAsFloat = static_cast<float>(value);
139✔
74

75
#if ARDUINOJSON_USE_DOUBLE
76
  if (value == valueAsFloat) {
137✔
77
    type_ = VALUE_IS_FLOAT;
36✔
78
    content_.asFloat = valueAsFloat;
36✔
79
  } else {
80
    auto extension = resources->allocExtension();
101✔
81
    if (extension) {
101✔
82
      type_ = VALUE_IS_DOUBLE;
101✔
83
      content_.asSlotId = extension.id();
101✔
84
      extension->asDouble = value;
101✔
85
    }
86
  }
87
#else
88
  type_ = VALUE_IS_FLOAT;
2✔
89
  content_.asFloat = valueAsFloat;
2✔
90
#endif
91
}
139✔
92

93
template <typename T>
94
enable_if_t<is_signed<T>::value> VariantData::setInteger(
1,524✔
95
    T value, ResourceManager* resources) {
96
  ARDUINOJSON_ASSERT(type_ == VALUE_IS_NULL);  // must call clear() first
97
  (void)resources;                             // silence warning
98

99
  if (canConvertNumber<int32_t>(value)) {
1,524✔
100
    type_ = VALUE_IS_INT32;
1,513✔
101
    content_.asInt32 = static_cast<int32_t>(value);
1,513✔
102
  }
103
#if ARDUINOJSON_USE_LONG_LONG
104
  else {
105
    auto extension = resources->allocExtension();
11✔
106
    if (extension) {
11✔
107
      type_ = VALUE_IS_INT64;
11✔
108
      content_.asSlotId = extension.id();
11✔
109
      extension->asInt64 = value;
11✔
110
    }
111
  }
112
#endif
113
}
1,524✔
114

115
template <typename T>
116
enable_if_t<is_unsigned<T>::value> VariantData::setInteger(
2,288✔
117
    T value, ResourceManager* resources) {
118
  ARDUINOJSON_ASSERT(type_ == VALUE_IS_NULL);  // must call clear() first
119
  (void)resources;                             // silence warning
120

121
  if (canConvertNumber<uint32_t>(value)) {
2,288✔
122
    type_ = VALUE_IS_UINT32;
2,277✔
123
    content_.asUint32 = static_cast<uint32_t>(value);
2,277✔
124
  }
125
#if ARDUINOJSON_USE_LONG_LONG
126
  else {
127
    auto extension = resources->allocExtension();
10✔
128
    if (extension) {
10✔
129
      type_ = VALUE_IS_UINT64;
10✔
130
      content_.asSlotId = extension.id();
10✔
131
      extension->asUint64 = value;
10✔
132
    }
133
  }
134
#endif
135
}
2,288✔
136

137
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