mirror of
https://github.com/nextcloud/desktop.git
synced 2024-10-26 22:35:55 +03:00
Move Result to its own header
This commit is contained in:
parent
afed46afcc
commit
059f722b3b
3 changed files with 78 additions and 57 deletions
|
@ -32,63 +32,6 @@ namespace OCC {
|
|||
|
||||
class AbstractSslErrorHandler;
|
||||
|
||||
|
||||
/**
|
||||
* A Result of type T, or an Error that contains a code and a string
|
||||
**/
|
||||
template <typename T>
|
||||
class Result
|
||||
{
|
||||
struct Error
|
||||
{
|
||||
QString string;
|
||||
int code;
|
||||
};
|
||||
union {
|
||||
T _result;
|
||||
Error _error;
|
||||
};
|
||||
bool _isError;
|
||||
|
||||
public:
|
||||
Result(T value)
|
||||
: _result(std::move(value))
|
||||
, _isError(false){};
|
||||
Result(int code, QString str)
|
||||
: _error({ std::move(str), code })
|
||||
, _isError(true)
|
||||
{
|
||||
}
|
||||
~Result()
|
||||
{
|
||||
if (_isError)
|
||||
_error.~Error();
|
||||
else
|
||||
_result.~T();
|
||||
}
|
||||
explicit operator bool() const { return !_isError; }
|
||||
const T &operator*() const &
|
||||
{
|
||||
ASSERT(!_isError);
|
||||
return _result;
|
||||
}
|
||||
T operator*() &&
|
||||
{
|
||||
ASSERT(!_isError);
|
||||
return std::move(_result);
|
||||
}
|
||||
QString errorMessage() const
|
||||
{
|
||||
ASSERT(_isError);
|
||||
return _error.string;
|
||||
}
|
||||
int errorCode() const
|
||||
{
|
||||
return _isError ? _error.code : 0;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief The AbstractNetworkJob class
|
||||
* @ingroup libsync
|
||||
|
|
|
@ -18,6 +18,8 @@
|
|||
|
||||
#include "abstractnetworkjob.h"
|
||||
|
||||
#include "result.h"
|
||||
|
||||
#include <QBuffer>
|
||||
#include <QUrlQuery>
|
||||
#include <functional>
|
||||
|
|
76
src/libsync/result.h
Normal file
76
src/libsync/result.h
Normal file
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
* Copyright (C) by Olivier Goffart <ogoffart@woboq.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace OCC {
|
||||
|
||||
/**
|
||||
* A Result of type T, or an Error that contains a code and a string
|
||||
*
|
||||
* The code is an HTTP error code.
|
||||
**/
|
||||
template <typename T>
|
||||
class Result
|
||||
{
|
||||
struct Error
|
||||
{
|
||||
QString string;
|
||||
int code;
|
||||
};
|
||||
union {
|
||||
T _result;
|
||||
Error _error;
|
||||
};
|
||||
bool _isError;
|
||||
|
||||
public:
|
||||
Result(T value)
|
||||
: _result(std::move(value))
|
||||
, _isError(false){};
|
||||
Result(int code, QString str)
|
||||
: _error({ std::move(str), code })
|
||||
, _isError(true)
|
||||
{
|
||||
}
|
||||
~Result()
|
||||
{
|
||||
if (_isError)
|
||||
_error.~Error();
|
||||
else
|
||||
_result.~T();
|
||||
}
|
||||
explicit operator bool() const { return !_isError; }
|
||||
const T &operator*() const &
|
||||
{
|
||||
ASSERT(!_isError);
|
||||
return _result;
|
||||
}
|
||||
T operator*() &&
|
||||
{
|
||||
ASSERT(!_isError);
|
||||
return std::move(_result);
|
||||
}
|
||||
QString errorMessage() const
|
||||
{
|
||||
ASSERT(_isError);
|
||||
return _error.string;
|
||||
}
|
||||
int errorCode() const
|
||||
{
|
||||
return _isError ? _error.code : 0;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace OCC
|
Loading…
Reference in a new issue