00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://www.mrpt.org/ | 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 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 * \ingroup mrpt_base_grp 00047 */ 00048 class BASE_IMPEXP CMemoryStream : public CStream 00049 { 00050 protected: 00051 /** Method responsible for reading from the stream. 00052 */ 00053 size_t Read(void *Buffer, size_t Count); 00054 00055 /** Method responsible for writing to the stream. 00056 * Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written. 00057 */ 00058 size_t Write(const void *Buffer, size_t Count); 00059 00060 /** Internal data 00061 */ 00062 void_ptr_noncopy m_memory; 00063 uint64_t m_size, m_position, m_bytesWritten; 00064 uint64_t m_alloc_block_size; 00065 bool m_read_only; //!< If the memory block does not belong to the object. 00066 00067 /** Resizes the internal buffer size. 00068 */ 00069 void resize(uint64_t newSize); 00070 public: 00071 /** Default constructor 00072 */ 00073 CMemoryStream(); 00074 00075 /** 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. 00076 * \sa assignMemoryNotOwn 00077 */ 00078 CMemoryStream( const void *data, const uint64_t nBytesInData ); 00079 00080 /** 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. 00081 * 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. 00082 * This method resets the write and read positions to the beginning. 00083 */ 00084 void assignMemoryNotOwn( const void *data, const uint64_t nBytesInData ); 00085 00086 /** Destructor 00087 */ 00088 virtual ~CMemoryStream(); 00089 00090 /** Clears the memory buffer. 00091 */ 00092 void Clear(); 00093 00094 /** Change size. This would be rarely used. Use ">>" operators for writing to stream. 00095 * \sa Stream 00096 */ 00097 void changeSize( uint64_t newSize ); 00098 00099 /** Method for moving to a specified position in the streamed resource. 00100 * \sa CStream::Seek 00101 */ 00102 uint64_t Seek(long Offset, CStream::TSeekOrigin Origin = sFromBeginning); 00103 00104 /** Returns the total size of the internal buffer. 00105 */ 00106 uint64_t getTotalBytesCount(); 00107 00108 /** Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one. 00109 */ 00110 uint64_t getPosition(); 00111 00112 /** Method for getting a pointer to the raw stored data. 00113 * The lenght in bytes is given by getTotalBytesCount 00114 */ 00115 void* getRawBufferData(); 00116 00117 00118 /** Saves the entire buffer to a file 00119 * \return true on success, false on error 00120 */ 00121 bool saveBufferToFile( const std::string &file_name ); 00122 00123 /** Loads the entire buffer from a file 00124 * \return true on success, false on error 00125 */ 00126 bool loadBufferFromFile( const std::string &file_name ); 00127 00128 /** Change the size of the additional memory block that is reserved whenever the current block runs too short (default=0x10000 bytes) */ 00129 void setAllocBlockSize( uint64_t alloc_block_size ) 00130 { 00131 ASSERT_(alloc_block_size>0) 00132 m_alloc_block_size = alloc_block_size; 00133 } 00134 00135 }; // End of class def. 00136 00137 } // End of namespace 00138 } // end of namespace 00139 #endif
| Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011 |