Reference documentation for deal.II version 8.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
smartpointer.h
1 // ---------------------------------------------------------------------
2 // @f$Id: smartpointer.h 30036 2013-07-18 16:55:32Z maier @f$
3 //
4 // Copyright (C) 1998 - 2013 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // The deal.II library is free software; you can use it, redistribute
9 // it, and/or modify it under the terms of the GNU Lesser General
10 // Public License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 // The full text of the license can be found in the file LICENSE at
13 // the top level of the deal.II distribution.
14 //
15 // ---------------------------------------------------------------------
16 
17 #ifndef __deal2__smartpointer_h
18 #define __deal2__smartpointer_h
19 
20 
21 #include <deal.II/base/config.h>
22 #include <deal.II/base/subscriptor.h>
23 
24 DEAL_II_NAMESPACE_OPEN
25 
63 template<typename T, typename P = void>
65 {
66 public:
73  SmartPointer ();
74 
75  /*
76  * Copy constructor for
77  * SmartPointer. We do now
78  * copy the object subscribed to
79  * from <tt>tt</tt>, but subscribe
80  * ourselves to it again.
81  */
82  template <class Q>
83  SmartPointer (const SmartPointer<T,Q> &tt);
84 
85  /*
86  * Copy constructor for
87  * SmartPointer. We do now
88  * copy the object subscribed to
89  * from <tt>tt</tt>, but subscribe
90  * ourselves to it again.
91  */
92  SmartPointer (const SmartPointer<T,P> &tt);
93 
110  SmartPointer (T *t, const char *id);
111 
124  SmartPointer (T *t);
125 
126 
131  ~SmartPointer();
132 
144 
152  template <class Q>
154 
163 
168  void clear ();
169 
173  operator T *() const;
174 
181  T &operator * () const;
182 
189  T *operator -> () const;
190 
208  template <class Q>
209  void swap (SmartPointer<T,Q> &tt);
210 
226  void swap (T *&tt);
227 
237  std::size_t memory_consumption () const;
238 
239 private:
248  T *t;
253  const char *const id;
254 };
255 
256 
257 /* --------------------------- inline Template functions ------------------------------*/
258 
259 
260 template <typename T, typename P>
261 inline
263  :
264  t (0), id(typeid(P).name())
265 {}
266 
267 
268 
269 template <typename T, typename P>
270 inline
272  :
273  t (t), id(typeid(P).name())
274 {
275  if (t != 0)
276  t->subscribe(id);
277 }
278 
279 
280 
281 template <typename T, typename P>
282 inline
283 SmartPointer<T,P>::SmartPointer (T *t, const char *id)
284  :
285  t (t), id(id)
286 {
287  if (t != 0)
288  t->subscribe(id);
289 }
290 
291 
292 
293 template <typename T, typename P>
294 template <class Q>
295 inline
297  :
298  t (tt.t), id(tt.id)
299 {
300  if (t != 0)
301  t->subscribe(id);
302 }
303 
304 
305 
306 template <typename T, typename P>
307 inline
309  :
310  t (tt.t), id(tt.id)
311 {
312  if (t != 0)
313  t->subscribe(id);
314 }
315 
316 
317 
318 template <typename T, typename P>
319 inline
321 {
322  if (t != 0)
323  t->unsubscribe(id);
324 }
325 
326 
327 
328 template <typename T, typename P>
329 inline
330 void
332 {
333  if (t != 0)
334  {
335  t->unsubscribe(id);
336  delete t;
337  t = 0;
338  }
339 }
340 
341 
342 
343 template <typename T, typename P>
344 inline
346 {
347  // optimize if no real action is
348  // requested
349  if (t == tt)
350  return *this;
351 
352  if (t != 0)
353  t->unsubscribe(id);
354  t = tt;
355  if (tt != 0)
356  tt->subscribe(id);
357  return *this;
358 }
359 
360 
361 
362 template <typename T, typename P>
363 template <class Q>
364 inline
367 {
368  // if objects on the left and right
369  // hand side of the operator= are
370  // the same, then this is a no-op
371  if (&tt == this)
372  return *this;
373 
374  if (t != 0)
375  t->unsubscribe(id);
376  t = static_cast<T *>(tt);
377  if (tt != 0)
378  tt->subscribe(id);
379  return *this;
380 }
381 
382 
383 
384 template <typename T, typename P>
385 inline
388 {
389  // if objects on the left and right
390  // hand side of the operator= are
391  // the same, then this is a no-op
392  if (&tt == this)
393  return *this;
394 
395  if (t != 0)
396  t->unsubscribe(id);
397  t = static_cast<T *>(tt);
398  if (tt != 0)
399  tt->subscribe(id);
400  return *this;
401 }
402 
403 
404 
405 template <typename T, typename P>
406 inline
408 {
409  return t;
410 }
411 
412 
413 
414 template <typename T, typename P>
415 inline
417 {
418  Assert(t != 0, ExcNotInitialized());
419  return *t;
420 }
421 
422 
423 
424 template <typename T, typename P>
425 inline
427 {
428  Assert(t != 0, ExcNotInitialized());
429  return t;
430 }
431 
432 
433 
434 template <typename T, typename P>
435 template <class Q>
436 inline
438 {
439 #ifdef DEBUG
440  SmartPointer<T,P> aux(t,id);
441  *this = tt;
442  tt = aux;
443 #else
444  std::swap (t, tt.t);
445 #endif
446 }
447 
448 
449 
450 template <typename T, typename P>
451 inline
453 {
454  if (t != 0)
455  t->unsubscribe (id);
456 
457  std::swap (t, tt);
458 
459  if (t != 0)
460  t->subscribe (id);
461 }
462 
463 
464 
465 template <typename T, typename P>
466 inline
467 std::size_t
469 {
470  return sizeof(SmartPointer<T,P>);
471 }
472 
473 
474 
475 
481 template <typename T, typename P, class Q>
482 inline
484 {
485  t1.swap (t2);
486 }
487 
488 
489 
497 template <typename T, typename P>
498 inline
499 void swap (SmartPointer<T,P> &t1, T *&t2)
500 {
501  t1.swap (t2);
502 }
503 
504 
505 
513 template <typename T, typename P>
514 inline
515 void swap (T *&t1, SmartPointer<T,P> &t2)
516 {
517  t2.swap (t1);
518 }
519 
520 DEAL_II_NAMESPACE_CLOSE
521 
522 #endif
T & operator*() const
Definition: smartpointer.h:416
const char *const id
Definition: smartpointer.h:253
void swap(BlockVector< Number > &u, BlockVector< Number > &v)
Definition: block_vector.h:589
#define Assert(cond, exc)
Definition: exceptions.h:299
void swap(SmartPointer< T, Q > &tt)
Definition: smartpointer.h:437
SmartPointer< T, P > & operator=(T *tt)
Definition: smartpointer.h:345
::ExceptionBase & ExcNotInitialized()
std::size_t memory_consumption() const
Definition: smartpointer.h:468
T * operator->() const
Definition: smartpointer.h:426