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.

Result.h 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // Created by robin on 8/9/15.
  3. //
  4. #ifndef RESULT_H
  5. #define RESULT_H
  6. #include <string>
  7. #include <vector>
  8. #include <iostream>
  9. #include <errno.h>
  10. #include <string.h>
  11. namespace LibNfc
  12. {
  13. namespace Utils
  14. {
  15. template <class T> class Result
  16. {
  17. public:
  18. static const Result<T> ok(const T& data);
  19. static const Result<T> strerror();
  20. static const Result<T> error(const std::string& error);
  21. template <class U> static const Result<T> error(const Result<U>& other);
  22. T& getData();
  23. const bool isSuccess() const;
  24. const std::string& getError() const;
  25. bool operator !() const;
  26. operator bool() const;
  27. template<class U>
  28. friend std::ostream& operator<<(std::ostream& os, const Result<U>& res);
  29. const Result<T>& print() const;
  30. private:
  31. Result();
  32. T _data;
  33. std::string _error;
  34. bool _success;
  35. };
  36. typedef Result<bool> ResultBool;
  37. typedef Result<int> ResultInt;
  38. typedef Result<long> ResultLong;
  39. typedef Result<float> ResultFloat;
  40. typedef Result<double> ResultDouble;
  41. typedef Result<char> ResultChar;
  42. typedef Result<std::string> ResultString;
  43. template<class T>
  44. Result<T>::Result()
  45. {
  46. }
  47. template<class T>
  48. const Result<T> Result<T>::ok(const T& data)
  49. {
  50. Result<T> r;
  51. r._success = true;
  52. r._data = data;
  53. return r;
  54. }
  55. template<class T>
  56. const Result<T> Result<T>::strerror()
  57. {
  58. Result<T> r;
  59. r._success = false;
  60. r._error = ::strerror(errno);
  61. return r;
  62. }
  63. template<class T>
  64. const Result<T> Result<T>::error(const std::string& error)
  65. {
  66. Result<T> r;
  67. r._success = false;
  68. r._error = error;
  69. return r;
  70. }
  71. template<class T>
  72. template<class U>
  73. const Result<T> Result<T>::error(const Result<U>& other)
  74. {
  75. Result<T> r;
  76. r._success = false;
  77. r.error(other.getError());
  78. return r;
  79. }
  80. template<class T>
  81. T &Result<T>::getData()
  82. {
  83. return _data;
  84. }
  85. template<class T>
  86. const bool Result<T>::isSuccess() const
  87. {
  88. return _success;
  89. }
  90. template<class T>
  91. bool Result<T>::operator!() const
  92. {
  93. return !isSuccess();
  94. }
  95. template<class T>
  96. Result<T>::operator bool() const
  97. {
  98. return isSuccess();
  99. }
  100. template<class T>
  101. const std::string &Result<T>::getError() const
  102. {
  103. return _error;
  104. }
  105. template<class U>
  106. std::ostream& operator<<(std::ostream& os, const Result<U>& res)
  107. {
  108. if (res._success) {
  109. os << "Success";// << res._data;
  110. }
  111. else {
  112. os << "Error: " << (res._error.empty() ? "Unknown error" : res._error);
  113. }
  114. return os;
  115. }
  116. template<class T>
  117. const Result<T>& Result<T>::print() const
  118. {
  119. (_success ? std::cout : std::cerr) << *this << std::endl;
  120. return *this;
  121. }
  122. }; // Utils
  123. }; // LibNfc
  124. #endif //RESULT_H