Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleFactory.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 /**
26  @example ExampleFactory.cpp
27  @relates BaseFactory
28  @brief Example for class factory
29  @ingroup g_examples
30  @author woelk
31  @date 02/2006
32 */
33 
34 #include <Base/Common/BaseFactory.hh>
35 
36 #include <string>
37 #include <iostream>
38 
39 using namespace BIAS;
40 using namespace std;
41 
42 /** \cond POORLY_NAMED_EXAMPLE_CLASSES
43  * \internal */
44 class Base
45 {
46 public:
47  Base(const string& name)
48  : Name_(name) {}
49  inline std::string GetTag() const { return Name_; }
50  void Who() { cout << "I am "<<Name_<<endl; }
51  std::string Name_;
52 };
53 
54 /** \author woelk 02/2006
55  * \internal */
56 class A : public Base
57 {
58 public:
59  A() : Base("a") {};
60  static Base *Allocator() { return dynamic_cast<Base *>(new A); }
61 };
62 
63 
64 /** \author woelk 02/2006
65  * \internal */
66 class B : public Base
67 {
68 public:
69  B() : Base("b") {};
70  static Base *Allocator() { return dynamic_cast<Base *>(new B); }
71 };
72 
73 
74 /** \author woelk 02/2006
75  * \internal */
76 class C : public Base
77 {
78 public:
79  C() : Base("c") {};
80  static Base *Allocator() { return dynamic_cast<Base *>(new C); }
81 };
82 /** \endcond */
83 
84 
85 /* @brief explains the usage of the Factory class
86  @author woelk 02/2006 */
87 int main()
88 {
89  // main knows nothing of A, B and C
90  // all knowledge is in factory and henec D coudl be introduced without
91  // changing main
93 #ifdef BIAS_BUILD_SHARED_LIBS
94  fac->Add2Directories(".");
95 #else
96  fac->AddType((&A::Allocator));
97  fac->AddType((&B::Allocator));
98  fac->AddType((&C::Allocator));
99 #endif
100 
101  Base *p = fac->Generate("a");
102  if (!p) return -1;
103  p->Who();
104  delete p; p=NULL;
105  p = fac->Generate("b");
106  if (!p) return -1;
107  p->Who();
108  delete p; p=NULL;
109  p = fac->Generate("c");
110  if (!p) return -1;
111  p->Who();
112  delete p; p=NULL;
113  // factory does know nothing of class with tag "d"
114  p = fac->Generate("d");
115  if (p!=NULL){
116  p->Who();
117  return -1;
118  } else {
119  cerr << "could not generate class with name \"d\""<<endl;
120  }
121 
122  return 0;
123 }
void AddType(AllocatorFunctionPointer allocFunc)
This function is only meaningfull, when static libraries are used.
Definition: BaseFactory.hh:421
static BaseFactory< BaseClassType, TagType > * GetInstance()
BaseClassType * Generate(const TagType &tag)
function for generic class generation from
Definition: BaseFactory.hh:325
void Add2Directories(const std::string &directories)
This function only makes sense when shared libraries are used: Initialize the factory with &#39;:&#39; (linux...
Definition: BaseFactory.hh:290
simple factory class designed for usage as a singleton
Definition: BaseFactory.hh:49