1//
2// Manifest.h
3//
4// Library: Foundation
5// Package: SharedLibrary
6// Module: ClassLoader
7//
8// Definition of the Manifest class.
9//
10// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
11// and Contributors.
12//
13// SPDX-License-Identifier: BSL-1.0
14//
15
16
17#ifndef Foundation_Manifest_INCLUDED
18#define Foundation_Manifest_INCLUDED
19
20
21#include "Poco/Foundation.h"
22#include "Poco/MetaObject.h"
23#include <map>
24#include <typeinfo>
25
26
27namespace Poco {
28
29
30class Foundation_API ManifestBase
31 /// ManifestBase is a common base class for
32 /// all instantiations of Manifest.
33{
34public:
35 ManifestBase();
36 virtual ~ManifestBase();
37
38 virtual const char* className() const = 0;
39 /// Returns the type name of the manifest's class.
40};
41
42
43template <class B>
44class Manifest: public ManifestBase
45 /// A Manifest maintains a list of all classes
46 /// contained in a dynamically loadable class
47 /// library.
48 /// Internally, the information is held
49 /// in a map. An iterator is provided to
50 /// iterate over all the classes in a Manifest.
51{
52public:
53 typedef AbstractMetaObject<B> Meta;
54 typedef std::map<std::string, const Meta*> MetaMap;
55
56 class Iterator
57 /// The Manifest's very own iterator class.
58 {
59 public:
60 Iterator(const typename MetaMap::const_iterator& it)
61 {
62 _it = it;
63 }
64 Iterator(const Iterator& it)
65 {
66 _it = it._it;
67 }
68 ~Iterator()
69 {
70 }
71 Iterator& operator = (const Iterator& it)
72 {
73 _it = it._it;
74 return *this;
75 }
76 inline bool operator == (const Iterator& it) const
77 {
78 return _it == it._it;
79 }
80 inline bool operator != (const Iterator& it) const
81 {
82 return _it != it._it;
83 }
84 Iterator& operator ++ () // prefix
85 {
86 ++_it;
87 return *this;
88 }
89 Iterator operator ++ (int) // postfix
90 {
91 Iterator result(_it);
92 ++_it;
93 return result;
94 }
95 inline const Meta* operator * () const
96 {
97 return _it->second;
98 }
99 inline const Meta* operator -> () const
100 {
101 return _it->second;
102 }
103
104 private:
105 typename MetaMap::const_iterator _it;
106 };
107
108 Manifest()
109 /// Creates an empty Manifest.
110 {
111 }
112
113 virtual ~Manifest()
114 /// Destroys the Manifest.
115 {
116 clear();
117 }
118
119 Iterator find(const std::string& rClassName) const
120 /// Returns an iterator pointing to the MetaObject
121 /// for the given class. If the MetaObject cannot
122 /// be found, the iterator points to end().
123 {
124 return Iterator(_metaMap.find(rClassName));
125 }
126
127 Iterator begin() const
128 {
129 return Iterator(_metaMap.begin());
130 }
131
132 Iterator end() const
133 {
134 return Iterator(_metaMap.end());
135 }
136
137 bool insert(const Meta* pMeta)
138 /// Inserts a MetaObject. Returns true if insertion
139 /// was successful, false if a class with the same
140 /// name already exists.
141 {
142 return _metaMap.insert(typename MetaMap::value_type(pMeta->name(), pMeta)).second;
143 }
144
145 void clear()
146 /// Removes all MetaObjects from the manifest.
147 {
148 for (typename MetaMap::iterator it = _metaMap.begin(); it != _metaMap.end(); ++it)
149 {
150 delete it->second;
151 }
152 _metaMap.clear();
153 }
154
155 int size() const
156 /// Returns the number of MetaObjects in the Manifest.
157 {
158 return int(_metaMap.size());
159 }
160
161 bool empty() const
162 /// Returns true iff the Manifest does not contain any MetaObjects.
163 {
164 return _metaMap.empty();
165 }
166
167 const char* className() const
168 {
169 return typeid(*this).name();
170 }
171
172private:
173 MetaMap _metaMap;
174};
175
176
177} // namespace Poco
178
179
180#endif // Foundation_Manifest_INCLUDED
181