25 #ifndef _UCOMMON_MAPREF_H_ 26 #define _UCOMMON_MAPREF_H_ 28 #ifndef _UCOMMON_CPR_H_ 32 #ifndef _UCOMMON_ATOMIC_H_ 36 #ifndef _UCOMMON_PROTOCOLS_H_ 40 #ifndef _UCOMMON_OBJECT_H_ 44 #ifndef _UCOMMON_TYPEREF_H_ 48 #ifndef _UCOMMON_LINKED_H_ 52 #ifndef _UCOMMON_THREAD_H_ 58 class __EXPORT MapRef :
public TypeRef
64 class __EXPORT Index :
public LinkedObject
72 explicit Index(LinkedObject **origin);
79 class __EXPORT Map :
public Counted
85 friend class Instance;
87 virtual void dealloc() __OVERRIDE;
94 LinkedObject *free, *last;
97 explicit Map(
void *
addr,
size_t indexes,
size_t paging = 0);
99 inline LinkedObject **get(
void) {
100 return reinterpret_cast<LinkedObject **>(((caddr_t)(
this)) +
sizeof(Map));
103 Index *create(
size_t path);
107 void remove(Index *index,
size_t path);
109 LinkedObject *modify(
size_t key = 0);
111 LinkedObject *access(
size_t key = 0);
114 class __EXPORT Instance
123 Instance(MapRef& from);
125 explicit Instance(Map *map);
127 Instance(
const Instance&
copy);
129 void assign(
const Instance&
copy);
131 void assign(MapRef& from);
150 inline operator bool() {
151 return index != NULL;
154 inline bool operator!() {
155 return index == NULL;
159 MapRef(
size_t paths,
size_t paging = 0);
160 MapRef(
const MapRef&
copy);
163 void assign(TypeRef& key, TypeRef& value);
165 static Map *create(
size_t paths,
size_t paging = 0);
167 linked_pointer<Index> access(
size_t keyvalue = 0);
169 linked_pointer<Index> modify(
size_t keyvalue = 0);
171 void append(TypeRef& value);
173 void add(
size_t path, TypeRef& key, TypeRef& value);
175 void update(Index *ind, TypeRef& value);
177 void remove(Index *ind,
size_t path = 0);
190 static size_t index(
size_t& key,
const uint8_t *
addr,
size_t len);
194 inline size_t mapkeypath(typeref<T>&
object)
196 size_t path =
sizeof(T);
197 return MapRef::index(path, (
const uint8_t *)(
object()),
sizeof(T));
201 inline size_t mapkeypath<const char *>(typeref<const char *>&
object)
204 return MapRef::index(path, (
const uint8_t *)(*
object),
object.len());
208 inline size_t mapkeypath<const uint8_t *>(typeref<const uint8_t *>&
object)
210 size_t path =
object.size();
211 return MapRef::index(path, *
object,
object.size());
214 template<
typename K,
typename V>
215 class mapref :
public MapRef
218 bool erase(typeref<K>& key) {
219 size_t path = mapkeypath<K>(key);
220 linked_pointer<Index> ip = modify(path);
222 typeref<K> kv(ip->key);
223 if(
is(kv) && kv == key) {
224 MapRef::remove(*ip, path);
235 class instance :
public MapRef::Instance
238 inline instance(
const instance&
copy) : Instance(static_cast<const Instance&>(
copy)) {};
240 inline instance(mapref& from) : Instance(static_cast<MapRef&>(from)) {};
242 inline instance() : Instance() {};
244 inline typeref<K> key() {
245 return typeref<K>(Instance::key());
248 inline typeref<V> value() {
249 return typeref<V>(Instance::value());
252 inline instance& operator++() {
257 inline instance& operator=(
const instance&
copy) {
258 assign(static_cast<const Instance&>(
copy));
262 inline instance& operator=(mapref& from) {
263 assign(static_cast<MapRef&>(from));
268 inline mapref(
const mapref&
copy) : MapRef(
copy) {};
270 inline mapref(
size_t paths = 37,
size_t paging = 0) : MapRef(paths, paging) {};
272 inline mapref& operator=(
const mapref&
copy) {
277 inline instance operator*() {
278 return instance(
this);
281 void value(typeref<K>& key, typeref<V>& val) {
282 size_t path = mapkeypath<K>(key);
283 linked_pointer<Index> ip = modify(path);
285 typeref<K> kv(ip->key);
286 if(
is(kv) && kv == key) {
297 typeref<V> at(typeref<K>& key) {
298 linked_pointer<Index> ip = access(mapkeypath<K>(key));
300 typeref<K> kv(ip->key);
301 if(
is(kv) && kv == key) {
302 typeref<V> result(ip->value);
312 typeref<V> take(typeref<K>& key) {
313 size_t path = mapkeypath<K>(key);
314 linked_pointer<Index> ip = modify(path);
316 typeref<K> kv(ip->key);
317 if(
is(kv) && kv == key) {
318 typeref<V> result(ip->value);
320 MapRef::remove(*ip, path);
330 inline bool remove(typeref<K>& key) {
334 inline bool remove(K k) {
339 inline typeref<V> operator()(typeref<K>& key) {
343 inline typeref<V> operator()(K k) {
348 inline void operator()(typeref<K>& key, typeref<V>& val) {
352 inline void operator()(K k, V v) {
360 class listref :
public MapRef
363 bool erase(typeref<T>& value) {
364 linked_pointer<Index> ip = modify();
366 typeref<T> kv(ip->value);
367 if(
is(kv) && kv == value) {
379 class instance :
public MapRef::Instance
382 inline instance(
const instance&
copy) : Instance(static_cast<const Instance&>(
copy)) {};
384 inline instance(listref& from) : Instance(static_cast<MapRef&>(from)) {};
386 inline instance() : Instance() {};
388 inline const T& operator*() {
389 return *(Instance::value());
392 inline const T* operator->() {
393 return Instance::value();
396 inline instance& operator++() {
401 inline instance& operator=(
const instance&
copy) {
402 assign(static_cast<const Instance&>(
copy));
406 inline instance& operator=(listref& from) {
407 assign(static_cast<MapRef&>(from));
412 inline listref(
const listref&
copy) : MapRef(
copy) {};
414 inline listref(
size_t paging = 0) : MapRef(1, paging) {};
416 inline listref& operator=(
const listref&
copy) {
421 inline instance operator*() {
422 return instance(
this);
425 inline listref& operator<<(typeref<T>& value) {
430 inline listref& operator<<(T t) {
436 inline bool remove(typeref<T>& key) {
440 inline bool remove(T t) {
445 inline typeref<T> take(
size_t offset) {
446 linked_pointer<Index> ip = modify();
447 while(
is(ip) && offset--) {
450 typeref<T> v(ip->value);
457 inline typeref<T> at(
size_t offset) {
458 linked_pointer<Index> ip = access();
459 while(ip && offset--) {
462 typeref<T> v(ip->value);
467 inline typeref<T> operator[](
size_t offset) {
Linked objects, lists, templates, and containers.
void set(Counted *object)
Set our smart pointer to a specific heap container.
A common object base class with auto-pointer support.
bool is(T &object)
Convenience function to validate object assuming it is castable to bool.
const struct sockaddr * addr(Socket::address &address)
A convenience function to convert a socket address list into a socket address.
Atomic pointers and locks.
Thread classes and sychronization objects.
ConditionalLock condlock_t
Convenience type for using conditional locks.
Common namespace for all ucommon objects.
T copy(const T &src)
Convenience function to copy objects.
Abstract interfaces and support.
A thread-safe atomic heap management system.