]> git.tdb.fi Git - libs/datafile.git/blob - source/collection.cpp
Improve the API for future objects
[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 void Collection::add_future(const string &name)
15 {
16         for(TypeList::const_iterator i=types.begin(); i!=types.end(); ++i)
17                 if((*i)->match_name(name))
18                 {
19                         insert_unique(items, name, (*i)->create_future());
20                         return;
21                 }
22 }
23
24 void Collection::add_future_with_keyword(const string &name, const string &keyword)
25 {
26         for(TypeList::const_iterator i=types.begin(); i!=types.end(); ++i)
27                 if((*i)->get_keyword()==keyword)
28                 {
29                         insert_unique(items, name, (*i)->create_future());
30                         return;
31                 }
32
33         throw runtime_error("Collection::add_future_with_keyword");
34 }
35
36
37 Collection::Loader::Loader(Collection &c):
38         coll(c)
39 {       
40         for(TypeList::const_iterator i = coll.types.begin(); i!=coll.types.end(); ++i)
41                 (*i)->add_to_loader(*this);
42 }
43
44
45 CollectionItemTypeBase::CollectionItemTypeBase():
46         tag(0)
47 { }
48
49 CollectionItemTypeBase::~CollectionItemTypeBase()
50 {
51         delete tag;
52 }
53
54 void CollectionItemTypeBase::set_keyword(const string &k)
55 {
56         kwd = k;
57         if(suffixes.empty())
58                 add_suffix("."+kwd);
59 }
60
61 void CollectionItemTypeBase::add_suffix(const string &s)
62 {
63         suffixes.push_back(s);
64 }
65
66 bool CollectionItemTypeBase::match_name(const string &name) const
67 {
68         for(vector<string>::const_iterator i=suffixes.begin(); i!=suffixes.end(); ++i)
69                 if(name.size()>i->size() && !name.compare(name.size()-i->size(), string::npos, *i))
70                         return true;
71         return false;
72 }
73
74 } // namespace DataFile
75 } // namespace Msp