2 #include "collection.h"
9 Collection::Collection():
13 Collection::~Collection()
15 for(TypeList::iterator i = types.begin(); i!=types.end(); ++i)
19 const Variant &Collection::get_var(const string &name, const CollectionItemTypeBase *type)
21 ItemMap::iterator i = items.find(name);
28 if(type->can_create())
30 type->create_item(*this, name);
31 loaded = items.count(name);
33 for(SourceList::iterator j=sources.begin(); (!loaded && j!=sources.end()); ++j)
35 (*j)->load(*this, *type, name);
36 loaded = items.count(name);
38 if(!loaded && fallback)
39 if(CollectionItemTypeBase *fb_type = fallback->get_type(*type))
40 if(fallback->get_status(name, *fb_type))
41 return fallback->get_var(name, fb_type);
44 return get_item(items, name);
47 void Collection::gather_items(list<const Variant *> *vars, list<string> *names, const CollectionItemTypeBase &type, bool include_sources) const
49 for(ItemMap::const_iterator i=items.begin(); i!=items.end(); ++i)
50 if(type.check_item_type(i->second))
53 vars->push_back(&i->second);
55 names->push_back(i->first);
58 if(include_sources && names)
59 gather_names_from_sources(*names, type);
62 unsigned Collection::get_status(const string &name, const CollectionItemTypeBase &type) const
64 ItemMap::const_iterator i = items.find(name);
67 for(SourceList::const_iterator j=sources.begin(); j!=sources.end(); ++j)
68 if((*j)->is_loadable(type, name))
71 if(CollectionItemTypeBase *fb_type = fallback->get_type(type))
72 return fallback->get_status(name, *fb_type);
76 return type.check_item_type(i->second);
79 CollectionItemTypeBase *Collection::get_type(const CollectionItemTypeBase &type) const
81 for(TypeList::const_iterator j=types.begin(); j!=types.end(); ++j)
82 if((*j)->is_same_type(type))
87 CollectionItemTypeBase *Collection::get_type_for_item(const Variant &var) const
89 for(TypeList::const_iterator i=types.begin(); i!=types.end(); ++i)
90 if((*i)->check_item_type(var))
95 void Collection::add_source(const CollectionSource &s)
97 sources.push_back(&s);
100 IO::Seekable *Collection::open_raw(const string &name) const
102 for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
103 if(IO::Seekable *io = (*i)->open(name))
109 void Collection::gather_names_from_sources(list<string> &names, const CollectionItemTypeBase &type) const
111 set<string> new_names;
112 for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
114 std::list<std::string> available_names = (*i)->get_names(type);
115 for(std::list<std::string>::iterator j=available_names.begin(); j!=available_names.end(); ++j)
117 new_names.insert(*j);
119 names.insert(names.end(), new_names.begin(), new_names.end());
122 void Collection::load_items_from_sources(const CollectionItemTypeBase &type)
124 for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
126 std::list<std::string> available_names = (*i)->get_names(type);
127 for(std::list<std::string>::iterator j=available_names.begin(); j!=available_names.end(); ++j)
131 if(type.can_create())
133 type.create_item(*this, *j);
134 loaded = items.count(*j);
137 (*i)->load(*this, type, *j);
142 void Collection::set_fallback(Collection *f)
148 Collection::Loader::Loader(Collection &c):
151 for(TypeList::const_iterator i = coll.types.begin(); i!=coll.types.end(); ++i)
152 (*i)->add_to_loader(*this);
156 CollectionItemTypeBase::~CollectionItemTypeBase()
158 for(vector<ExtractorBase *>::iterator i=extractors.begin(); i!=extractors.end(); ++i)
162 void CollectionItemTypeBase::set_keyword(const string &k)
169 void CollectionItemTypeBase::add_suffix(const string &s)
171 suffixes.push_back(s);
174 bool CollectionItemTypeBase::match_name(const string &name) const
176 for(vector<string>::const_iterator i=suffixes.begin(); i!=suffixes.end(); ++i)
177 if(name.size()>i->size() && !name.compare(name.size()-i->size(), string::npos, *i))
182 } // namespace DataFile