1 // Copyright 2017 The PDFium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #ifndef FXJS_CJS_RESULT_H_ 8 #define FXJS_CJS_RESULT_H_ 9 10 #include <optional> 11 12 #include "fxjs/js_resources.h" 13 #include "v8/include/v8-forward.h" 14 15 class CJS_Result { 16 public: 17 // Wrap constructors with static methods so we can apply [[nodiscard]], 18 // otherwise we can't catch places where someone mistakenly writes: 19 // 20 // if (error) 21 // CJS_Result(JS_ERROR_CODE); 22 // 23 // instead of 24 // 25 // if (error) 26 // return CJS_Result(JS_ERROR_CODE); 27 // Success()28 [[nodiscard]] static CJS_Result Success() { return CJS_Result(); } Success(v8::Local<v8::Value> value)29 [[nodiscard]] static CJS_Result Success(v8::Local<v8::Value> value) { 30 return CJS_Result(value); 31 } Failure(const WideString & str)32 [[nodiscard]] static CJS_Result Failure(const WideString& str) { 33 return CJS_Result(str); 34 } Failure(JSMessage id)35 [[nodiscard]] static CJS_Result Failure(JSMessage id) { 36 return CJS_Result(id); 37 } 38 39 CJS_Result(const CJS_Result&); 40 ~CJS_Result(); 41 HasError()42 bool HasError() const { return error_.has_value(); } Error()43 const WideString& Error() const { return error_.value(); } 44 HasReturn()45 bool HasReturn() const { return !return_.IsEmpty(); } Return()46 v8::Local<v8::Value> Return() const { return return_; } 47 48 private: 49 CJS_Result(); // Successful but empty return. 50 explicit CJS_Result(v8::Local<v8::Value>); // Successful return with value. 51 explicit CJS_Result(const WideString&); // Error with custom message. 52 explicit CJS_Result(JSMessage id); // Error with stock message. 53 54 std::optional<WideString> error_; 55 v8::Local<v8::Value> return_; 56 }; 57 58 #endif // FXJS_CJS_RESULT_H_ 59