|  | @@ -2,8 +2,9 @@
 | 
		
	
		
			
			| 2 | 2 |  // Created by robin on 8/9/15.
 | 
		
	
		
			
			| 3 | 3 |  //
 | 
		
	
		
			
			| 4 | 4 |  
 | 
		
	
		
			
			| 5 |  | -#include <iostream>
 | 
		
	
		
			
			| 6 | 5 |  #include "Result.h"
 | 
		
	
		
			
			|  | 6 | +#include <errno.h>
 | 
		
	
		
			
			|  | 7 | +#include <string.h>
 | 
		
	
		
			
			| 7 | 8 |  
 | 
		
	
		
			
			| 8 | 9 |  template<class T>
 | 
		
	
		
			
			| 9 | 10 |  Result<T>::Result()
 | 
		
	
	
		
			
			|  | @@ -11,46 +12,52 @@ Result<T>::Result()
 | 
		
	
		
			
			| 11 | 12 |  }
 | 
		
	
		
			
			| 12 | 13 |  
 | 
		
	
		
			
			| 13 | 14 |  template<class T>
 | 
		
	
		
			
			| 14 |  | -Result<T>::Result(const T &data)
 | 
		
	
		
			
			| 15 |  | -        : _data(data)
 | 
		
	
		
			
			|  | 15 | +const Result<T> Result<T>::ok(const T& data)
 | 
		
	
		
			
			| 16 | 16 |  {
 | 
		
	
		
			
			|  | 17 | +    Result<T> r;
 | 
		
	
		
			
			|  | 18 | +    r._success = true;
 | 
		
	
		
			
			|  | 19 | +    r._data = data;
 | 
		
	
		
			
			|  | 20 | +    return r;
 | 
		
	
		
			
			| 17 | 21 |  }
 | 
		
	
		
			
			| 18 | 22 |  
 | 
		
	
		
			
			| 19 | 23 |  template<class T>
 | 
		
	
		
			
			| 20 |  | -Result<T>::~Result()
 | 
		
	
		
			
			|  | 24 | +const Result<T> Result<T>::strerror()
 | 
		
	
		
			
			| 21 | 25 |  {
 | 
		
	
		
			
			|  | 26 | +    Result<T> r;
 | 
		
	
		
			
			|  | 27 | +    r._success = false;
 | 
		
	
		
			
			|  | 28 | +    r._error = ::strerror(errno);
 | 
		
	
		
			
			|  | 29 | +    return r;
 | 
		
	
		
			
			| 22 | 30 |  }
 | 
		
	
		
			
			| 23 | 31 |  
 | 
		
	
		
			
			| 24 | 32 |  template<class T>
 | 
		
	
		
			
			| 25 |  | -Result<T> &Result<T>::ok(const T &data)
 | 
		
	
		
			
			|  | 33 | +const Result<T> Result<T>::error(const std::string& error)
 | 
		
	
		
			
			| 26 | 34 |  {
 | 
		
	
		
			
			| 27 |  | -    _data = data;
 | 
		
	
		
			
			| 28 |  | -    return *this;
 | 
		
	
		
			
			|  | 35 | +    Result<T> r;
 | 
		
	
		
			
			|  | 36 | +    r._success = false;
 | 
		
	
		
			
			|  | 37 | +    r._error = error;
 | 
		
	
		
			
			|  | 38 | +    return r;
 | 
		
	
		
			
			| 29 | 39 |  }
 | 
		
	
		
			
			| 30 | 40 |  
 | 
		
	
		
			
			| 31 | 41 |  template<class T>
 | 
		
	
		
			
			| 32 |  | -T &Result<T>::getData()
 | 
		
	
		
			
			|  | 42 | +template<class U>
 | 
		
	
		
			
			|  | 43 | +const Result<T> Result<T>::error(const Result<U>& other)
 | 
		
	
		
			
			| 33 | 44 |  {
 | 
		
	
		
			
			| 34 |  | -    return _data;
 | 
		
	
		
			
			|  | 45 | +    Result<T> r;
 | 
		
	
		
			
			|  | 46 | +    r._success = false;
 | 
		
	
		
			
			|  | 47 | +    r.error(other.getError());
 | 
		
	
		
			
			|  | 48 | +    return r;
 | 
		
	
		
			
			| 35 | 49 |  }
 | 
		
	
		
			
			| 36 | 50 |  
 | 
		
	
		
			
			| 37 | 51 |  template<class T>
 | 
		
	
		
			
			| 38 |  | -bool Result<T>::isSuccess() const
 | 
		
	
		
			
			|  | 52 | +T &Result<T>::getData()
 | 
		
	
		
			
			| 39 | 53 |  {
 | 
		
	
		
			
			| 40 |  | -    return _error.empty();
 | 
		
	
		
			
			|  | 54 | +    return _data;
 | 
		
	
		
			
			| 41 | 55 |  }
 | 
		
	
		
			
			| 42 | 56 |  
 | 
		
	
		
			
			| 43 | 57 |  template<class T>
 | 
		
	
		
			
			| 44 |  | -const std::vector<std::string>& Result<T>::getWarnings() const
 | 
		
	
		
			
			|  | 58 | +const bool Result<T>::isSuccess() const
 | 
		
	
		
			
			| 45 | 59 |  {
 | 
		
	
		
			
			| 46 |  | -    return _warnings;
 | 
		
	
		
			
			| 47 |  | -}
 | 
		
	
		
			
			| 48 |  | -
 | 
		
	
		
			
			| 49 |  | -template<class T>
 | 
		
	
		
			
			| 50 |  | -Result<T>& Result<T>::addWarning(const std::string &warning)
 | 
		
	
		
			
			| 51 |  | -{
 | 
		
	
		
			
			| 52 |  | -    _warnings.push_back(warning);
 | 
		
	
		
			
			| 53 |  | -    return *this;
 | 
		
	
		
			
			|  | 60 | +    return _success;
 | 
		
	
		
			
			| 54 | 61 |  }
 | 
		
	
		
			
			| 55 | 62 |  
 | 
		
	
		
			
			| 56 | 63 |  template<class T>
 | 
		
	
	
		
			
			|  | @@ -66,24 +73,26 @@ Result<T>::operator bool() const
 | 
		
	
		
			
			| 66 | 73 |  }
 | 
		
	
		
			
			| 67 | 74 |  
 | 
		
	
		
			
			| 68 | 75 |  template<class T>
 | 
		
	
		
			
			| 69 |  | -Result<T> &Result<T>::error(const std::string &error)
 | 
		
	
		
			
			|  | 76 | +const std::string &Result<T>::getError() const
 | 
		
	
		
			
			| 70 | 77 |  {
 | 
		
	
		
			
			| 71 |  | -    _error = error;
 | 
		
	
		
			
			| 72 |  | -    return *this;
 | 
		
	
		
			
			|  | 78 | +    return _error;
 | 
		
	
		
			
			| 73 | 79 |  }
 | 
		
	
		
			
			| 74 | 80 |  
 | 
		
	
		
			
			| 75 |  | -template<class T>
 | 
		
	
		
			
			| 76 |  | -const std::string &Result<T>::getError() const
 | 
		
	
		
			
			|  | 81 | +template<class U>
 | 
		
	
		
			
			|  | 82 | +std::ostream& operator<<(std::ostream& os, const Result<U>& res)
 | 
		
	
		
			
			| 77 | 83 |  {
 | 
		
	
		
			
			| 78 |  | -    return _error;
 | 
		
	
		
			
			|  | 84 | +    if (res._success) {
 | 
		
	
		
			
			|  | 85 | +        os << "Success";// << res._data;
 | 
		
	
		
			
			|  | 86 | +    }
 | 
		
	
		
			
			|  | 87 | +    else {
 | 
		
	
		
			
			|  | 88 | +        os << "Error: " << (res._error.empty() ? "Unknown error" : res._error);
 | 
		
	
		
			
			|  | 89 | +    }
 | 
		
	
		
			
			|  | 90 | +    return os;
 | 
		
	
		
			
			| 79 | 91 |  }
 | 
		
	
		
			
			| 80 | 92 |  
 | 
		
	
		
			
			| 81 | 93 |  template<class T>
 | 
		
	
		
			
			| 82 | 94 |  const Result<T>& Result<T>::print() const
 | 
		
	
		
			
			| 83 | 95 |  {
 | 
		
	
		
			
			| 84 |  | -    for (auto warning : _warnings)
 | 
		
	
		
			
			| 85 |  | -        std::cerr << "WARNING: " << warning << std::endl;
 | 
		
	
		
			
			| 86 |  | -    if (!isSuccess())
 | 
		
	
		
			
			| 87 |  | -        std::cerr << "ERROR: " << _error << std::endl;
 | 
		
	
		
			
			|  | 96 | +    (_success ? std::cout : std::cerr) << *this << std::endl;
 | 
		
	
		
			
			| 88 | 97 |      return *this;
 | 
		
	
		
			
			| 89 |  | -}
 | 
		
	
		
			
			|  | 98 | +}
 |