replicode
base.h
Go to the documentation of this file.
1 /*
2  * HUMANOBS - mBrane
3  *
4  * Eric Nivel
5  * Center for Analysis and Design of Intelligent Agents
6  * Reykjavik University, Menntavegur 1, 101 Reykjavik, Iceland
7  * http://cadia.ru.is
8  * Copyright(c)2012
9  *
10  * This software was developed by the above copyright holder as part of
11  * the HUMANOBS EU research project, in collaboration with the
12  * following parties:
13  *
14  * Autonomous Systems Laboratory
15  * Technical University of Madrid, Spain
16  * http://www.aslab.org/
17  *
18  * Communicative Machines
19  * Edinburgh, United Kingdom
20  * http://www.cmlabs.com/
21  *
22  * Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
23  * University of Lugano and SUPSI, Switzerland
24  * http://www.idsia.ch/
25  *
26  * Institute of Cognitive Sciences and Technologies
27  * Consiglio Nazionale delle Ricerche, Italy
28  * http://www.istc.cnr.it/
29  *
30  * Dipartimento di Ingegneria Informatica
31  * University of Palermo, Italy
32  * http://roboticslab.dinfo.unipa.it/index.php/Main/HomePage
33  *
34  *
35  * --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, is permitted provided that the following conditions
39  * are met:
40  *
41  * - Redistributions of source code must retain the above copyright
42  * and collaboration notice, this list of conditions and the
43  * following disclaimer.
44  *
45  * - Redistributions in binary form must reproduce the above copyright
46  * notice, this list of conditions and the following
47  * disclaimer in the documentation and/or other materials provided
48  * with the distribution.
49  *
50  * - Neither the name of its copyright holders nor the names of its
51  * contributors may be used to endorse or promote products
52  * derived from this software without specific prior written permission.
53  *
54  * - CADIA Clause: The license granted in and to the software under this
55  * agreement is a limited-use license. The software may not be used in
56  * furtherance of:
57  * (i) intentionally causing bodily injury or severe emotional distress
58  * to any person;
59  * (ii) invading the personal privacy or violating the human rights of
60  * any person; or
61  * (iii) committing or preparing for any act of war.
62  *
63  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
64  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
65  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
66  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
67  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
68  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
69  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
70  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
71  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
72  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
73  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
74  */
75 
76 #ifndef core_base_h
77 #define core_base_h
78 
79 #include <cstdlib>
80 #include <atomic>
81 
82 #include "types.h"
83 
84 
85 namespace core
86 {
87 
88 class _Object;
89 
90 // Smart pointer (ref counting, deallocates when ref count<=0).
91 // No circular refs (use std c++ ptrs).
92 // No passing in functions (cast P<C> into C*).
93 // Cannot be a value returned by a function (return C* instead).
94 template<class C> class P
95 {
96 private:
98 public:
99  P();
100  P(C *o);
101  P(const P<C> &p);
102  ~P();
103  C *operator ->() const;
104  template<class D> operator D *() const
105  {
106  return (D *)object;
107  }
108  bool operator ==(C *c) const;
109  bool operator !=(C *c) const;
110  bool operator !() const;
111  template<class D> bool operator ==(P<D> &p) const;
112  template<class D> bool operator !=(P<D> &p) const;
113  P<C> &operator =(C *c);
114  P<C> &operator =(const P<C> &p);
115  template<class D> P<C> &operator =(const P<D> &p);
116 };
117 
119 
120 // Root smart-pointable object class.
121 class core_dll _Object
122 {
123  template<class C> friend class P;
124  friend class _P;
125 protected:
126 #ifdef ARCH_32
127  uint32_t __vfptr_padding_Object;
128 #endif
129  std::atomic_int refCount;
130  _Object();
131 public:
132  virtual ~_Object();
133  void incRef();
134  virtual void decRef();
135 };
136 
137 // Template version of the well-known DP. Adapts C to _Object.
138 template<class C> class _ObjectAdapter:
139  public C,
140  public _Object
141 {
142 protected:
143  _ObjectAdapter();
144 };
145 }
146 
147 
148 #include "base.tpl.cpp"
149 
150 
151 #endif
_ObjectAdapter()
Definition: base.tpl.cpp:163
Definition: base.h:138
Definition: base.h:121
Definition: base.cpp:81
P()
Definition: base.tpl.cpp:79
bool operator!() const
Definition: base.tpl.cpp:129
Definition: base.h:94
bool operator!=(C *c) const
Definition: base.tpl.cpp:114
C * operator->() const
Definition: base.tpl.cpp:104
bool operator==(C *c) const
Definition: base.tpl.cpp:109
_Object * object
Definition: base.h:97
std::atomic_int refCount
Definition: base.h:129
~P()
Definition: base.tpl.cpp:97
P< C > & operator=(C *c)
Definition: base.tpl.cpp:134