PageRenderTime 38ms CodeModel.GetById 1ms app.highlight 32ms RepoModel.GetById 1ms app.codeStats 1ms

/indra/lscript/lscript_library.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 427 lines | 365 code | 35 blank | 27 comment | 53 complexity | c82908a3bba2507ba111c25228bc3f9f MD5 | raw file
  1/** 
  2 * @file lscript_library.h
  3 * @brief External library interface
  4 *
  5 * $LicenseInfo:firstyear=2002&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#ifndef LL_LSCRIPT_LIBRARY_H
 28#define LL_LSCRIPT_LIBRARY_H
 29
 30#include "lscript_byteformat.h"
 31#include "v3math.h"
 32#include "llquaternion.h"
 33#include "lluuid.h"
 34#include "lscript_byteconvert.h"
 35
 36class LLScriptLibData;
 37
 38class LLScriptLibraryFunction
 39{
 40public:
 41	LLScriptLibraryFunction(F32 eu, F32 st, void (*exec_func)(LLScriptLibData *, LLScriptLibData *, const LLUUID &), const char *name, const char *ret_type, const char *args, BOOL god_only = FALSE);
 42	~LLScriptLibraryFunction();
 43
 44	F32  mEnergyUse;
 45	F32  mSleepTime;
 46	void (*mExecFunc)(LLScriptLibData *, LLScriptLibData *, const LLUUID &);
 47	const char *mName;
 48	const char *mReturnType;
 49	const char *mArgs;
 50	BOOL mGodOnly;
 51};
 52
 53class LLScriptLibrary
 54{
 55public:
 56	LLScriptLibrary();
 57	~LLScriptLibrary();
 58
 59	void init();
 60
 61	void addFunction(F32 eu, F32 st, void (*exec_func)(LLScriptLibData *, LLScriptLibData *, const LLUUID &), const char *name, const char *ret_type, const char *args, BOOL god_only = FALSE);
 62	void assignExec(const char *name, void (*exec_func)(LLScriptLibData *, LLScriptLibData *, const LLUUID &));
 63
 64	std::vector<LLScriptLibraryFunction>	mFunctions;
 65};
 66
 67
 68
 69class LLScriptLibData
 70{
 71public:
 72	// TODO: Change this to a union
 73	LSCRIPTType		mType;
 74	S32				mInteger;
 75	F32				mFP;
 76	char			*mKey;
 77	char			*mString;
 78	LLVector3		mVec;
 79	LLQuaternion	mQuat;
 80	LLScriptLibData *mListp;
 81
 82	friend bool operator<=(const LLScriptLibData &a, const LLScriptLibData &b)
 83	{
 84		if (a.mType == b.mType)
 85		{
 86			if (a.mType == LST_INTEGER)
 87			{
 88				return a.mInteger <= b.mInteger;
 89			}
 90			if (a.mType == LST_FLOATINGPOINT)
 91			{
 92				return a.mFP <= b.mFP;
 93			}
 94			if (a.mType == LST_STRING)
 95			{
 96				return strcmp(a.mString, b.mString) <= 0;
 97			}
 98			if (a.mType == LST_KEY)
 99			{
100				return strcmp(a.mKey, b.mKey) <= 0;
101			}
102			if (a.mType == LST_VECTOR)
103			{
104				return a.mVec.magVecSquared() <= b.mVec.magVecSquared();
105			}
106		}
107		return TRUE;
108	}
109
110	friend bool operator==(const LLScriptLibData &a, const LLScriptLibData &b)
111	{
112		if (a.mType == b.mType)
113		{
114			if (a.mType == LST_INTEGER)
115			{
116				return a.mInteger == b.mInteger;
117			}
118			if (a.mType == LST_FLOATINGPOINT)
119			{
120				return a.mFP == b.mFP;
121			}
122			if (a.mType == LST_STRING)
123			{
124				return !strcmp(a.mString, b.mString);
125			}
126			if (a.mType == LST_KEY)
127			{
128				return !strcmp(a.mKey, b.mKey);
129			}
130			if (a.mType == LST_VECTOR)
131			{
132				return a.mVec == b.mVec;
133			}
134			if (a.mType == LST_QUATERNION)
135			{
136				return a.mQuat == b.mQuat;
137			}
138		}
139		return FALSE;
140	}
141
142	S32 getListLength() const
143	{
144		const LLScriptLibData *data = this;
145		S32 retval = 0;
146		while (data->mListp)
147		{
148			retval++;
149			data = data->mListp;
150		}
151		return retval;
152	}
153
154	BOOL checkForMultipleLists()
155	{
156		LLScriptLibData *data = this;
157		while (data->mListp)
158		{
159			data = data->mListp;
160			if (data->mType == LST_LIST)
161				return TRUE;
162		}
163		return FALSE;
164	}
165
166	S32  getSavedSize()
167	{
168		S32 size = 0;
169		// mType
170		size += 4;
171
172		switch(mType)
173		{
174		case LST_INTEGER:
175			size += 4;
176			break;
177		case LST_FLOATINGPOINT:
178			size += 4;
179			break;
180		case LST_KEY:
181			size += (S32)strlen(mKey) + 1;	/*Flawfinder: ignore*/
182			break;
183		case LST_STRING:
184			size += (S32)strlen(mString) + 1;	/*Flawfinder: ignore*/
185			break;
186		case LST_LIST:
187			break;
188		case LST_VECTOR:
189			size += 12;
190			break;
191		case LST_QUATERNION:
192			size += 16;
193			break;
194		default:
195			break;
196		}
197		return size;
198	}
199
200	S32	 write2bytestream(U8 *dest)
201	{
202		S32 offset = 0;
203		integer2bytestream(dest, offset, mType);
204		switch(mType)
205		{
206		case LST_INTEGER:
207			integer2bytestream(dest, offset, mInteger);
208			break;
209		case LST_FLOATINGPOINT:
210			float2bytestream(dest, offset, mFP);
211			break;
212		case LST_KEY:
213			char2bytestream(dest, offset, mKey);
214			break;
215		case LST_STRING:
216			char2bytestream(dest, offset, mString);
217			break;
218		case LST_LIST:
219			break;
220		case LST_VECTOR:
221			vector2bytestream(dest, offset, mVec);
222			break;
223		case LST_QUATERNION:
224			quaternion2bytestream(dest, offset, mQuat);
225			break;
226		default:
227			break;
228		}
229		return offset;
230	}
231
232	LLScriptLibData() : mType(LST_NULL), mInteger(0), mFP(0.f), mKey(NULL), mString(NULL), mVec(), mQuat(), mListp(NULL)
233	{
234	}
235
236	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)
237	{
238		if (data.mKey)
239		{
240			mKey = new char[strlen(data.mKey) + 1];	/* Flawfinder: ignore */
241			if (mKey == NULL)
242			{
243				llerrs << "Memory Allocation Failed" << llendl;
244				return;
245			}
246			strcpy(mKey, data.mKey);	/* Flawfinder: ignore */
247		}
248		if (data.mString)
249		{
250			mString = new char[strlen(data.mString) + 1];	/* Flawfinder: ignore */
251			if (mString == NULL)
252			{
253				llerrs << "Memory Allocation Failed" << llendl;
254				return;
255			}
256			strcpy(mString, data.mString);	/* Flawfinder: ignore */
257		}
258	}
259
260	LLScriptLibData(U8 *src, S32 &offset) : mListp(NULL)
261	{
262		static char temp[TOP_OF_MEMORY];	/* Flawfinder: ignore */
263		mType = (LSCRIPTType)bytestream2integer(src, offset);
264		switch(mType)
265		{
266		case LST_INTEGER:
267			mInteger = bytestream2integer(src, offset);
268			break;
269		case LST_FLOATINGPOINT:
270			mFP = bytestream2float(src, offset);
271			break;
272		case LST_KEY:
273			{
274				bytestream2char(temp, src, offset, sizeof(temp));
275				mKey = new char[strlen(temp) + 1];	/* Flawfinder: ignore */
276				if (mKey == NULL)
277				{
278					llerrs << "Memory Allocation Failed" << llendl;
279					return;
280				}
281				strcpy(mKey, temp);	/* Flawfinder: ignore */
282			}
283			break;
284		case LST_STRING:
285			{
286				bytestream2char(temp, src, offset, sizeof(temp));
287				mString = new char[strlen(temp) + 1];	/* Flawfinder: ignore */
288				if (mString == NULL)
289				{
290					llerrs << "Memory Allocation Failed" << llendl;
291					return;
292				}
293				strcpy(mString, temp);	/* Flawfinder: ignore */
294			}
295			break;
296		case LST_LIST:
297			break;
298		case LST_VECTOR:
299			bytestream2vector(mVec, src, offset);
300			break;
301		case LST_QUATERNION:
302			bytestream2quaternion(mQuat, src, offset);
303			break;
304		default:
305			break;
306		}
307	}
308
309	void set(U8 *src, S32 &offset)
310	{
311		static char temp[TOP_OF_MEMORY];	/* Flawfinder: ignore */
312		mType = (LSCRIPTType)bytestream2integer(src, offset);
313		switch(mType)
314		{
315		case LST_INTEGER:
316			mInteger = bytestream2integer(src, offset);
317			break;
318		case LST_FLOATINGPOINT:
319			mFP = bytestream2float(src, offset);
320			break;
321		case LST_KEY:
322			{
323				bytestream2char(temp, src, offset, sizeof(temp));
324				mKey = new char[strlen(temp) + 1];	/* Flawfinder: ignore */
325				if (mKey == NULL)
326				{
327					llerrs << "Memory Allocation Failed" << llendl;
328					return;
329				}
330				strcpy(mKey, temp);	/* Flawfinder: ignore */
331			}
332			break;
333		case LST_STRING:
334			{
335				bytestream2char(temp, src, offset, sizeof(temp));
336				mString = new char[strlen(temp) + 1];	/* Flawfinder: ignore */
337				if (mString == NULL)
338				{
339					llerrs << "Memory Allocation Failed" << llendl;
340					return;
341				}
342				strcpy(mString, temp);	/* Flawfinder: ignore */
343			}
344			break;
345		case LST_LIST:
346			break;
347		case LST_VECTOR:
348			bytestream2vector(mVec, src, offset);
349			break;
350		case LST_QUATERNION:
351			bytestream2quaternion(mQuat, src, offset);
352			break;
353		default:
354			break;
355		}
356	}
357
358	void print(std::ostream &s, BOOL b_prepend_comma);
359	void print_separator(std::ostream& ostr, BOOL b_prepend_sep, char* sep);
360
361	void setFromCSV(const char *src)
362	{
363		mType = LST_STRING;
364		mString = new char[strlen(src) + 1];	/* Flawfinder: ignore */
365		if (mString == NULL)
366		{
367			llerrs << "Memory Allocation Failed" << llendl;
368			return;
369		}
370		strcpy(mString, src);	/* Flawfinder: ignore */
371	}
372
373	LLScriptLibData(S32 integer) : mType(LST_INTEGER), mInteger(integer), mFP(0.f), mKey(NULL), mString(NULL), mVec(), mQuat(), mListp(NULL)
374	{
375	}
376
377	LLScriptLibData(F32 fp) : mType(LST_FLOATINGPOINT), mInteger(0), mFP(fp), mKey(NULL), mString(NULL), mVec(), mQuat(), mListp(NULL)
378	{
379	}
380
381	LLScriptLibData(const LLUUID &id) : mType(LST_KEY), mInteger(0), mFP(0.f), mKey(NULL), mString(NULL), mVec(), mQuat(), mListp(NULL)
382	{
383		std::string idstr;
384		id.toString(idstr);
385		mKey = new char[idstr.length()+1];
386		LLStringUtil::copy(mKey,idstr.c_str(),idstr.length()+1);
387	}
388
389	LLScriptLibData(const char *string) : mType(LST_STRING), mInteger(0), mFP(0.f), mKey(NULL), mString(NULL), mVec(), mQuat(), mListp(NULL)
390	{
391		if (!string)
392		{
393			mString = new char[1];
394			mString[0] = 0;
395		}
396		else
397		{
398			mString = new char[strlen(string) + 1];	/* Flawfinder: ignore */
399			if (mString == NULL)
400			{
401				llerrs << "Memory Allocation Failed" << llendl;
402				return;
403			}
404			strcpy(mString, string);	/* Flawfinder: ignore */
405		}
406	}
407
408	LLScriptLibData(const LLVector3 &vec) : mType(LST_VECTOR), mInteger(0), mFP(0.f), mKey(NULL), mString(NULL), mVec(vec), mQuat(), mListp(NULL)
409	{
410	}
411
412	LLScriptLibData(const LLQuaternion &quat) : mType(LST_QUATERNION), mInteger(0), mFP(0.f), mKey(NULL), mString(NULL), mVec(), mQuat(quat), mListp(NULL)
413	{
414	}
415
416	~LLScriptLibData()
417	{
418		delete mListp;
419		delete [] mKey;
420		delete [] mString;
421	}
422
423};
424
425extern LLScriptLibrary gScriptLibrary;
426
427#endif