• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Tint Authors
2 //
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 #ifndef SRC_READER_SPIRV_FAIL_STREAM_H_
16 #define SRC_READER_SPIRV_FAIL_STREAM_H_
17 
18 #include <ostream>
19 
20 namespace tint {
21 namespace reader {
22 namespace spirv {
23 
24 /// A FailStream object accumulates values onto a given std::ostream,
25 /// and can be used to record failure by writing the false value
26 /// to given a pointer-to-bool.
27 class FailStream {
28  public:
29   /// Creates a new fail stream
30   /// @param status_ptr where we will write false to indicate failure. Assumed
31   /// to be a valid pointer to bool.
32   /// @param out output stream where a message should be written to explain
33   /// the failure
FailStream(bool * status_ptr,std::ostream * out)34   FailStream(bool* status_ptr, std::ostream* out)
35       : status_ptr_(status_ptr), out_(out) {}
36   /// Copy constructor
37   /// @param other the fail stream to clone
38   FailStream(const FailStream& other) = default;
39 
40   /// Converts to a boolean status. A true result indicates success,
41   /// and a false result indicates failure.
42   /// @returns the status
43   operator bool() const { return *status_ptr_; }
44   /// Returns the current status value.  This can be more readable
45   /// the conversion operator.
46   /// @returns the status
status()47   bool status() const { return *status_ptr_; }
48 
49   /// Records failure.
50   /// @returns a FailStream
Fail()51   FailStream& Fail() {
52     *status_ptr_ = false;
53     return *this;
54   }
55 
56   /// Appends the given value to the message output stream.
57   /// @param val the value to write to the output stream.
58   /// @returns this object
59   template <typename T>
60   FailStream& operator<<(const T& val) {
61     *out_ << val;
62     return *this;
63   }
64 
65  private:
66   bool* status_ptr_;
67   std::ostream* out_;
68 };
69 
70 }  // namespace spirv
71 }  // namespace reader
72 }  // namespace tint
73 
74 #endif  // SRC_READER_SPIRV_FAIL_STREAM_H_
75