]> git.tdb.fi Git - libs/datafile.git/blob - source/collection.cpp
Move most of Collection::get implementation to collection.cpp
[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 void Collection::add_source(CollectionSource &s)
39 {
40         sources.push_back(&s);
41 }
42
43
44 Collection::Loader::Loader(Collection &c):
45         coll(c)
46 {       
47         for(TypeList::const_iterator i = coll.types.begin(); i!=coll.types.end(); ++i)
48                 (*i)->add_to_loader(*this);
49 }
50
51
52 CollectionItemTypeBase::CollectionItemTypeBase():
53         tag(0)
54 { }
55
56 CollectionItemTypeBase::~CollectionItemTypeBase()
57 {
58         delete tag;
59 }
60
61 void CollectionItemTypeBase::set_keyword(const string &k)
62 {
63         kwd = k;
64         if(suffixes.empty())
65                 add_suffix("."+kwd);
66 }
67
68 void CollectionItemTypeBase::add_suffix(const string &s)
69 {
70         suffixes.push_back(s);
71 }
72
73 bool CollectionItemTypeBase::match_name(const string &name) const
74 {
75         for(vector<string>::const_iterator i=suffixes.begin(); i!=suffixes.end(); ++i)
76                 if(name.size()>i->size() && !name.compare(name.size()-i->size(), string::npos, *i))
77                         return true;
78         return false;
79 }
80
81 } // namespace DataFile
82 } // namespace Msp