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_INSPECTOR_SCALAR_H_ 16 #define SRC_INSPECTOR_SCALAR_H_ 17 18 #include <cstdint> 19 20 namespace tint { 21 namespace inspector { 22 23 /// Contains a literal scalar value 24 class Scalar { 25 public: 26 /// Null Constructor 27 Scalar(); 28 /// @param val literal scalar value to contain 29 explicit Scalar(bool val); 30 /// @param val literal scalar value to contain 31 explicit Scalar(uint32_t val); 32 /// @param val literal scalar value to contain 33 explicit Scalar(int32_t val); 34 /// @param val literal scalar value to contain 35 explicit Scalar(float val); 36 37 /// @returns true if this is a null 38 bool IsNull() const; 39 /// @returns true if this is a bool 40 bool IsBool() const; 41 /// @returns true if this is a unsigned integer. 42 bool IsU32() const; 43 /// @returns true if this is a signed integer. 44 bool IsI32() const; 45 /// @returns true if this is a float. 46 bool IsFloat() const; 47 48 /// @returns scalar value if bool, otherwise undefined behaviour. 49 bool AsBool() const; 50 /// @returns scalar value if unsigned integer, otherwise undefined behaviour. 51 uint32_t AsU32() const; 52 /// @returns scalar value if signed integer, otherwise undefined behaviour. 53 int32_t AsI32() const; 54 /// @returns scalar value if float, otherwise undefined behaviour. 55 float AsFloat() const; 56 57 private: 58 typedef enum { 59 kNull, 60 kBool, 61 kU32, 62 kI32, 63 kFloat, 64 } Type; 65 66 typedef union { 67 bool b; 68 uint32_t u; 69 int32_t i; 70 float f; 71 } Value; 72 73 Type type_; 74 Value value_; 75 }; 76 77 } // namespace inspector 78 } // namespace tint 79 80 #endif // SRC_INSPECTOR_SCALAR_H_ 81