2 #include "collection.h"
9 Collection::Collection():
13 Collection::~Collection()
15 for(TypeList::iterator i = types.begin(); i!=types.end(); ++i)
19 void Collection::add_var(const string &name, const CollectionItemTypeBase *type, const Variant &var)
21 insert_unique(items, name, var);
23 type->notify_item(*this, name, var);
26 const Variant &Collection::get_var(const string &name, const CollectionItemTypeBase *type)
28 const Variant *var = find_var(name, type);
32 throw key_error(name);
35 const Variant *Collection::find_var(const string &name, const CollectionItemTypeBase *type)
37 ItemMap::iterator i = items.find(name);
44 if(type->can_create())
46 type->create_item(*this, name);
47 loaded = items.count(name);
49 for(SourceList::iterator j=sources.begin(); (!loaded && j!=sources.end()); ++j)
51 (*j)->load(*this, *type, name);
52 loaded = items.count(name);
54 if(!loaded && fallback)
55 if(CollectionItemTypeBase *fb_type = fallback->get_type(*type))
56 if(fallback->get_status(name, *fb_type))
57 return fallback->find_var(name, fb_type);
61 return (i!=items.end() ? &i->second : 0);
64 void Collection::gather_items(vector<const Variant *> *vars, list<string> *names, const CollectionItemTypeBase &type, bool include_sources) const
66 for(ItemMap::const_iterator i=items.begin(); i!=items.end(); ++i)
67 if(type.check_item_type(i->second))
70 vars->push_back(&i->second);
72 names->push_back(i->first);
75 if(include_sources && names)
76 gather_names_from_sources(*names, type);
79 unsigned Collection::get_status(const string &name, const CollectionItemTypeBase &type) const
81 ItemMap::const_iterator i = items.find(name);
84 for(SourceList::const_iterator j=sources.begin(); j!=sources.end(); ++j)
85 if((*j)->is_loadable(type, name))
88 if(CollectionItemTypeBase *fb_type = fallback->get_type(type))
89 return fallback->get_status(name, *fb_type);
93 return type.check_item_type(i->second);
96 CollectionItemTypeBase *Collection::get_type(const CollectionItemTypeBase &type) const
98 for(TypeList::const_iterator j=types.begin(); j!=types.end(); ++j)
99 if((*j)->is_same_type(type))
104 CollectionItemTypeBase *Collection::get_type_for_item(const Variant &var) const
106 for(TypeList::const_iterator i=types.begin(); i!=types.end(); ++i)
107 if((*i)->check_item_type(var))
112 void Collection::add_source(const CollectionSource &s)
114 sources.push_back(&s);
117 IO::Seekable *Collection::open_raw(const string &name) const
119 for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
120 if(IO::Seekable *io = (*i)->open(name))
126 void Collection::gather_names_from_sources(list<string> &names, const CollectionItemTypeBase &type) const
128 set<string> new_names;
129 for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
131 std::list<std::string> available_names = (*i)->get_names(type);
132 for(std::list<std::string>::iterator j=available_names.begin(); j!=available_names.end(); ++j)
134 new_names.insert(*j);
136 names.insert(names.end(), new_names.begin(), new_names.end());
139 void Collection::load_items_from_sources(const CollectionItemTypeBase &type)
141 for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
143 std::list<std::string> available_names = (*i)->get_names(type);
144 for(std::list<std::string>::iterator j=available_names.begin(); j!=available_names.end(); ++j)
148 if(type.can_create())
150 type.create_item(*this, *j);
151 loaded = items.count(*j);
154 (*i)->load(*this, type, *j);
159 void Collection::set_fallback(Collection *f)
165 Collection::Loader::Loader(Collection &c):
168 for(TypeList::const_iterator i = coll.types.begin(); i!=coll.types.end(); ++i)
169 (*i)->add_to_loader(*this);
173 CollectionItemTypeBase::~CollectionItemTypeBase()
175 for(vector<ExtractorBase *>::iterator i=extractors.begin(); i!=extractors.end(); ++i)
179 void CollectionItemTypeBase::set_keyword(const string &k)
186 void CollectionItemTypeBase::add_suffix(const string &s)
188 suffixes.push_back(s);
191 bool CollectionItemTypeBase::match_name(const string &name) const
193 for(vector<string>::const_iterator i=suffixes.begin(); i!=suffixes.end(); ++i)
194 if(name.size()>i->size() && !name.compare(name.size()-i->size(), string::npos, *i))
199 } // namespace DataFile