Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Buffer.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Barbarian Group
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
6  the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and
9  the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
11  the following disclaimer in the documentation and/or other materials provided with the distribution.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
15  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
17  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20  POSSIBILITY OF SUCH DAMAGE.
21 */
22 
23 #pragma once
24 
25 #include "cinder/Cinder.h"
26 
27 #define DEFAULT_COMPRESSION_LEVEL 6
28 
29 namespace cinder {
30 
31 class Buffer {
32  private:
33  struct Obj {
34  Obj( void * aBuffer, size_t aSize, bool aOwnsData );
35  ~Obj();
36 
37  void * mData;
38  size_t mAllocatedSize;
39  size_t mDataSize;
40  bool mOwnsData;
41  };
42 
43  public:
44  Buffer() {}
45  Buffer( void * aBuffer, size_t aSize );
46  Buffer( size_t size );
48  explicit Buffer( std::shared_ptr<class DataSource> dataSource );
49 
50  size_t getAllocatedSize() const { return mObj->mAllocatedSize; }
51  size_t getDataSize() const { return mObj->mDataSize; }
52  void setDataSize( size_t aSize ) { mObj->mDataSize = aSize; }
53 
54  void * getData() { return mObj->mData; }
55  const void * getData() const { return mObj->mData; }
56 
58  std::shared_ptr<uint8_t> convertToSharedPtr();
59 
60  void resize( size_t newSize );
61 
62  void copyFrom( const void * aData, size_t length );
63  //TODO: copy from region of another buffer
64 
66  void write( std::shared_ptr<class DataTarget> dataTarget );
67 
68  private:
69  std::shared_ptr<Obj> mObj;
70 
71  public:
73  typedef std::shared_ptr<Obj> Buffer::*unspecified_bool_type;
75  operator unspecified_bool_type() const { return ( mObj.get() == 0 ) ? 0 : &Buffer::mObj; }
76  void reset() { mObj.reset(); }
78 };
79 
80 Buffer compressBuffer( const Buffer &aBuffer, int8_t compressionLevel = DEFAULT_COMPRESSION_LEVEL, bool resizeResult = true );
81 Buffer decompressBuffer( const Buffer &aBuffer, bool resizeResult = true, bool useGZip = false );
82 
83 } //namespace
Buffer decompressBuffer(const Buffer &aBuffer, bool resizeResult=true, bool useGZip=false)
Definition: Buffer.cpp:108
std::shared_ptr< Obj > Buffer::* unspecified_bool_type
Emulates shared_ptr-like behavior.
Definition: Buffer.h:74
size_t getAllocatedSize() const
Definition: Buffer.h:50
size_t getDataSize() const
Definition: Buffer.h:51
void reset()
Emulates shared_ptr-like behavior.
Definition: Buffer.h:76
Definition: Buffer.h:31
void copyFrom(const void *aData, size_t length)
Definition: Buffer.cpp:72
std::shared_ptr< uint8_t > convertToSharedPtr()
Returns a shared_ptr for the data and gives up ownership of the data.
Definition: Buffer.cpp:83
#define DEFAULT_COMPRESSION_LEVEL
Definition: Buffer.h:27
GLuint GLsizei GLsizei * length
Definition: GLee.h:2313
void write(std::shared_ptr< class DataTarget > dataTarget)
Writes a Buffer to a DataTarget.
Definition: Buffer.cpp:77
void resize(size_t newSize)
Definition: Buffer.cpp:63
Buffer compressBuffer(const Buffer &aBuffer, int8_t compressionLevel=DEFAULT_COMPRESSION_LEVEL, bool resizeResult=true)
Definition: Buffer.cpp:89
void setDataSize(size_t aSize)
Definition: Buffer.h:52
Buffer()
Definition: Buffer.h:44
void * getData()
Definition: Buffer.h:54
const void * getData() const
Definition: Buffer.h:55
BufferT< float > Buffer
Definition: Buffer.h:285
GLsizeiptr size
Definition: GLee.h:2089