ListPtr.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 __ListPtr_hpp__
22 #define __ListPtr_hpp__
23 
24 
25 #include <list>
26 
27 
28 #include "types.hpp"
29 #include "uniquePtr.hpp"
30 #include "error.hpp"
31 #include "iOstream.hpp"
32 
33 
34 namespace pFlow
35 {
36 
37 template<typename T >
38 class ListPtr
39 {
40 public:
41 
43  using listType = std::list<T*>;
44 
45 
46  template<typename... Args>
47  inline static uniquePtr<T> makeSafe(Args&&... args)
48  {
49  return uniquePtr<T>(new T(std::forward<Args>(args)...));
50  }
51 
52 protected:
53 
55 
56  // - list of pointers
57  std::list<T*> list_;
58 
59 
60 
62 
63  // - copy the content to this list
64  bool copy(const ListPtrType& src);
65 
66  // - return ith pointer
67  T* ptr(label i);
68 
69  // - return ith const poiter
70  const T* ptr(label i)const;
71 
72  // - iterator position of ith element
73  auto pos(label i);
74 
75  // - const iterator position of ith element
76  auto pos(label i) const;
77 
78 public:
79 
80  // - Type info
81  TypeInfoTemplateNV("ListPtr", T);
82 
84 
85  // - empty list
87  :
88  list_()
89  {}
90 
91  // - a list with initial length of len
92  ListPtr(size_t len)
93  :
94  list_(len)
95  {}
96 
97  // - copy construct, create new objects out of the pointers in the src
98  ListPtr(const ListPtrType& src);
99 
100 
101  //- copy assignment, create new objects out of he pointers in the src
102  ListPtrType& operator=(const ListPtrType& rhs);
103 
104 
105  // - move construct
106  // Simply move the pointers, so the new object takes the
107  // ownership of the pointers.
109  :
110  list_(std::move(src))
111  {}
112 
113  // - move assignment
114  // the lhs object takes the ownership of pointers and rhs loses the ownership
116  {
117  // clears the content of this
118  clear();
119 
120  list_.operator=(std::move(rhs));
121  return *this;
122  }
123 
125  {
126  auto ptr = makeUnique<ListPtrType>(*this);
127  return ptr.release();
128  }
129 
131  {
132  return makeUnique<ListPtrType>(*this);
133  }
134 
135  // - remove /delete all the objectes associated with pointers
137  {
138  clear();
139  }
140 
142 
143  // - set the ith element
144  T* set(label i, T* ptr);
145 
146  // - set the ith element and take the ownership from uniquePtr
148 
149  // - create the object in-place and set the pointer in ith position
150  // if oject creation fails, uniquePtr deletes the memeory
151  template<typename... Args>
152  uniquePtr<T> setSafe(label i, Args&&... args);
153 
154  // - put the pointer at the end
155  void push_back(T* ptr);
156 
157  // - put the pointer at the end
158  void push_back(uniquePtr<T>& ptr);
159 
160  // - safely create (in-place) and put the pointer at the end
161  template<typename... Args>
162  void push_backSafe(Args&&... args);
163 
164  // - access to ith element
165  // fatalexit if out of range or nullptr
166  T& operator[](label i);
167 
168  // - const access to ith element
169  // fatalexit if out of range or nullptr
170  const T& operator[](label i) const;
171 
172  // size of container
173  size_t size()const;
174 
175  // check if the container empty
176  auto empty() const;
177 
178  // release the ownership of ith pointer
180 
181  // - clear the content of list and delete objects
182  void clear();
183 
184  // - clear the ith element
185  void clear(label i);
186 
187  // - clone the object
188 
189 };
190 
191 
192 } // pFlow
193 
194 
195 #include "ListPtrI.hpp"
196 
197 #endif
pFlow::ListPtr::ListPtrType
ListPtr< T > ListPtrType
Definition: ListPtr.hpp:42
pFlow::ListPtr::size
size_t size() const
Definition: ListPtrI.hpp:252
pFlow::ListPtr
Definition: ListPtr.hpp:38
pFlow::ListPtr::~ListPtr
~ListPtr()
Definition: ListPtr.hpp:136
types.hpp
pFlow::ListPtr::copy
bool copy(const ListPtrType &src)
Definition: ListPtrI.hpp:22
pFlow::ListPtr::ptr
T * ptr(label i)
Definition: ListPtrI.hpp:42
pFlow::ListPtr::push_backSafe
void push_backSafe(Args &&... args)
Definition: ListPtrI.hpp:210
pFlow::ListPtr::ListPtr
ListPtr(ListPtrType &&src)
Definition: ListPtr.hpp:108
pFlow::ListPtr::release
uniquePtr< T > release(label i)
Definition: ListPtrI.hpp:265
pFlow::ListPtr::clear
void clear()
Definition: ListPtrI.hpp:275
pFlow::ListPtr::pos
auto pos(label i)
Definition: ListPtrI.hpp:70
pFlow
Definition: demComponent.hpp:28
pFlow::ListPtr::push_back
void push_back(T *ptr)
Definition: ListPtrI.hpp:195
pFlow::ListPtr::ListPtr
ListPtr(size_t len)
Definition: ListPtr.hpp:92
pFlow::ListPtr::clone
uniquePtr< ListPtrType > clone() const
Definition: ListPtr.hpp:130
uniquePtr.hpp
pFlow::ListPtr::operator[]
T & operator[](label i)
Definition: ListPtrI.hpp:218
pFlow::ListPtr::clonePtr
ListPtrType * clonePtr() const
Definition: ListPtr.hpp:124
pFlow::ListPtr::TypeInfoTemplateNV
TypeInfoTemplateNV("ListPtr", T)
pFlow::ListPtr::operator=
ListPtrType & operator=(ListPtrType &&rhs)
Definition: ListPtr.hpp:115
pFlow::ListPtr::operator=
ListPtrType & operator=(const ListPtrType &rhs)
Definition: ListPtrI.hpp:123
pFlow::ListPtr< pFlow::processField >::listType
std::list< pFlow::processField * > listType
Definition: ListPtr.hpp:43
pFlow::ListPtr::makeSafe
static uniquePtr< T > makeSafe(Args &&... args)
Definition: ListPtr.hpp:47
pFlow::uniquePtr
Definition: uniquePtr.hpp:44
pFlow::ListPtr::list_
std::list< T * > list_
Definition: ListPtr.hpp:57
pFlow::label
std::size_t label
Definition: builtinTypes.hpp:61
pFlow::ListPtr::set
T * set(label i, T *ptr)
Definition: ListPtrI.hpp:148
iOstream.hpp
pFlow::ListPtr::ListPtr
ListPtr()
Definition: ListPtr.hpp:86
pFlow::ListPtr::empty
auto empty() const
Definition: ListPtrI.hpp:258
error.hpp
pFlow::ListPtr::setSafe
uniquePtr< T > setSafe(label i, Args &&... args)
ListPtrI.hpp