Elaztek Developer Hub
Blamite Game Engine - blam!  00368.02.12.23.1347.blamite
The core library for the Blamite Game Engine.
prettywriter.h
Go to the documentation of this file.
1 // Tencent is pleased to support the open source community by making RapidJSON available.
2 //
3 // Copyright (C) 2015 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 #ifndef RAPIDJSON_PRETTYWRITER_H_
16 #define RAPIDJSON_PRETTYWRITER_H_
17 
18 #include "writer.h"
19 
20 #ifdef __GNUC__
21 RAPIDJSON_DIAG_PUSH
22 RAPIDJSON_DIAG_OFF(effc++)
23 #endif
24 
25 #if defined(__clang__)
26 RAPIDJSON_DIAG_PUSH
27 RAPIDJSON_DIAG_OFF(c++98-compat)
28 #endif
29 
31 
33 
38 };
39 
41 
47 template<typename OutputStream, typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator, unsigned writeFlags = kWriteDefaultFlags>
48 class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags> {
49 public:
51  typedef typename Base::Ch Ch;
52 
54 
58  explicit PrettyWriter(OutputStream& os, StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) :
60 
61 
62  explicit PrettyWriter(StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) :
64 
65 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
66  PrettyWriter(PrettyWriter&& rhs) :
68 #endif
69 
71 
75  PrettyWriter& SetIndent(Ch indentChar, unsigned indentCharCount) {
76  RAPIDJSON_ASSERT(indentChar == ' ' || indentChar == '\t' || indentChar == '\n' || indentChar == '\r');
77  indentChar_ = indentChar;
78  indentCharCount_ = indentCharCount;
79  return *this;
80  }
81 
83 
87  return *this;
88  }
89 
94 
96  bool Bool(bool b) { PrettyPrefix(b ? kTrueType : kFalseType); return Base::EndValue(Base::WriteBool(b)); }
98  bool Uint(unsigned u) { PrettyPrefix(kNumberType); return Base::EndValue(Base::WriteUint(u)); }
102 
103  bool RawNumber(const Ch* str, SizeType length, bool copy = false) {
104  RAPIDJSON_ASSERT(str != 0);
105  (void)copy;
107  return Base::EndValue(Base::WriteString(str, length));
108  }
109 
110  bool String(const Ch* str, SizeType length, bool copy = false) {
111  RAPIDJSON_ASSERT(str != 0);
112  (void)copy;
114  return Base::EndValue(Base::WriteString(str, length));
115  }
116 
117 #if RAPIDJSON_HAS_STDSTRING
118  bool String(const std::basic_string<Ch>& str) {
119  return String(str.data(), SizeType(str.size()));
120  }
121 #endif
122 
123  bool StartObject() {
125  new (Base::level_stack_.template Push<typename Base::Level>()) typename Base::Level(false);
126  return Base::WriteStartObject();
127  }
128 
129  bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); }
130 
131 #if RAPIDJSON_HAS_STDSTRING
132  bool Key(const std::basic_string<Ch>& str) {
133  return Key(str.data(), SizeType(str.size()));
134  }
135 #endif
136 
137  bool EndObject(SizeType memberCount = 0) {
138  (void)memberCount;
139  RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level)); // not inside an Object
140  RAPIDJSON_ASSERT(!Base::level_stack_.template Top<typename Base::Level>()->inArray); // currently inside an Array, not Object
141  RAPIDJSON_ASSERT(0 == Base::level_stack_.template Top<typename Base::Level>()->valueCount % 2); // Object has a Key without a Value
142 
143  bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
144 
145  if (!empty) {
146  Base::os_->Put('\n');
147  WriteIndent();
148  }
149  bool ret = Base::EndValue(Base::WriteEndObject());
150  (void)ret;
151  RAPIDJSON_ASSERT(ret == true);
152  if (Base::level_stack_.Empty()) // end of json text
153  Base::Flush();
154  return true;
155  }
156 
157  bool StartArray() {
159  new (Base::level_stack_.template Push<typename Base::Level>()) typename Base::Level(true);
160  return Base::WriteStartArray();
161  }
162 
163  bool EndArray(SizeType memberCount = 0) {
164  (void)memberCount;
165  RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level));
166  RAPIDJSON_ASSERT(Base::level_stack_.template Top<typename Base::Level>()->inArray);
167  bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
168 
169  if (!empty && !(formatOptions_ & kFormatSingleLineArray)) {
170  Base::os_->Put('\n');
171  WriteIndent();
172  }
173  bool ret = Base::EndValue(Base::WriteEndArray());
174  (void)ret;
175  RAPIDJSON_ASSERT(ret == true);
176  if (Base::level_stack_.Empty()) // end of json text
177  Base::Flush();
178  return true;
179  }
180 
182 
185 
187  bool String(const Ch* str) { return String(str, internal::StrLen(str)); }
188  bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); }
189 
191 
193 
201  bool RawValue(const Ch* json, size_t length, Type type) {
202  RAPIDJSON_ASSERT(json != 0);
203  PrettyPrefix(type);
204  return Base::EndValue(Base::WriteRawValue(json, length));
205  }
206 
207 protected:
208  void PrettyPrefix(Type type) {
209  (void)type;
210  if (Base::level_stack_.GetSize() != 0) { // this value is not at root
211  typename Base::Level* level = Base::level_stack_.template Top<typename Base::Level>();
212 
213  if (level->inArray) {
214  if (level->valueCount > 0) {
215  Base::os_->Put(','); // add comma if it is not the first element in array
217  Base::os_->Put(' ');
218  }
219 
221  Base::os_->Put('\n');
222  WriteIndent();
223  }
224  }
225  else { // in object
226  if (level->valueCount > 0) {
227  if (level->valueCount % 2 == 0) {
228  Base::os_->Put(',');
229  Base::os_->Put('\n');
230  }
231  else {
232  Base::os_->Put(':');
233  Base::os_->Put(' ');
234  }
235  }
236  else
237  Base::os_->Put('\n');
238 
239  if (level->valueCount % 2 == 0)
240  WriteIndent();
241  }
242  if (!level->inArray && level->valueCount % 2 == 0)
243  RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name
244  level->valueCount++;
245  }
246  else {
247  RAPIDJSON_ASSERT(!Base::hasRoot_); // Should only has one and only one root.
248  Base::hasRoot_ = true;
249  }
250  }
251 
252  void WriteIndent() {
253  size_t count = (Base::level_stack_.GetSize() / sizeof(typename Base::Level)) * indentCharCount_;
254  PutN(*Base::os_, static_cast<typename OutputStream::Ch>(indentChar_), count);
255  }
256 
260 
261 private:
262  // Prohibit copy constructor & assignment operator.
263  PrettyWriter(const PrettyWriter&);
264  PrettyWriter& operator=(const PrettyWriter&);
265 };
266 
268 
269 #if defined(__clang__)
270 RAPIDJSON_DIAG_POP
271 #endif
272 
273 #ifdef __GNUC__
274 RAPIDJSON_DIAG_POP
275 #endif
276 
277 #endif // RAPIDJSON_RAPIDJSON_H_
RAPIDJSON_NAMESPACE_END
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:124
Writer::WriteRawValue
bool WriteRawValue(const Ch *json, size_t length)
Definition: writer.h:460
kNullType
@ kNullType
null
Definition: rapidjson.h:730
PrettyWriter::EndArray
bool EndArray(SizeType memberCount=0)
Definition: prettywriter.h:163
RAPIDJSON_NAMESPACE_BEGIN
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:121
Writer::WriteDouble
bool WriteDouble(double d)
Definition: writer.h:349
PrettyWriter::String
bool String(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:110
kFalseType
@ kFalseType
false
Definition: rapidjson.h:731
kArrayType
@ kArrayType
array
Definition: rapidjson.h:734
PrettyWriter::Int64
bool Int64(int64_t i64)
Definition: prettywriter.h:99
Type
Type
Type of JSON value.
Definition: rapidjson.h:729
Writer::hasRoot_
bool hasRoot_
Definition: writer.h:503
PrettyWriter::SetIndent
PrettyWriter & SetIndent(Ch indentChar, unsigned indentCharCount)
Set custom indentation.
Definition: prettywriter.h:75
PrettyWriter::indentChar_
Ch indentChar_
Definition: prettywriter.h:257
PrettyWriter::StartArray
bool StartArray()
Definition: prettywriter.h:157
kObjectType
@ kObjectType
object
Definition: rapidjson.h:733
Writer::WriteEndArray
bool WriteEndArray()
Definition: writer.h:458
Writer::Level::valueCount
size_t valueCount
number of values in this level
Definition: writer.h:292
Writer::WriteUint64
bool WriteUint64(uint64_t u64)
Definition: writer.h:340
PrettyWriter::StartObject
bool StartObject()
Definition: prettywriter.h:123
Writer::WriteString
bool WriteString(const Ch *str, SizeType length)
Definition: writer.h:377
kFormatSingleLineArray
@ kFormatSingleLineArray
Format arrays on a single line.
Definition: prettywriter.h:37
SizeType
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:415
Writer::WriteUint
bool WriteUint(unsigned u)
Definition: writer.h:322
allocator
Blam::LinearAllocator allocator
– TO BE FILLED IN BY VERTIGO –
Definition: main.cpp:76
PrettyWriter::Base
Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags > Base
Definition: prettywriter.h:50
kStringType
@ kStringType
string
Definition: rapidjson.h:735
PrettyFormatOptions
PrettyFormatOptions
Combination of PrettyWriter format flags.
Definition: prettywriter.h:35
Writer::WriteStartArray
bool WriteStartArray()
Definition: writer.h:457
Writer
JSON writer.
Definition: fwd.h:95
RAPIDJSON_ASSERT
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:437
PrettyWriter::Null
bool Null()
Definition: prettywriter.h:95
PrettyWriter
Writer with indentation and spacing.
Definition: fwd.h:100
Writer::WriteBool
bool WriteBool(bool b)
Definition: writer.h:301
uint64_t
unsigned __int64 uint64_t
Definition: stdint.h:136
kNumberType
@ kNumberType
number
Definition: rapidjson.h:736
PrettyWriter::Bool
bool Bool(bool b)
Definition: prettywriter.h:96
Writer::WriteInt64
bool WriteInt64(int64_t i64)
Definition: writer.h:331
PrettyWriter::WriteIndent
void WriteIndent()
Definition: prettywriter.h:252
Writer::WriteEndObject
bool WriteEndObject()
Definition: writer.h:456
PrettyWriter::SetFormatOptions
PrettyWriter & SetFormatOptions(PrettyFormatOptions options)
Set pretty writer formatting options.
Definition: prettywriter.h:85
internal::Stack::GetSize
size_t GetSize() const
Definition: stack.h:178
PrettyWriter::Uint64
bool Uint64(uint64_t u64)
Definition: prettywriter.h:100
Writer::Level::inArray
bool inArray
true if in array, otherwise in object
Definition: writer.h:293
Writer::level_stack_
internal::Stack< StackAllocator > level_stack_
Definition: writer.h:501
PrettyWriter::Int
bool Int(int i)
Definition: prettywriter.h:97
Writer::Flush
void Flush()
Flush the output stream.
Definition: writer.h:282
PrettyWriter::PrettyWriter
PrettyWriter(OutputStream &os, StackAllocator *allocator=0, size_t levelDepth=Base::kDefaultLevelDepth)
Constructor.
Definition: prettywriter.h:58
Writer::os_
OutputStream * os_
Definition: writer.h:500
int64_t
signed __int64 int64_t
Definition: stdint.h:135
PrettyWriter::Key
bool Key(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:129
PrettyWriter::Double
bool Double(double d)
Definition: prettywriter.h:101
PrettyWriter::Key
bool Key(const Ch *str)
Definition: prettywriter.h:188
PrettyWriter::Uint
bool Uint(unsigned u)
Definition: prettywriter.h:98
Writer::WriteStartObject
bool WriteStartObject()
Definition: writer.h:455
Writer::Ch
SourceEncoding::Ch Ch
Definition: writer.h:92
copy
The original diffuse textures are from the NVIDIA texture with other maps generated from them by Steve Streeting The following license applies to all textures Single Product or Project Use of NVSDK Art Imagery THIS IS A LEGAL immediately delete the NVSDK Art Imagery from your hard disk If you proceed to use any of the you thereby signify that you have agreed to all of the terms and conditions set forth below You may not copy
Definition: TextureUsageAgreement.txt:6
PrettyWriter::PrettyPrefix
void PrettyPrefix(Type type)
Definition: prettywriter.h:208
kTrueType
@ kTrueType
true
Definition: rapidjson.h:732
PrettyWriter::String
bool String(const Ch *str)
Simpler but slower overload.
Definition: prettywriter.h:187
kFormatDefault
@ kFormatDefault
Default pretty formatting.
Definition: prettywriter.h:36
Writer::kDefaultLevelDepth
static const size_t kDefaultLevelDepth
Definition: writer.h:286
options
Detailed options
Definition: README.txt:27
PrettyWriter::EndObject
bool EndObject(SizeType memberCount=0)
Definition: prettywriter.h:137
Writer::WriteNull
bool WriteNull()
Definition: writer.h:296
PrettyWriter::RawValue
bool RawValue(const Ch *json, size_t length, Type type)
Write a raw JSON value.
Definition: prettywriter.h:201
Writer::WriteInt
bool WriteInt(int i)
Definition: writer.h:313
PrettyWriter::indentCharCount_
unsigned indentCharCount_
Definition: prettywriter.h:258
PrettyWriter::Ch
Base::Ch Ch
Definition: prettywriter.h:51
Writer::Level
Information for each nested level.
Definition: writer.h:290
PrettyWriter::formatOptions_
PrettyFormatOptions formatOptions_
Definition: prettywriter.h:259
PrettyWriter::PrettyWriter
PrettyWriter(StackAllocator *allocator=0, size_t levelDepth=Base::kDefaultLevelDepth)
Definition: prettywriter.h:62
PrettyWriter::RawNumber
bool RawNumber(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:103
PutN
void PutN(FileWriteStream &stream, char c, size_t n)
Implement specialized version of PutN() with memset() for better performance.
Definition: filewritestream.h:94
internal::StrLen
SizeType StrLen(const Ch *s)
Custom strlen() which works on different character types.
Definition: strfunc.h:31
Writer::EndValue
bool EndValue(bool ret)
Definition: writer.h:494
writer.h