Reference.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2014 Markus Schöngart
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef ROCKETCOREREFERENCE_H
  28. #define ROCKETCOREREFERENCE_H
  29. #include "Header.h"
  30. #include <algorithm>
  31. namespace Rocket {
  32. namespace Core {
  33. template< class ReferenceCountable >
  34. class ROCKETCORE_API SharedReference;
  35. /**
  36. A smart pointer class template that manages ReferenceCountables.
  37. SharedReference allows unrestricted access to the shared object via every reference.
  38. @author Markus Schöngart
  39. @see ReferenceCountable
  40. */
  41. template< class ReferenceCountable >
  42. class ROCKETCORE_API SharedReference
  43. {
  44. public:
  45. typedef ReferenceCountable ReferenceType;
  46. typedef SharedReference< ReferenceCountable > ThisType;
  47. /// Constructor. Does not increase the object's reference count.
  48. /// @param[in] object The object to refer to.
  49. SharedReference(ReferenceType* object = 0) : object(object)
  50. { /*if (object) object->AddReference();*/ }
  51. /// Copy constructor. Increases the object's reference count.
  52. /// @param[in] other The other Reference.
  53. SharedReference(const ThisType& other) : object(other.object)
  54. { if (object) object->AddReference(); }
  55. /// Destructor. Decrements the stored object's reference count.
  56. ~SharedReference()
  57. { if (object) object->RemoveReference(); }
  58. /// Swaps the contents of two References.
  59. void Swap(ThisType& other) throw()
  60. { std::swap(object, other.object); }
  61. /// Assign another referenced object to this smart pointer
  62. const ThisType& operator=(ReferenceType* ref)
  63. { ThisType tmp(ref); Swap(tmp); return *this; }
  64. /// Assign another referenced object to this smart pointer
  65. const ThisType& operator=(const ThisType& other)
  66. { ThisType tmp(other); Swap(tmp); return *this; }
  67. const ReferenceType& operator*() const throw()
  68. { return *object; }
  69. ReferenceType& operator*() throw()
  70. { return *object; }
  71. const ReferenceType* operator->() const throw()
  72. { return object; }
  73. ReferenceType* operator->() throw()
  74. { return object; }
  75. private:
  76. mutable ReferenceType *object;
  77. };
  78. }
  79. }
  80. #endif