123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- //
- // Created by robin on 8/9/15.
- //
-
- #ifndef RESULT_H
- #define RESULT_H
-
- #include <string>
- #include <vector>
- #include <iostream>
- #include <errno.h>
- #include <string.h>
-
- namespace LibNfc
- {
- namespace Utils
- {
-
- template <class T> class Result
- {
- public:
- static const Result<T> ok(const T& data);
- static const Result<T> strerror();
- static const Result<T> error(const std::string& error);
- template <class U> static const Result<T> error(const Result<U>& other);
-
- T& getData();
- const bool isSuccess() const;
- const std::string& getError() const;
-
- bool operator !() const;
- operator bool() const;
-
- template<class U>
- friend std::ostream& operator<<(std::ostream& os, const Result<U>& res);
-
- const Result<T>& print() const;
-
- private:
- Result();
-
- T _data;
-
- std::string _error;
-
- bool _success;
- };
-
- typedef Result<bool> ResultBool;
-
- typedef Result<int> ResultInt;
-
- typedef Result<long> ResultLong;
-
- typedef Result<float> ResultFloat;
-
- typedef Result<double> ResultDouble;
-
- typedef Result<char> ResultChar;
-
- typedef Result<std::string> ResultString;
-
-
- template<class T>
- Result<T>::Result()
- {
- }
-
- template<class T>
- const Result<T> Result<T>::ok(const T& data)
- {
- Result<T> r;
- r._success = true;
- r._data = data;
- return r;
- }
-
- template<class T>
- const Result<T> Result<T>::strerror()
- {
- Result<T> r;
- r._success = false;
- r._error = ::strerror(errno);
- return r;
- }
-
- template<class T>
- const Result<T> Result<T>::error(const std::string& error)
- {
- Result<T> r;
- r._success = false;
- r._error = error;
- return r;
- }
-
- template<class T>
- template<class U>
- const Result<T> Result<T>::error(const Result<U>& other)
- {
- Result<T> r;
- r._success = false;
- r.error(other.getError());
- return r;
- }
-
- template<class T>
- T &Result<T>::getData()
- {
- return _data;
- }
-
- template<class T>
- const bool Result<T>::isSuccess() const
- {
- return _success;
- }
-
- template<class T>
- bool Result<T>::operator!() const
- {
- return !isSuccess();
- }
-
- template<class T>
- Result<T>::operator bool() const
- {
- return isSuccess();
- }
-
- template<class T>
- const std::string &Result<T>::getError() const
- {
- return _error;
- }
-
- template<class U>
- std::ostream& operator<<(std::ostream& os, const Result<U>& res)
- {
- if (res._success) {
- os << "Success";// << res._data;
- }
- else {
- os << "Error: " << (res._error.empty() ? "Unknown error" : res._error);
- }
- return os;
- }
-
- template<class T>
- const Result<T>& Result<T>::print() const
- {
- (_success ? std::cout : std::cerr) << *this << std::endl;
- return *this;
- }
-
- }; // Utils
- }; // LibNfc
-
- #endif //RESULT_H
|