You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
2.2 KiB

  1. /*
  2. ISC License
  3. Copyright (c) 2020, Antonio SJ Musumeci <trapexit@spawn.link>
  4. Permission to use, copy, modify, and/or distribute this software for any
  5. purpose with or without fee is hereby granted, provided that the above
  6. copyright notice and this permission notice appear in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #pragma once
  16. #include "from_string.hpp"
  17. #include "to_string.hpp"
  18. #include "tofrom_string.hpp"
  19. #include <errno.h>
  20. template<typename T>
  21. class ToFromWrapper : public ToFromString
  22. {
  23. public:
  24. int
  25. from_string(const std::string &s_) final
  26. {
  27. return str::from(s_,&_data);
  28. }
  29. std::string
  30. to_string(void) const final
  31. {
  32. return str::to(_data);
  33. }
  34. public:
  35. ToFromWrapper<T>()
  36. {
  37. }
  38. ToFromWrapper<T>(const T data_)
  39. : _data(data_)
  40. {
  41. }
  42. public:
  43. ToFromWrapper<T>&
  44. operator=(const T &data_)
  45. {
  46. _data = data_;
  47. return *this;
  48. }
  49. public:
  50. operator const T&() const
  51. {
  52. return _data;
  53. }
  54. T*
  55. operator->()
  56. {
  57. return &_data;
  58. }
  59. const
  60. T*
  61. operator->() const
  62. {
  63. return &_data;
  64. }
  65. public:
  66. bool
  67. operator==(const T &data_) const
  68. {
  69. return (_data == data_);
  70. }
  71. private:
  72. T _data;
  73. };
  74. template<typename T>
  75. class ROToFromWrapper : public ToFromString
  76. {
  77. public:
  78. int
  79. from_string(const std::string &s_) final
  80. {
  81. return -EINVAL;
  82. }
  83. std::string
  84. to_string(void) const final
  85. {
  86. return str::to(_data);
  87. }
  88. public:
  89. ROToFromWrapper<T>()
  90. {
  91. }
  92. ROToFromWrapper<T>(const T data_)
  93. : _data(data_)
  94. {
  95. }
  96. public:
  97. operator T() const
  98. {
  99. return _data;
  100. }
  101. T*
  102. operator->()
  103. {
  104. return &_data;
  105. }
  106. public:
  107. bool
  108. operator==(const T &data_) const
  109. {
  110. return (_data == data_);
  111. }
  112. private:
  113. T _data;
  114. };