shared_object.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010 by Joel Andersson, Moritz Diehl, K.U.Leuven. All rights reserved.
6  *
7  * CasADi is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 3 of the License, or (at your option) any later version.
11  *
12  * CasADi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with CasADi; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  */
22 
23 #ifndef SHARED_OBJECT_HPP
24 #define SHARED_OBJECT_HPP
25 
26 #include "printable_object.hpp"
27 #include "casadi_exception.hpp"
28 #include <map>
29 #include <vector>
30 
31 namespace CasADi{
32 
33 // Forward declaration of internal class
34 class SharedObjectNode;
35 
67 #ifndef SWIG
68  template<class B> friend B shared_cast(SharedObject& A);
69  template<class B> friend const B shared_cast(const SharedObject& A);
70 #endif // SWIG
71 
72  public:
73 #ifndef SWIG
74  SharedObject();
76 
78  SharedObject(const SharedObject& ref);
79 
81  SharedObject clone() const;
82 
84  ~SharedObject();
85 
87  SharedObject& operator=(const SharedObject& ref);
88 
91 
94 
96  const SharedObjectNode* get() const;
97 
99  SharedObjectNode* get();
100 
102  int getCount() const;
103 
105  void swap(SharedObject& other);
106 
109 
111  const SharedObjectNode* operator->() const;
112 
114  virtual void repr(std::ostream &stream) const;
115 
117  virtual void print(std::ostream &stream=std::cout) const;
118 
119  #endif // SWIG
120 
122  void init();
123 
125  bool isInit() const;
126 
128  void assertInit() const;
129 
131  bool isNull() const;
132 
134  virtual bool checkNode() const;
135 
137  void makeUnique(bool clone_members=true);
139 #ifndef SWIG
140  void makeUnique(std::map<SharedObjectNode*,SharedObject>& already_copied, bool clone_members=true);
142 
143  private:
145  void count_up(); // increase counter of the node
146  void count_down(); // decrease counter of the node
147 #endif // SWIG
148 };
149 
150 #ifndef SWIG
151 
154  friend class SharedObject;
155  public:
156 
159 
162 
165 
167  virtual ~SharedObjectNode() = 0;
168 
170  virtual SharedObjectNode* clone() const=0;
171 
173  virtual void deepCopyMembers(std::map<SharedObjectNode*,SharedObject>& already_copied);
174 
176  int getCount() const;
177 
179  virtual void init();
180 
182  bool isInit() const;
183 
185  void assertInit() const;
186 
188  virtual void repr(std::ostream &stream) const;
189 
191  virtual void print(std::ostream &stream) const;
192 
193  protected:
195  template<class B>
196  B shared_from_this();
197 
199  template<class B>
200  const B shared_from_this() const;
201 
203  bool is_init_;
204 
205  private:
207  unsigned int count;
208 };
209 
211 template<class B>
213 
215  SharedObjectNode* ptr = A.get();
216 
218  B ret;
219 
221  ret.assignNode(ptr);
222 
224  if(ptr && !ret.checkNode())
225  ret.assignNode(0);
226 
227  return ret;
228 }
229 
231 template<class B>
232 const B shared_cast(const SharedObject& A){
233  SharedObject A_copy = A;
234  return shared_cast<B>(A_copy);
235 }
236 
238 template<class B>
239 bool is_a(const SharedObject& A){
240  casadi_assert(!A.isNull());
241  return !shared_cast<B>(A).isNull();
242 }
243 
245 template<class A>
247 A deepcopy(const A& a){
248  A ret = a;
249  ret.makeUnique();
250  return ret;
251 }
252 
253 template<class A>
254 A deepcopy(const A& a, std::map<SharedObjectNode*,SharedObject>& already_copied){
255  A ret = a;
256  ret.makeUnique(already_copied);
257  return ret;
258 }
259 
260 template<class A>
261 std::vector<A> deepcopy(const std::vector<A>& a, std::map<SharedObjectNode*,SharedObject>& already_copied){
262  std::vector<A> ret = a;
263  for(typename std::vector<A>::iterator it=ret.begin(); it!=ret.end(); ++it){
264  it->makeUnique(already_copied);
265  }
266  return ret;
267 }
269 
270 // Template function implementations
271 template<class B>
273  B ret;
274  ret.assignNode(this);
275 
276  // Assert that the object is valid
277  casadi_assert(ret.checkNode());
278 
279  return ret;
280 }
281 
282 template<class B>
284  B ret;
285  ret.assignNode(const_cast<SharedObjectNode*>(this));
286 
287  // Assert that the object is valid
288  casadi_assert(ret.checkNode());
289 
290  return ret;
291 }
292 
293 
294 #endif // SWIG
295 
296 
297 } // namespace CasADi
298 
299 
300 #endif // SHARED_OBJECT_HPP
301 
Internal class for the reference counting framework, see comments on the public class.
void assignNode(SharedObjectNode *node)
Assign the node to a node class pointer (or null)
bool isInit() const
Is initialized?
#define casadi_assert(x)
void assertInit() const
Assert that it is initialized.
SharedObject()
Default constructor.
void makeUnique(bool clone_members=true)
If there are other references to the object, then make a deep copy of it and point to this new object...
B shared_from_this()
Get a shared object from the current internal object.
SharedObject implements a reference counting framework simular for effient and easily-maintained memo...
const SharedObjectNode * get() const
Get a const pointer to the node.
bool is_a(const SharedObject &A)
Check if a shared object is of a certain type.
unsigned int count
Number of references pointing to the object.
virtual bool checkNode() const
Assert that the node is pointing to the right type of object.
SharedObject & operator=(const SharedObject &ref)
Assignment operator.
void assignNodeNoCount(SharedObjectNode *node)
Assign the node to a node class pointer without reference counting: inproper use will cause memory le...
friend B shared_cast(SharedObject &A)
Typecast a shared object to a base class to a shared object to a derived class, cf. dynamic_cast.
int getCount() const
Get the reference count.
SharedObjectNode * operator->()
Access a member function or object.
Base class for objects that have a natural string representation.
bool isNull() const
Is a null pointer?
void swap(SharedObject &other)
Swap content with another instance.
A deepcopy(const A &a)
Make a deep copy of an object (Note: default is a shallow copy!)
SharedObject clone() const
Deep copy.
void init()
Initialize the object: more documentation in the node class (SharedObjectNode and derived classes) ...
SharedObjectNode * node
virtual void repr(std::ostream &stream) const
Print a representation of the object.
bool is_init_
Has the function been initialized?
~SharedObject()
Destructor.
virtual void print(std::ostream &stream=std::cout) const
Print a destription of the object.


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:35:04