www.cemf.ir
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 #ifndef __uniquePtr_hpp__
22 #define __uniquePtr_hpp__
23 
24 
25 #include <memory>
26 #include <typeinfo>
27 
28 #include "pFlowMacros.hpp"
29 #include "error.hpp"
30 
31 // just for preventing the use of std namespace and adding some minor functionalities
32 
33 
34 namespace pFlow
35 {
36 
37 
38 
39 template<
40  typename T
41 >
42 class uniquePtr
43 :
44  public std::unique_ptr<T>
45 {
46 public:
47 
48  using uniquePtrType = std::unique_ptr<T>;
49 
50  // using base constructors
51  using uniquePtrType::unique_ptr;
52 
53 
54  template<typename... Args>
55  inline static uniquePtr<T> makeUnique(Args&&... args)
56  {
57  return uniquePtr<T>(new T(std::forward<Args>(args)...));
58  }
59 
60  void clear()
61  {
62  this->reset(nullptr);
63  }
64 
65  // ref to the object of type T
67  {
68  if(!*this)
69  {
71  "uniquePtr is empty, and you are trying to get its reference. \n" <<
72  "Type name is "<< typeid(T).name()<<"\n";
73  fatalExit;
74  }
75  return *this->get();
76  }
77 
78  // const ref to the object of type T
79  const T& operator() () const
80  {
81  if(!*this)
82  {
84  "uniquePtr is empty, and you are trying to get its reference. \n" <<
85  "Type name is "<< typeid(T).name()<<"\n";
86  fatalExit;
87  }
88  return static_cast<const T&>(*this->get());
89  }
90 
91  explicit operator bool() const
92  {
93  return this->get()!= nullptr;
94  }
95 
96 };
97 
98 template<class T, class... Args>
99 inline uniquePtr<T> makeUnique(Args&&... args)
100 {
101  return uniquePtr<T>(new T(std::forward<Args>(args)...));
102 }
103 
104 }
105 
106 #endif
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
pFlow
Definition: demGeometry.hpp:27
pFlowMacros.hpp
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
pFlow::makeUnique
uniquePtr< T > makeUnique(Args &&... args)
Definition: uniquePtr.hpp:99
pFlow::uniquePtr::clear
void clear()
Definition: uniquePtr.hpp:60
pFlow::uniquePtr
Definition: uniquePtr.hpp:42
pFlow::uniquePtr::makeUnique
static uniquePtr< T > makeUnique(Args &&... args)
Definition: uniquePtr.hpp:55
pFlow::uniquePtr::operator()
T & operator()()
Definition: uniquePtr.hpp:66
pFlow::uniquePtr< pFlow::particleIdHandler >::uniquePtrType
std::unique_ptr< pFlow::particleIdHandler > uniquePtrType
Definition: uniquePtr.hpp:48
error.hpp