Main MRPT website > C++ reference
MRPT logo

CReferencedMemBlock.h

Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                   http://mrpt.sourceforge.net/                            |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef  CReferencedMemBlock_H
00029 #define  CReferencedMemBlock_H
00030 
00031 #include <mrpt/utils/utils_defs.h>
00032 #include <utility>
00033 
00034 namespace mrpt
00035 {
00036         namespace utils
00037         {
00038                 /** Represents a memory block (via "void*") that can be shared between several objects through copy operator (=).
00039                   *  It keeps the reference count and only when it comes to zero, the memory block is really freed.
00040                   */
00041                 class BASE_IMPEXP CReferencedMemBlock : public stlplus::smart_ptr< vector_byte >
00042                 {
00043                 public:
00044                         /** Constructor with an optional size of the memory block */
00045                         CReferencedMemBlock(size_t mem_block_size = 0 );
00046 
00047                         /** Destructor, calls dereference_once. */
00048                         virtual ~CReferencedMemBlock();
00049 
00050                         /** Resize the shared memory block. */
00051                         void resize(size_t mem_block_size );
00052 
00053                         template <class T> T getAs()
00054                         {
00055                                 if (!stlplus::smart_ptr< vector_byte >::present())
00056                                         THROW_EXCEPTION("Trying to access to an uninitialized memory block");
00057 
00058                                 if( stlplus::smart_ptr< vector_byte >::operator ->()->empty() )
00059                                         THROW_EXCEPTION("Trying to access to a memory block of size 0");
00060 
00061                                 return reinterpret_cast<T>( & stlplus::smart_ptr< vector_byte >::operator ->()->operator [](0) );
00062                         }
00063 
00064                         template <class T> T getAs() const
00065                         {
00066                                 if (!stlplus::smart_ptr< vector_byte >::present())
00067                                         THROW_EXCEPTION("Trying to access to an uninitialized memory block");
00068 
00069                                 if( stlplus::smart_ptr< vector_byte >::operator ->()->empty() )
00070                                         THROW_EXCEPTION("Trying to access to a memory block of size 0");
00071 
00072                                 return reinterpret_cast<const T>( & stlplus::smart_ptr< vector_byte >::operator ->()->operator [](0) );
00073                         }
00074 
00075                 }; // End of class
00076 
00077         } // End of namespace
00078 } // End of namespace
00079 
00080 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:40:17 UTC 2011