Milo  0.1
Milo:EquationManipulationProgram
smart.h
Go to the documentation of this file.
1 #ifndef __SMART_H
2 #define __SMART_H
3 
4 /* Copyright (C) 2017 - James Terman
5  *
6  * milo is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * milo is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
27 #include <vector>
28 #include <memory>
29 #include <initializer_list>
30 
37 template <class T>
38 class SmartPtr : public std::shared_ptr<T>
39 {
40 public:
44  explicit SmartPtr() : std::shared_ptr<T>() {}
45 
49  explicit SmartPtr(T* p) : std::shared_ptr<T>(p) {}
50 
54  explicit SmartPtr(const std::shared_ptr<T>& sp) : std::shared_ptr<T>(sp) {}
55 
59  SmartPtr& operator=(T* p) { SmartPtr a(p->getSharedPtr()); *this = a; return *this; }
60 
64  bool operator==(T* p) const { return this->get() == p; }
65 
69  bool operator!=(T* p) const { return this->get() != p; }
70 
74  operator T*() const { return this->get(); }
75 };
76 
81 template <class T>
82 class SmartVector : public std::vector< SmartPtr<T> >
83 {
84 public:
88  using iterator = typename std::vector< SmartPtr<T> >::iterator;
89  using const_iterator = typename std::vector< SmartPtr<T> >::const_iterator;
90  using size_type = typename std::vector< std::shared_ptr<T> >::size_type;
91  using base = typename std::vector< SmartPtr<T> >;
92 
97 
101  explicit SmartVector(std::initializer_list<T*> li)
102  {
103  for ( T* n : li ) { this->push_back(n); }
104  }
105 
109  explicit SmartVector(T* p) { SmartPtr<T> a; a = p; this->base::push_back(a); }
110 
114  SmartVector(std::initializer_list< SmartPtr<T> > li) : base(li) {}
115 
119  void push_back(T* p) { SmartPtr<T> a; a = p; this->base::push_back(a); }
120 
124  void push_back(const SmartPtr<T>&) = delete;
125 
129  void push_back(const SmartPtr<T>&&) = delete;
130 
134  iterator insert(const_iterator pos, T* val)
135  {
136  return this->base::insert(pos, SmartPtr<T>(val));
137  }
138 
142  iterator insert(const_iterator pos, size_type n, const T* val)
143  {
144  return this->base::insert(pos, n, SmartPtr<T>(val));
145  }
146 
150  iterator insert (const_iterator position, iterator first, iterator last)
151  {
152  return this->base::insert(position, first, last);
153  }
154 
158  iterator insert(const_iterator pos, const SmartPtr<T> val)
159  {
160  return this->base::insert(pos, val);
161  }
162 
166  iterator insert(const_iterator pos, size_type n, const SmartPtr<T> val) = delete;
167 
171  iterator insert(const_iterator pos, std::initializer_list< SmartPtr<T> > il) = delete;
172 
176  void swap(SmartVector<T>& v) { this->base::swap(v); }
177 
181  void swap(base&) = delete;
182 
187  {
188  this->base::reserve(this->base::size() + v.size());
189  insert(pos, v.base::begin(), v.base::end());
190  v.base::clear();
191  }
192 
196  void merge(SmartVector<T>& v) { this->merge(v, this->base::begin()); }
200  void insert_index(int index, T* e)
201  {
202  insert((index < 0 ? base::end() : base::begin()) + index, e);
203  }
204 
208  void erase_index(int index)
209  {
210  this->erase((index < 0 ? base::end() - 1 : base::begin()) + index);
211  }
212 
216  int get_index(T* e) const
217  {
218  const base* base_this = this;
219  int index = 0;
220  for ( auto n : *base_this ) {
221  if (n.get() == e) return index;
222  ++index;
223  }
224  return -1;
225  }
226 
230  iterator find(T* e) { return this->base::begin() + get_index(e); }
231 };
232 
237 template<class T>
238 bool operator==(const SmartPtr<T>& p, const typename SmartVector<T>::iterator& it)
239 {
240  return p.get() == it->get();
241 }
242 
247 template<class T>
248 bool operator!=(const SmartPtr<T>& p, const typename SmartVector<T>::iterator& it)
249 {
250  return p.get() != it->get();
251 }
252 
257 template<class T>
258 bool operator==(const typename SmartVector<T>::iterator& it, const SmartPtr<T>& p)
259 {
260  return p.get() == it->get();
261 }
262 
267 template<class T>
268 bool operator!=(const typename SmartVector<T>::iterator& it, const SmartPtr<T>& p)
269 {
270  return p.get() != it->get();
271 }
273 
274 #endif // __SMART_H
Definition: smart.h:38
void erase_index(int index)
Definition: smart.h:208
iterator insert(const_iterator pos, const SmartPtr< T > val)
Definition: smart.h:158
iterator insert(const_iterator position, iterator first, iterator last)
Definition: smart.h:150
void push_back(T *p)
Definition: smart.h:119
typename std::vector< SmartPtr< Term > >::iterator iterator
Definition: smart.h:88
Definition: test.cpp:48
SmartPtr & operator=(T *p)
Definition: smart.h:59
void merge(SmartVector< T > &v)
Definition: smart.h:196
bool operator==(T *p) const
Definition: smart.h:64
iterator insert(const_iterator pos, T *val)
Definition: smart.h:134
iterator insert(const_iterator pos, size_type n, const T *val)
Definition: smart.h:142
int get_index(T *e) const
Definition: smart.h:216
iterator find(T *e)
Definition: smart.h:230
SmartPtr(const std::shared_ptr< T > &sp)
Definition: smart.h:54
SmartVector()
Definition: smart.h:96
void swap(SmartVector< T > &v)
Definition: smart.h:176
Definition: smart.h:82
void merge(SmartVector< T > &v, iterator pos)
Definition: smart.h:186
SmartPtr()
Definition: smart.h:44
SmartVector(std::initializer_list< T * > li)
Definition: smart.h:101
bool operator!=(T *p) const
Definition: smart.h:69
SmartVector(std::initializer_list< SmartPtr< T > > li)
Definition: smart.h:114
SmartVector(T *p)
Definition: smart.h:109
void insert_index(int index, T *e)
Definition: smart.h:200
SmartPtr(T *p)
Definition: smart.h:49