]> git.tdb.fi Git - libs/datafile.git/blob - source/collection.cpp
Add a mechanism for collections to be notified when an item is added
[libs/datafile.git] / source / collection.cpp
1 #include <set>
2 #include "collection.h"
3
4 using namespace std;
5
6 namespace Msp {
7 namespace DataFile {
8
9 Collection::Collection():
10         fallback(0)
11 { }
12
13 Collection::~Collection()
14 {
15         for(TypeList::iterator i = types.begin(); i!=types.end(); ++i)
16                 delete *i;
17 }
18
19 void Collection::add_var(const string &name, const CollectionItemTypeBase *type, const Variant &var)
20 {
21         insert_unique(items, name, var);
22         if(type)
23                 type->notify_item(*this, name, var);
24 }
25
26 const Variant &Collection::get_var(const string &name, const CollectionItemTypeBase *type)
27 {
28         const Variant *var = find_var(name, type);
29         if(var)
30                 return *var;
31
32         throw key_error(name);
33 }
34
35 const Variant *Collection::find_var(const string &name, const CollectionItemTypeBase *type)
36 {
37         ItemMap::iterator i = items.find(name);
38         if(i!=items.end())
39                 return &i->second;
40
41         if(type)
42         {
43                 bool loaded = false;
44                 if(type->can_create())
45                 {
46                         type->create_item(*this, name);
47                         loaded = items.count(name);
48                 }
49                 for(SourceList::iterator j=sources.begin(); (!loaded && j!=sources.end()); ++j)
50                 {
51                         (*j)->load(*this, *type, name);
52                         loaded = items.count(name);
53                 }
54                 if(!loaded && fallback)
55                         if(CollectionItemTypeBase *fb_type = fallback->get_type(*type))
56                                 if(fallback->get_status(name, *fb_type))
57                                         return fallback->find_var(name, fb_type);
58         }
59
60         i = items.find(name);
61         return (i!=items.end() ? &i->second : 0);
62 }
63
64 void Collection::gather_items(vector<const Variant *> *vars, list<string> *names, const CollectionItemTypeBase &type, bool include_sources) const
65 {
66         for(ItemMap::const_iterator i=items.begin(); i!=items.end(); ++i)
67                 if(type.check_item_type(i->second))
68                 {
69                         if(vars)
70                                 vars->push_back(&i->second);
71                         if(names)
72                                 names->push_back(i->first);
73                 }
74
75         if(include_sources && names)
76                 gather_names_from_sources(*names, type);
77 }
78
79 unsigned Collection::get_status(const string &name, const CollectionItemTypeBase &type) const
80 {
81         ItemMap::const_iterator i = items.find(name);
82         if(i==items.end())
83         {
84                 for(SourceList::const_iterator j=sources.begin(); j!=sources.end(); ++j)
85                         if((*j)->is_loadable(type, name))
86                                 return 2;
87                 if(fallback)
88                         if(CollectionItemTypeBase *fb_type = fallback->get_type(type))
89                                 return fallback->get_status(name, *fb_type);
90                 return 0;
91         }
92
93         return type.check_item_type(i->second);
94 }
95
96 CollectionItemTypeBase *Collection::get_type(const CollectionItemTypeBase &type) const
97 {
98         for(TypeList::const_iterator j=types.begin(); j!=types.end(); ++j)
99                 if((*j)->is_same_type(type))
100                         return *j;
101         return 0;
102 }
103
104 CollectionItemTypeBase *Collection::get_type_for_item(const Variant &var) const
105 {
106         for(TypeList::const_iterator i=types.begin(); i!=types.end(); ++i)
107                 if((*i)->check_item_type(var))
108                         return *i;
109         return 0;
110 }
111
112 void Collection::add_source(const CollectionSource &s)
113 {
114         sources.push_back(&s);
115 }
116
117 IO::Seekable *Collection::open_raw(const string &name) const
118 {
119         for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
120                 if(IO::Seekable *io = (*i)->open(name))
121                         return io;
122
123         return 0;
124 }
125
126 void Collection::gather_names_from_sources(list<string> &names, const CollectionItemTypeBase &type) const
127 {
128         set<string> new_names;
129         for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
130         {
131                 std::list<std::string> available_names = (*i)->get_names(type);
132                 for(std::list<std::string>::iterator j=available_names.begin(); j!=available_names.end(); ++j)
133                         if(!items.count(*j))
134                                 new_names.insert(*j);
135         }
136         names.insert(names.end(), new_names.begin(), new_names.end());
137 }
138
139 void Collection::load_items_from_sources(const CollectionItemTypeBase &type)
140 {
141         for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
142         {
143                 std::list<std::string> available_names = (*i)->get_names(type);
144                 for(std::list<std::string>::iterator j=available_names.begin(); j!=available_names.end(); ++j)
145                         if(!items.count(*j))
146                         {
147                                 bool loaded = false;
148                                 if(type.can_create())
149                                 {
150                                         type.create_item(*this, *j);
151                                         loaded = items.count(*j);
152                                 }
153                                 if(!loaded)
154                                         (*i)->load(*this, type, *j);
155                         }
156         }
157 }
158
159 void Collection::set_fallback(Collection *f)
160 {
161         fallback = f;
162 }
163
164
165 Collection::Loader::Loader(Collection &c):
166         coll(c)
167 {       
168         for(TypeList::const_iterator i = coll.types.begin(); i!=coll.types.end(); ++i)
169                 (*i)->add_to_loader(*this);
170 }
171
172
173 CollectionItemTypeBase::~CollectionItemTypeBase()
174 {
175         for(vector<ExtractorBase *>::iterator i=extractors.begin(); i!=extractors.end(); ++i)
176                 delete *i;
177 }
178
179 void CollectionItemTypeBase::set_keyword(const string &k)
180 {
181         kwd = k;
182         if(suffixes.empty())
183                 add_suffix("."+kwd);
184 }
185
186 void CollectionItemTypeBase::add_suffix(const string &s)
187 {
188         suffixes.push_back(s);
189 }
190
191 bool CollectionItemTypeBase::match_name(const string &name) const
192 {
193         for(vector<string>::const_iterator i=suffixes.begin(); i!=suffixes.end(); ++i)
194                 if(name.size()>i->size() && !name.compare(name.size()-i->size(), string::npos, *i))
195                         return true;
196         return false;
197 }
198
199 } // namespace DataFile
200 } // namespace Msp