member_clone_ptr.h
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2015.
2 // Distributed under the GNU General Public License and under the
3 // BSD 3-Clause License (See accompanying file LICENSE.txt).
4 
5 /** @file */
6 
7 #ifndef NUKLEI_MEMBER_CLONE_PTR_H
8 #define NUKLEI_MEMBER_CLONE_PTR_H
9 
10 #include <memory>
11 #include <nuklei/Definitions.h>
12 
13 
14 namespace nuklei {
15 
16  template<typename T>
18  {
19  member_clone_ptr() {}
20  member_clone_ptr(const T& element) : ptr_(element.clone()) {}
22  {
23  if (p.isDefined()) ptr_ = p->clone();
24  }
25  member_clone_ptr<T>& operator=(const member_clone_ptr<T>& p)
26  {
27  if (p.isDefined()) ptr_ = p->clone();
28  else ptr_.reset();
29  return *this;
30  }
31 
32  void assertConsistency() const
33  {
34  NUKLEI_TRACE_BEGIN();
35  if (isDefined()) get().assertConsistency();
36  NUKLEI_TRACE_END();
37  }
38 
39  T& operator* () { return get(); }
40  const T& operator* () const { return get(); }
41 
42  T* operator-> () { return &get(); }
43  const T* operator-> () const { return &get(); }
44 
45  bool isDefined() const { return ptr_.get() != NULL; }
46 
47  T& get()
48  { NUKLEI_ASSERT(isDefined()); return *ptr_; }
49  const T& get() const
50  { NUKLEI_ASSERT(isDefined()); return *ptr_; }
51 
52  void set(const T& element) { ptr_ = element.clone(); }
53 
54  void clear() { ptr_.reset(); }
55 
56  private:
57  NUKLEI_UNIQUE_PTR<T> ptr_;
58 
59  friend class NUKLEI_SERIALIZATION_FRIEND_CLASSNAME;
60  template<class Archive>
61  void serialize(Archive &ar, const unsigned int version)
62  {
63  ar & NUKLEI_SERIALIZATION_NVP(ptr_);
64  }
65  };
66 
67 }
68 
69 #endif
70 
Public namespace.
Definition: Color.cpp:9
#define NUKLEI_ASSERT(expression)
Throws an Error if expression is not true.
Definition: Common.h:113
© Copyright 2007-2013 Renaud Detry.
Distributed under the terms of the GNU General Public License (GPL).
(See accompanying file LICENSE.txt or copy at http://www.gnu.org/copyleft/gpl.html.)
Revised Sun Sep 13 2020 19:10:06.