ref.h
Go to the documentation of this file.
1 /*
2  @copyright Russell Standish 2000-2013
3  @author Russell Standish
4  This file is part of Classdesc
5 
6  Open source licensed under the MIT license. See LICENSE for details.
7 */
8 
13 #ifndef REF_H
14 #define REF_H
15 #include "pack_base.h"
16 #include "pack_graph.h"
17 #include "function.h"
18 #include <algorithm>
19 
20 namespace classdesc
21 {
22 
26  template <class T>
27  class ref
28  {
29  struct Payload: public T
30  {
31  int count;
32  Payload(): count(1) {}
33  Payload(const T& x): T(x), count(1) {}
34  };
35  Payload* item;
36  void refdec() {if (item){item->count--; if (item->count==0) delete item;}}
37  void asg(const ref& x)
38  {
39  if (x.item)
40  {item=x.item; item->count++;}
41  else
42  item=NULL;
43  }
44  void newitem() {item=new Payload;}
45  void newitem(const T& x) {item=new Payload(x);}
46  template <class U> bool operator==(const U&) const;
47  public:
48  ref(): item(NULL) {} /* unitialised refs don't consume space */
49  ref(const ref& x) {asg(x);}
50  ref(const T& x) {newitem(x);}
51  ref& operator=(const ref& x) { if (x.item!=item) {refdec(); asg(x);} return *this;}
52  ref& operator=(const T& x) {refdec(); newitem(x); return *this;}
53  ~ref() {refdec();}
55  T* operator->() {if (!item) newitem(); return (T*)item;}
56  template <class M>
57  functional::bound_method<T,M> operator->*(M& m) {
58  return functional::bound_method<T,M>(**this, m);
59  }
60  template <class M>
61  functional::bound_method<T,M> operator->*(M& m) const {
62  return functional::bound_method<T,M>(**this, m);
63  }
65  T& operator*() {if (!item) newitem(); return *(T*)item;}
67  const T* operator->() const {assert(item); return (T*)item;}
69  const T& operator*() const {assert(item); return *(T*)item;}
71  void nullify() { refdec(); item=NULL;}
73  bool nullref() const { return item==NULL;}
75  operator bool () const {return !nullref();}
77  int refCount() const {if (item) {return item->count;} else return 0;}
78  bool operator==(const ref& x) const {return x.item==item;}
79  bool operator==(const T* x) const {return x==item;}
80  bool operator==(const T& x) const {return x==*item;}
81  template <class U>
82  bool operator!=(const U& x) const {return !operator==(x);}
83  /* used for maps and sets - needed for unpack */
84  bool operator<(const ref& x) const {return item<x.item;}
85  void swap(ref& x) {Payload*tmp=x.item; x.item=item; item=tmp;}
86  };
87 
91  template <class T>
92  struct Alloc<classdesc::ref<T> >
93  {
94  /* operator*() creates new space, if x is NULL */
95  /* there is no need to store references into alloced, as ref
96  will handle its own cleanup */
97  void operator()(pack_t& buf, ref<T>& x) {x=T();}
98  };
99 
100 }
101 
102 #ifdef _CLASSDESC
103 #pragma omit pack classdesc::ref
104 #pragma omit unpack classdesc::ref
105 #pragma omit isa classdesc::ref
106 #endif
107 
108 namespace classdesc_access
109 {
110  template <class T>
111  struct access_pack<classdesc::ref<T> >
112  {
113  template <class U>
114  void operator()(classdesc::pack_t& t, const classdesc::string& d, U& a)
115  {pack_graph(t,a);}
116  };
117 
118  template <class T>
119  struct access_unpack<classdesc::ref<T> >
120  {
121  template <class U>
122  void operator()(classdesc::pack_t& t, const classdesc::string& d, U& a)
123  {unpack_graph(t,a);}
124  };
125 }
126 
127 namespace std
128 {
129  template <class T> void swap(classdesc::ref<T>& x,classdesc::ref<T>& y) {x.swap(y);}
130 }
131 
132 #endif
Metaprogramming support for processing functions of multiple arguments.
const T * operator->() const
dereference - throws in debug mode if null
Definition: ref.h:67
Definition: ref.h:27
Definition: graph.h:537
serialisation descriptor
const T & operator*() const
dereference - throws in debug mode if null
Definition: ref.h:69
T * operator->()
dereference - creates default object if null
Definition: ref.h:55
Definition: function.h:70
class to allow access to private members
Definition: classdesc_access.h:21
bool nullref() const
true if reference is null
Definition: ref.h:73
T & operator*()
dereference - creates default object if null
Definition: ref.h:65
void nullify()
make reference null
Definition: ref.h:71
class to allow access to private members
Definition: classdesc_access.h:22
int refCount() const
return the payloads reference count
Definition: ref.h:77
Contains definitions related to classdesc functionality.
Definition: arrays.h:2514
Definition: pack_base.h:124
serialisation for dynamic structures (graphs/trees and so on)
Contains access_* structs, and nothing else. These structs are used to gain access to private members...
Definition: accessor.h:55
Definition: pack_graph.h:73