CTR Pia  4.11.3
Game Communication Engine
common_StringStream.h
1 /*---------------------------------------------------------------------------*
2  Project: Pia
3  File: common_StringStream.h
4 
5  Copyright Nintendo. All rights reserved.
6 
7  These coded instructions, statements, and computer programs contain
8  proprietary information of Nintendo of America Inc. and/or Nintendo
9  Company Ltd., and are protected by Federal copyright law. They may
10  not be disclosed to third parties or copied or duplicated in any form,
11  in whole or in part, without the prior written consent of Nintendo.
12  *---------------------------------------------------------------------------*/
13 
14 
15 #pragma once
16 
17 #include <pia/platform.h>
18 #include <pia/common/common_RootObject.h>
19 
20 #define NN_PIA_T(x) x
21 
22 //n2788: Added 0-padded hexadecimal display.
23 #define NN_PIA_ENABLE_ZERO_FILL_HEX 1
24 
25 namespace nn
26 {
27 namespace pia
28 {
29 namespace common
30 {
31 
32 #define NN_PIA_STRINGSTREAM_INITIAL_SIZE 128
33 
34 //n2619: Determine whether to enable processing when a global <tt>new</tt> is invoked outside <tt>BeginSetup</tt> and <tt>EndSetup</tt>. (Basically, always set to 0.)
35 //
36 #define PIA_ENABLE_RESIZE_BUFFER 0
37 
38 /*!
39 @cond PRIVATE
40 @brief Represents the string stream class.
41 Concatenates strings using the manipulator (<<).
42 */
43 class StringStream : public RootObject
44 {
45 public:
46 /*!
47 @brief Instantiates the object.
48 */
49  StringStream(void);
50 
51 /*!
52 @brief Destroys the object.
53 */
54  virtual ~StringStream(void);
55 
56  // Returns the stream length.
57  u32 GetLength() const;
58 
59  // Returns the <tt>m_szBuffer</tt> size (capacity).
60  u32 GetSize() const
61  {
62  return m_uiSize;
63  }
64 
65  const char* CStr() const
66  {
67  return m_szBuffer;
68  }
69 
70  void Clear();
71 
72  // Streaming of basic types.
73  StringStream& operator<<(const char* szText);
74  StringStream& operator<<(const void* pPointer);
75  StringStream& operator<<(bool bValue);
76  StringStream& operator<<(f64 dValue);
77  StringStream& operator<<(f32 fValue);
78 
79  StringStream& operator<<(u8 uiValue)
80  {
81  return StreamNumber(uiValue);
82  }
83  StringStream& operator<<(s8 iValue)
84  {
85  return StreamNumber(iValue);
86  }
87  StringStream& operator<<(u16 uiValue)
88  {
89  return StreamNumber(uiValue);
90  }
91  StringStream& operator<<(s16 iValue)
92  {
93  return StreamNumber(iValue);
94  }
95  StringStream& operator<<(u32 uiValue)
96  {
97  return StreamNumber(uiValue);
98  }
99  StringStream& operator<<(s32 iValue)
100  {
101  return StreamNumber(iValue);
102  }
103 
104  StringStream& operator<<(u64 uiValue);
105  StringStream& operator<<(s64 uiValue);
106 
107  StringStream& operator<<(const StringStream& oSS);
108 
109  // Manipulators.
110  StringStream& operator<<(StringStream& (*pf)(StringStream&))
111  {
112  return (*pf)(*this);
113  }
114 
115 #if !NN_PIA_ENABLE_ZERO_FILL_HEX
116  StringStream& Manip_hex()
117  {
118  m_Hex = true;
119  return *this;
120  }
121  StringStream& Manip_dec()
122  {
123  m_Hex = false;
124  return *this;
125  }
126 #else // !NN_PIA_ENABLE_ZERO_FILL_HEX
127  StringStream& Manip_hex()
128  {
129  m_Hex = true;
130  m_ZeroHex = false;
131  return *this;
132  }
133  StringStream& Manip_dec()
134  {
135  m_Hex = false;
136  m_ZeroHex = false;
137  return *this;
138  }
139  StringStream& Manip_zerohex()
140  {
141  m_Hex = true;
142  m_ZeroHex = true;
143  return *this;
144  }
145 #endif // !NN_PIA_ENABLE_ZERO_FILL_HEX
146 
147  StringStream& Manip_showbase()
148  {
149  m_ShowBase = true;
150  return *this;
151  }
152  StringStream& Manip_noshowbase()
153  {
154  m_ShowBase = false;
155  return *this;
156  }
157 
158  StringStream& Manip_boolalpha()
159  {
160  m_BoolAlpha = true;
161  return *this;
162  }
163  StringStream& Manip_noboolalpha()
164  {
165  m_BoolAlpha = false;
166  return *this;
167  }
168 
169  const char& operator[](u32 uiPos) const
170  {
171  return m_szBuffer[uiPos];
172  }
173 
174 protected:
175 private:
176 /*!
177 @brief This copy constructor is private.
178 */
179  StringStream(const StringStream&);
180 
181 /*!
182 @brief This assignment operator is private.
183 */
184  StringStream& operator=(const StringStream&);
185 
186 #if PIA_ENABLE_RESIZE_BUFFER
187  void FreeBuffer();
188  void FreeBuffer(char* szBuffer);
189  void ResizeBuffer(u32 uiSize);
190 #endif
191  void AddBaseIfRequired();
192 
193  StringStream& StreamNumber(u32 uiValue);
194  StringStream& StreamNumber(s32 iValue);
195  void TestFreeRoom(u32 uiNewSize);
196 
197  //n2619
198  s32 AddStringToRoom(const char* szBuffer);
199 
200 private:
201  char* m_szBuffer;
202  u32 m_uiSize;
203  char* m_szCurrentPosition;
204 
205  char m_szInitialBuffer[NN_PIA_STRINGSTREAM_INITIAL_SIZE];
206  //n2619
207  char m_szBufferToAdd[NN_PIA_STRINGSTREAM_INITIAL_SIZE];
208 
209  bool m_Hex;
210  bool m_ShowBase;
211  bool m_BoolAlpha;
212 
213 #if NN_PIA_ENABLE_ZERO_FILL_HEX
214  //n2788:
215  bool m_ZeroHex;
216 #else
217  u8 m_padding1; // Avoids the compiler warning.
218 #endif // NN_PIA_ENABLE_ZERO_FILL_HEX
219 };
220 
221 // Manipulators.
222 
223 extern StringStream& hex(StringStream& oStringStream);
224 extern StringStream& dec(StringStream& oStringStream);
225 extern StringStream& endl(StringStream& oStringStream);
226 extern StringStream& showbase(StringStream& oStringStream);
227 extern StringStream& noshowbase(StringStream& oStringStream);
228 extern StringStream& boolalpha(StringStream& oStringStream);
229 extern StringStream& noboolalpha(StringStream& oStringStream);
230 
231 #if NN_PIA_ENABLE_ZERO_FILL_HEX
232 extern StringStream& zerohex(StringStream& oStringStream);
233 #endif // NN_PIA_ENABLE_ZERO_FILL_HEX
234 }
235 }
236 } // end of namespace nn::pia::common
237 //! @endcond
Definition: assert.h:115