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 ? params->GetParameter (id) : 0; \
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_VECTOR2_PAR(var,params,id) \
00040   const celData* p_##var = params ? params->GetParameter (id) : 0; \
00041   csVector2 var; \
00042   if (p_##var && p_##var->type == CEL_DATA_VECTOR2) { \
00043     var.Set (p_##var->value.v.x, p_##var->value.v.y); \
00044   } else { p_##var = 0; }
00045 #define CEL_FETCH_VECTOR3_PAR(var,params,id) \
00046   const celData* p_##var = params ? params->GetParameter (id) : 0; \
00047   csVector3 var; \
00048   if (p_##var && p_##var->type == CEL_DATA_VECTOR3) { \
00049     var.Set (p_##var->value.v.x, p_##var->value.v.y, p_##var->value.v.z); \
00050   } else { p_##var = 0; }
00051 #define CEL_FETCH_VECTOR4_PAR(var,params,id) \
00052   const celData* p_##var = params ? params->GetParameter (id) : 0; \
00053   csVector4 var; \
00054   if (p_##var && p_##var->type == CEL_DATA_VECTOR4) { \
00055     var.Set (p_##var->value.v.x, p_##var->value.v.y, p_##var->value.v.z, p_##var->value.v.w); \
00056   } else { p_##var = 0; }
00057 #define CEL_FETCH_COLOR_PAR(var,params,id) \
00058   const celData* p_##var = params ? params->GetParameter (id) : 0; \
00059   csColor var; \
00060   if (p_##var && p_##var->type == CEL_DATA_COLOR) { \
00061     var.Set (p_##var->value.col.red, p_##var->value.col.green, p_##var->value.col.blue); \
00062   } else { p_##var = 0; }
00063 #define CEL_FETCH_COLOR4_PAR(var,params,id) \
00064   const celData* p_##var = params ? params->GetParameter (id) : 0; \
00065   csColor4 var; \
00066   if (p_##var && p_##var->type == CEL_DATA_COLOR4) { \
00067     var.Set (p_##var->value.col.red, p_##var->value.col.green, p_##var->value.col.blue, p_##var->value.col.alpha); \
00068   } else { p_##var = 0; }
00069 #define CEL_FETCH_FLOAT_PAR(var,params,id) \
00070   const celData* p_##var = params ? params->GetParameter (id) : 0; \
00071   float var = 0.0f; \
00072   if (p_##var) { \
00073     if (p_##var->type == CEL_DATA_FLOAT) \
00074       var = p_##var->value.f; \
00075     else if (p_##var->type == CEL_DATA_LONG) \
00076       var = float (p_##var->value.l); \
00077     else p_##var = 0; \
00078   }
00079 #define CEL_FETCH_LONG_PAR(var,params,id) \
00080   const celData* p_##var = params ? params->GetParameter (id) : 0; \
00081   long var = 0; \
00082   if (p_##var) { \
00083     if (p_##var->type == CEL_DATA_LONG) \
00084       var = p_##var->value.l; \
00085     else if (p_##var->type == CEL_DATA_FLOAT) \
00086       var = long (p_##var->value.f); \
00087     else p_##var = 0; \
00088   }
00089 #define CEL_FETCH_BOOL_PAR(var,params,id) \
00090   const celData* p_##var = params ? params->GetParameter (id) : 0; \
00091   bool var = false; \
00092   if (p_##var) { \
00093     if (p_##var->type == CEL_DATA_BOOL) \
00094       var = p_##var->value.bo; \
00095     else if (p_##var->type == CEL_DATA_LONG) \
00096     var =  ((p_##var->value.l)? true : false); \
00097     else p_##var = 0; \
00098   }
00099 
00103 class celGenericParameterBlock : public scfImplementation1<
00104         celGenericParameterBlock, iCelParameterBlock>
00105 {
00106 private:
00107   size_t count;
00108   csStringID* ids;
00109   celData* data;
00110   char** names;
00111 
00112 public:
00113   celGenericParameterBlock (size_t count) :
00114     scfImplementationType (this)
00115   {
00116     celGenericParameterBlock::count = count;
00117     ids = new csStringID[count];
00118     data = new celData[count];
00119     names = new char*[count];
00120     memset (names, 0, sizeof (char*)*count);
00121   }
00122   virtual ~celGenericParameterBlock ()
00123   {
00124     delete[] ids;
00125     delete[] data;
00126     size_t i;
00127     for (i = 0 ; i < count ; i++)
00128       delete[] names[i];
00129     delete[] names;
00130   }
00131 
00132   void SetParameterDef (size_t idx, csStringID id, const char* parname)
00133   {
00134     ids[idx] = id;
00135     delete[] names[idx];
00136     names[idx] = csStrNew (parname);
00137   }
00138   celData& GetParameter (size_t idx) { return data[idx]; }
00139 
00140   virtual size_t GetParameterCount () const { return count; }
00141   virtual const char* GetParameter (size_t idx, csStringID& id,
00142         celDataType& t) const
00143   {
00144     if (/*idx < 0 || */idx >= count)
00145     {
00146       id = csInvalidStringID;
00147       t = CEL_DATA_NONE;
00148       return 0;
00149     }
00150     id = ids[idx];
00151     t = data[idx].type;
00152     return names[idx];
00153   }
00154   virtual const celData* GetParameter (csStringID id) const
00155   {
00156     size_t i;
00157     for (i = 0 ; i < count ; i++)
00158       if (id == ids[i])
00159         return &data[i];
00160     return 0;
00161   }
00162   virtual const celData* GetParameterByIndex (size_t idx) const
00163   {
00164     return (idx >= count) ? 0 : &data[idx];
00165   }
00166 };
00167 
00171 class celVariableParameterBlock : public scfImplementation1<
00172         celVariableParameterBlock,iCelParameterBlock>
00173 {
00174 private:
00175   csArray<csStringID> ids;
00176   csArray<celData> data;
00177   csStringArray names;
00178 
00179 public:
00180   celVariableParameterBlock () : scfImplementationType (this)
00181   {
00182   }
00186   celVariableParameterBlock (iCelParameterBlock* other) :
00187     scfImplementationType (this)
00188   {
00189     if (other != 0)
00190     {
00191       const char* name = 0;
00192       csStringID id;
00193       celDataType type;
00194       for (size_t idx = 0; idx < other->GetParameterCount (); idx++)
00195       {
00196         name = other->GetParameter (idx, id, type);
00197         SetParameterDef (idx, id, name);
00198         data.GetExtend (idx) = *other->GetParameter (id);
00199       }
00200     }
00201   }
00202   virtual ~celVariableParameterBlock ()
00203   {
00204   }
00205 
00206   void SetParameterDef (size_t idx, csStringID id, const char* parname)
00207   {
00208     ids.GetExtend (idx) = id;
00209     if (idx >= names.GetSize ())
00210       names.SetSize (idx+1);
00211     names.Put (idx, parname);
00212   }
00213   celData& GetParameter (size_t idx) { return data.GetExtend (idx); }
00214 
00215   virtual size_t GetParameterCount () const { return data.GetSize (); }
00216   virtual const char* GetParameter (size_t idx, csStringID& id,
00217         celDataType& t) const
00218   {
00219     if (/*idx < 0 || */idx >= data.GetSize ())
00220     {
00221       id = csInvalidStringID;
00222       t = CEL_DATA_NONE;
00223       return 0;
00224     }
00225     id = ids[idx];
00226     t = data[idx].type;
00227     return names[idx];
00228   }
00229   virtual const celData* GetParameter (csStringID id) const
00230   {
00231     size_t i;
00232     for (i = 0 ; i < data.GetSize () ; i++)
00233       if (id == ids[i])
00234         return &data[i];
00235     return 0;
00236   }
00237   virtual const celData* GetParameterByIndex (size_t idx) const
00238   {
00239     return (idx >= data.GetSize ()) ? 0 : &data[idx];
00240   }
00241 };
00242 
00246 class celOneParameterBlock : public scfImplementation1<
00247         celOneParameterBlock, iCelParameterBlock>
00248 {
00249 private:
00250   csStringID id;
00251   celData data;
00252   char* name;
00253 
00254 public:
00255   celOneParameterBlock () : scfImplementationType (this)
00256   {
00257     name = 0;
00258   }
00259   virtual ~celOneParameterBlock ()
00260   {
00261     delete[] name;
00262   }
00263 
00264   void SetParameterDef (csStringID id, const char* parname)
00265   {
00266     celOneParameterBlock::id = id;
00267     delete[] name;
00268     name = csStrNew (parname);
00269   }
00270   celData& GetParameter (int) { return data; }
00271 
00272   virtual size_t GetParameterCount () const { return 1; }
00273   virtual const char* GetParameter (size_t idx, csStringID& id,
00274         celDataType& t) const
00275   {
00276     if (idx != 0)
00277     {
00278       id = csInvalidStringID;
00279       t = CEL_DATA_NONE;
00280       return 0;
00281     }
00282     id = celOneParameterBlock::id;
00283     t = data.type;
00284     return name;
00285   }
00286   virtual const celData* GetParameter (csStringID id) const
00287   {
00288     if (id != celOneParameterBlock::id) return 0;
00289     return &data;
00290   }
00291   virtual const celData* GetParameterByIndex (size_t idx) const
00292   {
00293     return (idx != 0) ? 0 : &data;
00294   }
00295 };
00296 
00297 #endif // __CEL_CELTOOL_PARAMS__
00298 

Generated for CEL: Crystal Entity Layer 1.2 by doxygen 1.4.7