Move Result to its own header
authorOlivier Goffart <ogoffart@woboq.com>
Mon, 15 Oct 2018 14:51:24 +0000 (16:51 +0200)
committerKevin Ottens <kevin.ottens@nextcloud.com>
Tue, 15 Dec 2020 09:58:08 +0000 (10:58 +0100)
src/libsync/abstractnetworkjob.h
src/libsync/networkjobs.h
src/libsync/result.h [new file with mode: 0644]

index 8a98af6654ac1a9fa71043c4d43fb84bb500d002..c169e5451616cb267afc70ae8a7448e7e43ca946 100644 (file)
@@ -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
index 743b87ac507dc9552035f4cba1398b6c13d7e4c3..634e792a7a30b1dcc89f8243931b83e20e588785 100644 (file)
@@ -18,6 +18,8 @@
 
 #include "abstractnetworkjob.h"
 
+#include "result.h"
+
 #include <QBuffer>
 #include <QUrlQuery>
 #include <functional>
diff --git a/src/libsync/result.h b/src/libsync/result.h
new file mode 100644 (file)
index 0000000..58f70c4
--- /dev/null
@@ -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