ucommon  7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
  Fossies Dox: ucommon-7.0.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

shared.h
Go to the documentation of this file.
1 // Copyright (C) 2015 Cherokees of Idaho.
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 
24 #ifndef _UCOMMON_SHARED_H_
25 #define _UCOMMON_SHARED_H_
26 
27 #ifndef _UCOMMON_CPR_H_
28 #include <ucommon/cpr.h>
29 #endif
30 
31 #ifndef _UCOMMON_ATOMIC_H_
32 #include <ucommon/atomic.h>
33 #endif
34 
35 #ifndef _UCOMMON_PROTOCOLS_H_
36 #include <ucommon/protocols.h>
37 #endif
38 
39 #ifndef _UCOMMON_OBJECT_H_
40 #include <ucommon/object.h>
41 #endif
42 
43 #ifndef _UCOMMON_TYPEREF_H_
44 #include <ucommon/typeref.h>
45 #endif
46 
47 #ifndef _UCOMMON_THREAD_H_
48 #include <ucommon/thread.h>
49 #endif
50 
51 #ifndef _UCOMMON_SOCKET_H_
52 #include <ucommon/socket.h>
53 #endif
54 
55 namespace ucommon {
56 
57 class __EXPORT SharedRef : protected TypeRef
58 {
59 private:
61 
62 protected:
64 
65  SharedRef();
66 
67  TypeRef get();
68 
69  void get(TypeRef& object);
70 
71  void put(TypeRef& object);
72 };
73 
74 template<typename T>
75 class sharedref : private SharedRef
76 {
77 private:
79 
80 public:
81  inline sharedref() : SharedRef() {};
82 
83  inline operator typeref<T>() {
84  lock.acquire();
85  typeref<T> ptr(ref);
86  lock.release();
87  return ptr;
88  }
89 
90  inline typeref<T> operator*() {
91  lock.acquire();
92  typeref<T> ptr(ref);
93  lock.release();
94  return ptr;
95  }
96 
97  inline void put(typeref<T>& ptr) {
98  SharedRef::put(ptr);
99  }
100 
102  SharedRef::get(ptr);
103  return *this;
104  }
105 
106  inline sharedref& operator=(T obj) {
107  typeref<T> ptr(obj);
108  SharedRef::get(ptr);
109  return *this;
110  }
111 };
112 
114 {
115 private:
117 
118 protected:
119  class __EXPORT Index : public LinkedObject
120  {
121  public:
122  explicit Index(LinkedObject **origin);
123 
124  const void *key;
125  void *value;
126  };
127 
129 
130  LinkedObject *free, **list;
131 
133 
134  size_t paths;
135 
136  MappedPointer(size_t indexes, condlock_t *locking = NULL, size_t paging = 0);
137  ~MappedPointer();
138 
139  LinkedObject *access(size_t path);
140 
141  LinkedObject *modify(size_t path);
142 
143  void release(void *obj);
144 
145  void insert(const void *key, void *value, size_t path);
146 
147  void replace(Index *ind, void *value);
148 
149  void remove(Index *ind, size_t path);
150 
151 public:
152  static size_t keypath(const uint8_t *addr, size_t size);
153 };
154 
155 template<typename T>
156 inline size_t mapped_keypath(const T *addr)
157 {
158  if(!addr)
159  return 0;
160 
161  return MappedPointer::keypath((const uint8_t *)addr, sizeof(T));
162 }
163 
164 template<typename T>
165 inline bool mapped_keyequal(const T* key1, const T* key2)
166 {
167  if(!key1 || !key2)
168  return false;
169  return !memcmp(key1, key2, sizeof(T));
170 }
171 
172 template<>
173 inline size_t mapped_keypath<char>(const char *addr)
174 {
175  if(!addr)
176  return 0;
177 
178  return MappedPointer::keypath((const uint8_t *)addr, strlen(addr));
179 }
180 
181 template<>
182 inline bool mapped_keyequal<char>(const char *k1, const char *k2)
183 {
184  if(!k1 || !k2)
185  return false;
186 
187  return eq(k1, k2);
188 }
189 
190 template<>
191 inline size_t mapped_keypath<struct sockaddr>(const struct sockaddr *addr)
192 {
193  if(!addr)
194  return 0;
195 
196  return MappedPointer::keypath((const uint8_t *)addr, Socket::len(addr));
197 }
198 
199 template<>
200 inline bool mapped_keyequal<struct sockaddr>(const struct sockaddr *s1, const struct sockaddr *s2)
201 {
202  if(!s1 || !s2)
203  return false;
204  return Socket::equal(s1, s2);
205 }
206 
207 template<typename K, typename V>
209 {
210 public:
211  inline mapped_pointer(size_t indexes = 37, condlock_t *locking = NULL, size_t paging = 0) : MappedPointer(indexes, locking, paging) {}
212 
213  inline void release(V* object) {
214  MappedPointer::release(object);
215  }
216 
217  void remove(const K* key) {
218  size_t path = mapped_keypath<K>(key);
219  linked_pointer<Index> ip = modify(path);
220  while(is(ip)) {
221  if(mapped_keyequal<K>((const K*)(ip->key), key)) {
222  MappedPointer::remove(*ip, path);
223  return;
224  }
225  ip.next();
226  }
227  lock->commit();
228  }
229 
230  V* get(const K* key) {
231  linked_pointer<Index> ip = access(mapped_keypath<K>(key));
232  while(is(ip)) {
233  if(mapped_keyequal<K>((const K*)(ip->key), key)) {
234  return static_cast<V*>(ip->value);
235  }
236  ip.next();
237  }
238  lock->release();
239  return nullptr;
240  }
241 
242  void set(const K* key, V* ptr) {
243  size_t path = mapped_keypath<K>(key);
244  linked_pointer<Index> ip = modify(path);
245  while(is(ip)) {
246  if(mapped_keyequal<K>((const K*)(ip->key), key)) {
247  replace(*ip, ptr);
248  return;
249  }
250  }
251  insert((const void *)key, (void *)ptr, path);
252  }
253 };
254 
255 } // namespace
256 
257 #endif
ucommon::mapped_keypath< struct sockaddr >
size_t mapped_keypath< struct sockaddr >(const struct sockaddr *addr)
Definition: shared.h:191
ucommon::MappedPointer::pager
memalloc pager
Definition: shared.h:132
ucommon::SharedRef
Definition: shared.h:57
ucommon::MappedPointer::Index::key
const void * key
Definition: shared.h:124
ucommon::mapped_keypath
size_t mapped_keypath(const T *addr)
Definition: shared.h:156
ucommon::MappedPointer::paths
size_t paths
Definition: shared.h:134
ucommon::ConditionalLock
Definition: condition.h:508
ucommon::sharedref::operator*
typeref< T > operator*()
Definition: shared.h:90
ucommon::SharedRef::lock
Mutex lock
Definition: shared.h:63
ucommon
Definition: access.cpp:23
ucommon::mapped_pointer::mapped_pointer
mapped_pointer(size_t indexes=37, condlock_t *locking=NULL, size_t paging=0)
Definition: shared.h:211
ucommon::sharedref::operator=
sharedref & operator=(T obj)
Definition: shared.h:106
protocols.h
ucommon::MappedPointer::insert
void insert(const void *key, void *value, size_t path)
Definition: shared.cpp:119
object.h
ucommon::MappedPointer::list
LinkedObject ** list
Definition: shared.h:130
ucommon::ConditionalLock::commit
void commit(void)
Definition: condition.cpp:455
ucommon::MappedPointer::release
void release(void *obj)
Definition: shared.cpp:98
ucommon::sharedref::put
void put(typeref< T > &ptr)
Definition: shared.h:97
ucommon::MappedPointer::Index::value
void * value
Definition: shared.h:125
ucommon::mapped_keyequal< struct sockaddr >
bool mapped_keyequal< struct sockaddr >(const struct sockaddr *s1, const struct sockaddr *s2)
Definition: shared.h:200
ucommon::mapped_pointer::release
void release(V *object)
Definition: shared.h:213
ucommon::mapped_keyequal
bool mapped_keyequal(const T *key1, const T *key2)
Definition: shared.h:165
ucommon::MappedPointer
Definition: shared.h:113
ucommon::MappedPointer::replace
void replace(Index *ind, void *value)
Definition: shared.cpp:104
ucommon::MappedPointer::modify
LinkedObject * modify(size_t path)
Definition: shared.cpp:92
ucommon::Mutex::acquire
void acquire(void)
Definition: thread.h:486
ucommon::mapped_pointer
Definition: shared.h:208
__EXPORT
#define __EXPORT
Definition: config.h:49
ucommon::ConditionalLock::release
void release(void)
Definition: condition.cpp:468
thread.h
ucommon::Mutex::release
void release(void)
Definition: thread.h:507
ucommon::MappedPointer::access
LinkedObject * access(size_t path)
Definition: shared.cpp:86
ucommon::eq
bool eq(const struct sockaddr *s1, const struct sockaddr *s2)
Definition: socket.h:2100
ucommon::linked_pointer
Definition: linked.h:991
ucommon::MappedPointer::keypath
static size_t keypath(const uint8_t *addr, size_t size)
Definition: shared.cpp:133
ucommon::mapped_pointer::remove
void remove(const K *key)
Definition: shared.h:217
cpr.h
__DELETE_COPY
#define __DELETE_COPY(x)
Definition: platform.h:160
ucommon::LinkedObject
Definition: linked.h:55
ucommon::linked_pointer::next
void next(void)
Definition: linked.h:1106
ucommon::typeref
Definition: typeref.h:326
ucommon::mapped_pointer::get
V * get(const K *key)
Definition: shared.h:230
ucommon::memalloc
Definition: memory.h:61
ucommon::sharedref
Definition: shared.h:75
ucommon::TypeRef
Definition: typeref.h:68
ucommon::SharedRef::get
TypeRef get()
Definition: shared.cpp:32
atomic.h
ucommon::MappedPointer::remove
void remove(Index *ind, size_t path)
Definition: shared.cpp:110
ucommon::Socket::equal
static bool equal(const struct sockaddr *address1, const struct sockaddr *address2)
Definition: socket.cpp:3340
ucommon::sharedref::operator=
sharedref & operator=(typeref< T > ptr)
Definition: shared.h:101
ucommon::sharedref::__DELETE_COPY
__DELETE_COPY(sharedref)
ucommon::Socket::len
static socklen_t len(const struct sockaddr *address)
Definition: socket.cpp:3410
socket.h
ucommon::mapped_keyequal< char >
bool mapped_keyequal< char >(const char *k1, const char *k2)
Definition: shared.h:182
ucommon::MappedPointer::lock
condlock_t * lock
Definition: shared.h:128
ucommon::SharedRef::put
void put(TypeRef &object)
Definition: shared.cpp:52
ucommon::addr
const struct sockaddr * addr(Socket::address &address)
Definition: socket.h:2089
ucommon::Mutex
Definition: thread.h:459
ucommon::sharedref::sharedref
sharedref()
Definition: shared.h:81
ucommon::TypeRef::ref
Counted * ref
Definition: typeref.h:164
ucommon::MappedPointer::Index
Definition: shared.h:119
ucommon::is
bool is(T &object)
Definition: generics.h:292
typeref.h
ucommon::mapped_keypath< char >
size_t mapped_keypath< char >(const char *addr)
Definition: shared.h:173
ucommon::mapped_pointer::set
void set(const K *key, V *ptr)
Definition: shared.h:242