lscript_library.h

Go to the documentation of this file.
00001 
00032 #ifndef LL_LSCRIPT_LIBRARY_H
00033 #define LL_LSCRIPT_LIBRARY_H
00034 
00035 #include "lscript_byteformat.h"
00036 #include "v3math.h"
00037 #include "llquaternion.h"
00038 #include "lluuid.h"
00039 #include "lscript_byteconvert.h"
00040 
00041 class LLScriptLibData;
00042 
00043 class LLScriptLibraryFunction
00044 {
00045 public:
00046         LLScriptLibraryFunction(F32 eu, F32 st, void (*exec_func)(LLScriptLibData *, LLScriptLibData *, const LLUUID &), char *name, char *ret_type, char *args, char *desc, BOOL god_only = FALSE);
00047         ~LLScriptLibraryFunction();
00048 
00049         F32  mEnergyUse;
00050         F32  mSleepTime;
00051         void (*mExecFunc)(LLScriptLibData *, LLScriptLibData *, const LLUUID &);
00052         char *mName;
00053         char *mReturnType;
00054         char *mArgs;
00055         char *mDesc;
00056         BOOL mGodOnly;
00057 };
00058 
00059 class LLScriptLibrary
00060 {
00061 public:
00062         LLScriptLibrary();
00063         ~LLScriptLibrary();
00064 
00065         void init();
00066 
00067         void addFunction(LLScriptLibraryFunction *func);
00068         void assignExec(char *name, void (*exec_func)(LLScriptLibData *, LLScriptLibData *, const LLUUID &));
00069 
00070         S32                                             mNextNumber;
00071         LLScriptLibraryFunction **mFunctions;
00072 };
00073 
00074 extern LLScriptLibrary gScriptLibrary;
00075 
00076 class LLScriptLibData
00077 {
00078 public:
00079         // TODO: Change this to a union
00080         LSCRIPTType             mType;
00081         S32                             mInteger;
00082         F32                             mFP;
00083         char                    *mKey;
00084         char                    *mString;
00085         LLVector3               mVec;
00086         LLQuaternion    mQuat;
00087         LLScriptLibData *mListp;
00088 
00089         friend bool operator<=(const LLScriptLibData &a, const LLScriptLibData &b)
00090         {
00091                 if (a.mType == b.mType)
00092                 {
00093                         if (a.mType == LST_INTEGER)
00094                         {
00095                                 return a.mInteger <= b.mInteger;
00096                         }
00097                         if (a.mType == LST_FLOATINGPOINT)
00098                         {
00099                                 return a.mFP <= b.mFP;
00100                         }
00101                         if (a.mType == LST_STRING)
00102                         {
00103                                 return strcmp(a.mString, b.mString) <= 0;
00104                         }
00105                         if (a.mType == LST_KEY)
00106                         {
00107                                 return strcmp(a.mKey, b.mKey) <= 0;
00108                         }
00109                         if (a.mType == LST_VECTOR)
00110                         {
00111                                 return a.mVec.magVecSquared() <= b.mVec.magVecSquared();
00112                         }
00113                 }
00114                 return TRUE;
00115         }
00116 
00117         friend bool operator==(const LLScriptLibData &a, const LLScriptLibData &b)
00118         {
00119                 if (a.mType == b.mType)
00120                 {
00121                         if (a.mType == LST_INTEGER)
00122                         {
00123                                 return a.mInteger == b.mInteger;
00124                         }
00125                         if (a.mType == LST_FLOATINGPOINT)
00126                         {
00127                                 return a.mFP == b.mFP;
00128                         }
00129                         if (a.mType == LST_STRING)
00130                         {
00131                                 return !strcmp(a.mString, b.mString);
00132                         }
00133                         if (a.mType == LST_KEY)
00134                         {
00135                                 return !strcmp(a.mKey, b.mKey);
00136                         }
00137                         if (a.mType == LST_VECTOR)
00138                         {
00139                                 return a.mVec == b.mVec;
00140                         }
00141                         if (a.mType == LST_QUATERNION)
00142                         {
00143                                 return a.mQuat == b.mQuat;
00144                         }
00145                 }
00146                 return FALSE;
00147         }
00148 
00149         S32 getListLength()
00150         {
00151                 LLScriptLibData *data = this;
00152                 S32 retval = 0;
00153                 while (data->mListp)
00154                 {
00155                         retval++;
00156                         data = data->mListp;
00157                 }
00158                 return retval;
00159         }
00160 
00161         BOOL checkForMultipleLists()
00162         {
00163                 LLScriptLibData *data = this;
00164                 while (data->mListp)
00165                 {
00166                         data = data->mListp;
00167                         if (data->mType == LST_LIST)
00168                                 return TRUE;
00169                 }
00170                 return FALSE;
00171         }
00172 
00173         S32  getSavedSize()
00174         {
00175                 S32 size = 0;
00176                 // mType
00177                 size += 4;
00178 
00179                 switch(mType)
00180                 {
00181                 case LST_INTEGER:
00182                         size += 4;
00183                         break;
00184                 case LST_FLOATINGPOINT:
00185                         size += 4;
00186                         break;
00187                 case LST_KEY:
00188                         size += (S32)strlen(mKey) + 1;  /*Flawfinder: ignore*/
00189                         break;
00190                 case LST_STRING:
00191                         size += (S32)strlen(mString) + 1;       /*Flawfinder: ignore*/
00192                         break;
00193                 case LST_LIST:
00194                         break;
00195                 case LST_VECTOR:
00196                         size += 12;
00197                         break;
00198                 case LST_QUATERNION:
00199                         size += 16;
00200                         break;
00201                 default:
00202                         break;
00203                 }
00204                 return size;
00205         }
00206 
00207         S32      write2bytestream(U8 *dest)
00208         {
00209                 S32 offset = 0;
00210                 integer2bytestream(dest, offset, mType);
00211                 switch(mType)
00212                 {
00213                 case LST_INTEGER:
00214                         integer2bytestream(dest, offset, mInteger);
00215                         break;
00216                 case LST_FLOATINGPOINT:
00217                         float2bytestream(dest, offset, mFP);
00218                         break;
00219                 case LST_KEY:
00220                         char2bytestream(dest, offset, mKey);
00221                         break;
00222                 case LST_STRING:
00223                         char2bytestream(dest, offset, mString);
00224                         break;
00225                 case LST_LIST:
00226                         break;
00227                 case LST_VECTOR:
00228                         vector2bytestream(dest, offset, mVec);
00229                         break;
00230                 case LST_QUATERNION:
00231                         quaternion2bytestream(dest, offset, mQuat);
00232                         break;
00233                 default:
00234                         break;
00235                 }
00236                 return offset;
00237         }
00238 
00239         LLScriptLibData() : mType(LST_NULL), mInteger(0), mFP(0.f), mKey(NULL), mString(NULL), mVec(), mQuat(), mListp(NULL)
00240         {
00241         }
00242 
00243         LLScriptLibData(const LLScriptLibData &data) : mType(data.mType), mInteger(data.mInteger), mFP(data.mFP), mKey(NULL), mString(NULL), mVec(data.mVec), mQuat(data.mQuat), mListp(NULL)
00244         {
00245                 if (data.mKey)
00246                 {
00247                         mKey = new char[strlen(data.mKey) + 1]; /* Flawfinder: ignore */
00248                         if (mKey == NULL)
00249                         {
00250                                 llerrs << "Memory Allocation Failed" << llendl;
00251                                 return;
00252                         }
00253                         strcpy(mKey, data.mKey);        /* Flawfinder: ignore */
00254                 }
00255                 if (data.mString)
00256                 {
00257                         mString = new char[strlen(data.mString) + 1];   /* Flawfinder: ignore */
00258                         if (mString == NULL)
00259                         {
00260                                 llerrs << "Memory Allocation Failed" << llendl;
00261                                 return;
00262                         }
00263                         strcpy(mString, data.mString);  /* Flawfinder: ignore */
00264                 }
00265         }
00266 
00267         LLScriptLibData(U8 *src, S32 &offset) : mListp(NULL)
00268         {
00269                 static char temp[TOP_OF_MEMORY];        /* Flawfinder: ignore */
00270                 mType = (LSCRIPTType)bytestream2integer(src, offset);
00271                 switch(mType)
00272                 {
00273                 case LST_INTEGER:
00274                         mInteger = bytestream2integer(src, offset);
00275                         break;
00276                 case LST_FLOATINGPOINT:
00277                         mFP = bytestream2float(src, offset);
00278                         break;
00279                 case LST_KEY:
00280                         {
00281                                 bytestream2char(temp, src, offset);
00282                                 mKey = new char[strlen(temp) + 1];      /* Flawfinder: ignore */
00283                                 if (mKey == NULL)
00284                                 {
00285                                         llerrs << "Memory Allocation Failed" << llendl;
00286                                         return;
00287                                 }
00288                                 strcpy(mKey, temp);     /* Flawfinder: ignore */
00289                         }
00290                         break;
00291                 case LST_STRING:
00292                         {
00293                                 bytestream2char(temp, src, offset);
00294                                 mString = new char[strlen(temp) + 1];   /* Flawfinder: ignore */
00295                                 if (mString == NULL)
00296                                 {
00297                                         llerrs << "Memory Allocation Failed" << llendl;
00298                                         return;
00299                                 }
00300                                 strcpy(mString, temp);  /* Flawfinder: ignore */
00301                         }
00302                         break;
00303                 case LST_LIST:
00304                         break;
00305                 case LST_VECTOR:
00306                         bytestream2vector(mVec, src, offset);
00307                         break;
00308                 case LST_QUATERNION:
00309                         bytestream2quaternion(mQuat, src, offset);
00310                         break;
00311                 default:
00312                         break;
00313                 }
00314         }
00315 
00316         void set(U8 *src, S32 &offset)
00317         {
00318                 static char temp[TOP_OF_MEMORY];        /* Flawfinder: ignore */
00319                 mType = (LSCRIPTType)bytestream2integer(src, offset);
00320                 switch(mType)
00321                 {
00322                 case LST_INTEGER:
00323                         mInteger = bytestream2integer(src, offset);
00324                         break;
00325                 case LST_FLOATINGPOINT:
00326                         mFP = bytestream2float(src, offset);
00327                         break;
00328                 case LST_KEY:
00329                         {
00330                                 bytestream2char(temp, src, offset);
00331                                 mKey = new char[strlen(temp) + 1];      /* Flawfinder: ignore */
00332                                 if (mKey == NULL)
00333                                 {
00334                                         llerrs << "Memory Allocation Failed" << llendl;
00335                                         return;
00336                                 }
00337                                 strcpy(mKey, temp);     /* Flawfinder: ignore */
00338                         }
00339                         break;
00340                 case LST_STRING:
00341                         {
00342                                 bytestream2char(temp, src, offset);
00343                                 mString = new char[strlen(temp) + 1];   /* Flawfinder: ignore */
00344                                 if (mString == NULL)
00345                                 {
00346                                         llerrs << "Memory Allocation Failed" << llendl;
00347                                         return;
00348                                 }
00349                                 strcpy(mString, temp);  /* Flawfinder: ignore */
00350                         }
00351                         break;
00352                 case LST_LIST:
00353                         break;
00354                 case LST_VECTOR:
00355                         bytestream2vector(mVec, src, offset);
00356                         break;
00357                 case LST_QUATERNION:
00358                         bytestream2quaternion(mQuat, src, offset);
00359                         break;
00360                 default:
00361                         break;
00362                 }
00363         }
00364 
00365         void print(std::ostream &s, BOOL b_prepend_comma);
00366         void print_separator(std::ostream& ostr, BOOL b_prepend_sep, char* sep);
00367 
00368         void setFromCSV(char *src)
00369         {
00370                 mType = LST_STRING;
00371                 mString = new char[strlen(src) + 1];    /* Flawfinder: ignore */
00372                 if (mString == NULL)
00373                 {
00374                         llerrs << "Memory Allocation Failed" << llendl;
00375                         return;
00376                 }
00377                 strcpy(mString, src);   /* Flawfinder: ignore */
00378         }
00379 
00380         LLScriptLibData(S32 integer) : mType(LST_INTEGER), mInteger(integer), mFP(0.f), mKey(NULL), mString(NULL), mVec(), mQuat(), mListp(NULL)
00381         {
00382         }
00383 
00384         LLScriptLibData(F32 fp) : mType(LST_FLOATINGPOINT), mInteger(0), mFP(fp), mKey(NULL), mString(NULL), mVec(), mQuat(), mListp(NULL)
00385         {
00386         }
00387 
00388         LLScriptLibData(const LLUUID &id) : mType(LST_KEY), mInteger(0), mFP(0.f), mKey(NULL), mString(NULL), mVec(), mQuat(), mListp(NULL)
00389         {
00390                 mKey = new char[UUID_STR_LENGTH];
00391                 id.toString(mKey);
00392         }
00393 
00394         LLScriptLibData(char *string) : mType(LST_STRING), mInteger(0), mFP(0.f), mKey(NULL), mString(NULL), mVec(), mQuat(), mListp(NULL)
00395         {
00396                 if (!string)
00397                 {
00398                         mString = new char[1];
00399                         mString[0] = 0;
00400                 }
00401                 else
00402                 {
00403                         mString = new char[strlen(string) + 1]; /* Flawfinder: ignore */
00404                         if (mString == NULL)
00405                         {
00406                                 llerrs << "Memory Allocation Failed" << llendl;
00407                                 return;
00408                         }
00409                         strcpy(mString, string);        /* Flawfinder: ignore */
00410                 }
00411         }
00412 
00413         LLScriptLibData(const char *string) : mType(LST_STRING), mInteger(0), mFP(0.f), mKey(NULL), mString(NULL), mVec(), mQuat(), mListp(NULL)
00414         {
00415                 if (!string)
00416                 {
00417                         mString = new char[1];
00418                         mString[0] = 0;
00419                 }
00420                 else
00421                 {
00422                         mString = new char[strlen(string) + 1]; /* Flawfinder: ignore */
00423                         if (mString == NULL)
00424                         {
00425                                 llerrs << "Memory Allocation Failed" << llendl;
00426                                 return;
00427                         }
00428                         strcpy(mString, string);        /* Flawfinder: ignore */
00429                 }
00430         }
00431 
00432         LLScriptLibData(const LLVector3 &vec) : mType(LST_VECTOR), mInteger(0), mFP(0.f), mKey(NULL), mString(NULL), mVec(vec), mQuat(), mListp(NULL)
00433         {
00434         }
00435 
00436         LLScriptLibData(const LLQuaternion &quat) : mType(LST_QUATERNION), mInteger(0), mFP(0.f), mKey(NULL), mString(NULL), mVec(), mQuat(quat), mListp(NULL)
00437         {
00438         }
00439 
00440         ~LLScriptLibData()
00441         {
00442                 delete mListp;
00443                 delete [] mKey;
00444                 delete [] mString;
00445         }
00446 
00447 };
00448 
00449 #endif

Generated on Thu Jul 1 06:09:51 2010 for Second Life Viewer by  doxygen 1.4.7