Skip to content

Commit

Permalink
Fixed build on GCC 5.3 and Visual Studio 2010
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Feb 3, 2016
1 parent 5f589d3 commit 793a076
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 98 deletions.
48 changes: 38 additions & 10 deletions include/ArduinoJson/Arduino/Print.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@

#ifndef ARDUINO

#include "../TypeTraits/EnableIf.hpp"
#include "../TypeTraits/IsIntegral.hpp"
#include "../Internals/JsonFloat.hpp"
#include "../Internals/JsonInteger.hpp"

#include <stddef.h>
#include <stdint.h>
#include <stdio.h>

#if defined(_MSC_VER) && _MSC_VER <= 1800
// snprintf has been added in Visual Studio 2015
#define ARDUINOJSON_SNPRINTF _snprintf
#else
#define ARDUINOJSON_SNPRINTF snprintf
#endif

// This class reproduces Arduino's Print class
class Print {
Expand All @@ -22,13 +30,33 @@ class Print {

virtual size_t write(uint8_t) = 0;

size_t print(const char[]);
size_t print(double, int = 2);
size_t print(const char* s) {
size_t n = 0;
while (*s) {
n += write(*s++);
}
return n;
}

size_t print(ArduinoJson::Internals::JsonFloat value, int digits = 2) {
char tmp[32];

// https://github.com/arduino/Arduino/blob/db8cbf24c99dc930b9ccff1a43d018c81f178535/hardware/arduino/sam/cores/arduino/Print.cpp#L220
bool isBigDouble = value > 4294967040.0 || value < -4294967040.0;

if (isBigDouble) {
// Arduino's implementation prints "ovf"
// We prefer using the scientific notation, since we have sprintf
ARDUINOJSON_SNPRINTF(tmp, sizeof(tmp), "%g", value);
} else {
// Here we have the exact same output as Arduino's implementation
ARDUINOJSON_SNPRINTF(tmp, sizeof(tmp), "%.*f", digits, value);
}

return print(tmp);
}

template <typename TIntegral>
typename ArduinoJson::TypeTraits::EnableIf<
ArduinoJson::TypeTraits::IsIntegral<TIntegral>::value, size_t>::type
print(TIntegral value) {
size_t print(ArduinoJson::Internals::JsonInteger value) {
// see http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_3:Exercise_4
char buffer[22];

Expand All @@ -39,7 +67,7 @@ class Print {
}
uint8_t i = 0;
do {
TIntegral digit = value % 10;
ArduinoJson::Internals::JsonInteger digit = value % 10;
value /= 10;
buffer[i++] = static_cast<char>(digit >= 0 ? '0' + digit : '0' - digit);
} while (value);
Expand All @@ -51,7 +79,7 @@ class Print {
return n;
}

size_t println();
size_t println() { return write('\r') + write('\n'); }
};

#else
Expand Down
5 changes: 0 additions & 5 deletions include/ArduinoJson/JsonArraySubscript.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@ inline std::ostream& operator<<(std::ostream& os,
}
#endif

template <>
struct JsonVariant::IsConstructibleFrom<JsonArraySubscript> {
static const bool value = true;
};

} // namespace ArduinoJson

#ifdef _MSC_VER
Expand Down
4 changes: 0 additions & 4 deletions include/ArduinoJson/JsonObjectSubscript.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,6 @@ inline std::ostream& operator<<(
}
#endif

template <typename T>
struct JsonVariant::IsConstructibleFrom<JsonObjectSubscript<T> > {
static const bool value = true;
};
} // namespace ArduinoJson

#ifdef _MSC_VER
Expand Down
31 changes: 19 additions & 12 deletions include/ArduinoJson/JsonVariant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
#include "TypeTraits/EnableIf.hpp"
#include "TypeTraits/IsFloatingPoint.hpp"
#include "TypeTraits/IsIntegral.hpp"
#include "TypeTraits/IsSame.hpp"
#include "TypeTraits/RemoveConst.hpp"
#include "TypeTraits/RemoveReference.hpp"
#include "TypeTraits/IsSame.hpp"

namespace ArduinoJson {

Expand Down Expand Up @@ -191,17 +191,24 @@ inline JsonVariant double_with_n_digits(double value, uint8_t digits) {

template <typename T>
struct JsonVariant::IsConstructibleFrom {
static const bool value = TypeTraits::IsIntegral<T>::value ||
TypeTraits::IsFloatingPoint<T>::value ||
TypeTraits::IsSame<T, bool>::value ||
TypeTraits::IsSame<T, char *>::value ||
TypeTraits::IsSame<T, const char *>::value ||
TypeTraits::IsSame<T, JsonArray &>::value ||
TypeTraits::IsSame<T, const JsonArray &>::value ||
TypeTraits::IsSame<T, JsonObject &>::value ||
TypeTraits::IsSame<T, const JsonObject &>::value ||
TypeTraits::IsSame<T, JsonVariant &>::value ||
TypeTraits::IsSame<T, const JsonVariant &>::value;
static const bool value =
TypeTraits::IsIntegral<T>::value ||
TypeTraits::IsFloatingPoint<T>::value ||
TypeTraits::IsSame<T, bool>::value ||
TypeTraits::IsSame<T, char *>::value ||
TypeTraits::IsSame<T, const char *>::value ||
TypeTraits::IsSame<T, JsonArray &>::value ||
TypeTraits::IsSame<T, const JsonArray &>::value ||
TypeTraits::IsSame<T, JsonArraySubscript &>::value ||
TypeTraits::IsSame<T, const JsonArraySubscript &>::value ||
TypeTraits::IsSame<T, JsonObject &>::value ||
TypeTraits::IsSame<T, const JsonObject &>::value ||
TypeTraits::IsSame<T, JsonObjectSubscript<const char *> &>::value ||
TypeTraits::IsSame<T, const JsonObjectSubscript<const char *> &>::value ||
TypeTraits::IsSame<T, JsonObjectSubscript<String> &>::value ||
TypeTraits::IsSame<T, const JsonObjectSubscript<String> &>::value ||
TypeTraits::IsSame<T, JsonVariant &>::value ||
TypeTraits::IsSame<T, const JsonVariant &>::value;
};
}

Expand Down
13 changes: 9 additions & 4 deletions include/ArduinoJson/TypeTraits/IsIntegral.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#pragma once

#include "../Configuration.hpp"
#include "IsSame.hpp"

#include <stdint.h>
Expand All @@ -25,10 +26,14 @@ struct IsIntegral {
TypeTraits::IsSame<T, unsigned int>::value ||
TypeTraits::IsSame<T, signed long>::value ||
TypeTraits::IsSame<T, unsigned long>::value ||
#ifndef ARDUINO
// on a computer add support for 64 bit
TypeTraits::IsSame<T, int64_t>::value ||
TypeTraits::IsSame<T, uint64_t>::value ||
#if ARDUINOJSON_USE_LONG_LONG
TypeTraits::IsSame<T, long long>::value ||
TypeTraits::IsSame<T, unsigned long long>::value ||
#endif

#if ARDUINOJSON_USE_INT64
TypeTraits::IsSame<T, __int64>::value ||
TypeTraits::IsSame<T, unsigned __int64>::value ||
#endif
TypeTraits::IsSame<T, char>::value;
};
Expand Down
60 changes: 0 additions & 60 deletions src/Arduino/Print.cpp

This file was deleted.

3 changes: 2 additions & 1 deletion test/JsonArray_Subscript_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
// https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!

#include <gtest/gtest.h>
#define ARDUINOJSON_USE_LONG_LONG 1
#include <ArduinoJson.h>
#include <gtest/gtest.h>
#include <stdint.h>

class JsonArray_Subscript_Tests : public ::testing::Test {
Expand Down
3 changes: 2 additions & 1 deletion test/JsonVariant_As_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
// https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!

#include <gtest/gtest.h>
#define ARDUINOJSON_USE_LONG_LONG 1
#include <ArduinoJson.h>

#include <gtest/gtest.h>
#include <stdint.h>

static const char* null = 0;
Expand Down
4 changes: 3 additions & 1 deletion test/JsonVariant_Storage_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
// https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!

#include <gtest/gtest.h>
#include <stdint.h>
#include <limits>
#include <gtest/gtest.h>

#define ARDUINOJSON_USE_LONG_LONG 1
#include <ArduinoJson.h>

class JsonVariant_Storage_Tests : public ::testing::Test {
Expand Down

0 comments on commit 793a076

Please sign in to comment.