new internal port type, round I, plus tiny fix for legalize_for_xml() (also for 2...
[ardour.git] / libs / ardour / ardour / buffer.h
1 /*
2     Copyright (C) 2006 Paul Davis 
3     
4     This program is free software; you can redistribute it and/or modify it
5     under the terms of the GNU General Public License as published by the Free
6     Software Foundation; either version 2 of the License, or (at your option)
7     any later version.
8     
9     This program is distributed in the hope that it will be useful, but WITHOUT
10     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12     for more 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     675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #ifndef __ardour_buffer_h__
20 #define __ardour_buffer_h__
21
22 #include <cstdlib>
23 #include <cassert>
24 #include <iostream>
25 #include <boost/utility.hpp>
26 #include <ardour/types.h>
27 #include <ardour/data_type.h>
28 #include <ardour/runtime_functions.h>
29
30 namespace ARDOUR {
31
32
33 /** A buffer of recordable/playable data.
34  *
35  * This is a datatype-agnostic base class for all buffers (there are no
36  * methods to actually access the data).  This provides a way for code that
37  * doesn't care about the data type to still deal with buffers (which is
38  * why the base class can't be a template).
39  * 
40  * To actually read/write buffer contents, use the appropriate derived class.
41  */
42 class Buffer : public boost::noncopyable
43 {
44 public:
45         virtual ~Buffer() {}
46
47         /** Factory function */
48         static Buffer* create(DataType type, size_t capacity);
49
50         /** Maximum capacity of buffer.
51          * Note in some cases the entire buffer may not contain valid data, use size. */
52         size_t capacity() const { return _capacity; }
53
54         /** Amount of valid data in buffer.  Use this over capacity almost always. */
55         size_t size() const { return _size; }
56
57         /** Type of this buffer.
58          * Based on this you can static cast a Buffer* to the desired type. */
59         DataType type() const { return _type; }
60
61         bool silent() const { return _silent; }
62         
63         /** Reallocate the buffer used internally to handle at least @a size_t units of data.
64          *
65          * The buffer is not silent after this operation. the @a capacity argument
66          * passed to the constructor must have been non-zero.
67          */
68         virtual void resize(size_t) = 0;
69
70         /** Clear (eg zero, or empty) buffer starting at TIME @a offset */
71         virtual void silence(nframes_t len, nframes_t offset=0) = 0;
72         
73         /** Clear the entire buffer */
74         virtual void clear() { silence(_capacity, 0); }
75
76         virtual void read_from(const Buffer& src, nframes_t offset, nframes_t len) = 0;
77
78 protected:
79         Buffer(DataType type, size_t capacity)
80         : _type(type), _capacity(capacity), _size(0), _silent(true)
81         {}
82
83         DataType _type;
84         size_t   _capacity;
85         size_t   _size;
86         bool     _silent;
87 };
88
89
90 } // namespace ARDOUR
91
92 #endif // __ardour_buffer_h__