|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef JSSTREAMWRITER_H |
|
7 #define JSSTREAMWRITER_H |
|
8 |
|
9 #include <ostream> |
|
10 #include <stdlib.h> |
|
11 #include "nsDeque.h" |
|
12 |
|
13 class JSStreamWriter |
|
14 { |
|
15 public: |
|
16 JSStreamWriter(std::ostream& aStream); |
|
17 ~JSStreamWriter(); |
|
18 |
|
19 void BeginObject(); |
|
20 void EndObject(); |
|
21 void BeginArray(); |
|
22 void EndArray(); |
|
23 void Name(const char *name); |
|
24 void Value(int value); |
|
25 void Value(double value); |
|
26 void Value(const char *value, size_t valueLength); |
|
27 void Value(const char *value); |
|
28 template <typename T> |
|
29 void NameValue(const char *aName, T aValue) |
|
30 { |
|
31 Name(aName); |
|
32 Value(aValue); |
|
33 } |
|
34 |
|
35 private: |
|
36 std::ostream& mStream; |
|
37 bool mNeedsComma; |
|
38 bool mNeedsName; |
|
39 |
|
40 nsDeque mStack; |
|
41 |
|
42 // This class can't be copied |
|
43 JSStreamWriter(const JSStreamWriter&); |
|
44 JSStreamWriter& operator=(const JSStreamWriter&); |
|
45 |
|
46 void* operator new(size_t); |
|
47 void* operator new[](size_t); |
|
48 void operator delete(void*) { |
|
49 // Since JSStreamWriter has a virtual destructor the compiler |
|
50 // has to provide a destructor in the object file that will call |
|
51 // operate delete in case there is a derived class since its |
|
52 // destructor won't know how to free this instance. |
|
53 abort(); |
|
54 } |
|
55 void operator delete[](void*); |
|
56 }; |
|
57 |
|
58 #endif |