UCommon
|
00001 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks. 00002 // 00003 // This file is part of GNU uCommon C++. 00004 // 00005 // GNU uCommon C++ is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU Lesser General Public License as published 00007 // by the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // GNU uCommon C++ is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU Lesser General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Lesser General Public License 00016 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. 00017 00030 #ifndef _UCOMMON_PROTOCOLS_H_ 00031 #define _UCOMMON_PROTOCOLS_H_ 00032 00033 #ifndef _UCOMMON_CONFIG_H_ 00034 #include <ucommon/platform.h> 00035 #endif 00036 00037 NAMESPACE_UCOMMON 00038 00039 class string; 00040 00041 class __EXPORT MemoryProtocol 00042 { 00043 protected: 00044 friend class MemoryRedirect; 00052 virtual void *_alloc(size_t size) = 0; 00053 00054 public: 00060 inline void *alloc(size_t size) 00061 {return _alloc(size);}; 00062 00070 void *zalloc(size_t size); 00071 00078 char *dup(const char *string); 00079 00086 void *dup(void *memory, size_t size); 00087 }; 00088 00094 class __EXPORT MemoryRedirect : public MemoryProtocol 00095 { 00096 private: 00097 MemoryProtocol *target; 00098 00099 public: 00100 MemoryRedirect(MemoryProtocol *protocol); 00101 00102 virtual void *_alloc(size_t size); 00103 }; 00104 00112 class __EXPORT LockingProtocol 00113 { 00114 protected: 00115 virtual void _lock(void); 00116 virtual void _unlock(void); 00117 }; 00118 00124 class __EXPORT CharacterProtocol 00125 { 00126 protected: 00131 virtual int _getch(void) = 0; 00132 00138 virtual int _putch(int code) = 0; 00139 00140 public: 00145 inline int get(void) 00146 {return _getch();}; 00147 00153 inline int put(int code) 00154 {return _putch(code);}; 00155 }; 00156 00165 class __EXPORT BufferProtocol : public CharacterProtocol 00166 { 00167 public: 00168 typedef enum {BUF_RD, BUF_WR, BUF_RDWR} type_t; 00169 00170 private: 00171 const char *eol; 00172 char *buffer; 00173 char *input, *output; 00174 size_t bufsize, bufpos, insize, outsize; 00175 bool end; 00176 00177 protected: 00178 const char *format; 00179 00183 BufferProtocol(); 00184 00190 BufferProtocol(size_t size, type_t access = BUF_RDWR); 00191 00195 ~BufferProtocol(); 00196 00204 inline void seteol(const char *string) 00205 {eol = string;}; 00206 00213 void allocate(size_t size, type_t access = BUF_RDWR); 00214 00218 void release(void); 00219 00227 char *request(size_t size); 00228 00235 char *gather(size_t size); 00236 00244 virtual size_t _push(const char *address, size_t size) = 0; 00245 00253 virtual size_t _pull(char *address, size_t size) = 0; 00254 00259 virtual int _err(void) const = 0; 00260 00264 virtual void _clear(void) = 0; 00265 00269 virtual bool _blocking(void); 00270 00274 virtual bool _pending(void); 00275 00279 virtual bool _flush(void); 00280 00286 inline size_t unread(void) 00287 {return bufpos;}; 00288 00293 inline size_t unsaved(void) 00294 {return outsize;}; 00295 00296 public: 00304 size_t get(char *address, size_t count); 00305 00314 size_t put(const char *address, size_t count = 0); 00315 00320 int _getch(void); 00321 00326 int _putch(int ch); 00327 00334 size_t printf(const char *format, ...) __PRINTF(2, 3); 00335 00340 inline bool flush(void) 00341 {return _flush();} 00342 00346 void purge(void); 00347 00351 void reset(void); 00352 00363 size_t getline(char *string, size_t size); 00364 00374 size_t getline(string& buffer); 00375 00382 size_t putline(const char *string); 00383 00388 bool eof(void); 00389 00394 inline operator bool() 00395 {return buffer != NULL;} 00396 00401 inline bool operator!() 00402 {return buffer == NULL;} 00403 00408 inline bool is_open(void) 00409 {return buffer != NULL;} 00410 00415 inline bool is_input(void) 00416 {return input != NULL;} 00417 00422 inline bool is_output(void) 00423 {return output != NULL;} 00424 00429 inline bool is_pending(void) 00430 {return _pending();} 00431 00435 inline void seteof(void) 00436 {end = true;} 00437 00438 inline int err(void) 00439 {return _err();} 00440 }; 00441 00442 END_NAMESPACE 00443 00444 #endif