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 const Variant *var = find_var(name, type);
25 throw key_error(name);
28 const Variant *Collection::find_var(const string &name, const CollectionItemTypeBase *type)
30 ItemMap::iterator i = items.find(name);
37 if(type->can_create())
39 type->create_item(*this, name);
40 loaded = items.count(name);
42 for(SourceList::iterator j=sources.begin(); (!loaded && j!=sources.end()); ++j)
44 (*j)->load(*this, *type, name);
45 loaded = items.count(name);
47 if(!loaded && fallback)
48 if(CollectionItemTypeBase *fb_type = fallback->get_type(*type))
49 if(fallback->get_status(name, *fb_type))
50 return fallback->find_var(name, fb_type);
54 return (i!=items.end() ? &i->second : 0);
57 void Collection::gather_items(vector<const Variant *> *vars, list<string> *names, const CollectionItemTypeBase &type, bool include_sources) const
59 for(ItemMap::const_iterator i=items.begin(); i!=items.end(); ++i)
60 if(type.check_item_type(i->second))
63 vars->push_back(&i->second);
65 names->push_back(i->first);
68 if(include_sources && names)
69 gather_names_from_sources(*names, type);
72 unsigned Collection::get_status(const string &name, const CollectionItemTypeBase &type) const
74 ItemMap::const_iterator i = items.find(name);
77 for(SourceList::const_iterator j=sources.begin(); j!=sources.end(); ++j)
78 if((*j)->is_loadable(type, name))
81 if(CollectionItemTypeBase *fb_type = fallback->get_type(type))
82 return fallback->get_status(name, *fb_type);
86 return type.check_item_type(i->second);
89 CollectionItemTypeBase *Collection::get_type(const CollectionItemTypeBase &type) const
91 for(TypeList::const_iterator j=types.begin(); j!=types.end(); ++j)
92 if((*j)->is_same_type(type))
97 CollectionItemTypeBase *Collection::get_type_for_item(const Variant &var) const
99 for(TypeList::const_iterator i=types.begin(); i!=types.end(); ++i)
100 if((*i)->check_item_type(var))
105 void Collection::add_source(const CollectionSource &s)
107 sources.push_back(&s);
110 IO::Seekable *Collection::open_raw(const string &name) const
112 for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
113 if(IO::Seekable *io = (*i)->open(name))
119 void Collection::gather_names_from_sources(list<string> &names, const CollectionItemTypeBase &type) const
121 set<string> new_names;
122 for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
124 std::list<std::string> available_names = (*i)->get_names(type);
125 for(std::list<std::string>::iterator j=available_names.begin(); j!=available_names.end(); ++j)
127 new_names.insert(*j);
129 names.insert(names.end(), new_names.begin(), new_names.end());
132 void Collection::load_items_from_sources(const CollectionItemTypeBase &type)
134 for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
136 std::list<std::string> available_names = (*i)->get_names(type);
137 for(std::list<std::string>::iterator j=available_names.begin(); j!=available_names.end(); ++j)
141 if(type.can_create())
143 type.create_item(*this, *j);
144 loaded = items.count(*j);
147 (*i)->load(*this, type, *j);
152 void Collection::set_fallback(Collection *f)
158 Collection::Loader::Loader(Collection &c):
161 for(TypeList::const_iterator i = coll.types.begin(); i!=coll.types.end(); ++i)
162 (*i)->add_to_loader(*this);
166 CollectionItemTypeBase::~CollectionItemTypeBase()
168 for(vector<ExtractorBase *>::iterator i=extractors.begin(); i!=extractors.end(); ++i)
172 void CollectionItemTypeBase::set_keyword(const string &k)
179 void CollectionItemTypeBase::add_suffix(const string &s)
181 suffixes.push_back(s);
184 bool CollectionItemTypeBase::match_name(const string &name) const
186 for(vector<string>::const_iterator i=suffixes.begin(); i!=suffixes.end(); ++i)
187 if(name.size()>i->size() && !name.compare(name.size()-i->size(), string::npos, *i))
192 } // namespace DataFile