Skip to content

Commit e0f68a4

Browse files
authored
Merge pull request Tencent#1877 from hendrikmuhs/windows-GetObject-conflict-#1418
add a workaround for GetObject macro defined by windows.h
2 parents 47b837e + 3cdfde1 commit e0f68a4

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

include/rapidjson/document.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ RAPIDJSON_DIAG_OFF(4244) // conversion from kXxxFlags to 'uint16_t', possible lo
4242
RAPIDJSON_DIAG_OFF(effc++)
4343
#endif // __GNUC__
4444

45+
#ifdef GetObject
46+
// see https://github.com/Tencent/rapidjson/issues/1448
47+
// a former included windows.h might have defined a macro called GetObject, which affects
48+
// GetObject defined here. This ensures the macro does not get applied
49+
#pragma push_macro("GetObject")
50+
#define RAPIDJSON_WINDOWS_GETOBJECT_WORKAROUND_APPLIED
51+
#undef GetObject
52+
#endif
53+
4554
#ifndef RAPIDJSON_NOMEMBERITERATORCLASS
4655
#include <iterator> // std::random_access_iterator_tag
4756
#endif
@@ -1602,7 +1611,9 @@ class GenericValue {
16021611
}
16031612

16041613
Object GetObject() { RAPIDJSON_ASSERT(IsObject()); return Object(*this); }
1614+
Object GetObj() { RAPIDJSON_ASSERT(IsObject()); return Object(*this); }
16051615
ConstObject GetObject() const { RAPIDJSON_ASSERT(IsObject()); return ConstObject(*this); }
1616+
ConstObject GetObj() const { RAPIDJSON_ASSERT(IsObject()); return ConstObject(*this); }
16061617

16071618
//@}
16081619

@@ -3008,4 +3019,9 @@ class GenericObject {
30083019
RAPIDJSON_NAMESPACE_END
30093020
RAPIDJSON_DIAG_POP
30103021

3022+
#ifdef RAPIDJSON_WINDOWS_GETOBJECT_WORKAROUND_APPLIED
3023+
#pragma pop_macro("GetObject")
3024+
#undef RAPIDJSON_WINDOWS_GETOBJECT_WORKAROUND_APPLIED
3025+
#endif
3026+
30113027
#endif // RAPIDJSON_DOCUMENT_H_

test/unittest/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ set(UNITTEST_SOURCES
1616
jsoncheckertest.cpp
1717
namespacetest.cpp
1818
pointertest.cpp
19+
platformtest.cpp
1920
prettywritertest.cpp
2021
ostreamwrappertest.cpp
2122
readertest.cpp

test/unittest/platformtest.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Tencent is pleased to support the open source community by making RapidJSON available.
2+
//
3+
// Copyright (C) 2021 THL A29 Limited, a Tencent company, and Milo Yip.
4+
//
5+
// Licensed under the MIT License (the "License"); you may not use this file except
6+
// in compliance with the License. You may obtain a copy of the License at
7+
//
8+
// http://opensource.org/licenses/MIT
9+
//
10+
// Unless required by applicable law or agreed to in writing, software distributed
11+
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12+
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13+
// specific language governing permissions and limitations under the License.
14+
15+
#include "unittest.h"
16+
17+
// see https://github.com/Tencent/rapidjson/issues/1448
18+
// including windows.h on purpose to provoke a compile time problem as GetObject is a
19+
// macro that gets defined when windows.h is included
20+
#ifdef _WIN32
21+
#include <windows.h>
22+
#endif
23+
24+
#include "rapidjson/document.h"
25+
#undef GetObject
26+
27+
using namespace rapidjson;
28+
29+
TEST(Platform, GetObject) {
30+
Document doc;
31+
doc.Parse(" { \"object\" : { \"pi\": 3.1416} } ");
32+
EXPECT_TRUE(doc.IsObject());
33+
EXPECT_TRUE(doc.HasMember("object"));
34+
const Document::ValueType& o = doc["object"];
35+
EXPECT_TRUE(o.IsObject());
36+
Value::ConstObject sub = o.GetObject();
37+
EXPECT_TRUE(sub.HasMember("pi"));
38+
Value::ConstObject sub2 = o.GetObj();
39+
EXPECT_TRUE(sub2.HasMember("pi"));
40+
}

0 commit comments

Comments
 (0)