fix evoral assignment operator (copy buffer)
[ardour.git] / libs / evoral / src / Event.cpp
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 #include <glib.h>
20 #include "evoral/Event.hpp"
21
22 namespace Evoral {
23
24 static event_id_t _event_id_counter = 0;
25
26 event_id_t
27 event_id_counter()
28 {
29         return g_atomic_int_get (&_event_id_counter); 
30 }
31
32 void 
33 init_event_id_counter(event_id_t n) 
34
35         g_atomic_int_set (&_event_id_counter, n); 
36 }
37
38 event_id_t
39 next_event_id ()
40 {
41         return g_atomic_int_add (&_event_id_counter, 1);
42 }
43
44 #ifdef EVORAL_EVENT_ALLOC
45
46 template<typename Timestamp>
47 Event<Timestamp>::Event(EventType type, Timestamp time, uint32_t size, uint8_t* buf, bool alloc)
48         : _type(type)
49         , _original_time(time)
50         , _nominal_time(time)
51         , _size(size)
52         , _buf(buf)
53         , _id(-1)
54         , _owns_buf(alloc)
55 {
56         if (alloc) {
57                 _buf = (uint8_t*)malloc(_size);
58                 if (buf) {
59                         memcpy(_buf, buf, _size);
60                 } else {
61                         memset(_buf, 0, _size);
62                 }
63         }
64 }
65
66 template<typename Timestamp>
67 Event<Timestamp>::Event(const Event& copy, bool owns_buf)
68         : _type(copy._type)
69         , _original_time(copy._original_time)
70         , _nominal_time(copy._nominal_time)
71         , _size(copy._size)
72         , _buf(copy._buf)
73         , _id(copy.id())
74         , _owns_buf(owns_buf)
75 {
76         if (owns_buf) {
77                 _buf = (uint8_t*)malloc(_size);
78                 if (copy._buf) {
79                         memcpy(_buf, copy._buf, _size);
80                 } else {
81                         memset(_buf, 0, _size);
82                 }
83         }
84 }
85
86 template<typename Timestamp>
87 Event<Timestamp>::~Event() {
88         if (_owns_buf) {
89                 free(_buf);
90         }
91 }
92
93 template<typename Timestamp>
94 const Event<Timestamp>&
95 Event<Timestamp>::operator=(const Event& copy)
96 {
97         _id = copy.id(); // XXX is this right? do we want ID copy semantics?
98         _type = copy._type;
99         _original_time = copy._original_time;
100         _nominal_time = copy._nominal_time;
101         _owns_buf = copy._owns_buf;
102         if (_owns_buf) {
103                 if (copy._buf) {
104                         if (copy._size > _size) {
105                                 _buf = (uint8_t*)::realloc(_buf, copy._size);
106                         }
107                         memcpy(_buf, copy._buf, copy._size);
108                 } else {
109                         free(_buf);
110                         _buf = NULL;
111                 }
112         } else {
113                 _buf = copy._buf;
114         }
115
116         _size = copy._size;
117         return *this;
118 }
119
120 template<typename Timestamp>
121 void
122 Event<Timestamp>::set (const uint8_t* buf, uint32_t size, Timestamp t)
123 {
124         if (_owns_buf) {
125                 if (_size < size) {
126                         _buf = (uint8_t*) ::realloc(_buf, size);
127                 }
128                 memcpy (_buf, buf, size);
129         } else {
130                 /* XXX this is really dangerous given the
131                    const-ness of buf. The API should really
132                    intervene here.
133                 */
134                 _buf = const_cast<uint8_t*> (buf);
135         }
136
137         _original_time = t;
138         _nominal_time = t;
139         _size = size;
140 }
141
142 template<typename Timestamp>
143 void
144 Event<Timestamp>::set_time (Timestamp t)
145 {
146         _nominal_time = t;
147 }
148
149 template<typename Timestamp>
150 void
151 Event<Timestamp>::set_original_time (Timestamp t)
152 {
153         _original_time = t;
154 }
155         
156 #endif // EVORAL_EVENT_ALLOC
157
158 template class Event<Evoral::MusicalTime>;
159 template class Event<int64_t>;
160
161 } // namespace Evoral
162