CEL

Public API Reference

celtool/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) { \
00049     if (p_##var->type == CEL_DATA_FLOAT) \
00050       var = p_##var->value.f; \
00051     else if (p_##var->type == CEL_DATA_LONG) \
00052       var = float (p_##var->value.l); \
00053     else p_##var = 0; \
00054   }
00055 #define CEL_FETCH_LONG_PAR(var,params,id) \
00056   const celData* p_##var = params->GetParameter (id); \
00057   long var = 0; \
00058   if (p_##var) { \
00059     if (p_##var->type == CEL_DATA_LONG) \
00060       var = p_##var->value.l; \
00061     else if (p_##var->type == CEL_DATA_FLOAT) \
00062       var = long (p_##var->value.f); \
00063     else p_##var = 0; \
00064   }
00065 #define CEL_FETCH_BOOL_PAR(var,params,id) \
00066   const celData* p_##var = params->GetParameter (id); \
00067   bool var = false; \
00068   if (p_##var) { \
00069     if (p_##var->type == CEL_DATA_BOOL) \
00070       var = p_##var->value.bo; \
00071     else if (p_##var->type == CEL_DATA_LONG) \
00072     var =  ((p_##var->value.l)? true : false); \
00073     else p_##var = 0; \
00074   }
00075 
00079 class celGenericParameterBlock : public iCelParameterBlock
00080 {
00081 private:
00082   size_t count;
00083   csStringID* ids;
00084   celData* data;
00085   char** names;
00086 
00087 public:
00088   celGenericParameterBlock (size_t count)
00089   {
00090     SCF_CONSTRUCT_IBASE (0);
00091     celGenericParameterBlock::count = count;
00092     ids = new csStringID[count];
00093     data = new celData[count];
00094     names = new char*[count];
00095     memset (names, 0, sizeof (char*)*count);
00096   }
00097   virtual ~celGenericParameterBlock ()
00098   {
00099     delete[] ids;
00100     delete[] data;
00101     size_t i;
00102     for (i = 0 ; i < count ; i++)
00103       delete[] names[i];
00104     delete[] names;
00105     SCF_DESTRUCT_IBASE ();
00106   }
00107 
00108   void SetParameterDef (size_t idx, csStringID id, const char* parname)
00109   {
00110     ids[idx] = id;
00111     delete[] names[idx];
00112     names[idx] = csStrNew (parname);
00113   }
00114   celData& GetParameter (size_t idx) { return data[idx]; }
00115 
00116   SCF_DECLARE_IBASE;
00117 
00118   virtual size_t GetParameterCount () const { return count; }
00119   virtual const char* GetParameter (size_t idx, csStringID& id,
00120         celDataType& t) const
00121   {
00122     if (/*idx < 0 || */idx >= count)
00123     {
00124       id = csInvalidStringID;
00125       t = CEL_DATA_NONE;
00126       return 0;
00127     }
00128     id = ids[idx];
00129     t = data[idx].type;
00130     return names[idx];
00131   }
00132   virtual const celData* GetParameter (csStringID id) const
00133   {
00134     size_t i;
00135     for (i = 0 ; i < count ; i++)
00136       if (id == ids[i])
00137         return &data[i];
00138     return 0;
00139   }
00140 };
00141 
00145 class celVariableParameterBlock : public iCelParameterBlock
00146 {
00147 private:
00148   csArray<csStringID> ids;
00149   csArray<celData> data;
00150   csStringArray names;
00151 
00152 public:
00153   celVariableParameterBlock ()
00154   {
00155     SCF_CONSTRUCT_IBASE (0);
00156   }
00160   celVariableParameterBlock (iCelParameterBlock* other)
00161   {
00162     SCF_CONSTRUCT_IBASE (0);
00163     if (other != 0)
00164     {
00165       const char* name = 0;
00166       csStringID id;
00167       celDataType type;
00168       for (size_t idx = 0; idx < other->GetParameterCount (); idx++)
00169       {
00170         name = other->GetParameter (idx, id, type);
00171         SetParameterDef (idx, id, name);
00172         data.GetExtend (idx) = *other->GetParameter (id);
00173       }
00174     }
00175   }
00176   virtual ~celVariableParameterBlock ()
00177   {
00178     SCF_DESTRUCT_IBASE ();
00179   }
00180 
00181   void SetParameterDef (size_t idx, csStringID id, const char* parname)
00182   {
00183     ids.GetExtend (idx) = id;
00184     if (idx >= names.Length ())
00185       names.SetLength (idx+1);
00186     names.Put (idx, parname);
00187   }
00188   celData& GetParameter (size_t idx) { return data.GetExtend (idx); }
00189 
00190   SCF_DECLARE_IBASE;
00191 
00192   virtual size_t GetParameterCount () const { return data.Length (); }
00193   virtual const char* GetParameter (size_t idx, csStringID& id,
00194         celDataType& t) const
00195   {
00196     if (/*idx < 0 || */idx >= data.Length ())
00197     {
00198       id = csInvalidStringID;
00199       t = CEL_DATA_NONE;
00200       return 0;
00201     }
00202     id = ids[idx];
00203     t = data[idx].type;
00204     return names[idx];
00205   }
00206   virtual const celData* GetParameter (csStringID id) const
00207   {
00208     size_t i;
00209     for (i = 0 ; i < data.Length () ; i++)
00210       if (id == ids[i])
00211         return &data[i];
00212     return 0;
00213   }
00214 };
00215 
00219 class celOneParameterBlock : public iCelParameterBlock
00220 {
00221 private:
00222   csStringID id;
00223   celData data;
00224   char* name;
00225 
00226 public:
00227   celOneParameterBlock ()
00228   {
00229     SCF_CONSTRUCT_IBASE (0);
00230     name = 0;
00231   }
00232   virtual ~celOneParameterBlock ()
00233   {
00234     delete[] name;
00235     SCF_DESTRUCT_IBASE ();
00236   }
00237 
00238   void SetParameterDef (csStringID id, const char* parname)
00239   {
00240     celOneParameterBlock::id = id;
00241     delete[] name;
00242     name = csStrNew (parname);
00243   }
00244   celData& GetParameter (int) { return data; }
00245 
00246   SCF_DECLARE_IBASE;
00247 
00248   virtual size_t GetParameterCount () const { return 1; }
00249   virtual const char* GetParameter (size_t idx, csStringID& id,
00250         celDataType& t) const
00251   {
00252     if (idx != 0)
00253     {
00254       id = csInvalidStringID;
00255       t = CEL_DATA_NONE;
00256       return 0;
00257     }
00258     id = celOneParameterBlock::id;
00259     t = data.type;
00260     return name;
00261   }
00262   virtual const celData* GetParameter (csStringID id) const
00263   {
00264     if (id != celOneParameterBlock::id) return 0;
00265     return &data;
00266   }
00267 };
00268 
00269 #endif // __CEL_CELTOOL_PARAMS__
00270 

Generated for CEL: Crystal Entity Layer by doxygen 1.4.6