00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 00005 | | 00006 | Copyright (C) 2005-2009 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 CMEMORYSTREAM_H 00029 #define CMEMORYSTREAM_H 00030 00031 #include <mrpt/utils/CStream.h> 00032 #include <mrpt/utils/safe_pointers.h> 00033 00034 /*--------------------------------------------------------------- 00035 Class 00036 ---------------------------------------------------------------*/ 00037 namespace mrpt 00038 { 00039 namespace utils 00040 { 00041 /** This CStream derived class allow using a memory buffer as a CStream. 00042 * This class is useful for storing any required set of variables or objects, 00043 * and then read them to other objects, or storing them to a file, for example. 00044 * 00045 * \sa CStream 00046 */ 00047 class MRPTDLLIMPEXP CMemoryStream : public CStream 00048 { 00049 protected: 00050 /** Method responsible for reading from the stream. 00051 */ 00052 size_t Read(void *Buffer, size_t Count); 00053 00054 /** Method responsible for writing to the stream. 00055 * Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written. 00056 */ 00057 size_t Write(const void *Buffer, size_t Count); 00058 00059 /** Internal data 00060 */ 00061 void_ptr_noncopy m_memory; 00062 size_t m_size, m_position, m_bytesWritten; 00063 size_t m_alloc_block_size; 00064 bool m_read_only; //!< If the memory block does not belong to the object. 00065 00066 /** Resizes the internal buffer size. 00067 */ 00068 void resize(size_t newSize); 00069 public: 00070 /** Default constructor 00071 */ 00072 CMemoryStream(); 00073 00074 /** Constructor to initilize the data in the stream from a block of memory (which is copied), and sets the current stream position at the beginning of the data. 00075 * \sa assignMemoryNotOwn 00076 */ 00077 CMemoryStream( const void *data, const size_t nBytesInData ); 00078 00079 /** Initilize the data in the stream from a block of memory which is NEITHER OWNED NOR COPIED by the object, so it must exist during the whole live of the object. 00080 * After assigning a block of data with this method, the object becomes "read-only", so further attempts to change the size of the buffer will raise an exception. 00081 * This method resets the write and read positions to the beginning. 00082 */ 00083 void assignMemoryNotOwn( const void *data, const size_t nBytesInData ); 00084 00085 /** Destructor 00086 */ 00087 virtual ~CMemoryStream(); 00088 00089 /** Clears the memory buffer. 00090 */ 00091 void Clear(); 00092 00093 /** Change size. This would be rarely used. Use ">>" operators for writing to stream. 00094 * \sa Stream 00095 */ 00096 void changeSize( size_t newSize ); 00097 00098 /** Method for moving to a specified position in the streamed resource. 00099 * \sa CStream::Seek 00100 */ 00101 size_t Seek(long Offset, CStream::TSeekOrigin Origin = sFromBeginning); 00102 00103 /** Returns the total size of the internal buffer. 00104 */ 00105 size_t getTotalBytesCount(); 00106 00107 /** Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one. 00108 */ 00109 size_t getPosition(); 00110 00111 /** Method for getting a pointer to the raw stored data. 00112 * The lenght in bytes is given by getTotalBytesCount 00113 */ 00114 void* getRawBufferData(); 00115 00116 00117 /** Saves the entire buffer to a file 00118 * \return true on success, false on error 00119 */ 00120 bool saveBufferToFile( const std::string &file_name ); 00121 00122 /** Loads the entire buffer from a file 00123 * \return true on success, false on error 00124 */ 00125 bool loadBufferFromFile( const std::string &file_name ); 00126 00127 /** Change the size of the additional memory block that is reserved whenever the current block runs too short (default=0x10000 bytes) */ 00128 void setAllocBlockSize( size_t alloc_block_size ) 00129 { 00130 ASSERT_(alloc_block_size>0) 00131 m_alloc_block_size = alloc_block_size; 00132 } 00133 00134 }; // End of class def. 00135 00136 } // End of namespace 00137 } // end of namespace 00138 #endif
Page generated by Doxygen 1.6.1 for MRPT 0.7.1 SVN: at Tue Dec 22 08:29:35 CET 2009 |