RefToValue.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // The Loki Library
  3. // Copyright (c) 2006 Richard Sposato
  4. // Copyright (c) 2006 Peter Kümmel
  5. // Permission to use, copy, modify, distribute and sell this software for any
  6. // purpose is hereby granted without fee, provided that the above copyright
  7. // notice appear in all copies and that both that copyright notice and this
  8. // permission notice appear in supporting documentation.
  9. // The authors make no representations about the
  10. // suitability of this software for any purpose. It is provided "as is"
  11. // without express or implied warranty.
  12. ////////////////////////////////////////////////////////////////////////////////
  13. #ifndef LOKI_REFTOVALUE_INC_
  14. #define LOKI_REFTOVALUE_INC_
  15. // $Id: RefToValue.h 751 2006-10-17 19:50:37Z syntheticpp $
  16. namespace Loki
  17. {
  18. ////////////////////////////////////////////////////////////////////////////////
  19. /// \class RefToValue
  20. ///
  21. /// \ingroup SmartPointerGroup
  22. /// Transports a reference as a value
  23. /// Serves to implement the Colvin/Gibbons trick for SmartPtr/ScopeGuard
  24. ////////////////////////////////////////////////////////////////////////////////
  25. template <class T>
  26. class RefToValue
  27. {
  28. public:
  29. RefToValue(T& ref) : ref_(ref)
  30. {}
  31. RefToValue(const RefToValue& rhs) : ref_(rhs.ref_)
  32. {}
  33. operator T& () const
  34. {
  35. return ref_;
  36. }
  37. private:
  38. // Disable - not implemented
  39. RefToValue();
  40. RefToValue& operator=(const RefToValue&);
  41. T& ref_;
  42. };
  43. ////////////////////////////////////////////////////////////////////////////////
  44. /// \ingroup ExceptionGroup
  45. /// RefToValue creator.
  46. ////////////////////////////////////////////////////////////////////////////////
  47. template <class T>
  48. inline RefToValue<T> ByRef(T& t)
  49. {
  50. return RefToValue<T>(t);
  51. }
  52. }
  53. #endif // end file guardian