UCommon
protocols.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This file is part of GNU uCommon C++.
5 //
6 // GNU uCommon C++ is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // GNU uCommon C++ is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18 
31 #ifndef _UCOMMON_PROTOCOLS_H_
32 #define _UCOMMON_PROTOCOLS_H_
33 
34 #ifndef _UCOMMON_CPR_H_
35 #include <ucommon/cpr.h>
36 #endif
37 
38 namespace ucommon {
39 
40 class String;
41 class StringPager;
42 
43 class __EXPORT MemoryProtocol
44 {
45 protected:
46  friend class MemoryRedirect;
47 
55  virtual void *_alloc(size_t size) = 0;
56 
57 public:
58  virtual ~MemoryProtocol();
59 
65  inline void *alloc(size_t size) {
66  return _alloc(size);
67  }
68 
76  void *zalloc(size_t size);
77 
84  char *dup(const char *string);
85 
92  void *dup(void *memory, size_t size);
93 };
94 
100 class __EXPORT MemoryRedirect : public __PROTOCOL MemoryProtocol
101 {
102 private:
103  MemoryProtocol *target;
104 
105 public:
106  MemoryRedirect(MemoryProtocol *protocol);
107 
108  virtual void *_alloc(size_t size) __OVERRIDE;
109 };
110 
118 class __EXPORT LockingProtocol
119 {
120 protected:
121  virtual void _lock(void);
122  virtual void _unlock(void);
123 
124 public:
125  virtual ~LockingProtocol();
126 };
127 
134 class __EXPORT PrintProtocol
135 {
136 public:
137  virtual ~PrintProtocol();
138 
142  virtual const char *_print(void) const = 0;
143 };
144 
153 class __EXPORT InputProtocol
154 {
155 public:
156  virtual ~InputProtocol();
157 
163  virtual int _input(int code) = 0;
164 };
165 
173 class __EXPORT ObjectProtocol
174 {
175 public:
179  virtual void retain(void) = 0;
180 
184  virtual void release(void) = 0;
185 
189  virtual ~ObjectProtocol();
190 
195 
199  inline void operator++(void) {
200  retain();
201  }
202 
206  inline void operator--(void) {
207  release();
208  }
209 };
210 
214 class __EXPORT KeyProtocol
215 {
216 protected:
217  virtual int keytype(void) const = 0;
218 
222  virtual size_t keysize(void) const = 0;
223 
227  virtual const void *keydata(void) const = 0;
228 
229  virtual bool equal(const KeyProtocol& compare) const;
230 
231  inline bool operator!=(const KeyProtocol& compare) const {
232  return !equal(compare);
233  }
234 
235  virtual ~KeyProtocol();
236 };
237 
238 } // namespace ucommon
239 
240 #endif
cpr.h
Runtime functions.
ucommon::dup
T * dup(const T &object)
Convenience function to duplicate object pointer to heap.
Definition: generics.h:324
ucommon::ObjectProtocol::copy
ObjectProtocol * copy(void)
Retain (increase retention of) object when copying.
ucommon::InputProtocol
Used for processing input.
Definition: protocols.h:154
ucommon::LockingProtocol
Common locking protocol.
Definition: protocols.h:119
ucommon::KeyProtocol
Key data protocol used for things like maps and ordered lists.
Definition: protocols.h:215
ucommon::PrintProtocol
Used for forming stream output.
Definition: protocols.h:135
ucommon::MemoryRedirect
A redirection base class for the memory protocol.
Definition: protocols.h:101
ucommon
Common namespace for all ucommon objects.
Definition: access.h:47
ucommon::KeyProtocol::keysize
virtual size_t keysize(void) const =0
Size of key data.
ucommon::KeyProtocol::keydata
virtual const void * keydata(void) const =0
Buffer of key value.
ucommon::ObjectProtocol::retain
virtual void retain(void)=0
Method to retain (or increase retention) of an object.
ucommon::ObjectProtocol::~ObjectProtocol
virtual ~ObjectProtocol()
Required virtual destructor.
ucommon::ObjectProtocol::operator--
void operator--(void)
Decrease retention operator.
Definition: protocols.h:206
ucommon::ObjectProtocol::operator++
void operator++(void)
Increase retention operator.
Definition: protocols.h:199
ucommon::ObjectProtocol::release
virtual void release(void)=0
Method to release (or decrease retention) of an object.
ucommon::ObjectProtocol
A common base class for all managed objects.
Definition: protocols.h:174
ucommon::PrintProtocol::_print
virtual const char * _print(void) const =0
Extract formatted string for object.
ucommon::InputProtocol::_input
virtual int _input(int code)=0
Extract formatted string for object.