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)
40 for(TypeList::const_iterator j=fallback->types.begin(); j!=fallback->types.end(); ++j)
41 if((*j)->is_same_type(*type))
43 if(fallback->get_status(name, **j))
44 return fallback->get_var(name, *j);
50 return get_item(items, name);
53 void Collection::gather_items(list<const Variant *> *vars, list<string> *names, const CollectionItemTypeBase &type, bool include_sources) const
55 for(ItemMap::const_iterator i=items.begin(); i!=items.end(); ++i)
56 if(type.check_item_type(i->second))
59 vars->push_back(&i->second);
61 names->push_back(i->first);
64 if(include_sources && names)
65 gather_names_from_sources(*names, type);
68 unsigned Collection::get_status(const string &name, const CollectionItemTypeBase &type) const
70 ItemMap::const_iterator i = items.find(name);
73 for(SourceList::const_iterator j=sources.begin(); j!=sources.end(); ++j)
74 if((*j)->is_loadable(type, name))
79 return type.check_item_type(i->second);
82 CollectionItemTypeBase *Collection::get_type_for_item(const Variant &var) const
84 for(TypeList::const_iterator i=types.begin(); i!=types.end(); ++i)
85 if((*i)->check_item_type(var))
90 void Collection::add_source(CollectionSource &s)
92 sources.push_back(&s);
95 IO::Seekable *Collection::open_raw(const string &name) const
97 for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
98 if(IO::Seekable *io = (*i)->open(name))
104 void Collection::gather_names_from_sources(list<string> &names, const CollectionItemTypeBase &type) const
106 set<string> new_names;
107 for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
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)
112 new_names.insert(*j);
114 names.insert(names.end(), new_names.begin(), new_names.end());
117 void Collection::load_items_from_sources(const CollectionItemTypeBase &type)
119 for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
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)
126 if(type.can_create())
128 type.create_item(*this, *j);
129 loaded = items.count(*j);
132 (*i)->load(*this, type, *j);
137 void Collection::set_fallback(Collection *f)
143 Collection::Loader::Loader(Collection &c):
146 for(TypeList::const_iterator i = coll.types.begin(); i!=coll.types.end(); ++i)
147 (*i)->add_to_loader(*this);
151 CollectionItemTypeBase::~CollectionItemTypeBase()
153 for(vector<ExtractorBase *>::iterator i=extractors.begin(); i!=extractors.end(); ++i)
157 void CollectionItemTypeBase::set_keyword(const string &k)
164 void CollectionItemTypeBase::add_suffix(const string &s)
166 suffixes.push_back(s);
169 bool CollectionItemTypeBase::match_name(const string &name) const
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))
177 } // namespace DataFile