/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/rapidjson/prettywriter.h Source File

/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/rapidjson/prettywriter.h Source File#

Composable Kernel: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/rapidjson/prettywriter.h Source File
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 
36 {
39 };
40 
42 
48 template <typename OutputStream,
49  typename SourceEncoding = UTF8<>,
50  typename TargetEncoding = UTF8<>,
51  typename StackAllocator = CrtAllocator,
52  unsigned writeFlags = kWriteDefaultFlags>
54  : public Writer<OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags>
55 {
56  public:
58  typedef typename Base::Ch Ch;
59 
61 
65  explicit PrettyWriter(OutputStream& os,
66  StackAllocator* allocator = 0,
67  size_t levelDepth = Base::kDefaultLevelDepth)
68  : Base(os, allocator, levelDepth),
69  indentChar_(' '),
72  {
73  }
74 
75  explicit PrettyWriter(StackAllocator* allocator = 0,
76  size_t levelDepth = Base::kDefaultLevelDepth)
77  : Base(allocator, levelDepth),
78  indentChar_(' '),
81  {
82  }
83 
84 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
86  : Base(std::forward<PrettyWriter>(rhs)),
90  {
91  }
92 #endif
93 
95 
99  PrettyWriter& SetIndent(Ch indentChar, unsigned indentCharCount)
100  {
101  RAPIDJSON_ASSERT(indentChar == ' ' || indentChar == '\t' || indentChar == '\n' ||
102  indentChar == '\r');
103  indentChar_ = indentChar;
104  indentCharCount_ = indentCharCount;
105  return *this;
106  }
107 
109 
112  {
113  formatOptions_ = options;
114  return *this;
115  }
116 
121 
122  bool Null()
123  {
126  }
127  bool Bool(bool b)
128  {
130  return Base::EndValue(Base::WriteBool(b));
131  }
132  bool Int(int i)
133  {
135  return Base::EndValue(Base::WriteInt(i));
136  }
137  bool Uint(unsigned u)
138  {
140  return Base::EndValue(Base::WriteUint(u));
141  }
142  bool Int64(int64_t i64)
143  {
145  return Base::EndValue(Base::WriteInt64(i64));
146  }
147  bool Uint64(uint64_t u64)
148  {
150  return Base::EndValue(Base::WriteUint64(u64));
151  }
152  bool Double(double d)
153  {
156  }
157 
158  bool RawNumber(const Ch* str, SizeType length, bool copy = false)
159  {
160  RAPIDJSON_ASSERT(str != 0);
161  (void)copy;
163  return Base::EndValue(Base::WriteString(str, length));
164  }
165 
166  bool String(const Ch* str, SizeType length, bool copy = false)
167  {
168  RAPIDJSON_ASSERT(str != 0);
169  (void)copy;
171  return Base::EndValue(Base::WriteString(str, length));
172  }
173 
174 #if RAPIDJSON_HAS_STDSTRING
175  bool String(const std::basic_string<Ch>& str)
176  {
177  return String(str.data(), SizeType(str.size()));
178  }
179 #endif
180 
181  bool StartObject()
182  {
184  new(Base::level_stack_.template Push<typename Base::Level>()) typename Base::Level(false);
185  return Base::WriteStartObject();
186  }
187 
188  bool Key(const Ch* str, SizeType length, bool copy = false)
189  {
190  return String(str, length, copy);
191  }
192 
193 #if RAPIDJSON_HAS_STDSTRING
194  bool Key(const std::basic_string<Ch>& str) { return Key(str.data(), SizeType(str.size())); }
195 #endif
196 
197  bool EndObject(SizeType memberCount = 0)
198  {
199  (void)memberCount;
201  sizeof(typename Base::Level)); // not inside an Object
202  RAPIDJSON_ASSERT(!Base::level_stack_.template Top<typename Base::Level>()
203  ->inArray); // currently inside an Array, not Object
204  RAPIDJSON_ASSERT(0 == Base::level_stack_.template Top<typename Base::Level>()->valueCount %
205  2); // Object has a Key without a Value
206 
207  bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
208 
209  if(!empty)
210  {
211  Base::os_->Put('\n');
212  WriteIndent();
213  }
214  bool ret = Base::EndValue(Base::WriteEndObject());
215  (void)ret;
216  RAPIDJSON_ASSERT(ret == true);
217  if(Base::level_stack_.Empty()) // end of json text
218  Base::Flush();
219  return true;
220  }
221 
222  bool StartArray()
223  {
225  new(Base::level_stack_.template Push<typename Base::Level>()) typename Base::Level(true);
226  return Base::WriteStartArray();
227  }
228 
229  bool EndArray(SizeType memberCount = 0)
230  {
231  (void)memberCount;
232  RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level));
233  RAPIDJSON_ASSERT(Base::level_stack_.template Top<typename Base::Level>()->inArray);
234  bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
235 
236  if(!empty && !(formatOptions_ & kFormatSingleLineArray))
237  {
238  Base::os_->Put('\n');
239  WriteIndent();
240  }
241  bool ret = Base::EndValue(Base::WriteEndArray());
242  (void)ret;
243  RAPIDJSON_ASSERT(ret == true);
244  if(Base::level_stack_.Empty()) // end of json text
245  Base::Flush();
246  return true;
247  }
248 
250 
253 
255  bool String(const Ch* str) { return String(str, internal::StrLen(str)); }
256  bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); }
257 
259 
261 
268  bool RawValue(const Ch* json, size_t length, Type type)
269  {
270  RAPIDJSON_ASSERT(json != 0);
271  PrettyPrefix(type);
272  return Base::EndValue(Base::WriteRawValue(json, length));
273  }
274 
275  protected:
276  void PrettyPrefix(Type type)
277  {
278  (void)type;
279  if(Base::level_stack_.GetSize() != 0)
280  { // this value is not at root
281  typename Base::Level* level = Base::level_stack_.template Top<typename Base::Level>();
282 
283  if(level->inArray)
284  {
285  if(level->valueCount > 0)
286  {
287  Base::os_->Put(','); // add comma if it is not the first element in array
289  Base::os_->Put(' ');
290  }
291 
293  {
294  Base::os_->Put('\n');
295  WriteIndent();
296  }
297  }
298  else
299  { // in object
300  if(level->valueCount > 0)
301  {
302  if(level->valueCount % 2 == 0)
303  {
304  Base::os_->Put(',');
305  Base::os_->Put('\n');
306  }
307  else
308  {
309  Base::os_->Put(':');
310  Base::os_->Put(' ');
311  }
312  }
313  else
314  Base::os_->Put('\n');
315 
316  if(level->valueCount % 2 == 0)
317  WriteIndent();
318  }
319  if(!level->inArray && level->valueCount % 2 == 0)
321  type == kStringType); // if it's in object, then even number should be a name
322  level->valueCount++;
323  }
324  else
325  {
326  RAPIDJSON_ASSERT(!Base::hasRoot_); // Should only has one and only one root.
327  Base::hasRoot_ = true;
328  }
329  }
330 
331  void WriteIndent()
332  {
333  size_t count =
334  (Base::level_stack_.GetSize() / sizeof(typename Base::Level)) * indentCharCount_;
335  PutN(*Base::os_, static_cast<typename OutputStream::Ch>(indentChar_), count);
336  }
337 
341 
342  private:
343  // Prohibit copy constructor & assignment operator.
344  PrettyWriter(const PrettyWriter&);
345  PrettyWriter& operator=(const PrettyWriter&);
346 };
347 
349 
350 #if defined(__clang__)
351 RAPIDJSON_DIAG_POP
352 #endif
353 
354 #ifdef __GNUC__
355 RAPIDJSON_DIAG_POP
356 #endif
357 
358 #endif // RAPIDJSON_RAPIDJSON_H_
C-runtime library allocator.
Definition: allocators.h:83
Writer with indentation and spacing.
Definition: prettywriter.h:55
void PrettyPrefix(Type type)
Simpler but slower overload.
Definition: prettywriter.h:276
PrettyFormatOptions formatOptions_
Simpler but slower overload.
Definition: prettywriter.h:340
bool EndArray(SizeType memberCount=0)
Definition: prettywriter.h:229
PrettyWriter & SetFormatOptions(PrettyFormatOptions options)
Set pretty writer formatting options.
Definition: prettywriter.h:111
bool Key(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:188
bool StartObject()
Definition: prettywriter.h:181
bool RawNumber(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:158
bool RawValue(const Ch *json, size_t length, Type type)
Write a raw JSON value.
Definition: prettywriter.h:268
PrettyWriter(StackAllocator *allocator=0, size_t levelDepth=Base::kDefaultLevelDepth)
Definition: prettywriter.h:75
bool Key(const Ch *str)
Simpler but slower overload.
Definition: prettywriter.h:256
bool EndObject(SizeType memberCount=0)
Definition: prettywriter.h:197
bool Bool(bool b)
Definition: prettywriter.h:127
Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags > Base
Definition: prettywriter.h:57
void WriteIndent()
Simpler but slower overload.
Definition: prettywriter.h:331
unsigned indentCharCount_
Simpler but slower overload.
Definition: prettywriter.h:339
bool String(const Ch *str)
Simpler but slower overload.
Definition: prettywriter.h:255
bool Uint(unsigned u)
Definition: prettywriter.h:137
PrettyWriter(OutputStream &os, StackAllocator *allocator=0, size_t levelDepth=Base::kDefaultLevelDepth)
Constructor.
Definition: prettywriter.h:65
bool Null()
Definition: prettywriter.h:122
bool Int(int i)
Definition: prettywriter.h:132
Ch indentChar_
Simpler but slower overload.
Definition: prettywriter.h:338
bool Uint64(uint64_t u64)
Definition: prettywriter.h:147
PrettyWriter & SetIndent(Ch indentChar, unsigned indentCharCount)
Set custom indentation.
Definition: prettywriter.h:99
bool Int64(int64_t i64)
Definition: prettywriter.h:142
bool Double(double d)
Definition: prettywriter.h:152
Base::Ch Ch
Definition: prettywriter.h:58
bool String(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:166
bool StartArray()
Definition: prettywriter.h:222
JSON writer.
Definition: writer.h:99
bool WriteUint(unsigned u)
Simpler but slower overload.
Definition: writer.h:389
bool WriteInt(int i)
Simpler but slower overload.
Definition: writer.h:379
OutputStream * os_
Simpler but slower overload.
Definition: writer.h:630
bool WriteStartArray()
Simpler but slower overload.
Definition: writer.h:570
bool WriteBool(bool b)
Simpler but slower overload.
Definition: writer.h:357
bool WriteNull()
Simpler but slower overload.
Definition: writer.h:347
bool WriteEndObject()
Simpler but slower overload.
Definition: writer.h:565
bool WriteStartObject()
Simpler but slower overload.
Definition: writer.h:560
void Flush()
Flush the output stream.
Definition: writer.h:334
bool WriteRawValue(const Ch *json, size_t length)
Simpler but slower overload.
Definition: writer.h:581
static const size_t kDefaultLevelDepth
Simpler but slower overload.
Definition: writer.h:336
internal::Stack< StackAllocator > level_stack_
Simpler but slower overload.
Definition: writer.h:631
bool WriteInt64(int64_t i64)
Simpler but slower overload.
Definition: writer.h:399
bool WriteEndArray()
Simpler but slower overload.
Definition: writer.h:575
SourceEncoding::Ch Ch
Definition: writer.h:101
bool WriteString(const Ch *str, SizeType length)
Simpler but slower overload.
Definition: writer.h:468
bool WriteUint64(uint64_t u64)
Simpler but slower overload.
Definition: writer.h:409
bool EndValue(bool ret)
Simpler but slower overload.
Definition: writer.h:623
bool WriteDouble(double d)
Simpler but slower overload.
Definition: writer.h:419
bool hasRoot_
Simpler but slower overload.
Definition: writer.h:633
size_t GetSize() const
Definition: stack.h:206
void PutN(FileWriteStream &stream, char c, size_t n)
Implement specialized version of PutN() with memset() for better performance.
Definition: filewritestream.h:124
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:451
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:121
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:124
auto copy(InputRange &&range, OutputIterator iter) -> decltype(std::copy(std::begin(std::forward< InputRange >(range)), std::end(std::forward< InputRange >(range)), iter))
Definition: algorithm.hpp:14
SizeType StrLen(const Ch *s)
Custom strlen() which works on different character types.
Definition: strfunc.h:32
PrettyFormatOptions
Combination of PrettyWriter format flags.
Definition: prettywriter.h:36
@ kFormatDefault
Default pretty formatting.
Definition: prettywriter.h:37
@ kFormatSingleLineArray
Format arrays on a single line.
Definition: prettywriter.h:38
Type
Type of JSON value.
Definition: rapidjson.h:760
@ kFalseType
false
Definition: rapidjson.h:762
@ kObjectType
object
Definition: rapidjson.h:764
@ kTrueType
true
Definition: rapidjson.h:763
@ kStringType
string
Definition: rapidjson.h:766
@ kNullType
null
Definition: rapidjson.h:761
@ kArrayType
array
Definition: rapidjson.h:765
@ kNumberType
number
Definition: rapidjson.h:767
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:429
signed __int64 int64_t
Definition: stdint.h:135
unsigned __int64 uint64_t
Definition: stdint.h:136
UTF-8 encoding.
Definition: encodings.h:98
Information for each nested level.
Definition: writer.h:341
size_t valueCount
number of values in this level
Definition: writer.h:343
bool inArray
true if in array, otherwise in object
Definition: writer.h:344
@ kWriteDefaultFlags
Definition: writer.h:72