ucommon
protocols.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
2 //
3 // This file is part of GNU uCommon C++.
4 //
5 // GNU uCommon C++ is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // GNU uCommon C++ is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17 
30 #ifndef _UCOMMON_PROTOCOLS_H_
31 #define _UCOMMON_PROTOCOLS_H_
32 
33 #ifndef _UCOMMON_CPR_H_
34 #include <ucommon/cpr.h>
35 #endif
36 
37 NAMESPACE_UCOMMON
38 
39 class String;
40 class StringPager;
41 
42 class __EXPORT MemoryProtocol
43 {
44 protected:
45  friend class MemoryRedirect;
46 
54  virtual void *_alloc(size_t size) = 0;
55 
59  virtual void fault(void) const;
60 
61 public:
62  virtual ~MemoryProtocol();
63 
69  inline void *alloc(size_t size)
70  {return _alloc(size);};
71 
79  void *zalloc(size_t size);
80 
87  char *dup(const char *string);
88 
95  void *dup(void *memory, size_t size);
96 };
97 
103 class __EXPORT MemoryRedirect : public MemoryProtocol
104 {
105 private:
106  MemoryProtocol *target;
107 
108 public:
109  MemoryRedirect(MemoryProtocol *protocol);
110 
111  virtual void *_alloc(size_t size);
112 };
113 
121 class __EXPORT LockingProtocol
122 {
123 protected:
124  virtual void _lock(void);
125  virtual void _unlock(void);
126 
127 public:
128  virtual ~LockingProtocol();
129 };
130 
137 class __EXPORT PrintProtocol
138 {
139 public:
140  virtual ~PrintProtocol();
141 
145  virtual const char *_print(void) const = 0;
146 };
147 
156 class __EXPORT InputProtocol
157 {
158 public:
159  virtual ~InputProtocol();
160 
166  virtual int _input(int code) = 0;
167 };
168 
174 class __EXPORT CharacterProtocol
175 {
176 protected:
177  const char *eol;
178  int back;
179 
181 
186  virtual int _getch(void) = 0;
187 
193  virtual int _putch(int code) = 0;
194 
199  inline void putback(int code)
200  {back = code;}
201 
209  inline void seteol(const char *string)
210  {eol = string;};
211 
212 public:
213  virtual ~CharacterProtocol();
214 
219  inline int getchar(void)
220  {return _getch();};
221 
227  inline int putchar(int code)
228  {return _putch(code);};
229 
230  size_t print(const PrintProtocol& format);
231 
232  size_t input(InputProtocol& format);
233 
244  size_t getline(char *string, size_t size);
245 
255  size_t getline(String& buffer);
256 
263  size_t putline(const char *string);
264 
265  size_t putchars(const char *string, size_t count = 0);
266 
273  size_t load(StringPager *list);
274 
280  size_t save(const StringPager *list);
281 };
282 
291 class __EXPORT BufferProtocol : public CharacterProtocol
292 {
293 public:
294  typedef enum {RDONLY, WRONLY, RDWR} mode_t;
295 
296 private:
297  char *buffer;
298  char *input, *output;
299  size_t bufsize, bufpos, insize, outsize;
300  bool end;
301 
302 protected:
303  const char *format;
304 
308  BufferProtocol();
309 
315  BufferProtocol(size_t size, mode_t access = RDWR);
316 
320  virtual ~BufferProtocol();
321 
325  virtual void fault(void) const;
326 
333  void allocate(size_t size, mode_t access = RDWR);
334 
338  void release(void);
339 
347  char *request(size_t size);
348 
355  char *gather(size_t size);
356 
364  virtual size_t _push(const char *address, size_t size) = 0;
365 
373  virtual size_t _pull(char *address, size_t size) = 0;
374 
379  virtual int _err(void) const = 0;
380 
384  virtual void _clear(void) = 0;
385 
389  virtual bool _blocking(void);
390 
394  virtual bool _pending(void);
395 
399  virtual bool _flush(void);
400 
401  virtual int _getch(void);
402 
403  virtual int _putch(int ch);
404 
410  inline size_t input_pending(void)
411  {return bufpos;};
412 
417  inline size_t output_waiting(void)
418  {return outsize;};
419 
420 public:
421  const char *endl(void)
422  {return eol;}
423 
431  size_t put(const void *address, size_t count);
432 
439  size_t get(void *address, size_t count);
440 
447  size_t printf(const char *format, ...) __PRINTF(2, 3);
448 
453  inline bool flush(void)
454  {return _flush();}
455 
459  void purge(void);
460 
464  void reset(void);
465 
470  bool eof(void);
471 
476  inline operator bool()
477  {return buffer != NULL;}
478 
483  inline bool operator!()
484  {return buffer == NULL;}
485 
490  inline bool is_open(void)
491  {return buffer != NULL;}
492 
497  inline bool is_input(void)
498  {return input != NULL;}
499 
504  inline bool is_output(void)
505  {return output != NULL;}
506 
511  inline bool is_pending(void)
512  {return _pending();}
513 
517  inline void seteof(void)
518  {end = true;}
519 
520  inline int err(void)
521  {return _err();}
522 
523  template<typename T> inline size_t write(const T& data)
524  {return put(&data, sizeof(T));}
525 
526  template<typename T> inline size_t read(T& data)
527  {return get(&data, sizeof(T));}
528 
529  template<typename T> inline size_t write(const T* data, unsigned count)
530  {return put(data, sizeof(T) * count) / sizeof(T);}
531 
532  template<typename T> inline size_t read(T* data, unsigned count)
533  {return get(data, sizeof(T) * count) / sizeof(T);}
534 
535 };
536 
544 class __EXPORT ObjectProtocol
545 {
546 public:
550  virtual void retain(void) = 0;
551 
555  virtual void release(void) = 0;
556 
560  virtual ~ObjectProtocol();
561 
565  ObjectProtocol *copy(void);
566 
570  inline void operator++(void)
571  {retain();};
572 
576  inline void operator--(void)
577  {release();};
578 };
579 
584 class __EXPORT _character_operators
585 {
586 private:
587  inline _character_operators() {};
588 
589 public:
590  static CharacterProtocol& print(CharacterProtocol& p, const char *s);
591 
592  static CharacterProtocol& print(CharacterProtocol& p, const char& ch);
593 
594  static CharacterProtocol& input(CharacterProtocol& p, char& ch);
595 
596  static CharacterProtocol& input(CharacterProtocol& p, String& str);
597 
598  static CharacterProtocol& print(CharacterProtocol& p, const long& value);
599 
600  static CharacterProtocol& input(CharacterProtocol& p, long& value);
601 
602  static CharacterProtocol& print(CharacterProtocol& p, const double& value);
603 
604  static CharacterProtocol& input(CharacterProtocol& p, double& value);
605 };
606 
607 inline CharacterProtocol& operator<< (CharacterProtocol& p, const char *s)
608  {return _character_operators::print(p, s);}
609 
610 inline CharacterProtocol& operator<< (CharacterProtocol& p, const char& ch)
611  {return _character_operators::print(p, ch);}
612 
613 inline CharacterProtocol& operator>> (CharacterProtocol& p, char& ch)
614  {return _character_operators::input(p, ch);}
615 
616 inline CharacterProtocol& operator>> (CharacterProtocol& p, String& str)
617  {return _character_operators::input(p, str);}
618 
619 inline CharacterProtocol& operator<< (CharacterProtocol& p, const PrintProtocol& format)
620  {p.print(format); return p;}
621 
622 inline CharacterProtocol& operator>> (CharacterProtocol& p, InputProtocol& format)
623  {p.input(format); return p;}
624 
625 inline CharacterProtocol& operator<< (CharacterProtocol& p, const StringPager& list)
626  {p.save(&list); return p;}
627 
628 inline CharacterProtocol& operator>> (CharacterProtocol& p, StringPager& list)
629  {p.load(&list); return p;}
630 
631 inline CharacterProtocol& operator<< (CharacterProtocol& p, const long& value)
632  {return _character_operators::print(p, value);}
633 
634 inline CharacterProtocol& operator>> (CharacterProtocol& p, long& value)
635  {return _character_operators::input(p, value);}
636 
637 inline CharacterProtocol& operator<< (CharacterProtocol& p, const double& value)
638  {return _character_operators::print(p, value);}
639 
640 inline CharacterProtocol& operator>> (CharacterProtocol& p, double& value)
641  {return _character_operators::input(p, value);}
642 
643 END_NAMESPACE
644 
645 #endif