-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Json::Value constructor: Accept any arithmetic type. #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e384e49
Merge pull request #63 from dreifachstein/master
cdunn2001 021f7de
Json::Value constructor: Accept any arithmetic type.
7ba2c54
Improve comments on arithmetic type constructor and helper traits.
1787b6d
Make 64-bit int work with 'long int' and 'int64_t' for 64 bit builds …
orodbhen 7e29d49
Only use fix-width types for C++11 (Issue #64)
orodbhen 337b0d2
Json::Value constructor: Accept any arithmetic type.
e555666
Improve comments on arithmetic type constructor and helper traits.
5512aaa
Merge branch 'accept_all_arithmetic_types' of https://github.com/open…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
#if !defined(JSON_IS_AMALGAMATION) | ||
#include "forwards.h" | ||
#endif // if !defined(JSON_IS_AMALGAMATION) | ||
#include <limits> | ||
#include <string> | ||
#include <vector> | ||
|
||
|
@@ -84,6 +85,30 @@ class JSON_API StaticString { | |
const char* str_; | ||
}; | ||
|
||
// Some helper traits for Json::Value | ||
namespace Detail { | ||
|
||
template <typename T> | ||
struct IsIntegral { | ||
enum { value = std::numeric_limits<T>::is_specialized && | ||
std::numeric_limits<T>::is_integer }; | ||
}; | ||
|
||
template <typename T> | ||
struct IsFloatingPoint { | ||
enum { value = std::numeric_limits<T>::is_specialized && | ||
!std::numeric_limits<T>::is_integer }; | ||
}; | ||
|
||
template <bool C, typename T = void> struct EnableIf { }; | ||
template <typename T> struct EnableIf<true, T> { typedef T type; }; | ||
|
||
template <typename T> | ||
struct EnableIfArithmetic | ||
: EnableIf<IsIntegral<T>::value || IsFloatingPoint<T>::value, int> {}; | ||
|
||
} // namespace Detail | ||
|
||
/** \brief Represents a <a HREF="http://www.json.org">JSON</a> value. | ||
* | ||
* This class is a discriminated union wrapper that can represents a: | ||
|
@@ -210,13 +235,26 @@ Json::Value obj_value(Json::objectValue); // {} | |
\endcode | ||
*/ | ||
Value(ValueType type = nullValue); | ||
Value(Int value); | ||
Value(UInt value); | ||
#if defined(JSON_HAS_INT64) | ||
Value(Int64 value); | ||
Value(UInt64 value); | ||
#endif // if defined(JSON_HAS_INT64) | ||
Value(double value); | ||
|
||
template <typename T> | ||
Value(T value, typename Detail::EnableIfArithmetic<T>::type = 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment explaining what is going on here, maybe with the key word 'SFINAE' for those unfamiliar. |
||
: allocated_(false), | ||
#ifdef JSON_VALUE_USE_INTERNAL_MAP | ||
itemIsUsed_(0), | ||
#endif | ||
comments_(0), start_(0), limit_(0) { | ||
if (Detail::IsFloatingPoint<T>::value) { | ||
type_ = realValue; | ||
value_.real_ = value; | ||
} else if (std::numeric_limits<T>::is_signed) { | ||
type_ = intValue; | ||
value_.int_ = value; | ||
} else { | ||
type_ = uintValue; | ||
value_.uint_ = value; | ||
} | ||
} | ||
|
||
Value(const char* value); | ||
Value(const char* beginValue, const char* endValue); | ||
/** \brief Constructs a value from a static string. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly out of curiosity: why this
is_specialized
here? According to cppreference,is_integer
is false for non-specialized types. Maybe that's not guaranteed for future versions of the standard?