summaryrefslogtreecommitdiffstats
path: root/src/base/Event.cpp
blob: e63e51b12aaf93adde257b48d8a4c56d9420a7c8 (plain)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// -*- c-basic-offset: 4 -*-

/*
    Rosegarden
    A sequencer and musical notation editor.

    This program is Copyright 2000-2008
        Guillaume Laurent   <glaurent@telegraph-road.org>,
        Chris Cannam        <cannam@all-day-breakfast.com>,
        Richard Bown        <bownie@bownie.com>

    The moral right of the authors to claim authorship of this work
    has been asserted.

    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.  See the file
    COPYING included with this distribution for more information.
*/

#include <cstdio>
#include <cctype>
#include <iostream>
#include "Event.h"
#include "XmlExportable.h"

#if (__GNUC__ < 3)
#include <strstream>
#define stringstream strstream
#else
#include <sstream>
#endif

namespace Rosegarden 
{
using std::string;
using std::ostream;

PropertyName Event::EventData::NotationTime = "!notationtime";
PropertyName Event::EventData::NotationDuration = "!notationduration";


Event::EventData::EventData(const std::string &type, timeT absoluteTime,
			    timeT duration, short subOrdering) :
    m_refCount(1),
    m_type(type),
    m_absoluteTime(absoluteTime),
    m_duration(duration),
    m_subOrdering(subOrdering),
    m_properties(0)
{
    // empty
}

Event::EventData::EventData(const std::string &type, timeT absoluteTime,
			    timeT duration, short subOrdering,
			    const PropertyMap *properties) :
    m_refCount(1),
    m_type(type),
    m_absoluteTime(absoluteTime),
    m_duration(duration),
    m_subOrdering(subOrdering),
    m_properties(properties ? new PropertyMap(*properties) : 0)
{
    // empty
}

Event::EventData *Event::EventData::unshare()
{
    --m_refCount;

    EventData *newData = new EventData
	(m_type, m_absoluteTime, m_duration, m_subOrdering, m_properties);

    return newData;
}

Event::EventData::~EventData()
{
    if (m_properties) delete m_properties;
}

timeT
Event::EventData::getNotationTime() const
{
    if (!m_properties) return m_absoluteTime;
    PropertyMap::const_iterator i = m_properties->find(NotationTime);
    if (i == m_properties->end()) return m_absoluteTime;
    else return static_cast<PropertyStore<Int> *>(i->second)->getData();
}

timeT
Event::EventData::getNotationDuration() const
{
    if (!m_properties) return m_duration;
    PropertyMap::const_iterator i = m_properties->find(NotationDuration);
    if (i == m_properties->end()) return m_duration;
    else return static_cast<PropertyStore<Int> *>(i->second)->getData();
}

void
Event::EventData::setTime(const PropertyName &name, timeT t, timeT deft)
{
    if (!m_properties) m_properties = new PropertyMap();
    PropertyMap::iterator i = m_properties->find(name);

    if (t != deft) {
	if (i == m_properties->end()) {
	    m_properties->insert(PropertyPair(name, new PropertyStore<Int>(t)));
	} else {
	    static_cast<PropertyStore<Int> *>(i->second)->setData(t);
	}
    } else if (i != m_properties->end()) {
	delete i->second;
	m_properties->erase(i);
    }
}

PropertyMap *
Event::find(const PropertyName &name, PropertyMap::iterator &i)
{
    PropertyMap *map = m_data->m_properties;

    if (!map || ((i = map->find(name)) == map->end())) {

	map = m_nonPersistentProperties;
	if (!map) return 0;

	i = map->find(name);
	if (i == map->end()) return 0;
    }

    return map;
}

bool
Event::has(const PropertyName &name) const
{
#ifndef NDEBUG
    ++m_hasCount;
#endif

    PropertyMap::const_iterator i;
    const PropertyMap *map = find(name, i);
    if (map) return true;
    else return false;
}

void
Event::unset(const PropertyName &name)
{
#ifndef NDEBUG
    ++m_unsetCount;
#endif

    unshare();
    PropertyMap::iterator i;
    PropertyMap *map = find(name, i);
    if (map) {
	delete i->second;
	map->erase(i);
    }
}
    

PropertyType
Event::getPropertyType(const PropertyName &name) const
    // throw (NoData)
{
    PropertyMap::const_iterator i;
    const PropertyMap *map = find(name, i);
    if (map) {
        return i->second->getType();
    } else {
        throw NoData(name.getName(), __FILE__, __LINE__);
    }
}
      

string
Event::getPropertyTypeAsString(const PropertyName &name) const
    // throw (NoData)
{
    PropertyMap::const_iterator i;
    const PropertyMap *map = find(name, i);
    if (map) {
        return i->second->getTypeName();
    } else {
        throw NoData(name.getName(), __FILE__, __LINE__);
    }
}
   

string
Event::getAsString(const PropertyName &name) const
    // throw (NoData)
{
    PropertyMap::const_iterator i;
    const PropertyMap *map = find(name, i);
    if (map) {
        return i->second->unparse();
    } else {
        throw NoData(name.getName(), __FILE__, __LINE__);
    }
}

// We could derive from XmlExportable and make this a virtual method
// overriding XmlExportable's pure virtual.  We don't, because this
// class has no other virtual methods and for such a core class we
// could do without the overhead (given that it wouldn't really gain
// us anything anyway).

string
Event::toXmlString()
{
    return toXmlString(0);
}

string
Event::toXmlString(timeT expectedTime)
{
    std::stringstream out;

    out << "<event";
    
    if (getType().length() != 0) {
	out << " type=\"" << getType() << "\"";
    }

    if (getDuration() != 0) {
	out << " duration=\"" << getDuration() << "\"";
    }

    if (getSubOrdering() != 0) {
	out << " subordering=\"" << getSubOrdering() << "\"";
    }

    if (expectedTime == 0) {
	out << " absoluteTime=\"" << getAbsoluteTime() << "\"";
    } else if (getAbsoluteTime() != expectedTime) {
	out << " timeOffset=\"" << (getAbsoluteTime() - expectedTime) << "\"";
    }

    out << ">";

    // Save all persistent properties as <property> elements

    PropertyNames propertyNames(getPersistentPropertyNames());
    for (PropertyNames::const_iterator i = propertyNames.begin();
         i != propertyNames.end(); ++i) {

	out << "<property name=\""
	    << XmlExportable::encode(i->getName()) << "\" ";
	string type = getPropertyTypeAsString(*i);
	for (unsigned int j = 0; j < type.size(); ++j) {
	    type[j] = (isupper(type[j]) ? tolower(type[j]) : type[j]);
	}

	out << type << "=\""
	    << XmlExportable::encode(getAsString(*i))
	    << "\"/>";
    }

    // Save non-persistent properties (the persistence applies to
    // copying events, not load/save) as <nproperty> elements
    // unless they're view-local.  View-local properties are
    // assumed to have "::" in their name somewhere.

    propertyNames = getNonPersistentPropertyNames();
    for (PropertyNames::const_iterator i = propertyNames.begin();
         i != propertyNames.end(); ++i) {

	std::string s(i->getName());
	if (s.find("::") != std::string::npos) continue;

	out << "<nproperty name=\""
	    << XmlExportable::encode(s) << "\" ";
	string type = getPropertyTypeAsString(*i);
	for (unsigned int j = 0; j < type.size(); ++j) {
	    type[j] = (isupper(type[j]) ? tolower(type[j]) : type[j]);
	}
	out << type << "=\""
	    << XmlExportable::encode(getAsString(*i))
	    << "\"/>";
    }
  
    out << "</event>";

#if (__GNUC__ < 3)
    out << std::ends;
#endif

    return out.str();
}


#ifndef NDEBUG
void
Event::dump(ostream& out) const
{
    out << "Event type : " << m_data->m_type.c_str() << '\n';

    out << "\tAbsolute Time : " << m_data->m_absoluteTime
	<< "\n\tDuration : " << m_data->m_duration
        << "\n\tSub-ordering : " << m_data->m_subOrdering
        << "\n\tPersistent properties : \n";

    if (m_data->m_properties) {
	for (PropertyMap::const_iterator i = m_data->m_properties->begin();
	     i != m_data->m_properties->end(); ++i) {
	    out << "\t\t" << i->first.getName() << " [" << i->first.getValue() << "] \t" << *(i->second) << "\n";
	}
    }

    if (m_nonPersistentProperties) {
	out << "\n\tNon-persistent properties : \n";

	for (PropertyMap::const_iterator i = m_nonPersistentProperties->begin();
	     i != m_nonPersistentProperties->end(); ++i) {
	    out << "\t\t" << i->first.getName() << " [" << i->first.getValue() << "] \t" << *(i->second) << '\n';
	}
    }

    out << "Event storage size : " << getStorageSize() << '\n';
}


int Event::m_getCount = 0;
int Event::m_setCount = 0;
int Event::m_setMaybeCount = 0;
int Event::m_hasCount = 0;
int Event::m_unsetCount = 0;
clock_t Event::m_lastStats = clock();

void
Event::dumpStats(ostream& out)
{
    clock_t now = clock();
    int ms = (now - m_lastStats) * 1000 / CLOCKS_PER_SEC;
    out << "\nEvent stats, since start of run or last report ("
	<< ms << "ms ago):" << std::endl;

    out << "Calls to get<>: " << m_getCount << std::endl;
    out << "Calls to set<>: " << m_setCount << std::endl;
    out << "Calls to setMaybe<>: " << m_setMaybeCount << std::endl;
    out << "Calls to has: " << m_hasCount << std::endl;
    out << "Calls to unset: " << m_unsetCount << std::endl;

    m_getCount = m_setCount = m_setMaybeCount = m_hasCount = m_unsetCount = 0;
    m_lastStats = clock();
}

#else

void
Event::dumpStats(ostream&)
{
    // nothing
}

#endif

Event::PropertyNames
Event::getPropertyNames() const
{
    PropertyNames v;
    if (m_data->m_properties) {
	for (PropertyMap::const_iterator i = m_data->m_properties->begin();
	     i != m_data->m_properties->end(); ++i) {
	    v.push_back(i->first);
	}
    }
    if (m_nonPersistentProperties) {
	for (PropertyMap::const_iterator i = m_nonPersistentProperties->begin();
	     i != m_nonPersistentProperties->end(); ++i) {
	    v.push_back(i->first);
	}
    }
    return v;
}

Event::PropertyNames
Event::getPersistentPropertyNames() const
{
    PropertyNames v;
    if (m_data->m_properties) {
	for (PropertyMap::const_iterator i = m_data->m_properties->begin();
	     i != m_data->m_properties->end(); ++i) {
	    v.push_back(i->first);
	}
    }
    return v;
}

Event::PropertyNames
Event::getNonPersistentPropertyNames() const
{
    PropertyNames v;
    if (m_nonPersistentProperties) {
	for (PropertyMap::const_iterator i = m_nonPersistentProperties->begin();
	     i != m_nonPersistentProperties->end(); ++i) {
	    v.push_back(i->first);
	}
    }
    return v;
}

void
Event::clearNonPersistentProperties()
{
    if (m_nonPersistentProperties) m_nonPersistentProperties->clear();
}

size_t
Event::getStorageSize() const
{
    size_t s = sizeof(Event) + sizeof(EventData) + m_data->m_type.size();
    if (m_data->m_properties) {
	for (PropertyMap::const_iterator i = m_data->m_properties->begin();
	     i != m_data->m_properties->end(); ++i) {
	    s += sizeof(i->first);
	    s += i->second->getStorageSize();
	}
    }
    if (m_nonPersistentProperties) {
	for (PropertyMap::const_iterator i = m_nonPersistentProperties->begin();
	     i != m_nonPersistentProperties->end(); ++i) {
	    s += sizeof(i->first);
	    s += i->second->getStorageSize();
	}
    }
    return s;
}

bool
operator<(const Event &a, const Event &b)
{
    timeT at = a.getAbsoluteTime();
    timeT bt = b.getAbsoluteTime();
    if (at != bt) return at < bt;
    else return a.getSubOrdering() < b.getSubOrdering();
}

}