1 /*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: ObjectFactory.cxx
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14 =========================================================================*/
15
16 #include "vtkDebugLeaks.h"
17 #include "vtkObjectFactory.h"
18 #include "vtkObjectFactoryCollection.h"
19 #include "vtkOutputWindow.h"
20 #include "vtkOverrideInformation.h"
21 #include "vtkOverrideInformationCollection.h"
22 #include "vtkVersion.h"
23 #include "vtkPoints.h"
24
25 static int failed = 0;
26
27 class vtkTestPoints : public vtkPoints
28 {
29 public:
30 // Methods from vtkObject
~vtkTestPoints()31 ~vtkTestPoints()
32 {
33 }
34
35 vtkTypeMacro(vtkTestPoints,vtkPoints);
New()36 static vtkTestPoints* New() { return new vtkTestPoints; }
vtkTestPoints()37 vtkTestPoints() { }
38 private:
39 vtkTestPoints(const vtkTestPoints&);
40 void operator=(const vtkTestPoints&);
41 };
42
43
44 class vtkTestPoints2 : public vtkPoints
45 {
46 public:
~vtkTestPoints2()47 ~vtkTestPoints2()
48 {
49 }
50
51 // Methods from vtkObject
52 vtkTypeMacro(vtkTestPoints2,vtkPoints);
New()53 static vtkTestPoints2* New() { return new vtkTestPoints2; }
vtkTestPoints2()54 vtkTestPoints2() { }
55 private:
56 vtkTestPoints2(const vtkTestPoints2&);
57 void operator=(const vtkTestPoints2&);
58 };
59
60
61 VTK_CREATE_CREATE_FUNCTION(vtkTestPoints);
62 VTK_CREATE_CREATE_FUNCTION(vtkTestPoints2);
63
64 class VTK_EXPORT TestFactory : public vtkObjectFactory
65 {
66 public:
67 TestFactory();
New()68 static TestFactory* New() { return new TestFactory;}
GetVTKSourceVersion()69 virtual const char* GetVTKSourceVersion() { return VTK_SOURCE_VERSION; }
GetDescription()70 const char* GetDescription() { return "A fine Test Factory"; }
71
72 protected:
73 TestFactory(const TestFactory&);
74 void operator=(const TestFactory&);
75 };
76
77
78
79
80
81
TestFactory()82 TestFactory::TestFactory()
83 {
84 this->RegisterOverride("vtkPoints",
85 "vtkTestPoints",
86 "test vertex factory override",
87 1,
88 vtkObjectFactoryCreatevtkTestPoints);
89 this->RegisterOverride("vtkPoints", "vtkTestPoints2",
90 "test vertex factory override 2",
91 0,
92 vtkObjectFactoryCreatevtkTestPoints2);
93 }
94
TestNewPoints(vtkPoints * v,const char * expectedClassName)95 void TestNewPoints(vtkPoints* v, const char* expectedClassName)
96 {
97 if(strcmp(v->GetClassName(), expectedClassName) != 0)
98 {
99 failed = 1;
100 cout << "Test Failed" << endl;
101 }
102 }
103
104
TestObjectFactory(int,char * [])105 int TestObjectFactory(int, char *[])
106 {
107 vtkOutputWindow::GetInstance()->PromptUserOff();
108 vtkGenericWarningMacro("Test Generic Warning");
109 TestFactory* factory = TestFactory::New();
110 vtkObjectFactory::RegisterFactory(factory);
111 factory->Delete();
112 vtkPoints* v = vtkPoints::New();
113 TestNewPoints(v, "vtkTestPoints");
114 v->Delete();
115
116 // disable all vtkPoints creation with the
117 factory->Disable("vtkPoints");
118 v = vtkPoints::New();
119 TestNewPoints(v, "vtkPoints");
120
121 factory->SetEnableFlag(1, "vtkPoints", "vtkTestPoints2");
122 v->Delete();
123 v = vtkPoints::New();
124 TestNewPoints(v, "vtkTestPoints2");
125
126 factory->SetEnableFlag(0, "vtkPoints", "vtkTestPoints2");
127 factory->SetEnableFlag(1, "vtkPoints", "vtkTestPoints");
128 v->Delete();
129 v = vtkPoints::New();
130 TestNewPoints(v, "vtkTestPoints");
131 v->Delete();
132 vtkOverrideInformationCollection* oic =
133 vtkOverrideInformationCollection::New();
134 vtkObjectFactory::GetOverrideInformation("vtkPoints", oic);
135 vtkOverrideInformation* oi;
136 if(oic->GetNumberOfItems() != 2)
137 {
138 cout << "Incorrect number of overrides for vtkPoints, expected 2, got: "
139 << oic->GetNumberOfItems() << "\n";
140 failed = 1;
141 if(oic->GetNumberOfItems() < 2)
142 {
143 return 1;
144 }
145 }
146 vtkCollectionSimpleIterator oicit;
147 oic->InitTraversal(oicit);
148 oi = oic->GetNextOverrideInformation(oicit);
149 oi->GetObjectFactory();
150
151 if(strcmp(oi->GetClassOverrideName(), "vtkPoints"))
152 {
153 cout << "failed: GetClassOverrideName should be vtkPoints, is: "
154 << oi->GetClassOverrideName() << "\n";
155 failed = 1;
156 }
157 if(strcmp(oi->GetClassOverrideWithName(), "vtkTestPoints"))
158 {
159 cout << "failed: GetClassOverrideWithName should be vtkTestPoints, is: "
160 << oi->GetClassOverrideWithName() << "\n";
161 failed = 1;
162 }
163 if(strcmp(oi->GetDescription(), "test vertex factory override"))
164 {
165 cout << "failed: GetClassOverrideWithName should be test vertex factory override, is: "
166 << oi->GetDescription() << "\n";
167 failed = 1;
168 }
169
170 oi = oic->GetNextOverrideInformation(oicit);
171 if(strcmp(oi->GetClassOverrideName(), "vtkPoints"))
172 {
173 cout << "failed: GetClassOverrideName should be vtkPoints, is: "
174 << oi->GetClassOverrideName() << "\n";
175 failed = 1;
176 }
177 if(strcmp(oi->GetClassOverrideWithName(), "vtkTestPoints2"))
178 {
179 cout << "failed: GetClassOverrideWithName should be vtkTestPoints2, is: "
180 << oi->GetClassOverrideWithName() << "\n";
181 failed = 1;
182 }
183 if(strcmp(oi->GetDescription(), "test vertex factory override 2"))
184 {
185 cout << "failed: GetClassOverrideWithName should be test vertex factory override 2, is: "
186 << oi->GetDescription() << "\n";
187 failed = 1;
188 }
189 oic->Delete();
190 vtkObjectFactory::UnRegisterAllFactories();
191 return failed;
192 }
193