1 #include "collection.h"
8 Collection::Collection():
12 Collection::~Collection()
14 for(TypeList::iterator i = types.begin(); i!=types.end(); ++i)
18 const Variant &Collection::get_var(const string &name, const CollectionItemTypeBase *type)
20 ItemMap::iterator i = items.find(name);
27 if(type->can_create())
29 type->create_item(*this, name);
30 loaded = items.count(name);
32 for(SourceList::iterator j=sources.begin(); (!loaded && j!=sources.end()); ++j)
34 (*j)->load(*this, *type, name);
35 loaded = items.count(name);
37 if(!loaded && fallback)
39 for(TypeList::const_iterator j=fallback->types.begin(); j!=fallback->types.end(); ++j)
40 if((*j)->is_same_type(*type))
42 if(fallback->get_status(name, **j))
43 return fallback->get_var(name, *j);
49 return get_item(items, name);
52 void Collection::gather_items(list<const Variant *> *vars, list<string> *names, const CollectionItemTypeBase &type, bool include_sources) const
54 for(ItemMap::const_iterator i=items.begin(); i!=items.end(); ++i)
55 if(type.check_item_type(i->second))
58 vars->push_back(&i->second);
60 names->push_back(i->first);
63 if(include_sources && names)
64 gather_names_from_sources(*names, type);
67 unsigned Collection::get_status(const string &name, const CollectionItemTypeBase &type) const
69 ItemMap::const_iterator i = items.find(name);
72 for(SourceList::const_iterator j=sources.begin(); j!=sources.end(); ++j)
73 if((*j)->is_loadable(type, name))
78 return type.check_item_type(i->second);
81 CollectionItemTypeBase *Collection::get_type_for_item(const Variant &var) const
83 for(TypeList::const_iterator i=types.begin(); i!=types.end(); ++i)
84 if((*i)->check_item_type(var))
89 void Collection::add_source(CollectionSource &s)
91 sources.push_back(&s);
94 IO::Seekable *Collection::open_from_sources(const string &name)
96 for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
97 if(IO::Seekable *io = (*i)->open(name))
103 void Collection::gather_names_from_sources(list<string> &names, const CollectionItemTypeBase &type) const
105 for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
107 std::list<std::string> available_names = (*i)->get_names(type);
108 for(std::list<std::string>::iterator j=available_names.begin(); j!=available_names.end(); ++j)
114 void Collection::load_items_from_sources(const CollectionItemTypeBase &type)
116 for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
118 std::list<std::string> available_names = (*i)->get_names(type);
119 for(std::list<std::string>::iterator j=available_names.begin(); j!=available_names.end(); ++j)
121 (*i)->load(*this, type, *j);
125 void Collection::set_fallback(Collection *f)
131 Collection::Loader::Loader(Collection &c):
134 for(TypeList::const_iterator i = coll.types.begin(); i!=coll.types.end(); ++i)
135 (*i)->add_to_loader(*this);
139 CollectionItemTypeBase::~CollectionItemTypeBase()
141 for(vector<ExtractorBase *>::iterator i=extractors.begin(); i!=extractors.end(); ++i)
145 void CollectionItemTypeBase::set_keyword(const string &k)
152 void CollectionItemTypeBase::add_suffix(const string &s)
154 suffixes.push_back(s);
157 bool CollectionItemTypeBase::match_name(const string &name) const
159 for(vector<string>::const_iterator i=suffixes.begin(); i!=suffixes.end(); ++i)
160 if(name.size()>i->size() && !name.compare(name.size()-i->size(), string::npos, *i))
165 } // namespace DataFile