libvisual  0.5.0
lv_buffer.h
1 /* Libvisual - The audio visualisation framework.
2  *
3  * Copyright (C) 2012 Libvisual team
4  * 2004-2006 Dennis Smit
5  *
6  * Authors: Chong Kai Xiong <kaixiong@codeleft.sg>
7  * Dennis Smit <ds@nerds-incorporated.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as
11  * published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
24 #ifndef _LV_BUFFER_H
25 #define _LV_BUFFER_H
26 
27 #include <libvisual/lvconfig.h>
28 #include <libvisual/lv_defines.h>
29 #include <libvisual/lv_types.h>
30 
36 #ifdef __cplusplus
37 
38 #include <libvisual/lv_intrusive_ptr.hpp>
39 #include <memory>
40 #include <cstdlib>
41 
42 namespace LV {
43 
44  class Buffer;
45 
46  typedef IntrusivePtr<Buffer> BufferPtr;
47  typedef IntrusivePtr<Buffer const> BufferConstPtr;
48 
50  class LV_API Buffer
51  {
52  public:
53 
54  Buffer (Buffer const&) = delete;
55 
56  Buffer& operator= (Buffer const&) = delete;
57 
61  static BufferPtr create ();
62 
72  static BufferPtr wrap (void *data, std::size_t size, bool own = true);
73 
79  static BufferPtr create (std::size_t size);
80 
81  ~Buffer ();
82 
86  void destroy_content ();
87 
96  void set (void* data, std::size_t size);
97 
106  void set_size (std::size_t size);
107 
116  void set_data (void* data);
117 
126  void allocate (std::size_t size);
127 
133  void* get_data () const;
134 
142  void* get_data (std::size_t offset) const;
143 
149  std::size_t get_size () const;
150 
156  bool is_allocated () const;
157 
163  void copy (BufferConstPtr const& src);
164 
170  void copy_to (BufferPtr const& dest);
171 
178  void copy_to (void *dest, std::size_t size);
179 
186  void put (BufferConstPtr const& src, std::size_t offset);
187 
195  void put (void const* data, std::size_t size, std::size_t offset);
196 
202  void fill (uint8_t value);
203 
210  void fill_with_pattern (void const* data, std::size_t size);
211 
212  private:
213 
214  friend void intrusive_ptr_add_ref (Buffer const* buffer);
215  friend void intrusive_ptr_release (Buffer const* buffer);
216 
217  class Impl;
218  const std::unique_ptr<Impl> m_impl;
219 
220  mutable unsigned int m_ref_count;
221 
222  Buffer ();
223  };
224 
225  inline void intrusive_ptr_add_ref (Buffer const* buffer)
226  {
227  buffer->m_ref_count++;
228  }
229 
230  inline void intrusive_ptr_release (Buffer const* buffer)
231  {
232  if (--buffer->m_ref_count == 0) {
233  delete buffer;
234  }
235  }
236 
237 } // LV namespace
238 
239 #endif /* __cplusplus */
240 
241 #ifdef __cplusplus
242 typedef LV::Buffer VisBuffer;
243 #else
244 typedef struct _VisBuffer VisBuffer;
245 struct _VisBuffer;
246 #endif
247 
248 LV_BEGIN_DECLS
249 
250 LV_API VisBuffer *visual_buffer_new (void);
251 LV_API VisBuffer *visual_buffer_new_wrap_data (void *data, visual_size_t size, int own);
252 LV_API VisBuffer *visual_buffer_new_allocate (visual_size_t size);
253 LV_API VisBuffer *visual_buffer_clone (VisBuffer *source);
254 
255 LV_API void visual_buffer_set_data_pair (VisBuffer *buffer, void *data, visual_size_t size);
256 LV_API void visual_buffer_set_data (VisBuffer *buffer, void *data);
257 LV_API void *visual_buffer_get_data (VisBuffer *buffer);
258 LV_API void *visual_buffer_get_data_offset (VisBuffer *buffer, visual_size_t offset);
259 
260 LV_API void visual_buffer_set_size (VisBuffer *buffer, visual_size_t size);
261 LV_API visual_size_t visual_buffer_get_size (VisBuffer *buffer);
262 
263 LV_API int visual_buffer_is_allocated (VisBuffer *buffer);
264 LV_API void visual_buffer_allocate (VisBuffer *buffer, visual_size_t size);
265 LV_API void visual_buffer_destroy_content (VisBuffer *buffer);
266 
267 LV_API void visual_buffer_copy_to (VisBuffer *src, VisBuffer *dest);
268 LV_API void visual_buffer_copy_to_data (VisBuffer *src, void *dest, visual_size_t size);
269 
270 LV_API void visual_buffer_put (VisBuffer *dest, VisBuffer *src, visual_size_t offset);
271 LV_API void visual_buffer_put_data (VisBuffer *dest, const void *data, visual_size_t size, visual_size_t offset);
272 
273 LV_API void visual_buffer_fill (VisBuffer *buffer, uint8_t value);
274 LV_API void visual_buffer_fill_with_pattern (VisBuffer *buffer, const void *data, visual_size_t size);
275 
276 LV_API void visual_buffer_ref (VisBuffer *buffer);
277 LV_API void visual_buffer_unref (VisBuffer *buffer);
278 
279 LV_END_DECLS
280 
285 #endif /* _LV_BUFFER_H */