1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef UTILS_FILEMGMT_LIBFS_INCLUDE_FS_RESULT_H 17 #define UTILS_FILEMGMT_LIBFS_INCLUDE_FS_RESULT_H 18 19 #include "fs_error.h" 20 21 #include <optional> 22 #include <string> 23 #include <type_traits> 24 #include <utility> 25 26 namespace OHOS::FileManagement::ModuleFileIO { 27 28 template <typename T> 29 class FsResult { 30 using OptionalData = std::optional<T>; 31 32 public: Success()33 static FsResult<T> Success() 34 { 35 return FsResult(FsError(ERRNO_NOERR), std::nullopt); 36 } 37 Success(const T & data)38 static FsResult<T> Success(const T &data) 39 { 40 return FsResult(FsError(ERRNO_NOERR), std::make_optional<T>(data)); 41 } 42 Success(T && data)43 static FsResult<T> Success(T &&data) 44 { 45 return FsResult(FsError(ERRNO_NOERR), std::make_optional<T>(std::move(data))); 46 } 47 48 template <typename U = T, std::enable_if_t<std::is_same_v<U, std::string>, int> = 0> Success(const char * data)49 static FsResult<U> Success(const char *data) 50 { 51 return FsResult(FsError(ERRNO_NOERR), std::make_optional<U>(std::string(data))); 52 } 53 Error(const int32_t code)54 static FsResult<T> Error(const int32_t code) 55 { 56 return FsResult(FsError(code), std::nullopt); 57 } 58 IsSuccess()59 bool IsSuccess() const 60 { 61 return !error_; 62 } 63 GetData()64 const OptionalData &GetData() const 65 { 66 return data_; 67 } 68 GetData()69 OptionalData &GetData() 70 { 71 return data_; 72 } 73 GetError()74 const FsError &GetError() const 75 { 76 return error_; 77 } 78 79 FsResult(const FsResult &) = delete; 80 FsResult &operator=(const FsResult &) = delete; 81 FsResult(FsResult && other)82 FsResult(FsResult &&other) noexcept : error_(std::move(other.error_)), data_(std::move(other.data_)) {} 83 84 FsResult &operator=(FsResult &&other) noexcept 85 { 86 if (this != &other) { 87 error_ = std::move(other.error_); 88 data_ = std::move(other.data_); 89 } 90 return *this; 91 } 92 93 ~FsResult<T>() = default; 94 95 private: 96 FsError error_; 97 OptionalData data_; 98 FsResult(const FsError & err,const OptionalData & data)99 FsResult(const FsError &err, const OptionalData &data) : error_(err), data_(data) {} 100 FsResult(const FsError & err,OptionalData && data)101 FsResult(const FsError &err, OptionalData &&data) : error_(err), data_(std::move(data)) {} 102 }; 103 104 template <> 105 class FsResult<void> { 106 private: 107 FsError error_; FsResult(const FsError & err)108 explicit FsResult(const FsError &err) : error_(err) {} 109 110 public: Success()111 static FsResult<void> Success() 112 { 113 return FsResult(FsError(ERRNO_NOERR)); 114 } 115 Error(const int32_t code)116 static FsResult<void> Error(const int32_t code) 117 { 118 return FsResult(FsError(code)); 119 } 120 IsSuccess()121 bool IsSuccess() const 122 { 123 return !error_; 124 } 125 GetError()126 const FsError &GetError() const 127 { 128 return error_; 129 } 130 131 FsResult(const FsResult &) = delete; 132 FsResult &operator=(const FsResult &) = delete; 133 FsResult(FsResult && other)134 FsResult(FsResult &&other) noexcept : error_(std::move(other.error_)) {} 135 136 FsResult &operator=(FsResult &&other) noexcept 137 { 138 if (this != &other) { 139 error_ = std::move(other.error_); 140 } 141 return *this; 142 } 143 144 ~FsResult<void>() = default; 145 }; 146 147 } // namespace OHOS::FileManagement::ModuleFileIO 148 #endif // UTILS_FILEMGMT_LIBFS_INCLUDE_FS_RESULT_H 149