Added Result in Utilities

Introduced typedefs for Result's data
This commit is contained in:
Valentino Orlandi 2023-01-22 20:53:13 +01:00
parent 358c818954
commit 1e8d4a9bbc
Signed by: elB4RTO
GPG Key ID: 1719E976DB2D4E71
2 changed files with 107 additions and 0 deletions

View File

@ -0,0 +1,56 @@
#include "result.h"
#include "modules/exceptions.h"
template <typename T>
Result<T>::Result()
{
this->result = false;
}
template <typename T>
Result<T>::Result( const bool ok, const T& data )
{
this->result = ok;
this->data = std::move(data);
}
template <typename T>
Result<T>::operator bool()
{
return this->result;
}
/*
template <typename T>
const bool Result<T>::isOk()
{
return this->result == true;
}
template <typename T>
const bool Result<T>::isErr()
{
return this->result == false;
}
*/
template <typename T>
const T& Result<T>::getData()
{
if ( this->result ) {
return this->data;
} else {
// unexpected operation
throw GenericException( "Result is Err, no data to be returned", true );
}
}
template class Result< stats_dates_t >; // std::unordered_map<int, std::unordered_map<int, std::unordered_map<int, std::vector<int>>>>
template class Result< stats_warn_items_t >; // std::vector<std::vector<std::vector<std::vector<QString>>>>
template class Result< stats_speed_items_t >; // std::vector<std::tuple<long long, std::vector<QString>>>
template class Result< stats_day_items_t >; // std::unordered_map<int, std::unordered_map<int, int>>
template class Result< stats_relat_items_t >; // std::vector<std::tuple<long long, int>>
template class Result< stats_count_items_t >; // std::vector<std::tuple<QString, int>>

View File

@ -0,0 +1,51 @@
#ifndef RESULT_H
#define RESULT_H
#include <QString>
#include <unordered_map>
#include <vector>
#include <tuple>
//! Result
/*!
Holds the result for an operation and the relative data
*/
template <typename T>
class Result
{
public:
Result();
Result( const bool ok, const T& data );
explicit operator bool();
/*
//! Checks if the operation was successful
const bool isOk();
//! Checks if the operation has failed
const bool isErr();
*/
//! Returns the data
const T& getData();
private:
bool result;
T data;
};
// data types
typedef std::unordered_map<int, std::unordered_map<int, std::unordered_map<int, std::vector<int>>>> stats_dates_t;
typedef std::vector<std::vector<std::vector<std::vector<QString>>>> stats_warn_items_t;
typedef std::vector<std::tuple<long long, std::vector<QString>>> stats_speed_items_t;
typedef std::unordered_map<int, std::unordered_map<int, int>> stats_day_items_t;
typedef std::vector<std::tuple<long long, int>> stats_relat_items_t;
typedef std::vector<std::tuple<QString, int>> stats_count_items_t;
#endif // RESULT_H