1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
/*
Copyright (C) 2000 Stefan Westerfeld
stefan@space.twc.de
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef ARTS_NAMEDSTORE_H
#define ARTS_NAMEDSTORE_H
#include <string>
#include <list>
#include <vector>
#include "stdio.h"
/*
* BC - Status (2002-03-08): NamedStore
*
* None of these classes is considered part of the public API. Do NOT use it
* in your apps. These are part of the implementation of object.cc and not
* to be used elsewhere.
*/
namespace Arts {
/**
* -- internal class --
*
* this stores key-value pairs, where key is a string which is kept unique
*/
template<class T>
class NamedStore
{
private:
class Element {
public:
T t;
std::string name;
Element(const T& t, const std::string& name) :t(t), name(name) { }
};
typedef std::list<Element> Element_list;
Element_list elements;
public:
bool get(const std::string& name, T& result)
{
typename Element_list::iterator i;
for(i = elements.begin(); i != elements.end(); i++)
{
if(i->name == name)
{
result = i->t;
return true;
}
}
return false;
}
bool remove(const std::string& name)
{
typename Element_list::iterator i;
for(i = elements.begin(); i != elements.end(); i++)
{
if(i->name == name)
{
elements.erase(i);
return true;
}
}
return false;
}
std::vector<std::string> *contents()
{
std::vector<std::string> *result = new std::vector<std::string>;
typename Element_list::iterator i;
for(i = elements.begin(); i != elements.end(); i++)
result->push_back(i->name);
return result;
}
std::string put(const std::string& name, const T& t)
{
std::string xname = name;
int append = 1;
for(;;)
{
typename Element_list::iterator i;
i = elements.begin();
while(i != elements.end() && i->name != xname)
i++;
if(i == elements.end())
{
elements.push_back(Element(t,xname));
return xname;
}
char buffer[1024];
sprintf(buffer,"%d",append++);
xname = name + std::string(buffer);
}
}
};
}
#endif /* ARTS_NAMEDSTORE_H */
|