ucommon
protocols.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 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) const
411  {return bufpos;}
412 
417  inline size_t output_waiting(void) const
418  {return outsize;}
419 
420 public:
421  const char *endl(void) const
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() const
477  {return buffer != NULL;}
478 
483  inline bool operator!() const
484  {return buffer == NULL;}
485 
490  inline bool is_open(void) const
491  {return buffer != NULL;}
492 
497  inline bool is_input(void) const
498  {return input != NULL;}
499 
504  inline bool is_output(void) const
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) const
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 
583 class __EXPORT KeyProtocol
584 {
585 protected:
586  virtual int keytype(void) const = 0;
587 
591  virtual size_t keysize(void) const = 0;
592 
596  virtual const void *keydata(void) const = 0;
597 
598  virtual bool equal(const KeyProtocol& compare) const;
599 
600  inline bool operator!=(const KeyProtocol& compare) const {
601  return !equal(compare);
602  }
603 
604  virtual ~KeyProtocol();
605 };
606 
611 class __EXPORT _character_operators
612 {
613 private:
614  inline _character_operators() {}
615 
616 public:
617  static CharacterProtocol& print(CharacterProtocol& p, const char *s);
618 
619  static CharacterProtocol& print(CharacterProtocol& p, const char& ch);
620 
621  static CharacterProtocol& input(CharacterProtocol& p, char& ch);
622 
623  static CharacterProtocol& input(CharacterProtocol& p, String& str);
624 
625  static CharacterProtocol& print(CharacterProtocol& p, const long& value);
626 
627  static CharacterProtocol& input(CharacterProtocol& p, long& value);
628 
629  static CharacterProtocol& print(CharacterProtocol& p, const double& value);
630 
631  static CharacterProtocol& input(CharacterProtocol& p, double& value);
632 };
633 
634 inline CharacterProtocol& operator<< (CharacterProtocol& p, const char *s)
635  {return _character_operators::print(p, s);}
636 
637 inline CharacterProtocol& operator<< (CharacterProtocol& p, const char& ch)
638  {return _character_operators::print(p, ch);}
639 
640 inline CharacterProtocol& operator>> (CharacterProtocol& p, char& ch)
641  {return _character_operators::input(p, ch);}
642 
643 inline CharacterProtocol& operator>> (CharacterProtocol& p, String& str)
644  {return _character_operators::input(p, str);}
645 
646 inline CharacterProtocol& operator<< (CharacterProtocol& p, const PrintProtocol& format)
647  {p.print(format); return p;}
648 
649 inline CharacterProtocol& operator>> (CharacterProtocol& p, InputProtocol& format)
650  {p.input(format); return p;}
651 
652 inline CharacterProtocol& operator<< (CharacterProtocol& p, const StringPager& list)
653  {p.save(&list); return p;}
654 
655 inline CharacterProtocol& operator>> (CharacterProtocol& p, StringPager& list)
656  {p.load(&list); return p;}
657 
658 inline CharacterProtocol& operator<< (CharacterProtocol& p, const long& value)
659  {return _character_operators::print(p, value);}
660 
661 inline CharacterProtocol& operator>> (CharacterProtocol& p, long& value)
662  {return _character_operators::input(p, value);}
663 
664 inline CharacterProtocol& operator<< (CharacterProtocol& p, const double& value)
665  {return _character_operators::print(p, value);}
666 
667 inline CharacterProtocol& operator>> (CharacterProtocol& p, double& value)
668  {return _character_operators::input(p, value);}
669 
670 } // namespace ucommon
671 
672 #endif
A common base class for all managed objects.
Definition: protocols.h:544
size_t input_pending(void) const
Get current input position.
Definition: protocols.h:410
void putback(int code)
Write to back buffer.
Definition: protocols.h:199
At least with gcc, linking of stream operators was broken.
Definition: protocols.h:611
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
Definition: access.h:260
A redirection base class for the memory protocol.
Definition: protocols.h:103
A copy-on-write string class that operates by reference count.
Definition: string.h:82
Common namespace for all ucommon objects.
Definition: access.h:46
bool is_pending(void)
See if pending input.
Definition: protocols.h:511
bool is_input(void) const
See if input active.
Definition: protocols.h:497
void retain(ObjectProtocol *object)
Convenience function to access object retention.
Definition: object.h:465
void seteof(void)
Set eof flag.
Definition: protocols.h:517
void operator++(void)
Increase retention operator.
Definition: protocols.h:570
Runtime functions.
bool operator!() const
See if buffer closed.
Definition: protocols.h:483
int putchar(int code)
Put the next character.
Definition: protocols.h:227
void operator--(void)
Decrease retention operator.
Definition: protocols.h:576
Common buffer protocol class.
Definition: protocols.h:291
int getchar(void)
Get the next character.
Definition: protocols.h:219
String pager for storing lists of NULL terminated strings.
Definition: memory.h:364
Key data protocol used for things like maps and ordered lists.
Definition: protocols.h:583
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
Definition: object.h:479
size_t output_waiting(void) const
Get current output position.
Definition: protocols.h:417
bool is_open(void) const
See if buffer open.
Definition: protocols.h:490
Common locking protocol.
Definition: protocols.h:121
Used for processing input.
Definition: protocols.h:156
void seteol(const char *string)
Set end of line marker.
Definition: protocols.h:209
T * dup(const T &object)
Convenience function to duplicate object pointer to heap.
Definition: generics.h:475
void access(SharedAccess &object)
Convenience function to access (lock) shared object through it's protocol.
Definition: access.h:253
bool is_output(void) const
See if output active.
Definition: protocols.h:504
Data keys parsed from a keyfile.
Definition: keydata.h:57
Common character processing protocol.
Definition: protocols.h:174
Used for forming stream output.
Definition: protocols.h:137