shared_object.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #ifndef CASADI_SHARED_OBJECT_HPP
27 #define CASADI_SHARED_OBJECT_HPP
28 
29 #include "casadi_common.hpp"
30 #include "exception.hpp"
31 #include <map>
32 #include <vector>
33 
34 namespace casadi {
35 
36  // Forward declaration of weak reference class
37  class WeakRef;
38 
40  // Forward declaration of internal classes
41  class SharedObjectInternal;
42  class WeakRefInternal;
44 
84  class CASADI_EXPORT SharedObject {
85 #ifndef SWIG
86  template<class B> friend B shared_cast(SharedObject& A);
87  template<class B> friend const B shared_cast(const SharedObject& A);
88 #endif // SWIG
89 
90  public:
91 #ifndef SWIG
93  SharedObject();
94 
96  SharedObject(const SharedObject& ref);
97 
99  ~SharedObject();
100 
102  SharedObject& operator=(const SharedObject& ref);
103 
106  void own(SharedObjectInternal* node);
107 
113  void assign(SharedObjectInternal* node);
114 
116  SharedObjectInternal* get() const;
117 
119  casadi_int getCount() const;
120 
122  void swap(SharedObject& other);
123 
125  SharedObjectInternal* operator->() const;
127 #endif // SWIG
128 
132  std::string class_name() const;
133 
135  void disp(std::ostream& stream, bool more=false) const;
136 
138  std::string get_str(bool more=false) const {
139  std::stringstream ss;
140  disp(ss, more);
141  return ss.str();
142  }
143 
146  void print_ptr(std::ostream &stream=casadi::uout()) const;
148 
150  bool is_null() const;
151 
157  casadi_int __hash__() const;
158 
160 #ifndef SWIG
164  WeakRef* weak();
165  protected:
166  void count_up(); // increase counter of the node
167  void count_down(); // decrease counter of the node
168  private:
169  SharedObjectInternal *node;
170 #endif // SWIG
172  };
173 
181  class CASADI_EXPORT WeakRef : public SharedObject {
182  public:
183  friend class SharedObjectInternal;
184 
188  WeakRef(int dummy=0);
189 
194 
199 
203  bool alive() const;
204 
208  WeakRefInternal* operator->();
209 
213  const WeakRefInternal* operator->() const;
214 
215 #ifndef SWIG
216  private:
220  explicit WeakRef(SharedObjectInternal* raw);
221 
225  void kill();
226 #endif // SWIG
227  };
228 
229 #ifndef SWIG
230 
236  template<class B>
237  B shared_cast(SharedObject& A) {
238 
240  SharedObjectInternal* ptr = A.get();
241 
243  B ret;
244 
246  if (!B::test_cast(ptr)) return ret;
247 
249  ret.own(ptr);
250  return ret;
251  }
252 
258  template<class B>
259  const B shared_cast(const SharedObject& A) {
260  SharedObject A_copy = A;
261  return shared_cast<B>(A_copy);
262  }
263 
264 #endif // SWIG
265 
266 
267 } // namespace casadi
268 
269 
270 #endif // CASADI_SHARED_OBJECT_HPP
SharedObject implements a reference counting framework similar for efficient and.
std::string class_name() const
Get class name.
casadi_int __hash__() const
Returns a number that is unique for a given Node.
void disp(std::ostream &stream, bool more=false) const
Print a description of the object.
bool is_null() const
Is a null pointer?
std::string get_str(bool more=false) const
Get string representation.
Weak reference type.
SharedObject shared()
Get a shared (owning) reference.
WeakRef(int dummy=0)
Default constructor.
const WeakRefInternal * operator->() const
Const access functions of the node.
WeakRef(SharedObject shared)
Construct from a shared object (also implicit type conversion)
WeakRefInternal * operator->()
Access functions of the node.
bool alive() const
Check if alive.
The casadi namespace.
CASADI_EXPORT std::ostream & uout()