Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

104 righe
2.1 KiB

  1. #pragma once
  2. namespace utl
  3. {
  4. /* simple class that stores a value of type T */
  5. template<class T>
  6. struct wrapper
  7. {
  8. using value_type = T;
  9. value_type value;
  10. inline value_type& operator*()
  11. { return value; }
  12. inline const value_type& operator*() const
  13. { return value; }
  14. inline wrapper& operator=(value_type v)
  15. {
  16. value = v;
  17. return *this;
  18. }
  19. inline wrapper& operator=(const wrapper& other)
  20. {
  21. value = other.value;
  22. return *this;
  23. }
  24. inline wrapper& operator=(wrapper&& other)
  25. {
  26. value = std::move(other).value;
  27. return *this;
  28. }
  29. inline wrapper() :
  30. value(value_type())
  31. { }
  32. inline wrapper(const value_type& v) :
  33. value(v)
  34. { }
  35. inline wrapper(const wrapper& other) :
  36. value(other.value)
  37. { }
  38. inline wrapper(wrapper&& other) :
  39. value(std::move(other.value))
  40. { }
  41. };
  42. template<class T>
  43. struct wrapper<T&>
  44. {
  45. using value_type = T&;
  46. using storage_type = T*;
  47. storage_type value;
  48. inline value_type operator*() const
  49. {
  50. assert(value != nullptr);
  51. return *value;
  52. }
  53. inline wrapper& operator=(value_type v)
  54. {
  55. value = &v;
  56. return *this;
  57. }
  58. inline wrapper& operator=(const wrapper& other)
  59. {
  60. value = other.value;
  61. return *this;
  62. }
  63. inline wrapper& operator=(wrapper&& other)
  64. {
  65. value = std::move(other.value);
  66. return *this;
  67. }
  68. inline wrapper() :
  69. value(nullptr)
  70. { }
  71. inline wrapper(value_type v) :
  72. value(&v)
  73. { }
  74. inline wrapper(const wrapper& other) :
  75. value(other.value)
  76. { }
  77. inline wrapper(wrapper&& other) :
  78. value(std::move(other.value))
  79. { }
  80. };
  81. }