1 | // |
---|
2 | // Manifest.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/Manifest.h#1 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: SharedLibrary |
---|
8 | // Module: ClassLoader |
---|
9 | // |
---|
10 | // Definition of the Manifest class. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_Manifest_INCLUDED |
---|
40 | #define Foundation_Manifest_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/MetaObject.h" |
---|
45 | #include <map> |
---|
46 | #include <typeinfo> |
---|
47 | |
---|
48 | |
---|
49 | namespace Poco { |
---|
50 | |
---|
51 | |
---|
52 | class Foundation_API ManifestBase |
---|
53 | /// ManifestBase is a common base class for |
---|
54 | /// all instantiations of Manifest. |
---|
55 | { |
---|
56 | public: |
---|
57 | ManifestBase(); |
---|
58 | virtual ~ManifestBase(); |
---|
59 | |
---|
60 | virtual const char* className() const = 0; |
---|
61 | /// Returns the type name of the manifest's class. |
---|
62 | }; |
---|
63 | |
---|
64 | |
---|
65 | template <class B> |
---|
66 | class Manifest: public ManifestBase |
---|
67 | /// A Manifest maintains a list of all classes |
---|
68 | /// contained in a dynamically loadable class |
---|
69 | /// library. |
---|
70 | /// Internally, the information is held |
---|
71 | /// in a map. An iterator is provided to |
---|
72 | /// iterate over all the classes in a Manifest. |
---|
73 | { |
---|
74 | public: |
---|
75 | typedef AbstractMetaObject<B> Meta; |
---|
76 | typedef std::map<std::string, const Meta*> MetaMap; |
---|
77 | |
---|
78 | class Iterator |
---|
79 | /// The Manifest's very own iterator class. |
---|
80 | { |
---|
81 | public: |
---|
82 | Iterator(const typename MetaMap::const_iterator& it) |
---|
83 | { |
---|
84 | _it = it; |
---|
85 | } |
---|
86 | Iterator(const Iterator& it) |
---|
87 | { |
---|
88 | _it = it._it; |
---|
89 | } |
---|
90 | ~Iterator() |
---|
91 | { |
---|
92 | } |
---|
93 | Iterator& operator = (const Iterator& it) |
---|
94 | { |
---|
95 | _it = it._it; |
---|
96 | return *this; |
---|
97 | } |
---|
98 | inline bool operator == (const Iterator& it) const |
---|
99 | { |
---|
100 | return _it == it._it; |
---|
101 | } |
---|
102 | inline bool operator != (const Iterator& it) const |
---|
103 | { |
---|
104 | return _it != it._it; |
---|
105 | } |
---|
106 | Iterator& operator ++ () // prefix |
---|
107 | { |
---|
108 | ++_it; |
---|
109 | return *this; |
---|
110 | } |
---|
111 | Iterator operator ++ (int) // postfix |
---|
112 | { |
---|
113 | Iterator result(_it); |
---|
114 | ++_it; |
---|
115 | return result; |
---|
116 | } |
---|
117 | inline const Meta* operator * () const |
---|
118 | { |
---|
119 | return _it->second; |
---|
120 | } |
---|
121 | inline const Meta* operator -> () const |
---|
122 | { |
---|
123 | return _it->second; |
---|
124 | } |
---|
125 | |
---|
126 | private: |
---|
127 | typename MetaMap::const_iterator _it; |
---|
128 | }; |
---|
129 | |
---|
130 | Manifest() |
---|
131 | /// Creates an empty Manifest. |
---|
132 | { |
---|
133 | } |
---|
134 | |
---|
135 | virtual ~Manifest() |
---|
136 | /// Destroys the Manifest. |
---|
137 | { |
---|
138 | clear(); |
---|
139 | } |
---|
140 | |
---|
141 | Iterator find(const std::string& className) const |
---|
142 | /// Returns an iterator pointing to the MetaObject |
---|
143 | /// for the given class. If the MetaObject cannot |
---|
144 | /// be found, the iterator points to end(). |
---|
145 | { |
---|
146 | return Iterator(_metaMap.find(className)); |
---|
147 | } |
---|
148 | |
---|
149 | Iterator begin() const |
---|
150 | { |
---|
151 | return Iterator(_metaMap.begin()); |
---|
152 | } |
---|
153 | |
---|
154 | Iterator end() const |
---|
155 | { |
---|
156 | return Iterator(_metaMap.end()); |
---|
157 | } |
---|
158 | |
---|
159 | bool insert(const Meta* pMeta) |
---|
160 | /// Inserts a MetaObject. Returns true if insertion |
---|
161 | /// was successful, false if a class with the same |
---|
162 | /// name already exists. |
---|
163 | { |
---|
164 | return _metaMap.insert(typename MetaMap::value_type(pMeta->name(), pMeta)).second; |
---|
165 | } |
---|
166 | |
---|
167 | void clear() |
---|
168 | /// Removes all MetaObjects from the manifest. |
---|
169 | { |
---|
170 | for (typename MetaMap::iterator it = _metaMap.begin(); it != _metaMap.end(); ++it) |
---|
171 | { |
---|
172 | delete it->second; |
---|
173 | } |
---|
174 | _metaMap.clear(); |
---|
175 | } |
---|
176 | |
---|
177 | int size() const |
---|
178 | /// Returns the number of MetaObjects in the Manifest. |
---|
179 | { |
---|
180 | return int(_metaMap.size()); |
---|
181 | } |
---|
182 | |
---|
183 | bool empty() const |
---|
184 | /// Returns true iff the Manifest does not contain any MetaObjects. |
---|
185 | { |
---|
186 | return _metaMap.empty(); |
---|
187 | } |
---|
188 | |
---|
189 | const char* className() const |
---|
190 | { |
---|
191 | return typeid(*this).name(); |
---|
192 | } |
---|
193 | |
---|
194 | private: |
---|
195 | MetaMap _metaMap; |
---|
196 | }; |
---|
197 | |
---|
198 | |
---|
199 | } // namespace Poco |
---|
200 | |
---|
201 | |
---|
202 | #endif // Foundation_Manifest_INCLUDED |
---|