All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Decorator.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/common/Decorator.h
22 
23  \brief Defines a decorator abstraction.
24  */
25 #ifndef __TERRALIB_COMMON_INTERNAL_DECORATOR_H
26 #define __TERRALIB_COMMON_INTERNAL_DECORATOR_H
27 
28 namespace te
29 {
30  namespace common
31  {
32 
33  /*!
34  \class Decorator
35 
36  Decorators are classes that can change object behavioral in run-time.
37  For more informations about decorator pattern, see:
38  <A HREF="http://en.wikipedia.org/wiki/Decorator_pattern">Decorator pattern on Wikipedia.</A>
39 
40  \ingroup common
41  */
42  template<class T> class Decorator : public T
43  {
44  public:
45 
46  /*!
47  \brief Constructor.
48 
49  The \a deleteDecorated argument tells if the decorator HAS or NOT the ownership of the decorated pointer.
50  A \a true value gives the ownership to the decorator and the client do not need manage memory of the decorated pointer.
51  Otherwise, the client needs manage the memory of the decorated pointer.
52 
53  \param decorated The object to be decorated.
54  \param deleteDecorated Tells to decorator to also delete decorated pointer.
55  */
56  Decorator(T* decorated, bool deleteDecorated = false);
57 
58  /*! \brief Virtual destructor. */
59  virtual ~Decorator();
60 
61  /*!
62  \brief Copy constructor.
63 
64  \param rhs Object to be copied.
65  */
66  Decorator(const Decorator& rhs);
67 
68  /*!
69  \brief Copy operator.
70 
71  \param rhs Object to be copied.
72  */
73  Decorator & operator=(const Decorator& rhs);
74 
75  /*!
76  \brief Returns the pointer of decorated object.
77  */
78  T* getDecorated();
79 
80  protected:
81 
82  T * m_decorated; //!< The object decorated.
83  bool m_delDecorated; //!< If true, also delete decorated pointer.
84  };
85 
86  template<class T>
87  Decorator<T>::Decorator(T* decorated, bool deleteDecorated)
88  : T(),
89  m_decorated(decorated),
90  m_delDecorated(deleteDecorated)
91  {
92  }
93 
94  template<class T>
96  {
97  if(m_delDecorated)
98  delete m_decorated;
99  }
100 
101  template<class T>
103  : m_decorated(rhs.m_decorated),
104  m_delDecorated(rhs.m_delDecorated)
105  {
106  }
107 
108  template<class T>
110  {
111  m_decorated = rhs.m_decorated;
112  m_delDecorated = rhs.m_delDecorated;
113 
114  return *this;
115  }
116 
117  template<class T>
119  {
120  return m_decorated;
121  }
122 
123  } // end namespace common
124 } // end namespace te
125 
126 #endif //__TERRALIB_COMMON_INTERNAL_DECORATOR_H
Decorator(T *decorated, bool deleteDecorated=false)
Constructor.
Definition: Decorator.h:87
bool m_delDecorated
If true, also delete decorated pointer.
Definition: Decorator.h:83
T * getDecorated()
Returns the pointer of decorated object.
Definition: Decorator.h:118
Decorator & operator=(const Decorator &rhs)
Copy operator.
Definition: Decorator.h:109
T * m_decorated
The object decorated.
Definition: Decorator.h:82
virtual ~Decorator()
Virtual destructor.
Definition: Decorator.h:95