uniquePtr.hpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 
22 #ifndef __uniquePtr_hpp__
23 #define __uniquePtr_hpp__
24 
25 
26 #include <memory>
27 #include <typeinfo>
28 
29 #include "pFlowMacros.hpp"
30 #include "error.hpp"
31 
32 // just for preventing the use of std namespace and adding some minor functionalities
33 
34 
35 namespace pFlow
36 {
37 
38 
39 
40 template<
41  typename T,
42  typename Deleter = std::default_delete<T>
43 >
44 class uniquePtr
45 :
46  public std::unique_ptr<T, Deleter>
47 {
48 public:
49 
50  typedef std::unique_ptr<T,Deleter> uniquePtrType;
51 
52  // using base constructors
53  using uniquePtrType::unique_ptr;
54 
55 
56  template<typename... Args>
57  inline static uniquePtr<T> makeUnique(Args&&... args)
58  {
59  return uniquePtr<T>(new T(std::forward<Args>(args)...));
60  }
61 
62  void clear()
63  {
64  if( ! (*this) )
65  {
66  auto p = this->release();
67  delete p;
68  }
69  }
70 
71  // ref to the object of type T
73  {
74  if(!this->get())
75  {
77  "uniquePtr is empty, and you are trying to get its reference. \n" <<
78  "Type name is "<< typeid(T).name()<<"\n";
79  fatalExit;
80  }
81  return *this->get();
82  }
83 
84  // const ref to the object of type T
85  const T& operator() () const
86  {
87  if(!this->get())
88  {
90  "uniquePtr is empty, and you are trying to get its reference. \n" <<
91  "Type name is "<< typeid(T).name()<<"\n";
92  fatalExit;
93  }
94  return const_cast<T&>(*this->get());
95  }
96 
97 };
98 
99 template<class T, class... Args>
100 inline uniquePtr<T> makeUnique(Args&&... args)
101 {
102  return uniquePtr<T>(new T(std::forward<Args>(args)...));
103 }
104 
105 }
106 
107 
108 
109 
110 #endif
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::uniquePtr::uniquePtrType
std::unique_ptr< T, Deleter > uniquePtrType
Definition: uniquePtr.hpp:50
pFlow
Definition: demComponent.hpp:28
pFlowMacros.hpp
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
pFlow::makeUnique
uniquePtr< T > makeUnique(Args &&... args)
Definition: uniquePtr.hpp:100
pFlow::uniquePtr::clear
void clear()
Definition: uniquePtr.hpp:62
pFlow::uniquePtr
Definition: uniquePtr.hpp:44
pFlow::uniquePtr::makeUnique
static uniquePtr< T > makeUnique(Args &&... args)
Definition: uniquePtr.hpp:57
pFlow::uniquePtr::operator()
T & operator()()
Definition: uniquePtr.hpp:72
error.hpp