Graphite  Version 3
An experimental 3D geometry processing program
basic_factory.h
Go to the documentation of this file.
1 /*
2  * OGF/Graphite: Geometry and Graphics Programming Library + Utilities
3  * Copyright (C) 2000 Bruno Levy
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program 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 General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  * If you modify this software, you should include a notice giving the
20  * name of the person performing the modification, the date of modification,
21  * and the reason for such modification.
22  *
23  * Contact: Bruno Levy
24  *
25  * levy@loria.fr
26  *
27  * ISA Project
28  * LORIA, INRIA Lorraine,
29  * Campus Scientifique, BP 239
30  * 54506 VANDOEUVRE LES NANCY CEDEX
31  * FRANCE
32  *
33  * Note that the GNU General Public License does not permit incorporating
34  * the Software into proprietary programs.
35  */
36 
37 
38 #ifndef H_OGF_BASIC_TYPES_BASIC_FACTORY_H
39 #define H_OGF_BASIC_TYPES_BASIC_FACTORY_H
40 
42 
43 #include <map>
44 #include <vector>
45 #include <string>
46 
51 namespace OGF {
52 
53 //______________________________________________________________________________
54 
55  template<class BASE> class BasicFactory : public Counted {
56  public:
57  virtual BASE* create() = 0 ;
58  } ;
59 
60  template <class BASE, class T>
61  class GenericBasicFactory : public BasicFactory<BASE> {
62  public:
63  virtual BASE* create() { return new T() ; }
64  } ;
65 
66  template <class T> class BasicFactories {
67  private:
68  typedef std::map<std::string, SmartPointer<BasicFactory<T> > > FactoriesMap ;
69  public:
70  void register_factory(const std::string& name, BasicFactory<T>* f) {
71  ogf_assert(map_.find(name) == map_.end()) ;
72  map_[name] = f ;
73  }
74  void unregister_factory(const std::string& name) {
75  auto it = map_.find(name) ;
76  ogf_assert(it != map_.end()) ;
77  map_.erase(it) ;
78  }
79  bool factory_is_bound(const std::string& name) const {
80  return(map_.find(name) != map_.end()) ;
81  }
82  void list_factory_names(std::vector<std::string>& names) const {
83  names.clear() ;
84  for(auto& it : map_) {
85  names.push_back(it.first) ;
86  }
87  }
88  std::string factory_names() const {
89  std::string result = "";
90  for(auto& it : map_) {
91  if(result.length() != 0) {
92  result += ";" ;
93  }
94  result += it.first ;
95  }
96  return result ;
97  }
98  T* create(const std::string& name) {
99  auto it = map_.find(name) ;
100  ogf_assert(it != map_.end()) ;
101  return it->second->create() ;
102  }
103  private:
104  FactoriesMap map_ ;
105  } ;
106 
107 //_____________________________________________________________________________________
108 
109  template<class BASE, class ARG> class BasicFactoryWithArg : public Counted {
110  public:
111  virtual BASE* create(ARG x) = 0 ;
112  } ;
113 
114  template <class BASE, class T, class ARG>
116  public:
117  virtual BASE* create(ARG x) { return new T(x) ; }
118  } ;
119 
120  template <class T, class ARG> class BasicFactoriesWithArg {
121  private:
122  typedef std::map<std::string, SmartPointer<BasicFactoryWithArg<T,ARG> > > FactoriesMap ;
123  public:
124  void register_factory(const std::string& name, BasicFactoryWithArg<T,ARG>* f) {
125  ogf_assert(map_.find(name) == map_.end()) ;
126  map_[name] = f ;
127  }
128  void unregister_factory(const std::string& name) {
129  auto it = map_.find(name) ;
130  ogf_assert(it != map_.end()) ;
131  map_.erase(it) ;
132  }
133  bool factory_is_bound(const std::string& name) const {
134  return(map_.find(name) != map_.end()) ;
135  }
136  void list_factory_names(std::vector<std::string>& names) const {
137  names.clear() ;
138  for(auto& it : map_) {
139  names.push_back(it.first) ;
140  }
141  }
142  std::string factory_names() const {
143  std::string result = "";
144  for(auto& it : map_) {
145  if(result.length() != 0) {
146  result += ";" ;
147  }
148  result += it.first ;
149  }
150  return result ;
151  }
152  T* create(const std::string& name, ARG x) {
153  auto it = map_.find(name) ;
154  ogf_assert(it != map_.end()) ;
155  return it->second->create(x) ;
156  }
157  private:
158  FactoriesMap map_ ;
159  } ;
160 
161 //_____________________________________________________________________________________
162 
163 }
164 
165 #endif
Base class for reference-counted objects.
Definition: counted.h:71
Global Graphite namespace.
Definition: common.h:76
Definitions common to all include files in the basic library.