PageRenderTime 9ms CodeModel.GetById 2ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/lscript/lscript_execute/llscriptresource.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 93 lines | 50 code | 12 blank | 31 comment | 3 complexity | 9d617b90f12c7cc1515258715095cf27 MD5 | raw file
 1/** 
 2 * @file llscriptresource.cpp
 3 * @brief LLScriptResource class implementation for managing limited resources
 4 *
 5 * $LicenseInfo:firstyear=2008&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#include "linden_common.h"
28
29#include "llscriptresource.h"
30#include "llerror.h"
31
32LLScriptResource::LLScriptResource()
33: mTotal(0),
34  mUsed(0)
35{
36}
37
38bool LLScriptResource::request(S32 amount /* = 1 */)
39{
40	if (mUsed + amount <= mTotal)
41	{
42		mUsed += amount;
43		return true;
44	}
45
46	return false;
47}
48
49bool LLScriptResource::release(S32 amount /* = 1 */)
50{
51	if (mUsed >= amount)
52	{
53		mUsed -= amount;
54		return true;
55	}
56
57	return false;
58}
59
60S32 LLScriptResource::getAvailable() const
61{
62	if (mUsed > mTotal)
63	{
64		// It is possible after a parcel ownership change for more than total to be used
65		// In this case the user of this class just wants to know 
66		// whether or not they can use a resource
67		return 0;
68	}
69	return (mTotal - mUsed);
70}
71
72void LLScriptResource::setTotal(S32 amount)
73{
74	// This may cause this resource to be over spent
75	// such that more are in use than total allowed
76	// Until those resources are released getAvailable will return 0.
77	mTotal = amount;
78}
79
80S32 LLScriptResource::getTotal() const
81{
82	return mTotal;
83}
84
85S32 LLScriptResource::getUsed() const
86{
87	return mUsed;
88}
89
90bool LLScriptResource::isOverLimit() const
91{
92	return (mUsed > mTotal);
93}