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 void Collection::gather_names_from_sources(list<string> &names, const CollectionItemTypeBase &type) const
96 for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
98 std::list<std::string> available_names = (*i)->get_names(type);
99 for(std::list<std::string>::iterator j=available_names.begin(); j!=available_names.end(); ++j)
105 void Collection::load_items_from_sources(const CollectionItemTypeBase &type)
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 (*i)->load(*this, type, *j);
116 void Collection::set_fallback(Collection *f)
122 Collection::Loader::Loader(Collection &c):
125 for(TypeList::const_iterator i = coll.types.begin(); i!=coll.types.end(); ++i)
126 (*i)->add_to_loader(*this);
130 CollectionItemTypeBase::~CollectionItemTypeBase()
132 for(vector<ExtractorBase *>::iterator i=extractors.begin(); i!=extractors.end(); ++i)
136 void CollectionItemTypeBase::set_keyword(const string &k)
143 void CollectionItemTypeBase::add_suffix(const string &s)
145 suffixes.push_back(s);
148 bool CollectionItemTypeBase::match_name(const string &name) const
150 for(vector<string>::const_iterator i=suffixes.begin(); i!=suffixes.end(); ++i)
151 if(name.size()>i->size() && !name.compare(name.size()-i->size(), string::npos, *i))
156 } // namespace DataFile