public/IDataPack.h
author Scott Ehlert <ds@alliedmods.net>
Sun Jul 15 08:00:26 2007 +0000 (2007-07-15)
changeset 1116 40cc5e9f0f1b
parent 488 daf1bd692ef3
child 1226 478550a4d5ec
permissions -rw-r--r--
Removed strange debugging code
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod, Copyright (C) 2004-2007 AlliedModders LLC. 
     5  * All rights reserved.
     6  * ===============================================================
     7  *
     8  *  This file is part of the SourceMod/SourcePawn SDK.  This file may only be 
     9  * used or modified under the Terms and Conditions of its License Agreement, 
    10  * which is found in public/licenses/LICENSE.txt.  As of this notice, derivative 
    11  * works must be licensed under the GNU General Public License (version 2 or 
    12  * greater).  A copy of the GPL is included under public/licenses/GPL.txt.
    13  * 
    14  * To view the latest information, see: http://www.sourcemod.net/license.php
    15  *
    16  * Version: $Id$
    17  */
    18 
    19 #ifndef _INCLUDE_SOURCEMOD_INTERFACE_DATAPACK_H_
    20 #define _INCLUDE_SOURCEMOD_INTERFACE_DATAPACK_H_
    21 
    22 #include <sp_vm_api.h>
    23 
    24 /**
    25  * @file IDataPack.h
    26  * @brief Contains functions for packing data abstractly to/from plugins.  The wrappers
    27  * for creating these are contained in ISourceMod.h
    28  */
    29 
    30 namespace SourceMod
    31 {
    32 	/**
    33 	 * @brief Specifies a data pack that can only be read.
    34 	 */
    35 	class IDataReader
    36 	{
    37 	public:
    38 		/**
    39 		 * @brief Resets the position in the data stream to the beginning.
    40 		 */
    41 		virtual void Reset() const =0;
    42 
    43 		/**
    44 		 * @brief Retrieves the current stream position.
    45 		 *
    46 		 * @return			Index into the stream.
    47 		 */
    48 		virtual size_t GetPosition() const =0;
    49 
    50 		/**
    51 		 * @brief Sets the current stream position.
    52 		 *
    53 		 * @param pos		Index to set the stream at.
    54 		 * @return			True if succeeded, false if out of bounds.
    55 		 */
    56 		virtual bool SetPosition(size_t pos) const =0;
    57 
    58 		/**
    59 		 * @brief Reads one cell from the data stream.
    60 		 *
    61 		 * @return			A cell read from the current position.
    62 		 */
    63 		virtual cell_t ReadCell() const =0;
    64 
    65 		/**
    66 		 * @brief Reads one float from the data stream.
    67 		 *
    68 		 * @return			A float read from the current position.
    69 		 */
    70 		virtual float ReadFloat() const =0;
    71 
    72 		/**
    73 		 * @brief Returns whether or not a specified number of bytes from the current stream
    74 		 *  position to the end can be read.
    75 		 * 
    76 		 * @param bytes		Number of bytes to simulate reading.
    77 		 * @return			True if can be read, false otherwise.
    78 		 */
    79 		virtual bool IsReadable(size_t bytes) const =0;
    80 
    81 		/**
    82 		 * @brief Reads a string from the data stream.
    83 		 *
    84 		 * @param len		Optional pointer to store the string length.
    85 		 * @return			Pointer to the string, or NULL if out of bounds.
    86 		 */
    87 		virtual const char *ReadString(size_t *len) const =0;
    88 
    89 		/**
    90 		 * @brief Reads the current position as a generic address.
    91 		 *
    92 		 * @return			Pointer to the memory.
    93 		 */
    94 		virtual void *GetMemory() const =0;
    95 
    96 		/**
    97 		* @brief Reads the current position as a generic data type.
    98 		*
    99 		* @param size		Optional pointer to store the size of the data type.
   100 		* @return			Pointer to the data, or NULL if out of bounds.
   101 		*/
   102 		virtual void *ReadMemory(size_t *size) const =0;
   103 	};
   104 
   105 	/**
   106 	 * @brief Specifies a data pack that can only be written.
   107 	 */
   108 	class IDataPack : public IDataReader
   109 	{
   110 	public:
   111 		/**
   112 		* @brief Resets the used size of the stream back to zero.
   113 		*/
   114 		virtual void ResetSize() =0;
   115 
   116 		/**
   117 		 * @brief Packs one cell into the data stream.
   118 		 *
   119 		 * @param cell		Cell value to write.
   120 		 */
   121 		virtual void PackCell(cell_t cell) =0;
   122 
   123 		/**
   124 		 * @brief Packs one float into the data stream.
   125 		 *
   126 		 * @param val		Float value to write.
   127 		 */
   128 		virtual void PackFloat(float val) =0;
   129 
   130 		/**
   131 		 * @brief Packs one string into the data stream.
   132 		 * The length is recorded as well for buffer overrun protection.
   133 		 *
   134 		 * @param string	String to write.
   135 		 */
   136 		virtual void PackString(const char *string) =0;
   137 
   138 		/**
   139 		 * @brief Creates a generic block of memory in the stream.
   140 		 *
   141 		 * Note that the pointer it returns can be invalidated on further
   142 		 * writing, since the stream size may grow.  You may need to double back
   143 		 * and fetch the pointer again.
   144 		 *
   145 		 * @param size		Size of the memory to create in the stream.
   146 		 * @param addr		Optional pointer to store the relocated memory address.
   147 		 * @return			Current position of the stream beforehand.
   148 		 */
   149 		virtual size_t CreateMemory(size_t size, void **addr) =0;
   150 	};
   151 }
   152 
   153 #endif //_INCLUDE_SOURCEMOD_INTERFACE_DATAPACK_H_