PageRenderTime 28ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llcommon/llrefcount.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 89 lines | 47 code | 11 blank | 31 comment | 2 complexity | 5c4b65905e4cd4940c33c2f8d1e423f8 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llrefcount.h
  3. * @brief Base class for reference counted objects for use with LLPointer
  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. #ifndef LLREFCOUNT_H
  27. #define LLREFCOUNT_H
  28. #include <boost/noncopyable.hpp>
  29. #define LL_REF_COUNT_DEBUG 0
  30. #if LL_REF_COUNT_DEBUG
  31. class LLMutex ;
  32. #endif
  33. //----------------------------------------------------------------------------
  34. // RefCount objects should generally only be accessed by way of LLPointer<>'s
  35. // see llthread.h for LLThreadSafeRefCount
  36. //----------------------------------------------------------------------------
  37. class LL_COMMON_API LLRefCount
  38. {
  39. protected:
  40. LLRefCount(const LLRefCount& other);
  41. LLRefCount& operator=(const LLRefCount&);
  42. virtual ~LLRefCount(); // use unref()
  43. public:
  44. LLRefCount();
  45. #if LL_REF_COUNT_DEBUG
  46. void ref() const ;
  47. S32 unref() const ;
  48. #else
  49. inline void ref() const
  50. {
  51. mRef++;
  52. }
  53. inline S32 unref() const
  54. {
  55. llassert(mRef >= 1);
  56. if (0 == --mRef)
  57. {
  58. delete this;
  59. return 0;
  60. }
  61. return mRef;
  62. }
  63. #endif
  64. //NOTE: when passing around a const LLRefCount object, this can return different results
  65. // at different types, since mRef is mutable
  66. S32 getNumRefs() const
  67. {
  68. return mRef;
  69. }
  70. private:
  71. mutable S32 mRef;
  72. #if LL_REF_COUNT_DEBUG
  73. LLMutex* mMutexp ;
  74. mutable U32 mLockedThreadID ;
  75. mutable BOOL mCrashAtUnlock ;
  76. #endif
  77. };
  78. #endif