## Public API Reference

- Main Page
- Classes
- Files

# 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   virtual const celData* GetParameterByIndex (size_t idx) const
    00141   {
    00142     return (idx >= count) ? 0 : &data[idx];
    00143   }
    00144 };
    00145
    00149 class celVariableParameterBlock : public iCelParameterBlock
    00150 {
    00151 private:
    00152   csArray<csStringID> ids;
    00153   csArray<celData> data;
    00154   csStringArray names;
    00155
    00156 public:
    00157   celVariableParameterBlock ()
    00158   {
    00159     SCF_CONSTRUCT_IBASE (0);
    00160   }
    00164   celVariableParameterBlock (iCelParameterBlock* other)
    00165   {
    00166     SCF_CONSTRUCT_IBASE (0);
    00167     if (other != 0)
    00168     {
    00169       const char* name = 0;
    00170       csStringID id;
    00171       celDataType type;
    00172       for (size_t idx = 0; idx < other->GetParameterCount (); idx++)
    00173       {
    00174         name = other->GetParameter (idx, id, type);
    00175         SetParameterDef (idx, id, name);
    00176         data.GetExtend (idx) = *other->GetParameter (id);
    00177       }
    00178     }
    00179   }
    00180   virtual ~celVariableParameterBlock ()
    00181   {
    00182     SCF_DESTRUCT_IBASE ();
    00183   }
    00184
    00185   void SetParameterDef (size_t idx, csStringID id, const char* parname)
    00186   {
    00187     ids.GetExtend (idx) = id;
    00188     if (idx >= names.Length ())
    00189       names.SetLength (idx+1);
    00190     names.Put (idx, parname);
    00191   }
    00192   celData& GetParameter (size_t idx) { return data.GetExtend (idx); }
    00193
    00194   SCF_DECLARE_IBASE;
    00195
    00196   virtual size_t GetParameterCount () const { return data.Length (); }
    00197   virtual const char* GetParameter (size_t idx, csStringID& id,
    00198         celDataType& t) const
    00199   {
    00200     if (/*idx < 0 || */idx >= data.Length ())
    00201     {
    00202       id = csInvalidStringID;
    00203       t = CEL_DATA_NONE;
    00204       return 0;
    00205     }
    00206     id = ids[idx];
    00207     t = data[idx].type;
    00208     return names[idx];
    00209   }
    00210   virtual const celData* GetParameter (csStringID id) const
    00211   {
    00212     size_t i;
    00213     for (i = 0 ; i < data.Length () ; i++)
    00214       if (id == ids[i])
    00215         return &data[i];
    00216     return 0;
    00217   }
    00218   virtual const celData* GetParameterByIndex (size_t idx) const
    00219   {
    00220     return (idx >= data.Length ()) ? 0 : &data[idx];
    00221   }
    00222 };
    00223
    00227 class celOneParameterBlock : public iCelParameterBlock
    00228 {
    00229 private:
    00230   csStringID id;
    00231   celData data;
    00232   char* name;
    00233
    00234 public:
    00235   celOneParameterBlock ()
    00236   {
    00237     SCF_CONSTRUCT_IBASE (0);
    00238     name = 0;
    00239   }
    00240   virtual ~celOneParameterBlock ()
    00241   {
    00242     delete[] name;
    00243     SCF_DESTRUCT_IBASE ();
    00244   }
    00245
    00246   void SetParameterDef (csStringID id, const char* parname)
    00247   {
    00248     celOneParameterBlock::id = id;
    00249     delete[] name;
    00250     name = csStrNew (parname);
    00251   }
    00252   celData& GetParameter (int) { return data; }
    00253
    00254   SCF_DECLARE_IBASE;
    00255
    00256   virtual size_t GetParameterCount () const { return 1; }
    00257   virtual const char* GetParameter (size_t idx, csStringID& id,
    00258         celDataType& t) const
    00259   {
    00260     if (idx != 0)
    00261     {
    00262       id = csInvalidStringID;
    00263       t = CEL_DATA_NONE;
    00264       return 0;
    00265     }
    00266     id = celOneParameterBlock::id;
    00267     t = data.type;
    00268     return name;
    00269   }
    00270   virtual const celData* GetParameter (csStringID id) const
    00271   {
    00272     if (id != celOneParameterBlock::id) return 0;
    00273     return &data;
    00274   }
    00275   virtual const celData* GetParameterByIndex (size_t idx) const
    00276   {
    00277     return (idx != 0) ? 0 : &data;
    00278   }
    00279 };
    00280
    00281 #endif // __CEL_CELTOOL_PARAMS__
    00282