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 "generic_shared_internal.hpp"
30 
31 namespace casadi {
32 
34  // Forward declaration of internal classes
35  class SharedObjectInternal;
36  class WeakRefInternal;
38 
78  class CASADI_EXPORT SharedObject :
79  public GenericShared<SharedObject, SharedObjectInternal> {
80  public:
81 
85  std::string class_name() const;
86 
88  void disp(std::ostream& stream, bool more=false) const;
89 
91  std::string get_str(bool more=false) const {
92  std::stringstream ss;
93  disp(ss, more);
94  return ss.str();
95  }
96 
99  void print_ptr(std::ostream &stream=casadi::uout()) const;
101 
102  using internal_base_type = SharedObjectInternal;
104 
105  };
106 
114  class CASADI_EXPORT WeakRef :
115  public GenericWeakRef<SharedObject, SharedObjectInternal> {
116  public:
117  WeakRef(int dummy=0) : GenericWeakRef<SharedObject, SharedObjectInternal>(dummy) {
118  }
119  WeakRef(SharedObject shared) : GenericWeakRef<SharedObject, SharedObjectInternal>(shared) {
120  }
121  /*private:
122  explicit WeakRef(SharedObjectInternal* raw) : GenericWeakRef<SharedObject, SharedObjectInternal>(raw) {
123  };*/
124  };
125 
126 #ifndef SWIG
127  class CASADI_EXPORT SharedObjectInternal :
128  public GenericSharedInternal<SharedObject, SharedObjectInternal> {
129  friend class GenericShared<SharedObject, SharedObjectInternal>;
130  friend class SharedObject;
131  friend class GenericWeakRef<SharedObject, SharedObjectInternal>;
132  friend class GenericSharedInternal<SharedObject, SharedObjectInternal>;
133  friend class Memory;
134  friend class UniversalNodeOwner;
135  public:
139  SharedObjectInternal() : GenericSharedInternal<SharedObject, SharedObjectInternal>() {
140  }
142  SharedObjectInternal(const SharedObjectInternal& node) :
143  GenericSharedInternal<SharedObject, SharedObjectInternal>(node) {
144  }
145 
147  virtual std::string class_name() const = 0;
148 
150  virtual void disp(std::ostream& stream, bool more) const = 0;
151 
152  using weak_ref_type = WeakRefInternal;
153 
154  private:
156 #ifdef CASADI_WITH_THREAD
157  std::atomic<casadi_int> count;
158 #else // CASADI_WITH_THREAD
159  casadi_int count;
160 #endif
161 
162  };
163 
164  class CASADI_EXPORT WeakRefInternal :
165  public GenericWeakRefInternal<SharedObject, SharedObjectInternal> {
166  public:
168  void disp(std::ostream& stream, bool more) const override;
169 
171  std::string class_name() const override {return "WeakRefInternal";}
172 
173  using GenericWeakRefInternal<SharedObject, SharedObjectInternal>::GenericWeakRefInternal;
174 
175  };
176 
177 
178 #endif // SWIG
179 
180 } // namespace casadi
181 
182 #endif // CASADI_SHARED_OBJECT_HPP
GenericShared implements a reference counting framework similar for efficient and.
std::string class_name() const
Get class name.
void disp(std::ostream &stream, bool more=false) const
Print a description of the object.
std::string get_str(bool more=false) const
Get string representation.
SharedObjectInternal internal_base_type
Weak reference type.
WeakRef(int dummy=0)
WeakRef(SharedObject shared)
The casadi namespace.
Definition: archiver.hpp:32
CASADI_EXPORT std::ostream & uout()