/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. #include "linden_common.h"
  27. #include "llscriptresource.h"
  28. #include "llerror.h"
  29. LLScriptResource::LLScriptResource()
  30. : mTotal(0),
  31. mUsed(0)
  32. {
  33. }
  34. bool LLScriptResource::request(S32 amount /* = 1 */)
  35. {
  36. if (mUsed + amount <= mTotal)
  37. {
  38. mUsed += amount;
  39. return true;
  40. }
  41. return false;
  42. }
  43. bool LLScriptResource::release(S32 amount /* = 1 */)
  44. {
  45. if (mUsed >= amount)
  46. {
  47. mUsed -= amount;
  48. return true;
  49. }
  50. return false;
  51. }
  52. S32 LLScriptResource::getAvailable() const
  53. {
  54. if (mUsed > mTotal)
  55. {
  56. // It is possible after a parcel ownership change for more than total to be used
  57. // In this case the user of this class just wants to know
  58. // whether or not they can use a resource
  59. return 0;
  60. }
  61. return (mTotal - mUsed);
  62. }
  63. void LLScriptResource::setTotal(S32 amount)
  64. {
  65. // This may cause this resource to be over spent
  66. // such that more are in use than total allowed
  67. // Until those resources are released getAvailable will return 0.
  68. mTotal = amount;
  69. }
  70. S32 LLScriptResource::getTotal() const
  71. {
  72. return mTotal;
  73. }
  74. S32 LLScriptResource::getUsed() const
  75. {
  76. return mUsed;
  77. }
  78. bool LLScriptResource::isOverLimit() const
  79. {
  80. return (mUsed > mTotal);
  81. }