shared_object_internal.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_INTERNAL_HPP
27 #define CASADI_SHARED_OBJECT_INTERNAL_HPP
28 
29 #include "shared_object.hpp"
30 
31 #ifdef CASADI_WITH_THREAD
32 #include <atomic>
33 #endif // CASADI_WITH_THREAD
34 
35 namespace casadi {
36 
39  class CASADI_EXPORT SharedObjectInternal {
40  friend class SharedObject;
41  friend class Memory;
42  friend class UniversalNodeOwner;
43  public:
44 
46  SharedObjectInternal();
47 
49  SharedObjectInternal(const SharedObjectInternal& node);
50 
52  SharedObjectInternal& operator=(const SharedObjectInternal& node);
53 
55  virtual ~SharedObjectInternal() = 0;
56 
58  casadi_int getCount() const;
59 
61  virtual std::string class_name() const = 0;
62 
64  virtual void disp(std::ostream& stream, bool more) const = 0;
65 
69  WeakRef* weak();
70 
71  protected:
73  void initSingleton() {
74  casadi_assert_dev(count==0);
75  count++;
76  }
77 
79  void destroySingleton() {
80  count--;
81  }
82 
84  template<class B>
85  B shared_from_this();
86 
88  template<class B>
89  const B shared_from_this() const;
90 
91  private:
93 #ifdef CASADI_WITH_THREAD
94  std::atomic<casadi_int> count;
95 #else // CASADI_WITH_THREAD
96  casadi_int count;
97 #endif
99  WeakRef* weak_ref_;
100  };
101 
102  class CASADI_EXPORT WeakRefInternal : public SharedObjectInternal {
103  public:
104  // Constructor
105  WeakRefInternal(SharedObjectInternal* raw);
106 
107  // Destructor
108  ~WeakRefInternal() override;
109 
111  std::string class_name() const override {return "WeakRefInternal";}
112 
114  void disp(std::ostream& stream, bool more) const override;
115 
116  // Raw pointer to the cached object
117  SharedObjectInternal* raw_;
118  };
119 
120 
121  template<class A>
122  A getcopy(const A& a, std::map<SharedObjectInternal*, SharedObject>& already_copied) {
123  A ret;
124  if (!a.is_null()) {
125  std::map<SharedObjectInternal*, SharedObject>::iterator it =
126  already_copied.find(const_cast<SharedObjectInternal*>(a.get()));
127  if (it!=already_copied.end()) {
128  ret.own(it->second.get());
129  }
130  }
131  return ret;
132  }
133 
135  template<class B>
136  B SharedObjectInternal::shared_from_this() {
137  casadi_assert_dev(B::test_cast(this));
138  B ret;
139  ret.own(this);
140  return ret;
141  }
142 
143  template<class B>
144  const B SharedObjectInternal::shared_from_this() const {
145  casadi_assert_dev(B::test_cast(this));
146  B ret;
147  ret.own(const_cast<SharedObjectInternal*>(this));
148  return ret;
149  }
150 
152 
153 } // namespace casadi
154 
155 
156 #endif // CASADI_SHARED_OBJECT_INTERNAL_HPP
The casadi namespace.