]> git.tdb.fi Git - libs/datafile.git/blob - source/loadabletyperegistry.h
Deprecate LoadableTypeRegistry
[libs/datafile.git] / source / loadabletyperegistry.h
1 #ifndef MSP_DATAFILE_LOADABLETYPEREGISTRY_H_
2 #define MSP_DATAFILE_LOADABLETYPEREGISTRY_H_
3
4 #include <map>
5 #include <string>
6 #include <vector>
7 #include <msp/core/attributes.h>
8 #include <msp/core/maputils.h>
9 #include <msp/core/noncopyable.h>
10
11 namespace Msp {
12 namespace DataFile {
13
14 /**
15 Associates types with keywords for adding to a Loader.  The target Loader class
16 must be given as a template parameter, as well as a helper template struct to
17 handle the actual adding of keywords.
18
19 Deprecated; use TypeRegistry from mspcore instead.
20 */
21 template<typename L, template<typename> class A>
22 class DEPRECATED LoadableTypeRegistry: private NonCopyable
23 {
24 private:
25         class TypeBase
26         {
27         protected:
28                 std::string keyword;
29
30                 TypeBase(const std::string &kw): keyword(kw) { }
31         public:
32                 virtual ~TypeBase() { }
33
34                 virtual void add(L &) const = 0;
35         };
36
37         template<typename T>
38         class RegisteredType: public TypeBase
39         {
40         public:
41                 RegisteredType(const std::string &kw): TypeBase(kw) { }
42
43                 virtual void add(L &ldr) const { A<T>::add(ldr, this->keyword); }
44         };
45
46         typedef std::map<std::string, TypeBase *> TypeMap;
47
48         TypeMap types;
49
50 public:
51         ~LoadableTypeRegistry();
52
53         template<typename T>
54         void register_type(const std::string &);
55
56         void add_all(L &) const;
57 };
58
59 #pragma GCC diagnostic push
60 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
61 template<typename L, template<typename> class A>
62 LoadableTypeRegistry<L, A>::~LoadableTypeRegistry()
63 {
64         for(typename TypeMap::iterator i=types.begin(); i!=types.end(); ++i)
65                 delete i->second;
66 }
67
68 template<typename L, template<typename> class A>
69 template<typename T>
70 void LoadableTypeRegistry<L, A>::register_type(const std::string &kw)
71 {
72         if(types.count(kw))
73                 throw key_error(kw);
74
75         types[kw] = new RegisteredType<T>(kw);
76 }
77
78 template<typename L, template<typename> class A>
79 void LoadableTypeRegistry<L, A>::add_all(L &ldr) const
80 {
81         for(typename TypeMap::const_iterator i=types.begin(); i!=types.end(); ++i)
82                 i->second->add(ldr);
83 }
84 #pragma GCC diagnostic pop
85
86 } // namespace DataFile
87 } // namespace Msp
88
89 #endif