1 /*=========================================================================
2  *
3  *  Copyright Insight Software Consortium
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *=========================================================================*/
18 /*=========================================================================
19  *
20  *  Portions of this file are subject to the VTK Toolkit Version 3 copyright.
21  *
22  *  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
23  *
24  *  For complete copyright, license and disclaimer of warranty information
25  *  please refer to the NOTICE file at the top of the ITK source tree.
26  *
27  *=========================================================================*/
28 #ifndef itkDataObjectDecorator_h
29 #define itkDataObjectDecorator_h
30 
31 #include "itkDataObject.h"
32 #include "itkObjectFactory.h"
33 
34 namespace itk
35 {
36 /** \class DataObjectDecorator
37  * \brief Decorates any subclass of itkObject with a DataObject API
38  *
39  * DataObjectDecorator decorates an instance of a subclass of
40  * itkObject with a DataObject API. This allows any itkObject to be
41  * encapsulated into a DataObject that can be passed down the
42  * pipeline. To decorate simple types (float, int, std::vector) see
43  * SimpleDataObjectDecorator.
44  *
45  * The decorator provides two methods Set() and Get() to access the
46  * decorated object (referred internally as the component).
47  *
48  * Note that when an instance of DataObjectDecorator is created, the
49  * component is initialized with its default constructor (in this case
50  * a null pointer).
51  *
52  * DataObjectDecorator can decorate any subclass of itkObject. Two
53  * other decorators are provided. SimpleDataObjectDecorator can
54  * encapsulate simple types (float, int, std::vector).
55  * AutoPointerDataObjectDecorator will decorate any pointer type (for
56  * objects other than subclasses of itkObject) and manage the memory
57  * deallocationg of the component.
58  *
59  * \sa SimpleDataObjectDecorator
60  * \sa AutoPointerDataObjectDecorator
61  * \ingroup ITKSystemObjects
62  *
63  * \ingroup ITKCommon
64  */
65 template< typename T >
66 class ITK_TEMPLATE_EXPORT DataObjectDecorator:public DataObject
67 {
68 public:
69   ITK_DISALLOW_COPY_AND_ASSIGN(DataObjectDecorator);
70 
71   /** Standard type alias. */
72   using Self = DataObjectDecorator;
73   using Superclass = DataObject;
74   using Pointer = SmartPointer< Self >;
75   using ConstPointer = SmartPointer< const Self >;
76 
77   /** Typedef for the component type (object being decorated) */
78   using ComponentType = T;
79   using ComponentPointer = typename T::Pointer;
80   using ComponentConstPointer = typename T::ConstPointer;
81 
82   /** Method for creation through the object factory. */
83   itkNewMacro(Self);
84 
85   /** Run-time type information (and related methods). */
86   itkTypeMacro(DataObjectDecorator, DataObject);
87 
88   /** Set the contained object */
89   virtual void Set( const ComponentType *val);
90 
91   /** Get the contained object */
92   virtual const ComponentType * Get() const;
93   virtual ComponentType * GetModifiable();
94 
95   /** The most recent MTime of this object and the held component */
96   ModifiedTimeType GetMTime() const override;
97 
98   /** Restore the data object to its initial state. This means
99    *  releasing the help component.
100    */
101   void Initialize() override;
102 
103   /** \brief Graft the content of one decorator onto another
104    *
105    * The DataObject is dynamically_cast to this type, if successful
106    * then the component pointer is copies to that both decorators
107    * refer to the same object.
108    */
109   void Graft( const DataObject * ) override;
110   void Graft( const Self * decorator );
111 
112   /** Method to aid in dynamic Graft of polymorphic types.
113    *
114    * To this method by default a raw pointer must be used or explicit
115    * template parameter must be provided.
116    */
117   template <typename TOther>
Graft(const DataObjectDecorator<TOther> * decorator)118   void Graft( const DataObjectDecorator<TOther> * decorator )
119     {
120       auto * component = const_cast< ComponentType * >( dynamic_cast< const ComponentType * >( decorator->Get() ) );
121       if ( !component  )
122         {
123         return;
124         }
125       this->Set( component );
126     }
127 
128 protected:
129   DataObjectDecorator() = default;
130   ~DataObjectDecorator() override = default;
131   void PrintSelf(std::ostream & os, Indent indent) const override;
132 
133 protected:
134 
135 private:
136   ComponentPointer m_Component;
137 };
138 } // end namespace itk
139 
140 #ifndef ITK_MANUAL_INSTANTIATION
141 #include "itkDataObjectDecorator.hxx"
142 #endif
143 
144 #endif
145