libcsdbg  1.28
C++ exception (and generic) stack trace debug library
node.hpp
Go to the documentation of this file.
1 #ifndef _CSDBG_NODE
2 #define _CSDBG_NODE 1
3 
10 #include "./object.hpp"
11 
12 namespace csdbg {
13 
35 template <class T>
36 class node: virtual public object
37 {
38 protected:
39 
40  /* Protected variables */
41 
44  T *m_data;
47  /* Protected constructors, copy constructors and destructor */
48 
49  explicit node(T*);
50 
51  node(const node&);
52 
53  virtual ~node();
54 
55  virtual node* clone() const;
56 
57 
58  /* Protected accessor methods */
59 
60  virtual node* link(const node* = NULL) const;
61 
62  virtual node& link_to(const node*);
63 
64  virtual node& unlink_from(const node*);
65 
66  virtual T* detach();
67 
68 
69  /* Protected operator overloading methods */
70 
71  virtual node& operator=(const node&);
72 
73  virtual node* operator^(const node&) const;
74 
75 
76  /* Friend classes and functions */
77 
78  template <class F> friend class chain;
79 
80  template <class F> friend class stack;
81 };
82 
83 
91 template <class T>
92 inline node<T>::node(T *d):
93 m_link(NULL),
94 m_data(d)
95 {
96 }
97 
98 
108 template <class T>
109 inline node<T>::node(const node &src):
110 m_link(NULL),
111 m_data(NULL)
112 {
113  if ( likely(src.m_data != NULL) )
114  m_data = new T(*src.m_data);
115 }
116 
117 
125 template <class T>
127 {
128  delete m_data;
129  m_data = NULL;
130  m_link = NULL;
131 }
132 
133 
141 template <class T>
142 inline node<T>* node<T>::clone() const
143 {
144  return new node(*this);
145 }
146 
147 
155 template <class T>
156 inline node<T>* node<T>::link(const node<T> *prev) const
157 {
158  mem_addr_t addr = reinterpret_cast<mem_addr_t> (m_link);
159  mem_addr_t mask = reinterpret_cast<mem_addr_t> (prev);
160  return reinterpret_cast<node<T>*> (addr ^ mask);
161 }
162 
163 
171 template <class T>
173 {
174  m_link = link(n);
175  return *this;
176 }
177 
178 
186 template <class T>
188 {
189  m_link = link(n);
190  return *this;
191 }
192 
193 
199 template <class T>
200 inline T* node<T>::detach()
201 {
202  T *retval = m_data;
203  m_data = NULL;
204  return retval;
205 }
206 
207 
219 template <class T>
221 {
222  if ( unlikely(this == &rval) )
223  return *this;
224 
225  T *data = rval.m_data;
226  if ( unlikely(data == NULL) ) {
227  delete m_data;
228  m_data = NULL;
229  }
230 
231  else if ( unlikely(m_data == NULL) )
232  m_data = new T(*data);
233 
234  else
235  *m_data = *data;
236 
237  return *this;
238 }
239 
240 
248 template <class T>
249 inline node<T>* node<T>::operator^(const node<T> &prev) const
250 {
251  return link(&prev);
252 }
253 
254 }
255 
256 #endif
257 
This abstract class serves as the root of the class hierarchy tree.
Definition: object.hpp:17
T * m_data
Node data.
Definition: node.hpp:44
Lightweight, templated, doubly-linked list (using XOR linking)
Definition: chain.hpp:33
virtual node * operator^(const node &) const
Get the next node (XOR linking)
Definition: node.hpp:249
Lightweight, templated, singly-linked LIFO queue (stack)
Definition: stack.hpp:28
Class csdbg::object definition.
virtual node * link(const node *=NULL) const
Get the next node (using direct or XOR linking)
Definition: node.hpp:156
#define likely(expr)
Offer a hint (positive) to the pipeline branch predictor.
Definition: config.hpp:344
A node in a templated chain (doubly-linked list) or stack (singly-linked LIFO queue) ...
Definition: node.hpp:36
virtual node & unlink_from(const node *)
Unlink from a node (for XOR linking)
Definition: node.hpp:187
virtual T * detach()
Detach the data pointer from the node.
Definition: node.hpp:200
virtual node & operator=(const node &)
Assignment operator.
Definition: node.hpp:220
virtual node & link_to(const node *)
Link with a node (for XOR linking)
Definition: node.hpp:172
node(T *)
Object constructor.
Definition: node.hpp:92
virtual ~node()
Object destructor.
Definition: node.hpp:126
virtual node * clone() const
Object virtual copy constructor.
Definition: node.hpp:142
unsigned long long mem_addr_t
64-bit memory address
Definition: config.hpp:120
#define unlikely(expr)
Offer a hint (negative) to the pipeline branch predictor.
Definition: config.hpp:349
node * m_link
Next node link (direct or XOR link)
Definition: node.hpp:42