GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
SmartPtr.hpp
Go to the documentation of this file.
1 // This file is part of GranOO, a workbench for DEM simulation.
2 //
3 // Author(s) : - Damien Andre IRCER/UNILIM, Limoges France
4 // <damien.andre@unilim.fr>
5 // - Jean-luc Charles Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
6 // <jean-luc.charles@ensam.eu>
7 // - Jeremie Girardot Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
8 // <jeremie.girardot@ensam.eu>
9 // - Cedric Hubert LAMIH/UPHF, Valenciennes France
10 // <cedric.hubert@uphf.fr>
11 // - Ivan Iordanoff Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
12 // <ivan.iordanoff@ensam.eu>
13 //
14 // Copyright (C) 2008-2019 D. Andre, JL. Charles, J. Girardot, C. Hubert, I. Iordanoff
15 //
16 // This program is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 // This program is distributed in the hope that it will be useful,
22 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 // GNU General Public License for more details.
25 //
26 // You should have received a copy of the GNU General Public License
27 // along with this program. If not, see <http://www.gnu.org/licenses/>.
28 
29 
30 #ifndef _libDEM_SmartPtr_hpp_
31 #define _libDEM_SmartPtr_hpp_
32 
33 #include <set>
34 
35 #include "GranOO3/Core/Macro.hpp"
36 
37 namespace GranOO3
38 {
39 
40  namespace Core
41  {
42 
43  template<typename type> class SmartPtr;
44 
45  template<typename type>
47  {
48  public:
50  virtual ~SmartPtrClass();
51 
54 
55  private:
56  std::set<SmartPtr<type>*> allPtr_;
57  };
58 
59 
60  template<typename type>
61  class SmartPtr
62  {
63  friend class SmartPtrClass<type>;
64 
65  public:
67  SmartPtr(type*);
69 
71 
73  void attach_ptr(type*);
74  void delete_ptr();
75 
76  private:
77  type* ptr_;
78  };
79 
80 
81  template<typename type>
83  : allPtr_() {
84  }
85 
86  template<typename type>
88  for (auto pt: allPtr_)
89  pt->ptr_ = nullptr;
90  }
91 
92  template<typename type>
93  void
95  SafeModeAssert(allPtr_.count(pt)==0, "");
96  allPtr_.insert(pt);
97  }
98 
99  template<typename type>
100  void
102  SafeModeAssert(allPtr_.count(pt)==1, "");
103  allPtr_.erase(pt);
104  }
105 
106  template<typename type>
108  : ptr_(nullptr) {
109  }
110 
111  template<typename type>
113  : ptr_(nullptr) {
114  attach_ptr(pt);
115  }
116 
117  template<typename type>
119  : ptr_(nullptr) {
120  if (pt.ptr_ != nullptr)
121  attach_ptr(pt.ptr_);
122  }
123 
124  template<typename type>
126  if (ptr_ != nullptr)
127  static_cast<SmartPtrClass<type>*>(ptr_)->erase_ptr(this);
128 
129  }
130 
131  template<typename type>
132  void
134  ptr_ = pt;
135  static_cast<SmartPtrClass<type>*>(ptr_)->add_ptr(this);
136  }
137 
138  template<typename type>
139  void
141  if (ptr_ != nullptr) {
142  static_cast<SmartPtrClass<type>*>(ptr_)->erase_ptr(this);
143  delete ptr_;
144  ptr_ = nullptr;
145  }
146  }
147 
148  }
149 }
150 
151 
152 
153 
154 #endif
#define SafeModeAssert(condition, message)
Definition: Macro.hpp:47
Definition: SmartPtr.hpp:47
std::set< SmartPtr< type > * > allPtr_
Definition: SmartPtr.hpp:56
void add_ptr(SmartPtr< type > *)
Definition: SmartPtr.hpp:94
SmartPtrClass()
Definition: SmartPtr.hpp:82
virtual ~SmartPtrClass()
Definition: SmartPtr.hpp:87
void erase_ptr(SmartPtr< type > *)
Definition: SmartPtr.hpp:101
Definition: SmartPtr.hpp:62
SmartPtr()
Definition: SmartPtr.hpp:107
SmartPtr< type > & operator=(const SmartPtr< type > &)=delete
SmartPtr(const SmartPtr< type > &)
Definition: SmartPtr.hpp:118
void delete_ptr()
Definition: SmartPtr.hpp:140
void attach_ptr(type *)
Definition: SmartPtr.hpp:133
type * ptr_
Definition: SmartPtr.hpp:77
SmartPtr(type *)
Definition: SmartPtr.hpp:112
~SmartPtr()
Definition: SmartPtr.hpp:125
Definition: Common.hpp:198