-
Notifications
You must be signed in to change notification settings - Fork 41
/
objectstore.cpp
196 lines (160 loc) · 4.5 KB
/
objectstore.cpp
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/***************************************************************************
objectstore.cpp: store of Objects
-------------------
begin : November 22, 2006
copyright : (C) 2006 The University of Toronto
email : netterfield@astro.utoronto.ca
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "objectstore.h"
#include <QHash>
#include <QList>
#include <QString>
#include "object.h"
#include "datavector.h"
#include "datastring.h"
#include "datascalar.h"
#include "datamatrix.h"
// NAMEDEBUG: 0 for no debug, 1 for some debug, 2 for more debug, 3 for all debug
#define NAMEDEBUG 0
namespace Kst {
ObjectStore::ObjectStore()
{
override.fileName.clear();
override.f0 = override.N = override.skip = override.doAve = -5;
}
ObjectStore::~ObjectStore()
{
}
bool ObjectStore::removeObject(Object *o) {
if (!this) {
return false;
}
if (!o) {
return false;
}
KstWriteLocker l(&this->_lock);
DataSourcePtr ds = kst_cast<DataSource>(o);
if (ds) {
if (!_dataSourceList.contains(ds)) {
return false;
}
} else if (!_list.contains(o)) {
return false;
}
if (ds) {
ds->deleteDependents();
_dataSourceList.removeAll(ds);
} else {
o->deleteDependents();
_list.removeAll(o);
}
o->_store = 0;
return true;
}
ObjectPtr ObjectStore::retrieveObject(const QString& name) const {
int match = -1;
if (name.isEmpty()) {
return NULL;
}
QString shortName;
QRegExp rx("(\\(|^)([A-Z]\\d+)(\\)$|$)");
rx.indexIn(name);
shortName = rx.cap(2);
// 1) search for short names
int size = _list.size();
for (int i = 0; i < size; ++i) {
if (_list.at(i)->shortName()==shortName)
return _list.at(i);
}
// 3) search for descriptive names: must be unique
for (int i = 0; i < size; ++i) {
if (_list.at(i)->descriptiveName() == name) {
if (match != -1)
return NULL; // not unique, so... no match
match = i;
}
}
if (match >-1)
return _list.at(match);
return NULL;
}
void ObjectStore::rebuildDataSourceList() {
cleanUpDataSourceList();
foreach (DataSourcePtr ds, _dataSourceList) {
ds->writeLock();
ds->reset();
ds->unlock();
}
foreach (ObjectPtr object, _list) {
object->writeLock();
object->reset();
object->unlock();
}
}
void ObjectStore::cleanUpDataSourceList() {
DataSourceList currentSourceList;
currentSourceList.clear();
currentSourceList.append(_dataSourceList);
// clean up unused data sources
for (DataSourceList::Iterator it = currentSourceList.begin(); it != currentSourceList.end(); ++it) {
if ((*it)->getUsage() <= 1) {
removeObject(*it);
//qDebug() << "remove" << (*it)->fileName() << "usage" << (*it)->getUsage();
} else {
//qDebug() << " keep" << (*it)->fileName() << "usage" << (*it)->getUsage();
}
}
currentSourceList.clear();
}
bool ObjectStore::isEmpty() const {
KstReadLocker l(&_lock);
return _list.isEmpty();
}
void ObjectStore::clear() {
KstWriteLocker l(&_lock);
#if NAMEDEBUG > 0
qDebug () << "Clearing object store " << (void*) this;
#endif
foreach(DataSource *ds, _dataSourceList) {
removeObject(ds);
}
foreach(Object *o, _list) {
removeObject(o);
}
// Reset the named objects id's.
NamedObject::resetNameIndex();
}
DataSourceList ObjectStore::dataSourceList() const {
KstReadLocker l(&_lock);
return _dataSourceList;
}
QList<ObjectPtr> ObjectStore::objectList() {
KstReadLocker l(&_lock);
return _list;
}
void ObjectStore::clearUsedFlags() {
foreach (ObjectPtr p, _list) {
p->setUsed(false);
}
}
bool ObjectStore::deleteUnsetUsedFlags() {
QList<ObjectPtr> list = _list;
bool some_deleted = false;
foreach (ObjectPtr p, list) {
if (!p->used()) {
removeObject(p);
some_deleted = true;
}
}
return some_deleted;
}
}
// vim: ts=2 sw=2 et