comploader.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003-2005 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 
30 #ifndef TNT_COMPLOADER_H
31 #define TNT_COMPLOADER_H
32 
33 #include <cxxtools/smartptr.h>
34 #include <tnt/urlmapper.h>
35 #include <tnt/langlib.h>
36 #include <map>
37 #include <list>
38 #include <string>
39 #include <utility>
40 #include <dlfcn.h>
41 
42 namespace tnt
43 {
44  class Comploader;
45  class ComponentFactory;
46 
49  {
50  std::string libname;
51  public:
52  explicit LibraryNotFound(const std::string& libname_)
53  : libname(libname_)
54  { }
55  const std::string& getLibname() const { return libname; }
56  };
57 
58  template <typename objectType>
59  class Dlcloser
60  {
61  protected:
62  void destroy(objectType* ptr)
63  {
64  ::dlclose(*ptr);
65  delete ptr;
66  }
67  };
68 
70  {
71  friend class Comploader;
72 
73  typedef void* HandleType;
75  HandlePointer handlePtr;
76 
77  typedef std::map<std::string, ComponentFactory*> factoryMapType;
78  factoryMapType factoryMap;
79 
80  std::string libname;
81  std::string path;
82  typedef std::map<std::string, LangLib::PtrType> langlibsType;
83  langlibsType langlibs;
84 
85  void* dlopen(const std::string& name, bool local);
86  void init(const std::string& name, bool local);
87 
88  public:
90  { }
91 
92  ComponentLibrary(const std::string& path_, const std::string& name, bool local)
93  : libname(name),
94  path(path_)
95  { init(path_ + '/' + name, local); }
96 
97  ComponentLibrary(const std::string& name, bool local)
98  : libname(name)
99  { init(name, local); }
100 
101  operator const void* () const { return handlePtr.getPointer(); }
102 
103  Component* create(const std::string& component_name, Comploader& cl,
104  const Urlmapper& rootmapper);
105  LangLib::PtrType getLangLib(const std::string& lang);
106 
107  const std::string& getName() const { return libname; }
108  void registerFactory(const std::string& component_name, ComponentFactory* factory)
109  { factoryMap.insert(factoryMapType::value_type(component_name, factory)); }
110  };
111 
113  {
114  typedef std::map<std::string, ComponentLibrary> librarymap_type;
115  typedef std::map<Compident, Component*> componentmap_type;
116 
117  // loaded libraries
118  static librarymap_type& getLibrarymap();
119 
120  // map soname/compname to compinstance
121  componentmap_type componentmap;
122  static ComponentLibrary::factoryMapType* currentFactoryMap;
123 
124  public:
125  Component& fetchComp(const Compident& compident,
126  const Urlmapper& rootmapper = Urlmapper());
127  Component* createComp(const Compident& compident,
128  const Urlmapper& rootmapper);
129  const char* getLangData(const Compident& compident, const std::string& lang);
130 
131  // lookup library; load if needed
132  ComponentLibrary& fetchLib(const std::string& libname);
133 
134  static void registerFactory(const std::string& component_name, ComponentFactory* factory);
135  };
136 }
137 
138 #endif // TNT_COMPLOADER_H
139