public/IDataPack.h
author David Anderson <dvander@alliedmods.net>
Sun Oct 14 01:09:40 2007 +0000 (2007-10-14)
changeset 1543 53e656d87e2e
parent 1330 ab94316bb52b
child 1947 e73a1e51833b
permissions -rw-r--r--
added env vars to orig builds
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_SOURCEMOD_INTERFACE_DATAPACK_H_
    33 #define _INCLUDE_SOURCEMOD_INTERFACE_DATAPACK_H_
    34 
    35 #include <sp_vm_api.h>
    36 
    37 /**
    38  * @file IDataPack.h
    39  * @brief Contains functions for packing data abstractly to/from plugins.  The wrappers
    40  * for creating these are contained in ISourceMod.h
    41  */
    42 
    43 namespace SourceMod
    44 {
    45 	/**
    46 	 * @brief Specifies a data pack that can only be read.
    47 	 */
    48 	class IDataReader
    49 	{
    50 	public:
    51 		/**
    52 		 * @brief Resets the position in the data stream to the beginning.
    53 		 */
    54 		virtual void Reset() const =0;
    55 
    56 		/**
    57 		 * @brief Retrieves the current stream position.
    58 		 *
    59 		 * @return			Index into the stream.
    60 		 */
    61 		virtual size_t GetPosition() const =0;
    62 
    63 		/**
    64 		 * @brief Sets the current stream position.
    65 		 *
    66 		 * @param pos		Index to set the stream at.
    67 		 * @return			True if succeeded, false if out of bounds.
    68 		 */
    69 		virtual bool SetPosition(size_t pos) const =0;
    70 
    71 		/**
    72 		 * @brief Reads one cell from the data stream.
    73 		 *
    74 		 * @return			A cell read from the current position.
    75 		 */
    76 		virtual cell_t ReadCell() const =0;
    77 
    78 		/**
    79 		 * @brief Reads one float from the data stream.
    80 		 *
    81 		 * @return			A float read from the current position.
    82 		 */
    83 		virtual float ReadFloat() const =0;
    84 
    85 		/**
    86 		 * @brief Returns whether or not a specified number of bytes from the current stream
    87 		 *  position to the end can be read.
    88 		 * 
    89 		 * @param bytes		Number of bytes to simulate reading.
    90 		 * @return			True if can be read, false otherwise.
    91 		 */
    92 		virtual bool IsReadable(size_t bytes) const =0;
    93 
    94 		/**
    95 		 * @brief Reads a string from the data stream.
    96 		 *
    97 		 * @param len		Optional pointer to store the string length.
    98 		 * @return			Pointer to the string, or NULL if out of bounds.
    99 		 */
   100 		virtual const char *ReadString(size_t *len) const =0;
   101 
   102 		/**
   103 		 * @brief Reads the current position as a generic address.
   104 		 *
   105 		 * @return			Pointer to the memory.
   106 		 */
   107 		virtual void *GetMemory() const =0;
   108 
   109 		/**
   110 		* @brief Reads the current position as a generic data type.
   111 		*
   112 		* @param size		Optional pointer to store the size of the data type.
   113 		* @return			Pointer to the data, or NULL if out of bounds.
   114 		*/
   115 		virtual void *ReadMemory(size_t *size) const =0;
   116 	};
   117 
   118 	/**
   119 	 * @brief Specifies a data pack that can only be written.
   120 	 */
   121 	class IDataPack : public IDataReader
   122 	{
   123 	public:
   124 		/**
   125 		* @brief Resets the used size of the stream back to zero.
   126 		*/
   127 		virtual void ResetSize() =0;
   128 
   129 		/**
   130 		 * @brief Packs one cell into the data stream.
   131 		 *
   132 		 * @param cell		Cell value to write.
   133 		 */
   134 		virtual void PackCell(cell_t cell) =0;
   135 
   136 		/**
   137 		 * @brief Packs one float into the data stream.
   138 		 *
   139 		 * @param val		Float value to write.
   140 		 */
   141 		virtual void PackFloat(float val) =0;
   142 
   143 		/**
   144 		 * @brief Packs one string into the data stream.
   145 		 * The length is recorded as well for buffer overrun protection.
   146 		 *
   147 		 * @param string	String to write.
   148 		 */
   149 		virtual void PackString(const char *string) =0;
   150 
   151 		/**
   152 		 * @brief Creates a generic block of memory in the stream.
   153 		 *
   154 		 * Note that the pointer it returns can be invalidated on further
   155 		 * writing, since the stream size may grow.  You may need to double back
   156 		 * and fetch the pointer again.
   157 		 *
   158 		 * @param size		Size of the memory to create in the stream.
   159 		 * @param addr		Optional pointer to store the relocated memory address.
   160 		 * @return			Current position of the stream beforehand.
   161 		 */
   162 		virtual size_t CreateMemory(size_t size, void **addr) =0;
   163 	};
   164 }
   165 
   166 #endif //_INCLUDE_SOURCEMOD_INTERFACE_DATAPACK_H_