source: XMLIO_V2/external/include/Poco/MetaObject.h @ 80

Last change on this file since 80 was 80, checked in by ymipsl, 14 years ago

ajout lib externe

  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1//
2// MetaObject.h
3//
4// $Id: //poco/1.3/Foundation/include/Poco/MetaObject.h#1 $
5//
6// Library: Foundation
7// Package: SharedLibrary
8// Module:  ClassLoader
9//
10// Definition of the MetaObject 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_MetaObject_INCLUDED
40#define Foundation_MetaObject_INCLUDED
41
42
43#include "Poco/Foundation.h"
44#include "Poco/Exception.h"
45#include "Poco/SingletonHolder.h"
46#include <set>
47
48
49namespace Poco {
50
51
52template <class B>
53class AbstractMetaObject
54        /// A MetaObject stores some information
55        /// about a C++ class. The MetaObject class
56        /// is used by the Manifest class.
57        /// AbstractMetaObject is a common base class
58        /// for all MetaObject in a rooted class hierarchy.
59        /// A MetaObject can also be used as an object
60        /// factory for its class.
61{
62public:
63        AbstractMetaObject(const char* name): _name(name)
64        {
65        }
66
67        virtual ~AbstractMetaObject()
68        {
69                for (typename ObjectSet::iterator it = _deleteSet.begin(); it != _deleteSet.end(); ++it)
70                {
71                        delete *it;
72                }
73        }
74
75        const char* name() const
76        {
77                return _name;
78        }
79
80        virtual B* create() const = 0;
81                /// Create a new instance of a class.
82                /// Cannot be used for singletons.
83               
84        virtual B& instance() const = 0;
85                /// Returns a reference to the only instance
86                /// of the class. Used for singletons only.
87
88        virtual bool canCreate() const = 0;
89                /// Returns true iff the create method can be used
90                /// to create instances of the class.
91                /// Returns false if the class is a singleton.
92
93        virtual void destroy(B* pObject) const
94                /// If pObject was owned by meta object, the
95                /// ownership of the deleted object is removed
96                /// and the object is deleted.
97        {
98                typename ObjectSet::iterator it = _deleteSet.find(pObject);
99               
100                if (it != _deleteSet.end())
101                {
102                        _deleteSet.erase(pObject);
103                        delete pObject;
104                }
105        }
106
107        B* autoDelete(B* pObject) const
108                /// Give ownership of pObject to the meta object.
109                /// The meta object will delete all objects it owns
110                /// when it is destroyed.
111                ///
112                /// Returns pObject.
113        {
114                if (this->canCreate()) // guard against singleton
115                {
116                        poco_check_ptr (pObject);
117                        _deleteSet.insert(pObject);
118                }
119                else throw InvalidAccessException("Cannot take ownership of", this->name());
120
121                return pObject;
122        }
123
124        virtual bool isAutoDelete(B* pObject) const
125                /// Returns true if the object is owned
126                /// by meta object.
127                ///
128                /// Overloaded in MetaSingleton - returns true
129                /// if the class is a singleton.
130        {
131                return _deleteSet.find(pObject) != _deleteSet.end();
132        }
133
134private:
135        AbstractMetaObject();
136        AbstractMetaObject(const AbstractMetaObject&);
137        AbstractMetaObject& operator = (const AbstractMetaObject&);
138
139        typedef std::set<B*> ObjectSet;
140       
141        const char* _name;
142        mutable ObjectSet _deleteSet;
143};
144
145
146template <class C, class B>
147class MetaObject: public AbstractMetaObject<B>
148        /// A MetaObject stores some information
149        /// about a C++ class. The MetaObject class
150        /// is used by the Manifest class.
151        /// A MetaObject can also be used as an object
152        /// factory for its class.
153{
154public:
155        MetaObject(const char* name): AbstractMetaObject<B>(name)
156        {
157        }
158
159        ~MetaObject()
160        {
161        }
162
163        B* create() const
164        {
165                return new C;
166        }
167       
168        B& instance() const
169        {
170                throw InvalidAccessException("Not a singleton. Use create() to create instances of", this->name());
171        }
172       
173        bool canCreate() const
174        {
175                return true;
176        }
177};
178
179
180template <class C, class B> 
181class MetaSingleton: public AbstractMetaObject<B> 
182        /// A SingletonMetaObject disables the create() method
183        /// and instead offers an instance() method to access
184        /// the single instance of its class.
185{ 
186public: 
187        MetaSingleton(const char* name): AbstractMetaObject<B>(name) 
188        {
189        }
190       
191        ~MetaSingleton() 
192        {
193        }
194       
195        B* create() const
196        {
197                throw InvalidAccessException("Cannot create instances of a singleton class. Use instance() to obtain a", this->name());
198        }
199       
200        bool canCreate() const
201        {
202                return false;
203        }
204
205        B& instance() const
206        {
207                return *_object.get();
208        }
209     
210        bool isAutoDelete(B* pObject) const
211        {
212                return true;
213        }
214
215private: 
216        mutable SingletonHolder<C> _object; 
217}; 
218
219
220} // namespace Poco
221
222
223#endif // Foundation_MetaObject_INCLUDED
Note: See TracBrowser for help on using the repository browser.