UCommon
|
00001 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks. 00002 // Copyright (C) 2015 Cherokees of Idaho. 00003 // 00004 // This file is part of GNU uCommon C++. 00005 // 00006 // GNU uCommon C++ is free software: you can redistribute it and/or modify 00007 // it under the terms of the GNU Lesser General Public License as published 00008 // by the Free Software Foundation, either version 3 of the License, or 00009 // (at your option) any later version. 00010 // 00011 // GNU uCommon C++ is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU Lesser General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU Lesser General Public License 00017 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. 00018 00031 #ifndef _UCOMMON_PROTOCOLS_H_ 00032 #define _UCOMMON_PROTOCOLS_H_ 00033 00034 #ifndef _UCOMMON_CPR_H_ 00035 #include <ucommon/cpr.h> 00036 #endif 00037 00038 namespace ucommon { 00039 00040 class String; 00041 class StringPager; 00042 00043 class __EXPORT MemoryProtocol 00044 { 00045 protected: 00046 friend class MemoryRedirect; 00047 00055 virtual void *_alloc(size_t size) = 0; 00056 00060 virtual void fault(void) const; 00061 00062 public: 00063 virtual ~MemoryProtocol(); 00064 00070 inline void *alloc(size_t size) 00071 {return _alloc(size);} 00072 00080 void *zalloc(size_t size); 00081 00088 char *dup(const char *string); 00089 00096 void *dup(void *memory, size_t size); 00097 }; 00098 00104 class __EXPORT MemoryRedirect : public MemoryProtocol 00105 { 00106 private: 00107 MemoryProtocol *target; 00108 00109 public: 00110 MemoryRedirect(MemoryProtocol *protocol); 00111 00112 virtual void *_alloc(size_t size); 00113 }; 00114 00122 class __EXPORT LockingProtocol 00123 { 00124 protected: 00125 virtual void _lock(void); 00126 virtual void _unlock(void); 00127 00128 public: 00129 virtual ~LockingProtocol(); 00130 }; 00131 00138 class __EXPORT PrintProtocol 00139 { 00140 public: 00141 virtual ~PrintProtocol(); 00142 00146 virtual const char *_print(void) const = 0; 00147 }; 00148 00157 class __EXPORT InputProtocol 00158 { 00159 public: 00160 virtual ~InputProtocol(); 00161 00167 virtual int _input(int code) = 0; 00168 }; 00169 00175 class __EXPORT CharacterProtocol 00176 { 00177 protected: 00178 const char *eol; 00179 int back; 00180 00181 CharacterProtocol(); 00182 00187 virtual int _getch(void) = 0; 00188 00194 virtual int _putch(int code) = 0; 00195 00200 inline void putback(int code) 00201 {back = code;} 00202 00210 inline void seteol(const char *string) 00211 {eol = string;} 00212 00213 public: 00214 virtual ~CharacterProtocol(); 00215 00220 inline int getchar(void) 00221 {return _getch();} 00222 00228 inline int putchar(int code) 00229 {return _putch(code);} 00230 00231 size_t print(const PrintProtocol& format); 00232 00233 size_t input(InputProtocol& format); 00234 00245 size_t getline(char *string, size_t size); 00246 00256 size_t getline(String& buffer); 00257 00264 size_t putline(const char *string); 00265 00266 size_t putchars(const char *string, size_t count = 0); 00267 00274 size_t load(StringPager *list); 00275 00281 size_t save(const StringPager *list); 00282 }; 00283 00292 class __EXPORT BufferProtocol : public CharacterProtocol 00293 { 00294 public: 00295 typedef enum {RDONLY, WRONLY, RDWR} mode_t; 00296 00297 private: 00298 char *buffer; 00299 char *input, *output; 00300 size_t bufsize, bufpos, insize, outsize; 00301 bool end; 00302 00303 protected: 00304 const char *format; 00305 00309 BufferProtocol(); 00310 00316 BufferProtocol(size_t size, mode_t access = RDWR); 00317 00321 virtual ~BufferProtocol(); 00322 00326 virtual void fault(void) const; 00327 00334 void allocate(size_t size, mode_t access = RDWR); 00335 00339 void release(void); 00340 00348 char *request(size_t size); 00349 00356 char *gather(size_t size); 00357 00365 virtual size_t _push(const char *address, size_t size) = 0; 00366 00374 virtual size_t _pull(char *address, size_t size) = 0; 00375 00380 virtual int _err(void) const = 0; 00381 00385 virtual void _clear(void) = 0; 00386 00390 virtual bool _blocking(void); 00391 00395 virtual bool _pending(void); 00396 00400 virtual bool _flush(void); 00401 00402 virtual int _getch(void); 00403 00404 virtual int _putch(int ch); 00405 00411 inline size_t input_pending(void) const 00412 {return bufpos;} 00413 00418 inline size_t output_waiting(void) const 00419 {return outsize;} 00420 00421 public: 00422 const char *endl(void) const 00423 {return eol;} 00424 00432 size_t put(const void *address, size_t count); 00433 00440 size_t get(void *address, size_t count); 00441 00448 size_t printf(const char *format, ...) __PRINTF(2, 3); 00449 00454 inline bool flush(void) 00455 {return _flush();} 00456 00460 void purge(void); 00461 00465 void reset(void); 00466 00471 bool eof(void); 00472 00477 inline operator bool() const 00478 {return buffer != NULL;} 00479 00484 inline bool operator!() const 00485 {return buffer == NULL;} 00486 00491 inline bool is_open(void) const 00492 {return buffer != NULL;} 00493 00498 inline bool is_input(void) const 00499 {return input != NULL;} 00500 00505 inline bool is_output(void) const 00506 {return output != NULL;} 00507 00512 inline bool is_pending(void) 00513 {return _pending();} 00514 00518 inline void seteof(void) 00519 {end = true;} 00520 00521 inline int err(void) const 00522 {return _err();} 00523 00524 template<typename T> inline size_t write(const T& data) 00525 {return put(&data, sizeof(T));} 00526 00527 template<typename T> inline size_t read(T& data) 00528 {return get(&data, sizeof(T));} 00529 00530 template<typename T> inline size_t write(const T* data, unsigned count) 00531 {return put(data, sizeof(T) * count) / sizeof(T);} 00532 00533 template<typename T> inline size_t read(T* data, unsigned count) 00534 {return get(data, sizeof(T) * count) / sizeof(T);} 00535 00536 }; 00537 00545 class __EXPORT ObjectProtocol 00546 { 00547 public: 00551 virtual void retain(void) = 0; 00552 00556 virtual void release(void) = 0; 00557 00561 virtual ~ObjectProtocol(); 00562 00566 ObjectProtocol *copy(void); 00567 00571 inline void operator++(void) 00572 {retain();}; 00573 00577 inline void operator--(void) 00578 {release();}; 00579 }; 00580 00584 class __EXPORT KeyProtocol 00585 { 00586 protected: 00587 virtual int keytype(void) const = 0; 00588 00592 virtual size_t keysize(void) const = 0; 00593 00597 virtual const void *keydata(void) const = 0; 00598 00599 virtual bool equal(const KeyProtocol& compare) const; 00600 00601 inline bool operator!=(const KeyProtocol& compare) const { 00602 return !equal(compare); 00603 } 00604 00605 virtual ~KeyProtocol(); 00606 }; 00607 00612 class __EXPORT _character_operators 00613 { 00614 private: 00615 inline _character_operators() {} 00616 00617 public: 00618 static CharacterProtocol& print(CharacterProtocol& p, const char *s); 00619 00620 static CharacterProtocol& print(CharacterProtocol& p, const char& ch); 00621 00622 static CharacterProtocol& input(CharacterProtocol& p, char& ch); 00623 00624 static CharacterProtocol& input(CharacterProtocol& p, String& str); 00625 00626 static CharacterProtocol& print(CharacterProtocol& p, const long& value); 00627 00628 static CharacterProtocol& input(CharacterProtocol& p, long& value); 00629 00630 static CharacterProtocol& print(CharacterProtocol& p, const double& value); 00631 00632 static CharacterProtocol& input(CharacterProtocol& p, double& value); 00633 }; 00634 00635 inline CharacterProtocol& operator<< (CharacterProtocol& p, const char *s) 00636 {return _character_operators::print(p, s);} 00637 00638 inline CharacterProtocol& operator<< (CharacterProtocol& p, const char& ch) 00639 {return _character_operators::print(p, ch);} 00640 00641 inline CharacterProtocol& operator>> (CharacterProtocol& p, char& ch) 00642 {return _character_operators::input(p, ch);} 00643 00644 inline CharacterProtocol& operator>> (CharacterProtocol& p, String& str) 00645 {return _character_operators::input(p, str);} 00646 00647 inline CharacterProtocol& operator<< (CharacterProtocol& p, const PrintProtocol& format) 00648 {p.print(format); return p;} 00649 00650 inline CharacterProtocol& operator>> (CharacterProtocol& p, InputProtocol& format) 00651 {p.input(format); return p;} 00652 00653 inline CharacterProtocol& operator<< (CharacterProtocol& p, const StringPager& list) 00654 {p.save(&list); return p;} 00655 00656 inline CharacterProtocol& operator>> (CharacterProtocol& p, StringPager& list) 00657 {p.load(&list); return p;} 00658 00659 inline CharacterProtocol& operator<< (CharacterProtocol& p, const long& value) 00660 {return _character_operators::print(p, value);} 00661 00662 inline CharacterProtocol& operator>> (CharacterProtocol& p, long& value) 00663 {return _character_operators::input(p, value);} 00664 00665 inline CharacterProtocol& operator<< (CharacterProtocol& p, const double& value) 00666 {return _character_operators::print(p, value);} 00667 00668 inline CharacterProtocol& operator>> (CharacterProtocol& p, double& value) 00669 {return _character_operators::input(p, value);} 00670 00671 } // namespace ucommon 00672 00673 #endif