]> git.tdb.fi Git - libs/datafile.git/blob - source/collection.cpp
Invoke creators from get_list
[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 const Variant &Collection::get_var(const string &name, const CollectionItemTypeBase *type)
20 {
21         ItemMap::iterator i = items.find(name);
22         if(i!=items.end())
23                 return i->second;
24
25         if(type)
26         {
27                 bool loaded = false;
28                 if(type->can_create())
29                 {
30                         type->create_item(*this, name);
31                         loaded = items.count(name);
32                 }
33                 for(SourceList::iterator j=sources.begin(); (!loaded && j!=sources.end()); ++j)
34                 {
35                         (*j)->load(*this, *type, name);
36                         loaded = items.count(name);
37                 }
38                 if(!loaded && fallback)
39                 {
40                         for(TypeList::const_iterator j=fallback->types.begin(); j!=fallback->types.end(); ++j)
41                                 if((*j)->is_same_type(*type))
42                                 {
43                                         if(fallback->get_status(name, **j))
44                                                 return fallback->get_var(name, *j);
45                                         break;
46                                 }
47                 }
48         }
49
50         return get_item(items, name);
51 }
52
53 void Collection::gather_items(list<const Variant *> *vars, list<string> *names, const CollectionItemTypeBase &type, bool include_sources) const
54 {
55         for(ItemMap::const_iterator i=items.begin(); i!=items.end(); ++i)
56                 if(type.check_item_type(i->second))
57                 {
58                         if(vars)
59                                 vars->push_back(&i->second);
60                         if(names)
61                                 names->push_back(i->first);
62                 }
63
64         if(include_sources && names)
65                 gather_names_from_sources(*names, type);
66 }
67
68 unsigned Collection::get_status(const string &name, const CollectionItemTypeBase &type) const
69 {
70         ItemMap::const_iterator i = items.find(name);
71         if(i==items.end())
72         {
73                 for(SourceList::const_iterator j=sources.begin(); j!=sources.end(); ++j)
74                         if((*j)->is_loadable(type, name))
75                                 return 2;
76                 return 0;
77         }
78
79         return type.check_item_type(i->second);
80 }
81
82 CollectionItemTypeBase *Collection::get_type_for_item(const Variant &var) const
83 {
84         for(TypeList::const_iterator i=types.begin(); i!=types.end(); ++i)
85                 if((*i)->check_item_type(var))
86                         return *i;
87         return 0;
88 }
89
90 void Collection::add_source(CollectionSource &s)
91 {
92         sources.push_back(&s);
93 }
94
95 IO::Seekable *Collection::open_from_sources(const string &name)
96 {
97         for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
98                 if(IO::Seekable *io = (*i)->open(name))
99                         return io;
100
101         return 0;
102 }
103
104 void Collection::gather_names_from_sources(list<string> &names, const CollectionItemTypeBase &type) const
105 {
106         set<string> new_names;
107         for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
108         {
109                 std::list<std::string> available_names = (*i)->get_names(type);
110                 for(std::list<std::string>::iterator j=available_names.begin(); j!=available_names.end(); ++j)
111                         if(!items.count(*j))
112                                 new_names.insert(*j);
113         }
114         names.insert(names.end(), new_names.begin(), new_names.end());
115 }
116
117 void Collection::load_items_from_sources(const CollectionItemTypeBase &type)
118 {
119         for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
120         {
121                 std::list<std::string> available_names = (*i)->get_names(type);
122                 for(std::list<std::string>::iterator j=available_names.begin(); j!=available_names.end(); ++j)
123                         if(!items.count(*j))
124                         {
125                                 bool loaded = false;
126                                 if(type.can_create())
127                                 {
128                                         type.create_item(*this, *j);
129                                         loaded = items.count(*j);
130                                 }
131                                 if(!loaded)
132                                         (*i)->load(*this, type, *j);
133                         }
134         }
135 }
136
137 void Collection::set_fallback(Collection *f)
138 {
139         fallback = f;
140 }
141
142
143 Collection::Loader::Loader(Collection &c):
144         coll(c)
145 {       
146         for(TypeList::const_iterator i = coll.types.begin(); i!=coll.types.end(); ++i)
147                 (*i)->add_to_loader(*this);
148 }
149
150
151 CollectionItemTypeBase::~CollectionItemTypeBase()
152 {
153         for(vector<ExtractorBase *>::iterator i=extractors.begin(); i!=extractors.end(); ++i)
154                 delete *i;
155 }
156
157 void CollectionItemTypeBase::set_keyword(const string &k)
158 {
159         kwd = k;
160         if(suffixes.empty())
161                 add_suffix("."+kwd);
162 }
163
164 void CollectionItemTypeBase::add_suffix(const string &s)
165 {
166         suffixes.push_back(s);
167 }
168
169 bool CollectionItemTypeBase::match_name(const string &name) const
170 {
171         for(vector<string>::const_iterator i=suffixes.begin(); i!=suffixes.end(); ++i)
172                 if(name.size()>i->size() && !name.compare(name.size()-i->size(), string::npos, *i))
173                         return true;
174         return false;
175 }
176
177 } // namespace DataFile
178 } // namespace Msp