This commit is contained in:
Erik Frojdh 2019-08-23 15:11:57 +02:00
parent 5866e330c1
commit e89b65002a
2 changed files with 19 additions and 18 deletions

View File

@ -1,38 +1,43 @@
/** Examples on how to use Result<T> */
#include "Result.h" #include "Result.h"
#include "ToString.h" #include "ToString.h"
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
auto main() -> int { using sls::Result;
using sls::Result; // declared in namespace sls
using sls::ToString; using sls::ToString;
auto main() -> int {
std::cout << "Examples on usage of Result<T>\n"; std::cout << "Examples on usage of Result<T>\n";
// Result exposes the underlying constructors of std::vector /** Constructing Result<T> can be done in the same way as vectors */
Result<int> res{1, 2, 3, 4, 5}; Result<int> res{1, 2, 3, 4, 5};
std::cout << "res: " << res << '\n'; std::cout << "res: " << res << '\n';
Result<double> res2(5, 3.7); Result<double> res2(5, 3.7);
std::cout << "res2: " << res2 << '\n'; std::cout << "res2: " << res2 << '\n';
// and can be converted to and from a vector. However, the /** and Result can be converted to and from a vector. However, the
// conversion to a vector is not efficient since a copy is made * conversion to a vector is not efficient since a copy is made
// and should only be done when a vector is needed for further use * and should only be done when a vector is needed for further use
// in most sense and in standard algorithms Result<T> behaves as a * in most cases Result behaved as a vector, for example with standard
// vector. * algorithms */
std::vector<int> vec(5, 5); std::vector<int> vec(5, 5);
std::cout << "vec: " << ToString(vec) << "\n"; std::cout << "vec: " << ToString(vec) << "\n";
//Result from vector
Result<int> res3 = vec; Result<int> res3 = vec;
std::cout << "res3: " << res3 << '\n'; std::cout << "res3: " << res3 << '\n';
// Vector from Result
std::vector<int> vec2 = res3; std::vector<int> vec2 = res3;
std::cout << "vec2: " << ToString(vec2) << "\n"; std::cout << "vec2: " << ToString(vec2) << "\n";
// Using squash we can convert to a single value
// Using squash we can also convert to a single value
std::cout << "res.squash(): " << res.squash() << '\n'; std::cout << "res.squash(): " << res.squash() << '\n';
std::cout << "res3.squash(): " << res3.squash() << '\n'; std::cout << "res3.squash(): " << res3.squash() << '\n';
@ -43,17 +48,14 @@ auto main() -> int {
Result<int> ivec{1, 3, 5}; Result<int> ivec{1, 3, 5};
Result<sls::time::ns> nres(ivec); Result<sls::time::ns> nres(ivec);
// for (const auto& i : ivec)
// nres.push_back(sls::time::ns(i));
std::cout << "nres: " << sls::ToString(nres) << '\n'; std::cout << "nres: " << sls::ToString(nres) << '\n';
//
/* Convert from Result<int> to Result<bool> */ /* Convert from Result<int> to Result<bool> */
Result<int> int_result{0, 1, 0, 3, -5}; Result<int> int_result{0, 1, 0, 3, -5};
Result<bool> bool_result{int_result}; Result<bool> bool_result{int_result};
std::cout << bool_result << '\n'; std::cout << bool_result << '\n';
// Result can be printed using <<
Result<std::string> string_res{"ein", "zwei", "drei"}; Result<std::string> string_res{"ein", "zwei", "drei"};
std::cout << string_res << '\n'; std::cout << string_res << '\n';
} }

View File

@ -1,6 +1,5 @@
#ifndef CMD_LINE_PARSER_H #ifndef CMD_LINE_PARSER_H
#define CMD_LINE_PARSER_H #define CMD_LINE_PARSER_H
#include <stdexcept>
#include <string> #include <string>
#include <vector> #include <vector>