Powered by Pair ImageMagick logo
Image Magick
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals | Related Pages

ImageMagick-6.1.1/Magick++/lib/Blob.cpp

Go to the documentation of this file.
00001 // This may look like C code, but it is really -*- C++ -*- 00002 // 00003 // Copyright Bob Friesenhahn, 1999, 2000, 2001, 2002, 2004 00004 // 00005 // Implementation of Blob 00006 // 00007 00008 #define MAGICK_IMPLEMENTATION 1 00009 00010 #include "Magick++/Include.h" 00011 #include "Magick++/Blob.h" 00012 #include "Magick++/BlobRef.h" 00013 00014 #include <string.h> 00015 00016 // 00017 // Implementation of Magick::Blob 00018 // 00019 00020 // Default constructor 00021 Magick::Blob::Blob ( void ) 00022 : _blobRef(new Magick::BlobRef( 0, 0 )) 00023 { 00024 } 00025 00026 // Construct with data 00027 Magick::Blob::Blob ( const void* data_, size_t length_ ) 00028 : _blobRef(new Magick::BlobRef( data_, length_ )) 00029 { 00030 } 00031 00032 // Copy constructor (reference counted) 00033 Magick::Blob::Blob ( const Magick::Blob& blob_ ) 00034 : _blobRef(blob_._blobRef) 00035 { 00036 // Increase reference count 00037 Lock( &_blobRef->_mutexLock ); 00038 ++_blobRef->_refCount; 00039 } 00040 00041 // Destructor (reference counted) 00042 Magick::Blob::~Blob () 00043 { 00044 bool doDelete = false; 00045 { 00046 Lock( &_blobRef->_mutexLock ); 00047 if ( --_blobRef->_refCount == 0 ) 00048 doDelete = true; 00049 } 00050 00051 if ( doDelete ) 00052 { 00053 // Delete old blob reference with associated data 00054 delete _blobRef; 00055 } 00056 _blobRef=0; 00057 } 00058 00059 // Assignment operator (reference counted) 00060 Magick::Blob& Magick::Blob::operator= ( const Magick::Blob& blob_ ) 00061 { 00062 if(this != &blob_) 00063 { 00064 { 00065 Lock( &blob_._blobRef->_mutexLock ); 00066 ++blob_._blobRef->_refCount; 00067 } 00068 bool doDelete = false; 00069 { 00070 Lock( &_blobRef->_mutexLock ); 00071 if ( --_blobRef->_refCount == 0 ) 00072 doDelete = true; 00073 } 00074 if ( doDelete ) 00075 { 00076 delete _blobRef; 00077 } 00078 _blobRef = blob_._blobRef; 00079 } 00080 return *this; 00081 } 00082 00083 // Update object contents from Base64-encoded string representation. 00084 void Magick::Blob::base64 ( const std::string base64_ ) 00085 { 00086 size_t length; 00087 00088 unsigned char *decoded = 00089 Base64Decode( base64_.c_str(), &length ); 00090 00091 if(decoded) 00092 updateNoCopy( static_cast<void*>(decoded), length, 00093 Magick::Blob::MallocAllocator ); 00094 } 00095 00096 // Return Base64-encoded string representation. 00097 std::string Magick::Blob::base64 ( void ) 00098 { 00099 size_t encoded_length = 0; 00100 00101 char *encoded = 00102 Base64Encode(static_cast<const unsigned char*>(data()), length(), &encoded_length); 00103 00104 if(encoded) 00105 { 00106 std::string result(encoded,encoded_length); 00107 encoded=(char *) RelinquishMagickMemory(encoded); 00108 return result; 00109 } 00110 00111 return std::string(); 00112 } 00113 00114 // Update object contents, making a copy of the supplied data. 00115 // Any existing data in the object is deallocated. 00116 void Magick::Blob::update ( const void* data_, size_t length_ ) 00117 { 00118 bool doDelete = false; 00119 { 00120 Lock( &_blobRef->_mutexLock ); 00121 if ( --_blobRef->_refCount == 0 ) 00122 doDelete = true; 00123 } 00124 if ( doDelete ) 00125 { 00126 // Delete old blob reference with associated data 00127 delete _blobRef; 00128 } 00129 00130 _blobRef = new Magick::BlobRef( data_, length_ ); 00131 } 00132 00133 // Update object contents, using supplied pointer directly (no copy) 00134 // Any existing data in the object is deallocated. The user must 00135 // ensure that the pointer supplied is not deleted or otherwise 00136 // modified after it has been supplied to this method. 00137 void Magick::Blob::updateNoCopy ( void* data_, size_t length_, 00138 Magick::Blob::Allocator allocator_ ) 00139 { 00140 bool doDelete = false; 00141 { 00142 Lock( &_blobRef->_mutexLock ); 00143 if ( --_blobRef->_refCount == 0 ) 00144 doDelete = true; 00145 } 00146 if ( doDelete ) 00147 { 00148 // Delete old blob reference with associated data 00149 delete _blobRef; 00150 } 00151 _blobRef = new Magick::BlobRef( 0, 0 ); 00152 _blobRef->_data = data_; 00153 _blobRef->_length = length_; 00154 _blobRef->_allocator = allocator_; 00155 } 00156 00157 // Obtain pointer to data 00158 const void* Magick::Blob::data( void ) const 00159 { 00160 return _blobRef->_data; 00161 } 00162 00163 // Obtain data length 00164 size_t Magick::Blob::length( void ) const 00165 { 00166 return _blobRef->_length; 00167 } 00168

Generated on Mon Oct 25 13:40:59 2004 for ImageMagick by doxygen 1.3.7
ImageMagick Copyright © 2004, ImageMagick Studio LLC