public/IDataPack.h

Go to the documentation of this file.
00001 /**
00002  * vim: set ts=4 :
00003  * =============================================================================
00004  * SourceMod
00005  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
00006  * =============================================================================
00007  *
00008  * This program is free software; you can redistribute it and/or modify it under
00009  * the terms of the GNU General Public License, version 3.0, as published by the
00010  * Free Software Foundation.
00011  * 
00012  * This program is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00015  * details.
00016  *
00017  * You should have received a copy of the GNU General Public License along with
00018  * this program.  If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  * As a special exception, AlliedModders LLC gives you permission to link the
00021  * code of this program (as well as its derivative works) to "Half-Life 2," the
00022  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
00023  * by the Valve Corporation.  You must obey the GNU General Public License in
00024  * all respects for all other code used.  Additionally, AlliedModders LLC grants
00025  * this exception to all derivative works.  AlliedModders LLC defines further
00026  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
00027  * or <http://www.sourcemod.net/license.php>.
00028  *
00029  * Version: $Id: IDataPack.h 1964 2008-03-27 04:54:56Z damagedsoul $
00030  */
00031 
00032 #ifndef _INCLUDE_SOURCEMOD_INTERFACE_DATAPACK_H_
00033 #define _INCLUDE_SOURCEMOD_INTERFACE_DATAPACK_H_
00034 
00035 #include <sp_vm_api.h>
00036 
00037 /**
00038  * @file IDataPack.h
00039  * @brief Contains functions for packing data abstractly to/from plugins.  The wrappers
00040  * for creating these are contained in ISourceMod.h
00041  */
00042 
00043 namespace SourceMod
00044 {
00045           /**
00046            * @brief Specifies a data pack that can only be read.
00047            */
00048           class IDataReader
00049           {
00050           public:
00051                     /**
00052                      * @brief Resets the position in the data stream to the beginning.
00053                      */
00054                     virtual void Reset() const =0;
00055 
00056                     /**
00057                      * @brief Retrieves the current stream position.
00058                      *
00059                      * @return                              Index into the stream.
00060                      */
00061                     virtual size_t GetPosition() const =0;
00062 
00063                     /**
00064                      * @brief Sets the current stream position.
00065                      *
00066                      * @param pos                 Index to set the stream at.
00067                      * @return                              True if succeeded, false if out of bounds.
00068                      */
00069                     virtual bool SetPosition(size_t pos) const =0;
00070 
00071                     /**
00072                      * @brief Reads one cell from the data stream.
00073                      *
00074                      * @return                              A cell read from the current position.
00075                      */
00076                     virtual cell_t ReadCell() const =0;
00077 
00078                     /**
00079                      * @brief Reads one float from the data stream.
00080                      *
00081                      * @return                              A float read from the current position.
00082                      */
00083                     virtual float ReadFloat() const =0;
00084 
00085                     /**
00086                      * @brief Returns whether or not a specified number of bytes from the current stream
00087                      *  position to the end can be read.
00088                      * 
00089                      * @param bytes               Number of bytes to simulate reading.
00090                      * @return                              True if can be read, false otherwise.
00091                      */
00092                     virtual bool IsReadable(size_t bytes) const =0;
00093 
00094                     /**
00095                      * @brief Reads a string from the data stream.
00096                      *
00097                      * @param len                 Optional pointer to store the string length.
00098                      * @return                              Pointer to the string, or NULL if out of bounds.
00099                      */
00100                     virtual const char *ReadString(size_t *len) const =0;
00101 
00102                     /**
00103                      * @brief Reads the current position as a generic address.
00104                      *
00105                      * @return                              Pointer to the memory.
00106                      */
00107                     virtual void *GetMemory() const =0;
00108 
00109                     /**
00110                     * @brief Reads the current position as a generic data type.
00111                     *
00112                     * @param size                 Optional pointer to store the size of the data type.
00113                     * @return                     Pointer to the data, or NULL if out of bounds.
00114                     */
00115                     virtual void *ReadMemory(size_t *size) const =0;
00116           };
00117 
00118           /**
00119            * @brief Specifies a data pack that can only be written.
00120            */
00121           class IDataPack : public IDataReader
00122           {
00123           public:
00124                     /**
00125                     * @brief Resets the used size of the stream back to zero.
00126                     */
00127                     virtual void ResetSize() =0;
00128 
00129                     /**
00130                      * @brief Packs one cell into the data stream.
00131                      *
00132                      * @param cell                Cell value to write.
00133                      */
00134                     virtual void PackCell(cell_t cell) =0;
00135 
00136                     /**
00137                      * @brief Packs one float into the data stream.
00138                      *
00139                      * @param val                 Float value to write.
00140                      */
00141                     virtual void PackFloat(float val) =0;
00142 
00143                     /**
00144                      * @brief Packs one string into the data stream.
00145                      * The length is recorded as well for buffer overrun protection.
00146                      *
00147                      * @param string    String to write.
00148                      */
00149                     virtual void PackString(const char *string) =0;
00150 
00151                     /**
00152                      * @brief Creates a generic block of memory in the stream.
00153                      *
00154                      * Note that the pointer it returns can be invalidated on further
00155                      * writing, since the stream size may grow.  You may need to double back
00156                      * and fetch the pointer again.
00157                      *
00158                      * @param size                Size of the memory to create in the stream.
00159                      * @param addr                Optional pointer to store the relocated memory address.
00160                      * @return                              Current position of the stream beforehand.
00161                      */
00162                     virtual size_t CreateMemory(size_t size, void **addr) =0;
00163           };
00164 }
00165 
00166 #endif //_INCLUDE_SOURCEMOD_INTERFACE_DATAPACK_H_

Generated on Fri Nov 21 03:10:06 2008 for SourceMod SDK by  doxygen 1.5.1