remove file manager LRU cache from code.
[ardour.git] / libs / evoral / evoral / Event.hpp
1 /* This file is part of Evoral.
2  * Copyright (C) 2008 David Robillard <http://drobilla.net>
3  * Copyright (C) 2000-2008 Paul Davis
4  *
5  * Evoral is free software; you can redistribute it and/or modify it under the
6  * terms of the GNU General Public License as published by the Free Software
7  * Foundation; either version 2 of the License, or (at your option) any later
8  * version.
9  *
10  * Evoral is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #ifndef EVORAL_EVENT_HPP
20 #define EVORAL_EVENT_HPP
21
22 #include <cstdlib>
23 #include <cstring>
24 #include <sstream>
25 #include <stdint.h>
26
27 #include "evoral/visibility.h"
28 #include "evoral/types.hpp"
29
30 /** If this is not defined, all methods of MidiEvent are RT safe
31  * but MidiEvent will never deep copy and (depending on the scenario)
32  * may not be usable in STL containers, signals, etc.
33  */
34 #define EVORAL_EVENT_ALLOC 1
35
36 namespace Evoral {
37
38 LIBEVORAL_API event_id_t event_id_counter();
39 LIBEVORAL_API event_id_t next_event_id();
40 LIBEVORAL_API void init_event_id_counter(event_id_t n);
41
42 /** An event (much like a type generic jack_midi_event_t)
43  *
44  * Template parameter Time is the type of the time stamp used for this event.
45  */
46 template<typename Time>
47 class LIBEVORAL_API Event {
48 public:
49 #ifdef EVORAL_EVENT_ALLOC
50         Event (EventType type=0, Time time=Time(), uint32_t size=0, uint8_t* buf=NULL, bool alloc=false);
51
52         /** Copy \a copy.
53          *
54          * If \a alloc is true, the buffer will be copied and this method
55          * is NOT REALTIME SAFE.  Otherwise both events share a buffer and
56          * memory management semantics are the caller's problem.
57          */
58         Event(const Event& copy, bool alloc);
59
60         ~Event();
61
62         const Event& operator=(const Event& copy);
63
64         void set(const uint8_t* buf, uint32_t size, Time t);
65
66         inline bool operator==(const Event& other) const {
67                 if (_type != other._type)
68                         return false;
69
70                 if (_nominal_time != other._nominal_time)
71                         return false;
72
73                 if (_original_time != other._original_time)
74                         return false;
75
76                 if (_size != other._size)
77                         return false;
78
79                 if (_buf == other._buf)
80                         return true;
81
82                 for (uint32_t i=0; i < _size; ++i)
83                         if (_buf[i] != other._buf[i])
84                                 return false;
85
86                 return true;
87         }
88
89         inline bool operator!=(const Event& other) const { return ! operator==(other); }
90
91         inline bool owns_buffer() const { return _owns_buf; }
92
93         inline void set_buffer(uint32_t size, uint8_t* buf, bool own) {
94                 if (_owns_buf) {
95                         free(_buf);
96                         _buf = NULL;
97                 }
98                 _size     = size;
99                 _buf      = buf;
100                 _owns_buf = own;
101         }
102
103         inline void realloc(uint32_t size) {
104                 if (_owns_buf) {
105                         if (size > _size)
106                                 _buf = (uint8_t*) ::realloc(_buf, size);
107                 } else {
108                         _buf = (uint8_t*) ::malloc(size);
109                         _owns_buf = true;
110                 }
111
112                 _size = size;
113         }
114
115         inline void clear() {
116                 _type          = 0;
117                 _original_time = Time();
118                 _nominal_time  = Time();
119                 _size          = 0;
120                 _buf           = NULL;
121         }
122
123 #else
124
125         inline void set_buffer(uint8_t* buf) { _buf = buf; }
126
127 #endif // EVORAL_EVENT_ALLOC
128
129         inline EventType      event_type()    const { return _type; }
130         inline Time           time()          const { return _nominal_time; }
131         inline Time           original_time() const { return _original_time; }
132         inline uint32_t       size()          const { return _size; }
133         inline const uint8_t* buffer()        const { return _buf; }
134         inline uint8_t*       buffer()              { return _buf; }
135
136         inline void set_event_type(EventType t) { _type = t; }
137
138         void set_time(Time);
139         void set_original_time(Time);
140
141         inline event_id_t id() const           { return _id; }
142         inline void       set_id(event_id_t n) { _id = n; }
143
144 protected:
145         EventType  _type; /**< Type of event (application relative, NOT MIDI 'type') */
146         Time       _original_time; /**< Sample index (or beat time) at which event is valid */
147         Time       _nominal_time; /**< Quantized version of _time, used in preference */
148         uint32_t   _size; /**< Number of uint8_ts of data in \a buffer */
149         uint8_t*   _buf;  /**< Raw MIDI data */
150         event_id_t _id; /** UUID for each event, should probably be 64bit or at least unsigned */
151 #ifdef EVORAL_EVENT_ALLOC
152         bool       _owns_buf; /**< Whether buffer is locally allocated */
153 #endif
154 };
155
156 template<typename Time>
157 /*LIBEVORAL_API*/ std::ostream& operator<<(std::ostream& o, const Evoral::Event<Time>& ev) {
158         o << "Event #" << ev.id() << " type = " << ev.event_type() << " @ " << ev.time();
159         o << std::hex;
160         for (uint32_t n = 0; n < ev.size(); ++n) {
161                 o << ' ' << (int) ev.buffer()[n];
162         }
163         o << std::dec;
164         return o;
165 }
166
167 } // namespace Evoral
168
169 #endif // EVORAL_EVENT_HPP
170