⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 typereg_8h-source.html

📁 用来介绍ZIG Library游戏网络引擎的文档
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>typereg.h Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.2.18 --><center><a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="hierarchy.html">Class Hierarchy</a> &nbsp; <a class="qindex" href="annotated.html">Compound List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="functions.html">Compound Members</a> &nbsp; <a class="qindex" href="globals.html">File Members</a> &nbsp; </center><hr><h1>typereg.h</h1><a href="typereg_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <span class="comment">/*</span>00002 <span class="comment">    ZIG - An extendable, portable game engine focused on networking &amp; scripting</span>00003 <span class="comment">    Project Home: http://zige.sourceforge.net</span>00004 <span class="comment">    Copyright (C) 2002  F醔io Reis Cecin &lt;fcecin AT inf DOT ufrgs DOT br&gt;</span>00005 <span class="comment"></span>00006 <span class="comment">    This library is free software; you can redistribute it and/or</span>00007 <span class="comment">    modify it under the terms of the GNU Lesser General Public</span>00008 <span class="comment">    License as published by the Free Software Foundation; either</span>00009 <span class="comment">    version 2.1 of the License, or (at your option) any later version.</span>00010 <span class="comment"></span>00011 <span class="comment">    This library is distributed in the hope that it will be useful,</span>00012 <span class="comment">    but WITHOUT ANY WARRANTY; without even the implied warranty of</span>00013 <span class="comment">    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU</span>00014 <span class="comment">    Lesser General Public License for more details.</span>00015 <span class="comment"></span>00016 <span class="comment">    You should have received a copy of the GNU Lesser General Public</span>00017 <span class="comment">    License along with this library; if not, write to the Free Software</span>00018 <span class="comment">    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   </span>00019 <span class="comment"></span>00020 <span class="comment">----------------</span>00021 <span class="comment"></span>00022 <span class="comment">CTypeMaker, CTypeRegister, CTypeCreator</span>00023 <span class="comment"></span>00024 <span class="comment">   by Paulo Zaffari</span>00025 <span class="comment"></span>00026 <span class="comment">----------------------------------------------------</span>00027 <span class="comment">  HOW TO ADD OBJECT SERIALIZATION TO YOUR ZIG APP:</span>00028 <span class="comment">----------------------------------------------------</span>00029 <span class="comment"></span>00030 <span class="comment">---------------------------------------------------------</span>00031 <span class="comment">(1) define the class that has "serialization" support</span>00032 <span class="comment">---------------------------------------------------------</span>00033 <span class="comment"></span>00034 <span class="comment">#include &lt;typereg.h&gt;    //class "registry" support</span>00035 <span class="comment">#include &lt;buffer.h&gt;             //buffer_c &amp; serializable_c</span>00036 <span class="comment"></span>00037 <span class="comment">// you MUST EXTEND FROM serializable_c</span>00038 <span class="comment">//</span>00039 <span class="comment">//  can be indirect, like:</span>00040 <span class="comment">//    class blah : public superblah</span>00041 <span class="comment">//    class superblah : public serializable_c</span>00042 <span class="comment">//</span>00043 <span class="comment">//  can use multiple inheritance too:</span>00044 <span class="comment">//    class blah : public superblah, public serializable_c</span>00045 <span class="comment">//</span>00046 <span class="comment">class blah : public serializable_c {</span>00047 <span class="comment">public:</span>00048 <span class="comment"></span>00049 <span class="comment">        // MUST add this magical tag as member of the serializable class</span>00050 <span class="comment">        ZIG_SERIALIZABLE_CLASS_H(blah);</span>00051 <span class="comment"></span>00052 <span class="comment">        // define some fields to be persisted</span>00053 <span class="comment">        int a;</span>00054 <span class="comment">        int b;</span>00055 <span class="comment"></span>00056 <span class="comment">        // define some fields that are not persisted ("transient")</span>00057 <span class="comment">        int whatever;</span>00058 <span class="comment"></span>00059 <span class="comment">        blah() { a = b = 0; whatever = 333; }</span>00060 <span class="comment"></span>00061 <span class="comment">        // implement this serializable_c interface</span>00062 <span class="comment">        virtual void write(buffer_c &amp;out) const {</span>00063 <span class="comment">                out.putLongs(a); //"Longs" = NLlong, signed long, int</span>00064 <span class="comment">                out.putLongs(b);</span>00065 <span class="comment">        }</span>00066 <span class="comment"></span>00067 <span class="comment">        // implement this serializable_c interface</span>00068 <span class="comment">        virtual void read(buffer_c &amp;in) {</span>00069 <span class="comment">                a = in.getLongs();      //use same order that was used for write</span>00070 <span class="comment">                b = in.getLongs();      </span>00071 <span class="comment">        }</span>00072 <span class="comment">};</span>00073 <span class="comment"></span>00074 <span class="comment">// MUST add this magical tag in a .CPP module (where the class is implemented probably)</span>00075 <span class="comment">ZIG_SERIALIZABLE_CLASS_CPP(blah);</span>00076 <span class="comment"></span>00077 <span class="comment">---------------------------------------------------------</span>00078 <span class="comment">(2) now you can write and read your class to and from</span>00079 <span class="comment">    buffers (which of course you can send through the</span>00080 <span class="comment">        network)</span>00081 <span class="comment">---------------------------------------------------------</span>00082 <span class="comment"></span>00083 <span class="comment">// testing:</span>00084 <span class="comment">blah x;</span>00085 <span class="comment">x.a = 10;</span>00086 <span class="comment">x.b = 20;</span>00087 <span class="comment">x.whatever = 666;</span>00088 <span class="comment">buffer_c buf;</span>00089 <span class="comment">buf.putObject(x);               // put in the buffer</span>00090 <span class="comment">buf.seek(0);                    // reset buffer pos so we can read from the buffer</span>00091 <span class="comment">// read the object. we are actually creating a new instance of the CORRECT</span>00092 <span class="comment">// type ("blah") and returning a reference to a serializable_c</span>00093 <span class="comment">serializable_c *object = buf.getObject();</span>00094 <span class="comment">// now you can cast the "object" to the correct type ("blah") if you know</span>00095 <span class="comment">// for a fact that it is of class "blah", or you can cast to another </span>00096 <span class="comment">// superclass that you know for sure that is the superclass of ALL the </span>00097 <span class="comment">// classes you are serializing (say, "superblah")</span>00098 <span class="comment">blah *hx = (blah *)object;</span>00099 <span class="comment">printf("%i",hx-&gt;a);  // prints "10"</span>00100 <span class="comment">printf("%i",hx-&gt;b);  // prints "20"</span>00101 <span class="comment">printf("%i",hx-&gt;whatever);  // prints "333"</span>00102 <span class="comment">delete object;</span>00103 <span class="comment">*/</span>00104 00109 <span class="preprocessor">#ifndef _ZIG_HEADER_TYPEREG_H_</span>00110 <span class="preprocessor"></span><span class="preprocessor">#define _ZIG_HEADER_TYPEREG_H_</span>00111 <span class="preprocessor"></span>00112 <span class="preprocessor">#include &lt;map&gt;</span>00113 <span class="keyword">using</span> <span class="keyword">namespace </span>std;00114 <a name="l00116"></a><a class="code" href="typereg_8h.html#a0">00116</a> <span class="preprocessor">#define ZIG_SERIALIZABLE_CLASS_H(X)                                  \</span>00117 <span class="preprocessor">    static CTypeCreator&lt;X&gt; oCreator;                                 \</span>00118 <span class="preprocessor">    virtual int ZIG_GetMySizeof() const {return sizeof(X);}          \</span>00119 <span class="preprocessor">    virtual int ZIG_GetTypeId() const {return oCreator.GetTypeId();}</span>00120 <span class="preprocessor"></span><a name="l00122"></a><a class="code" href="typereg_8h.html#a1">00122</a> <span class="preprocessor">#define ZIG_SERIALIZABLE_CLASS_CPP(X) CTypeCreator &lt;X&gt; X::oCreator;</span>00123 <span class="preprocessor"></span>00124 <span class="comment">// ----------------- H -------------------</span>00125 00126 <span class="comment">// Classe Abstrata: interface que serve de base para a cria玢o da factory. N鉶 instanciar.</span>00127 <span class="keyword">class </span>CTypeMaker00128 {00129 <span class="keyword">public</span>:00130         <span class="comment">// Fun玢o abstrata para a cria玢o de objetos de um tipo a determinar.</span>00131         <span class="keyword">virtual</span> <span class="keywordtype">void</span> *CreateNew()<span class="keyword"> const </span>{<span class="keywordflow">return</span> 0;};00132         <span class="comment">// Fun玢o abstrata que devolve o id do tipo da f醔rica de objetos.</span>00133         <span class="keyword">virtual</span> <span class="keywordtype">int</span> GetTypeId()<span class="keyword"> const </span>{<span class="keywordflow">return</span> -1;};00134         <span class="comment">// Seta o tipo id do tipo do objeto da f醔rica de objetos. N鉶 usar.</span>00135         <span class="keyword">virtual</span> <span class="keywordtype">void</span> SetTypeId(<span class="keywordtype">int</span> iTypeId) {};00136         <span class="comment">// Retorna o tamanho do objeto, em bytes. Util para a cria玢o de c髉ias.</span>00137         <span class="keyword">virtual</span> <span class="keywordtype">int</span> GetSize()<span class="keyword"> const </span>{<span class="keywordflow">return</span> -1;};00138 };00139 00140 <span class="comment">// Classe Abstrata: cont閙 o registro de todos os criadodes de objeto e garante que os Ids de cada</span>00141 <span class="comment">// criador de objetos ser鉶 sempre crescentes.</span>00142 <span class="keyword">class </span>CTypeRegister00143 {00144 <span class="keyword">protected</span>:00145         <span class="comment">// Retorna o pr髕imo Id de criadores de objetos vago.</span>00146         <span class="keyword">static</span> <span class="keywordtype">int</span> GetNextTypeId(){<span class="keywordflow">return</span> iLastId++;};00147         <span class="comment">// Indica qual foi o 鷏timo Id utilizado.</span>00148         <span class="keyword">static</span> <span class="keywordtype">int</span> iLastId;00149 <span class="keyword">public</span>: <span class="comment">// azar</span>00150         <span class="comment">// Cont閙 a associa玢o de id do criador de objeto com o criador de objetos em si.</span>00151         <span class="comment">//static map &lt;int,CTypeMaker *&gt; m_mTypeMaker;</span>00152         <span class="keyword">static</span> map &lt;int,CTypeMaker *&gt;&amp; GetTypeMaker()00153         {00154                 <span class="keyword">static</span> map &lt;int,CTypeMaker *&gt; m_mTypeMaker;00155                 <span class="keywordflow">return</span> m_mTypeMaker;00156         }00157 };00158 00159 <span class="comment">// Classe template que d

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -