Skip to content

Commit 3b638e6

Browse files
authored
Merge pull request Tencent#1191 from Sumoren/msc_long
Support long and unsined long as int and unsigned on Microsft platforms
2 parents 8bf4f7b + a37f9d1 commit 3b638e6

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

include/rapidjson/document.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,26 @@ struct TypeHelper<ValueType, unsigned> {
451451
static ValueType& Set(ValueType& v, unsigned data, typename ValueType::AllocatorType&) { return v.SetUint(data); }
452452
};
453453

454+
#ifdef _MSC_VER
455+
RAPIDJSON_STATIC_ASSERT(sizeof(long) == sizeof(int));
456+
template<typename ValueType>
457+
struct TypeHelper<ValueType, long> {
458+
static bool Is(const ValueType& v) { return v.IsInt(); }
459+
static long Get(const ValueType& v) { return v.GetInt(); }
460+
static ValueType& Set(ValueType& v, long data) { return v.SetInt(data); }
461+
static ValueType& Set(ValueType& v, long data, typename ValueType::AllocatorType&) { return v.SetInt(data); }
462+
};
463+
464+
RAPIDJSON_STATIC_ASSERT(sizeof(unsigned long) == sizeof(unsigned));
465+
template<typename ValueType>
466+
struct TypeHelper<ValueType, unsigned long> {
467+
static bool Is(const ValueType& v) { return v.IsUint(); }
468+
static unsigned long Get(const ValueType& v) { return v.GetUint(); }
469+
static ValueType& Set(ValueType& v, unsigned long data) { return v.SetUint(data); }
470+
static ValueType& Set(ValueType& v, unsigned long data, typename ValueType::AllocatorType&) { return v.SetUint(data); }
471+
};
472+
#endif
473+
454474
template<typename ValueType>
455475
struct TypeHelper<ValueType, int64_t> {
456476
static bool Is(const ValueType& v) { return v.IsInt64(); }

test/unittest/valuetest.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,14 @@ TEST(Value, Int) {
439439
EXPECT_EQ(5678, z.Get<int>());
440440
EXPECT_EQ(5679, z.Set(5679).Get<int>());
441441
EXPECT_EQ(5680, z.Set<int>(5680).Get<int>());
442+
443+
#ifdef _MSC_VER
444+
// long as int on MSC platforms
445+
RAPIDJSON_STATIC_ASSERT(sizeof(long) == sizeof(int));
446+
z.SetInt(1234);
447+
EXPECT_TRUE(z.Is<long>());
448+
EXPECT_EQ(1234l, z.Get<long>());
449+
#endif
442450
}
443451

444452
TEST(Value, Uint) {
@@ -485,6 +493,14 @@ TEST(Value, Uint) {
485493
EXPECT_EQ(2147483648u, z.Get<unsigned>());
486494
EXPECT_EQ(2147483649u, z.Set(2147483649u).Get<unsigned>());
487495
EXPECT_EQ(2147483650u, z.Set<unsigned>(2147483650u).Get<unsigned>());
496+
497+
#ifdef _MSC_VER
498+
// unsigned long as unsigned on MSC platforms
499+
RAPIDJSON_STATIC_ASSERT(sizeof(unsigned long) == sizeof(unsigned));
500+
z.SetUint(1234);
501+
EXPECT_TRUE(z.Is<unsigned long>());
502+
EXPECT_EQ(1234ul, z.Get<unsigned long>());
503+
#endif
488504
}
489505

490506
TEST(Value, Int64) {

0 commit comments

Comments
 (0)