xref: /aosp_15_r20/external/grpc-grpc/third_party/upb/upb/base/status.hpp (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #ifndef UPB_BASE_STATUS_HPP_
9 #define UPB_BASE_STATUS_HPP_
10 
11 #ifdef __cplusplus
12 
13 #include "upb/base/status.h"
14 
15 namespace upb {
16 
17 class Status final {
18  public:
Status()19   Status() { upb_Status_Clear(&status_); }
20 
ptr()21   upb_Status* ptr() { return &status_; }
22 
23   // Returns true if there is no error.
ok() const24   bool ok() const { return upb_Status_IsOk(&status_); }
25 
26   // Guaranteed to be NULL-terminated.
error_message() const27   const char* error_message() const {
28     return upb_Status_ErrorMessage(&status_);
29   }
30 
31   // The error message will be truncated if it is longer than
32   // _kUpb_Status_MaxMessage-4.
SetErrorMessage(const char * msg)33   void SetErrorMessage(const char* msg) {
34     upb_Status_SetErrorMessage(&status_, msg);
35   }
SetFormattedErrorMessage(const char * fmt,...)36   void SetFormattedErrorMessage(const char* fmt, ...) {
37     va_list args;
38     va_start(args, fmt);
39     upb_Status_VSetErrorFormat(&status_, fmt, args);
40     va_end(args);
41   }
42 
43   // Resets the status to a successful state with no message.
Clear()44   void Clear() { upb_Status_Clear(&status_); }
45 
46  private:
47   upb_Status status_;
48 };
49 
50 }  // namespace upb
51 
52 #endif  // __cplusplus
53 
54 #endif  // UPB_BASE_STATUS_HPP_
55