2 #include <msp/core/algorithm.h>
3 #include "collection.h"
10 Collection::Collection():
14 Collection::~Collection()
16 for(CollectionItemTypeBase *t: types)
20 void Collection::add_var(const string &name, const CollectionItemTypeBase *type, const Variant &var)
22 insert_unique(items, name, var);
24 type->notify_item(name, var);
27 const Variant &Collection::get_var(const string &name, const CollectionItemTypeBase *type)
29 const Variant *var = find_var(name, type);
33 throw key_error(name);
36 const Variant *Collection::find_var(const string &name, const CollectionItemTypeBase *type)
38 ItemMap::iterator i = items.find(name);
45 if(type->can_create())
47 type->create_item(*this, name);
48 loaded = items.count(name);
50 for(auto j=sources.begin(); (!loaded && j!=sources.end()); ++j)
52 (*j)->load(*this, *type, name);
53 loaded = items.count(name);
55 if(!loaded && fallback)
56 if(CollectionItemTypeBase *fb_type = fallback->get_type(*type))
57 if(fallback->get_status(name, *fb_type))
58 return fallback->find_var(name, fb_type);
62 return (i!=items.end() ? &i->second : 0);
65 void Collection::gather_items(vector<const Variant *> *vars, list<string> *names, const CollectionItemTypeBase &type, bool include_sources) const
67 for(const auto &kvp: items)
68 if(type.check_item_type(kvp.second))
71 vars->push_back(&kvp.second);
73 names->push_back(kvp.first);
76 if(include_sources && names)
77 gather_names_from_sources(*names, type);
80 unsigned Collection::get_status(const string &name, const CollectionItemTypeBase &type) const
82 ItemMap::const_iterator i = items.find(name);
85 auto j = find_if(sources, [&name, &type](const CollectionSource *s){ return s->is_loadable(type, name); });
89 if(CollectionItemTypeBase *fb_type = fallback->get_type(type))
90 return fallback->get_status(name, *fb_type);
94 return type.check_item_type(i->second);
97 CollectionItemTypeBase *Collection::get_type(const CollectionItemTypeBase &type) const
99 for(CollectionItemTypeBase *t: types)
100 if(t->is_same_type(type))
105 CollectionItemTypeBase *Collection::get_type_for_item(const Variant &var) const
107 for(CollectionItemTypeBase *t: types)
108 if(t->check_item_type(var))
113 void Collection::add_source(const CollectionSource &s)
115 sources.push_back(&s);
118 IO::Seekable *Collection::open_raw(const string &name) const
120 for(const CollectionSource *s: sources)
121 if(IO::Seekable *io = s->open(name))
127 void Collection::gather_names_from_sources(list<string> &names, const CollectionItemTypeBase &type) const
129 set<string> new_names;
130 for(const CollectionSource *s: sources)
131 for(const string &n: s->get_names(type))
134 names.insert(names.end(), new_names.begin(), new_names.end());
137 void Collection::load_items_from_sources(const CollectionItemTypeBase &type)
139 for(const CollectionSource *s: sources)
140 for(const string &n: s->get_names(type))
144 if(type.can_create())
146 type.create_item(*this, n);
147 loaded = items.count(n);
150 s->load(*this, type, n);
154 void Collection::set_fallback(Collection *f)
160 Collection::Loader::Loader(Collection &c):
163 for(const CollectionItemTypeBase *t: coll.types)
164 t->add_to_loader(*this);
168 CollectionItemTypeBase::~CollectionItemTypeBase()
170 for(ExtractorBase *e: extractors)
174 void CollectionItemTypeBase::set_keyword(const string &k)
181 void CollectionItemTypeBase::add_suffix(const string &s)
183 suffixes.push_back(s);
186 bool CollectionItemTypeBase::match_name(const string &name) const
188 for(const string &s: suffixes)
189 if(name.size()>s.size() && !name.compare(name.size()-s.size(), string::npos, s))
194 } // namespace DataFile