Webmaster  |  Imprint 
C++ Server Pages
Main  |  License  |  Documentation  |  Download 

/home/tommi/tntdb/include/tntdb/bits/blob.h

00001 /*
00002  * Copyright (C) 2007-2008 Tommi Maekitalo
00003  * Copyright (C) 2007-2008 Marc Boris Duerner
00004  * 
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2.1 of the License, or (at your option) any later version.
00009  * 
00010  * As a special exception, you may use this file as part of a free
00011  * software library without restriction. Specifically, if other files
00012  * instantiate templates or use macros or inline functions from this
00013  * file, or you compile this file and link it with other files to
00014  * produce an executable, this file does not by itself cause the
00015  * resulting executable to be covered by the GNU General Public
00016  * License. This exception does not however invalidate any other
00017  * reasons why the executable file might be covered by the GNU Library
00018  * General Public License.
00019  * 
00020  * This library is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00023  * Lesser General Public License for more details.
00024  * 
00025  * You should have received a copy of the GNU Lesser General Public
00026  * License along with this library; if not, write to the Free Software
00027  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00028  */
00029 
00030 #ifndef TNTDB_BITS_BLOB_H
00031 #define TNTDB_BITS_BLOB_H
00032 
00033 #include <tntdb/iface/iblob.h>
00034 #include <tntdb/impl/blob.h>
00035 #include <cxxtools/smartptr.h>
00036 
00037 namespace tntdb
00038 {
00039 
00042 class Blob
00043 {
00044 public:
00046     template <typename T>
00047     struct Release
00048     {
00049         void destroy(IBlob* blob)
00050         { blob->destroy(); }
00051     };
00052 
00053 private:
00055     cxxtools::SmartPtr< IBlob, cxxtools::InternalRefCounted, Release > m_data;
00056 
00057 public:
00058     Blob()
00059     : m_data( BlobImpl::emptyInstance() )
00060     { }
00061 
00068     Blob(const char* data, std::size_t len)
00069     : m_data( new BlobImpl() )
00070     {
00071         m_data->assign(data, len);
00072     }
00073 
00076     explicit Blob(IBlob* b)
00077     : m_data(b)
00078     { }
00079 
00082     void assign(const char* data, std::size_t len)
00083     {
00084         // copy-on-write
00085         if ( m_data->refs() > 1 )
00086         {
00087             m_data = m_data->create();
00088         }
00089 
00090         m_data->assign(data, len);
00091     }
00092 
00097     char* reserve(std::size_t len, bool shrink = false)
00098     {
00099         return m_data->reserve(len, shrink);
00100     }
00101 
00102     bool operator==(const Blob& b) const
00103     {
00104         return *m_data == *b.m_data;
00105     }
00106 
00107     bool operator!=(const Blob& b) const
00108     {
00109         return !operator==(b);
00110     }
00111 
00114     const char* data() const
00115     {
00116         return  m_data->data();
00117     }
00118 
00121     std::size_t size() const
00122     {
00123         return m_data->size();
00124     }
00125 
00126 };
00127 
00128 }
00129 
00130 #endif // TNTDB_BITS_BLOB_H
00131 
Copyright © 2008 The Tntnet Development Team
Tntnet 1.6