CEL

Public API Reference

Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members

stdparams.h

00001 /*
00002     Crystal Space Entity Layer
00003     Copyright (C) 2003 by Jorrit Tyberghein
00004   
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009   
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014   
00015     You should have received a copy of the GNU Library General Public
00016     License along with this library; if not, write to the Free
00017     Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #ifndef __CEL_CELTOOL_PARAMS__
00021 #define __CEL_CELTOOL_PARAMS__
00022 
00023 #include "cstypes.h"
00024 #include "csutil/scf.h"
00025 #include "csutil/strhash.h"
00026 #include "csutil/util.h"
00027 #include "csutil/array.h"
00028 #include "csutil/stringarray.h"
00029 #include "behaviourlayer/behave.h"
00030 
00031 // The following macros will set 'var' to the required variable and
00032 // 'p_var' will be made to 0 if there is a failure.
00033 #define CEL_FETCH_STRING_PAR(var,params,id) \
00034   const celData* p_##var = params->GetParameter (id); \
00035   const char* var = 0; \
00036   if (p_##var && p_##var->type == CEL_DATA_STRING) { \
00037     var = p_##var->value.s->GetData (); \
00038   } else { p_##var = 0; }
00039 #define CEL_FETCH_VECTOR3_PAR(var,params,id) \
00040   const celData* p_##var = params->GetParameter (id); \
00041   csVector3 var; \
00042   if (p_##var && p_##var->type == CEL_DATA_VECTOR3) { \
00043     var.Set (p_##var->value.v.x, p_##var->value.v.y, p_##var->value.v.z); \
00044   } else { p_##var = 0; }
00045 #define CEL_FETCH_FLOAT_PAR(var,params,id) \
00046   const celData* p_##var = params->GetParameter (id); \
00047   float var = 0.0f; \
00048   if (p_##var && p_##var->type == CEL_DATA_FLOAT) { \
00049     var = p_##var->value.f; \
00050   } else { p_##var = 0; }
00051 #define CEL_FETCH_LONG_PAR(var,params,id) \
00052   const celData* p_##var = params->GetParameter (id); \
00053   long var = 0; \
00054   if (p_##var && p_##var->type == CEL_DATA_LONG) { \
00055     var = p_##var->value.l; \
00056   } else { p_##var = 0; }
00057 #define CEL_FETCH_BOOL_PAR(var,params,id) \
00058   const celData* p_##var = params->GetParameter (id); \
00059   bool var = false; \
00060   if (p_##var && p_##var->type == CEL_DATA_BOOL) { \
00061     var = p_##var->value.bo; \
00062   } else { p_##var = 0; }
00063 
00067 class celGenericParameterBlock : public iCelParameterBlock
00068 {
00069 private:
00070   size_t count;
00071   csStringID* ids;
00072   celData* data;
00073   char** names;
00074 
00075 public:
00076   celGenericParameterBlock (size_t count)
00077   {
00078     SCF_CONSTRUCT_IBASE (0);
00079     celGenericParameterBlock::count = count;
00080     ids = new csStringID[count];
00081     data = new celData[count];
00082     names = new char*[count];
00083     memset (names, 0, sizeof (char*)*count);
00084   }
00085   virtual ~celGenericParameterBlock ()
00086   {
00087     delete[] ids;
00088     delete[] data;
00089     size_t i;
00090     for (i = 0 ; i < count ; i++)
00091       delete[] names[i];
00092     delete[] names;
00093     SCF_DESTRUCT_IBASE ();
00094   }
00095 
00096   void SetParameterDef (size_t idx, csStringID id, const char* parname)
00097   {
00098     ids[idx] = id;
00099     delete[] names[idx];
00100     names[idx] = csStrNew (parname);
00101   }
00102   celData& GetParameter (size_t idx) { return data[idx]; }
00103 
00104   SCF_DECLARE_IBASE;
00105 
00106   virtual size_t GetParameterCount () const { return count; }
00107   virtual const char* GetParameter (size_t idx, csStringID& id,
00108         celDataType& t) const
00109   {
00110     if (/*idx < 0 || */idx >= count)
00111     {
00112       id = csInvalidStringID;
00113       t = CEL_DATA_NONE;
00114       return 0;
00115     }
00116     id = ids[idx];
00117     t = data[idx].type;
00118     return names[idx];
00119   }
00120   virtual const celData* GetParameter (csStringID id) const
00121   {
00122     size_t i;
00123     for (i = 0 ; i < count ; i++)
00124       if (id == ids[i])
00125         return &data[i];
00126     return 0;
00127   }
00128 };
00129 
00133 class celVariableParameterBlock : public iCelParameterBlock
00134 {
00135 private:
00136   csArray<csStringID> ids;
00137   csArray<celData> data;
00138   csStringArray names;
00139 
00140 public:
00141   celVariableParameterBlock ()
00142   {
00143     SCF_CONSTRUCT_IBASE (0);
00144   }
00145   virtual ~celVariableParameterBlock ()
00146   {
00147     SCF_DESTRUCT_IBASE ();
00148   }
00149 
00150   void SetParameterDef (size_t idx, csStringID id, const char* parname)
00151   {
00152     ids.GetExtend (idx) = id;
00153     if (idx >= names.Length ())
00154       names.SetLength (idx+1);
00155     names.Put (idx, parname);
00156   }
00157   celData& GetParameter (size_t idx) { return data.GetExtend (idx); }
00158 
00159   SCF_DECLARE_IBASE;
00160 
00161   virtual size_t GetParameterCount () const { return data.Length (); }
00162   virtual const char* GetParameter (size_t idx, csStringID& id,
00163         celDataType& t) const
00164   {
00165     if (/*idx < 0 || */idx >= data.Length ())
00166     {
00167       id = csInvalidStringID;
00168       t = CEL_DATA_NONE;
00169       return 0;
00170     }
00171     id = ids[idx];
00172     t = data[idx].type;
00173     return names[idx];
00174   }
00175   virtual const celData* GetParameter (csStringID id) const
00176   {
00177     size_t i;
00178     for (i = 0 ; i < data.Length () ; i++)
00179       if (id == ids[i])
00180         return &data[i];
00181     return 0;
00182   }
00183 };
00184 
00188 class celOneParameterBlock : public iCelParameterBlock
00189 {
00190 private:
00191   csStringID id;
00192   celData data;
00193   char* name;
00194 
00195 public:
00196   celOneParameterBlock ()
00197   {
00198     SCF_CONSTRUCT_IBASE (0);
00199     name = 0;
00200   }
00201   virtual ~celOneParameterBlock ()
00202   {
00203     delete[] name;
00204     SCF_DESTRUCT_IBASE ();
00205   }
00206 
00207   void SetParameterDef (csStringID id, const char* parname)
00208   {
00209     celOneParameterBlock::id = id;
00210     delete[] name;
00211     name = csStrNew (parname);
00212   }
00213   celData& GetParameter (int) { return data; }
00214 
00215   SCF_DECLARE_IBASE;
00216 
00217   virtual size_t GetParameterCount () const { return 1; }
00218   virtual const char* GetParameter (size_t idx, csStringID& id,
00219         celDataType& t) const
00220   {
00221     if (idx != 0)
00222     {
00223       id = csInvalidStringID;
00224       t = CEL_DATA_NONE;
00225       return 0;
00226     }
00227     id = celOneParameterBlock::id;
00228     t = data.type;
00229     return name;
00230   }
00231   virtual const celData* GetParameter (csStringID id) const
00232   {
00233     if (id != celOneParameterBlock::id) return 0;
00234     return &data;
00235   }
00236 };
00237 
00238 #endif // __CEL_CELTOOL_PARAMS__
00239 

Generated for CEL: Crystal Entity Layer by doxygen 1.4.4