SPHinXsys  alpha version
ownership.h
Go to the documentation of this file.
1 /* -------------------------------------------------------------------------*
2  * SPHinXsys *
3  * --------------------------------------------------------------------------*
4  * SPHinXsys (pronunciation: s'finksis) is an acronym from Smoothed Particle *
5  * Hydrodynamics for industrial compleX systems. It provides C++ APIs for *
6  * physical accurate simulation and aims to model coupled industrial dynamic *
7  * systems including fluid, solid, multi-body dynamics and beyond with SPH *
8  * (smoothed particle hydrodynamics), a meshless computational method using *
9  * particle discretization. *
10  * *
11  * SPHinXsys is partially funded by German Research Foundation *
12  * (Deutsche Forschungsgemeinschaft) DFG HU1527/6-1, HU1527/10-1 *
13  * and HU1527/12-1. *
14  * *
15  * Portions copyright (c) 2017-2020 Technical University of Munich and *
16  * the authors' affiliations. *
17  * *
18  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
19  * not use this file except in compliance with the License. You may obtain a *
20  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
21  * *
22  * --------------------------------------------------------------------------*/
40 #ifndef OWNERSHIP_H
41 #define OWNERSHIP_H
42 
43 #include "base_data_type.h"
44 
45 #include <typeinfo>
46 
47 namespace SPH
48 {
49  template <class CastingType, class OwnerType, class CastedType>
50  CastingType *DynamicCast(OwnerType *owner, CastedType *casted)
51  {
52  CastingType *tmp = dynamic_cast<CastingType *>(casted);
53  if (tmp == nullptr)
54  {
55  std::cout << "\n Error: pointer DynamicCasting " << typeid(*casted).name() << " leads to nullptr! \n";
56  std::cout << "\n This error locates in " << typeid(*owner).name() << '\n';
57  exit(1);
58  }
59  return tmp;
60  }
61 
62  template <class CastingType, class OwnerType, class CastedType>
63  CastingType &DynamicCast(OwnerType *owner, CastedType &casted)
64  {
65  CastingType *tmp = dynamic_cast<CastingType *>(&casted);
66  if (tmp == nullptr)
67  {
68  std::cout << "\n Error: reference DynamicCasting " << typeid(casted).name() << " leads to nullptr! \n";
69  std::cout << "\n This error locates in " << typeid(*owner).name() << '\n';
70  exit(1);
71  }
72  return *tmp;
73  }
74 
75  template <class T>
76  using UniquePtr = std::unique_ptr<T>;
77 
78  template <class T, typename... ConstructorArgs>
79  UniquePtr<T> makeUnique(ConstructorArgs &&...args)
80  {
81  return std::unique_ptr<T>(new T(std::forward<ConstructorArgs>(args)...));
82  }
83 
89  template <class BaseType>
91  {
92  public:
94  template <class DerivedType, typename... ConstructorArgs>
95  DerivedType *createPtr(ConstructorArgs &&...args)
96  {
97  ptr_member_.reset(new DerivedType(std::forward<ConstructorArgs>(args)...));
98  return static_cast<DerivedType *>(ptr_member_.get());
99  };
100 
102  template <class DerivedType, typename... ConstructorArgs>
103  DerivedType &createRef(ConstructorArgs &&...args)
104  {
105  ptr_member_.reset(new DerivedType(std::forward<ConstructorArgs>(args)...));
106  return *static_cast<DerivedType *>(ptr_member_.get());
107  };
108 
110  BaseType *movePtr(UniquePtr<BaseType> moved_unique_ptr)
111  {
112  ptr_member_ = std::move(moved_unique_ptr);
113  return ptr_member_.get();
114  };
115 
116  private:
117  UniquePtr<BaseType> ptr_member_;
118  };
119 
126  template <class BaseType>
128  {
129  public:
132  template <class DerivedType, typename... ConstructorArgs>
133  DerivedType *createPtr(ConstructorArgs &&...args)
134  {
135  ptr_keepers_.push_back(UniquePtrKeeper<BaseType>());
136  BaseType *observer = ptr_keepers_.back()
137  .template createPtr<DerivedType>(std::forward<ConstructorArgs>(args)...);
138  return static_cast<DerivedType *>(observer);
139  };
140 
141  UniquePtrKeeper<BaseType> &operator[](size_t index)
142  {
143  if (index < ptr_keepers_.size())
144  {
145  return ptr_keepers_[index];
146  }
147  std::cout << "\n Error in UniquePtrKeepers : UniquePtr index is out of bound! \n";
148  exit(1);
149  }
150 
151  private:
152  std::vector<UniquePtrKeeper<BaseType>> ptr_keepers_;
153  };
154 
155  template <class T>
156  using SharedPtr = std::shared_ptr<T>;
157 
158  template <class T, typename... ConstructorArgs>
159  SharedPtr<T> makeShared(ConstructorArgs &&...args)
160  {
161  return std::make_shared<T>(std::forward<ConstructorArgs>(args)...);
162  }
163 
169  template <class BaseType>
171  {
172  public:
174  template <class DerivedType, typename... ConstructorArgs>
175  DerivedType *resetPtr(ConstructorArgs &&...args)
176  {
177  ptr_member_ = makeShared<DerivedType>(std::forward<ConstructorArgs>(args)...);
178  return static_cast<DerivedType *>(ptr_member_.get());
179  };
180 
182  template <class DerivedType, typename... ConstructorArgs>
183  DerivedType &resetRef(ConstructorArgs &&...args)
184  {
185  return *resetPtr<DerivedType>(std::forward<ConstructorArgs>(args)...);
186  };
187 
189  BaseType *assignPtr(SharedPtr<BaseType> shared_ptr)
190  {
191  ptr_member_ = shared_ptr;
192  return ptr_member_.get();
193  };
194 
196  BaseType &assignRef(SharedPtr<BaseType> shared_ptr)
197  {
198  ptr_member_ = shared_ptr;
199  return *ptr_member_.get();
200  };
201 
202  private:
203  SharedPtr<BaseType> ptr_member_;
204  };
205 }
206 #endif // OWNERSHIP_H
DerivedType * resetPtr(ConstructorArgs &&...args)
Definition: ownership.h:175
DerivedType & resetRef(ConstructorArgs &&...args)
Definition: ownership.h:183
A wrapper to provide an ownership for a new derived object which previous often generated by new a ra...
Definition: ownership.h:90
DerivedType * createPtr(ConstructorArgs &&...args)
Definition: ownership.h:95
BaseType * assignPtr(SharedPtr< BaseType > shared_ptr)
Definition: ownership.h:189
BaseType * movePtr(UniquePtr< BaseType > moved_unique_ptr)
Definition: ownership.h:110
DerivedType & createRef(ConstructorArgs &&...args)
Definition: ownership.h:103
A wrapper to provide an ownership for a vector of base class pointers which point to derived objects...
Definition: ownership.h:127
A wrapper to provide an shared ownership for a new derived object which previous often generated by n...
Definition: ownership.h:170
BaseType & assignRef(SharedPtr< BaseType > shared_ptr)
Definition: ownership.h:196
DerivedType * createPtr(ConstructorArgs &&...args)
Definition: ownership.h:133
Definition: solid_body_supplementary.cpp:9