ITextStream.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Palmsource, Inc.
00003  * 
00004  * This software is licensed as described in the file LICENSE, which
00005  * you should have received as part of this distribution. The terms
00006  * are also available at http://www.openbinder.org/license.html.
00007  * 
00008  * This software consists of voluntary contributions made by many
00009  * individuals. For the exact contribution history, see the revision
00010  * history and logs, available at http://www.openbinder.org
00011  */
00012 
00013 #ifndef _SUPPORT_TEXTSTREAM_INTERFACE_H
00014 #define _SUPPORT_TEXTSTREAM_INTERFACE_H
00015 
00022 #include <support/IInterface.h>
00023 
00024 #include <sys/uio.h>
00025 
00026 #if _SUPPORTS_NAMESPACE
00027 namespace palmos {
00028 namespace support {
00029 #endif
00030 
00031 class SValue;
00032 
00033 /*-----------------------------------------------------------------*/
00034 
00039 
00040 
00045 class ITextOutput : public IInterface
00046 {
00047 public:
00048 
00049         // Note: binder remote interface not yet implemented.
00050 
00052         struct log_info {
00053             int32_t     tag;
00054             int32_t     team;
00055             int32_t     thread;
00056             nsecs_t     time;
00057             int32_t     indent;
00058             int32_t     front : 1;
00059             int32_t     reserved : 30;
00060             SValue*     extra;
00061         };
00062         
00065 
00067         virtual status_t                Print(  const char *debugText,
00068                                                 ssize_t len = -1) = 0;
00069         
00071         virtual void                    MoveIndent( int32_t delta) = 0;
00072         
00074 
00078         virtual status_t                LogV(   const log_info& info,
00079                                                 const iovec *vector,
00080                                                 ssize_t count,
00081                                                 uint32_t flags = 0) = 0;
00082 
00084 
00088         virtual void                    Flush() = 0;
00089 
00092 
00094         virtual status_t                Sync() = 0;
00095 };
00096 
00097 /*-----------------------------------------------------------------*/
00098 
00100 
00105 class ITextInput : public IInterface
00106 {
00107 public:
00108 
00109         // Note: binder remote interface not yet implemented.
00110 
00111         virtual ssize_t ReadChar() = 0;
00112         virtual ssize_t ReadLineBuffer(char* buffer, size_t size) = 0;
00113         // AppendLineTo: reads a single line from the stream and appends
00114         //   it to *outString.
00115         virtual ssize_t AppendLineTo(SString* outString) = 0;
00116 };
00117 
00118 /*-----------------------------------------------------------------*/
00119 
00125 
00127 typedef const sptr<ITextOutput>& (*ITextOutputManipFunc)(const sptr<ITextOutput>&);
00128 
00129 _IMPEXP_SUPPORT const sptr<ITextOutput>& endl(const sptr<ITextOutput>& io);
00130 _IMPEXP_SUPPORT const sptr<ITextOutput>& indent(const sptr<ITextOutput>& io);
00131 _IMPEXP_SUPPORT const sptr<ITextOutput>& dedent(const sptr<ITextOutput>& io);
00132 
00133 _IMPEXP_SUPPORT const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, const char* str);
00134 _IMPEXP_SUPPORT const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, char);     
00135 _IMPEXP_SUPPORT const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, bool);
00136 _IMPEXP_SUPPORT const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, int);
00137 _IMPEXP_SUPPORT const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, unsigned int);
00138 _IMPEXP_SUPPORT const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, unsigned long);
00139 _IMPEXP_SUPPORT const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, long);
00140 _IMPEXP_SUPPORT const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, uint64_t);
00141 _IMPEXP_SUPPORT const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, int64_t);
00142 _IMPEXP_SUPPORT const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, float);
00143 _IMPEXP_SUPPORT const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, double);
00144 _IMPEXP_SUPPORT const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, ITextOutputManipFunc func);
00145 _IMPEXP_SUPPORT const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, const void*);
00146 
00147 template <class TYPE>
00148 inline const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, const sptr<TYPE>& a)
00149 {
00150     return io << a.ptr();
00151 }
00152 
00153 template <class TYPE>
00154 inline const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, const wptr<TYPE>& a)
00155 {
00156     return io << a.unsafe_ptr_access();
00157 }
00158 
00159 /*-----------------------------------------------------------------*/
00160 
00161 inline const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, ITextOutputManipFunc func)
00162 {
00163     return (*func)(io);
00164 }
00165 
00166 /*-----------------------------------------------------------------*/
00167 
00169 class STypeCode 
00170 {
00171 public:
00172     STypeCode(type_code type);
00173     ~STypeCode();
00174 
00175     type_code TypeCode() const;
00176     
00177 private:
00178     type_code fType;
00179     int32_t _reserved;
00180 };
00181 
00182 _IMPEXP_SUPPORT const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, const STypeCode& type);
00183 
00184 /*-----------------------------------------------------------------*/
00185 
00187 class SDuration
00188 {
00189 public:
00190     SDuration(nsecs_t d);
00191     ~SDuration();
00192 
00193     nsecs_t Duration() const;
00194 
00195 private:
00196     nsecs_t m_duration;
00197 };
00198 
00199 _IMPEXP_SUPPORT const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, const SDuration& type);
00200 
00201 /*-----------------------------------------------------------------*/
00202 
00204 class SSize
00205 {
00206 public:
00207     SSize(size_t s);
00208     ~SSize();
00209 
00210     size_t Size() const;
00211 
00212 private:
00213     size_t m_size;
00214 };
00215 
00216 _IMPEXP_SUPPORT const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, const SSize& type);
00217 
00218 /*-----------------------------------------------------------------*/
00219 /*------- Formatting status_t codes -------------------------------*/
00220 
00221 class SStatus
00222 {
00223 public:
00224     SStatus(status_t s);
00225     ~SStatus();
00226 
00227     status_t Status() const;
00228     SString AsString() const;
00229 
00230 private:
00231     status_t m_status;
00232 };
00233 
00234 _IMPEXP_SUPPORT const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, const SStatus& type);
00235 
00236 /*-----------------------------------------------------------------*/
00237 
00239 class SHexDump
00240 {
00241 public:
00242     SHexDump(const void *buf, size_t length, size_t bytesPerLine=16);
00243     ~SHexDump();
00244     
00245     SHexDump& SetBytesPerLine(size_t bytesPerLine);
00246     SHexDump& SetSingleLineCutoff(int32_t bytes);
00247     SHexDump& SetAlignment(size_t alignment);
00248     SHexDump& SetCArrayStyle(bool enabled);
00249     
00250     const void* Buffer() const;
00251     size_t Length() const;
00252     size_t BytesPerLine() const;
00253     int32_t SingleLineCutoff() const;
00254     size_t Alignment() const;
00255     bool CArrayStyle() const;
00256 
00257 private:
00258     const void* fBuffer;
00259     size_t fLength;
00260     size_t fBytesPerLine;
00261     int32_t fSingleLineCutoff;
00262     size_t fAlignment;
00263     bool fCArrayStyle;
00264 };
00265 
00266 _IMPEXP_SUPPORT const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, const SHexDump& buffer);
00267 
00268 /*-----------------------------------------------------------------*/
00269 
00271 class SPrintf
00272 {
00273 public:
00274     SPrintf(const char *fmt, ...);
00275     ~SPrintf();
00276     const char* Buffer() const;
00277 private:
00278     char* m_buffer;
00279     uint32_t _reserved[3];
00280 };
00281 
00282 _IMPEXP_SUPPORT const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, const SPrintf& data);
00283 
00284 /*-----------------------------------------------------------------*/
00285 
00287 class SFloatDump
00288 {
00289 public:
00290     SFloatDump(float f);
00291     const float& Float() const { return f_; }
00292 private:
00293     float f_;
00294 };
00295 
00296 _IMPEXP_SUPPORT const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, const SFloatDump& buffer);
00297 
00298 /*-----------------------------------------------------------------*/
00299 /*------- Formatting multibyte character constants ----------------*/
00300 class SCharCode
00301 {
00302 public:
00303             SCharCode(uint32_t c);
00304     uint32_t    val;
00305 };
00306 
00307 inline SCharCode::SCharCode(uint32_t c)
00308     :val(c)
00309 {
00310 }
00311 
00312 inline const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, const SCharCode &v)
00313 {
00314     const char *n = (const char *)(&(v.val));
00315     char str[5];
00316 #if defined(B_HOST_IS_LENDIAN) && B_HOST_IS_LENDIAN
00317     str[0] = n[3];
00318     str[1] = n[2];
00319     str[2] = n[1];
00320     str[3] = n[0];
00321 #else
00322     str[0] = n[0];
00323     str[1] = n[1];
00324     str[2] = n[2];
00325     str[3] = n[3];
00326 #endif
00327     str[4] = 0;
00328     return io << str; 
00329 }
00330 
00331 /*-----------------------------------------------------------------*/
00332 
00334 
00335 #if _SUPPORTS_NAMESPACE
00336 } } // namespace palmos::support
00337 #endif
00338 
00339 #endif /* _SUPPORT_TEXTSTREAM_INTERFACE_H */