]> git.tdb.fi Git - libs/datafile.git/blob - source/collection.cpp
More flexible system for handling base classes in Collection
[libs/datafile.git] / source / collection.cpp
1 #include "collection.h"
2
3 using namespace std;
4
5 namespace Msp {
6 namespace DataFile {
7
8 Collection::~Collection()
9 {
10         for(TypeList::iterator i = types.begin(); i!=types.end(); ++i)
11                 delete *i;
12 }
13
14 const Variant &Collection::get_var(const string &name, const CollectionItemTypeBase *type)
15 {
16         ItemMap::iterator i = items.find(name);
17         if(i!=items.end())
18                 return i->second;
19
20         if(type)
21         {
22                 bool loaded = false;
23                 if(type->can_create())
24                 {
25                         type->create_item(*this, name);
26                         loaded = items.count(name);
27                 }
28                 for(SourceList::iterator j=sources.begin(); (!loaded && j!=sources.end()); ++j)
29                 {
30                         (*j)->load(*this, *type, name);
31                         loaded = items.count(name);
32                 }
33         }
34
35         return get_item(items, name);
36 }
37
38 CollectionItemTypeBase *Collection::get_type_for_item(const Variant &var) const
39 {
40         for(TypeList::const_iterator i=types.begin(); i!=types.end(); ++i)
41                 if((*i)->check_item_type(var))
42                         return *i;
43         return 0;
44 }
45
46 void Collection::add_source(CollectionSource &s)
47 {
48         sources.push_back(&s);
49 }
50
51
52 Collection::Loader::Loader(Collection &c):
53         coll(c)
54 {       
55         for(TypeList::const_iterator i = coll.types.begin(); i!=coll.types.end(); ++i)
56                 (*i)->add_to_loader(*this);
57 }
58
59
60 CollectionItemTypeBase::~CollectionItemTypeBase()
61 {
62         for(vector<ExtractorBase *>::iterator i=extractors.begin(); i!=extractors.end(); ++i)
63                 delete *i;
64 }
65
66 void CollectionItemTypeBase::set_keyword(const string &k)
67 {
68         kwd = k;
69         if(suffixes.empty())
70                 add_suffix("."+kwd);
71 }
72
73 void CollectionItemTypeBase::add_suffix(const string &s)
74 {
75         suffixes.push_back(s);
76 }
77
78 bool CollectionItemTypeBase::match_name(const string &name) const
79 {
80         for(vector<string>::const_iterator i=suffixes.begin(); i!=suffixes.end(); ++i)
81                 if(name.size()>i->size() && !name.compare(name.size()-i->size(), string::npos, *i))
82                         return true;
83         return false;
84 }
85
86 } // namespace DataFile
87 } // namespace Msp