Loading...
Searching...
No Matches
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
28namespace 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 */
74
75 /*!
76 \brief Returns the pointer of decorated object.
77 */
79
80 /*!
81 * \brief Returns the Decorator that decorates this item.
82 * \param decorated The decorated item that we are looking for.
83 *
84 * \return The Decorator we are looking for or this if there's no decorators or 0 if not found.
85 */
86 T* findDecorator(T* decorated);
87
88 /*!
89 * \brief Removes The decorator of the \a decorated.
90 * \param decorated The item that we are searching for the decorator.
91 *
92 * \return The new item to be used as new Delegate or NULL.
93 */
94 T* removeDecorator(T* decorated);
95
96 protected:
97
98 T * m_decorated; //!< The object decorated.
99 bool m_delDecorated; //!< If true, also delete decorated pointer.
100 };
101
102 template<class T>
103 Decorator<T>::Decorator(T* decorated, bool deleteDecorated)
104 : T(),
105 m_decorated(decorated),
106 m_delDecorated(deleteDecorated)
107 {
108 }
109
110 template<class T>
112 {
113 if(m_delDecorated)
114 delete m_decorated;
115 }
116
117 template<class T>
119 : m_decorated(rhs.m_decorated),
120 m_delDecorated(rhs.m_delDecorated)
121 {
122 }
123
124 template<class T>
126 {
127 m_decorated = rhs.m_decorated;
128 m_delDecorated = rhs.m_delDecorated;
129
130 return *this;
131 }
132
133 template<class T>
135 {
136 return m_decorated;
137 }
138
139 template<class T>
141 {
142 if(decorated == m_decorated)
143 return this;
144
145 Decorator<T>* d = dynamic_cast< Decorator<T>* >(m_decorated);
146
147 if(d != 0)
148 return findDecorator(d);
149
150 return 0;
151 }
152
153 template<class T>
155 {
156 if(decorated == this)
157 {
158 m_delDecorated = false;
159 return m_decorated;
160 }
161
162 T* fd = findDecorator(decorated);
163
164 Decorator<T>* aux = dynamic_cast< Decorator<T>* >(decorated);
165
166 if(aux != 0 && fd != 0)
167 {
168 aux->m_delDecorated = false;
169 ((Decorator<T>*)fd)->m_decorated = aux->m_decorated;
170 }
171
172 return 0;
173 }
174
175 } // end namespace common
176} // end namespace te
177
178#endif //__TERRALIB_COMMON_INTERNAL_DECORATOR_H
bool m_delDecorated
If true, also delete decorated pointer.
Definition: Decorator.h:99
Decorator(const Decorator &rhs)
Copy constructor.
Definition: Decorator.h:118
T * removeDecorator(T *decorated)
Removes The decorator of the decorated.
Definition: Decorator.h:154
virtual ~Decorator()
Virtual destructor.
Definition: Decorator.h:111
T * findDecorator(T *decorated)
Returns the Decorator that decorates this item.
Definition: Decorator.h:140
T * m_decorated
The object decorated.
Definition: Decorator.h:98
Decorator & operator=(const Decorator &rhs)
Copy operator.
Definition: Decorator.h:125
Decorator(T *decorated, bool deleteDecorated=false)
Constructor.
Definition: Decorator.h:103
T * getDecorated()
Returns the pointer of decorated object.
Definition: Decorator.h:134
TerraLib.