iMSTK
Interactive Medical Simulation Toolkit
imstkNew.h
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #pragma once
8 
9 #include <memory>
10 
11 namespace imstk
12 {
13 //template<typename T>
14 //using imstkSmartPtr = std::shared_ptr<T>;
15 //
16 //template<typename T>
17 //using imstkWeakPtr = std::weak_ptr<T>;
18 //
19 //template<typename T>
20 //using imstkUniquePtr = std::unique_ptr<T>;
21 
28 template<class T>
29 class imstkNew
30 {
31 public:
32  template<typename U>
33  friend class imstkNew;
34 
35 public:
36  imstkNew() : object(std::make_shared<T>()) { }
37 
38  template<typename ... Args>
39  explicit imstkNew(Args&& ... args) : object(std::make_shared<T>(std::forward<Args>(args) ...)) { }
40 
41  // Move, reference to old is removed
42  imstkNew(imstkNew&& obj) : object(obj.object) { obj.object = nullptr; }
43 
44 public:
45  T* operator->() const { return object.get(); }
46  T& operator*() const { return *object.get(); }
47 
51  T* getPtr() const { return object.get(); }
52 
56  const std::shared_ptr<T>& get() const { return object; }
57 
61  operator std::shared_ptr<T>() const { return object; }
62 
66  operator std::weak_ptr<T>() const { return object; }
67 
72  template<typename U>
73  operator std::shared_ptr<U>() const
74  {
75  static_assert(std::is_base_of<U, T>::value, "Argument U type not compatible with imstkNew<T>'s T");
76  return std::dynamic_pointer_cast<U>(object);
77  }
78 
83  template<typename U>
84  operator std::weak_ptr<U>() const
85  {
86  static_assert(std::is_base_of<U, T>::value, "Argument U type not compatible with imstkNew<T>'s T");
87  return std::dynamic_pointer_cast<U>(object);
88  }
89 
90 private:
91  imstkNew(const imstkNew<T>&) = delete;
92  void operator=(const imstkNew<T>&) = delete;
93  std::shared_ptr<T> object;
94 };
95 } // namespace imstk
Compound Geometry.
std::shared_ptr<T> obj = std::make_shared<T>(); equivalent, convenience class for STL shared allocati...
Definition: imstkNew.h:29
T * getPtr() const
Returns the raw pointer.
Definition: imstkNew.h:51