1 // Copyright 2018 The Amber 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 AMBER_VALUE_H_ 16 #define AMBER_VALUE_H_ 17 18 #include <stdint.h> 19 20 namespace amber { 21 22 /// Wrapper for a single value. The value will be either an integer or a 23 /// floating point value. 24 class Value { 25 public: 26 Value(); 27 Value(const Value&); 28 ~Value(); 29 30 Value& operator=(const Value&); 31 SetIntValue(uint64_t val)32 void SetIntValue(uint64_t val) { 33 type_ = kValueTypeInteger; 34 uint_value_ = val; 35 } IsInteger()36 bool IsInteger() const { return type_ == kValueTypeInteger; } 37 SetDoubleValue(double val)38 void SetDoubleValue(double val) { 39 type_ = kValueTypeFloat; 40 double_value_ = val; 41 } IsFloat()42 bool IsFloat() const { return type_ == kValueTypeFloat; } 43 AsUint8()44 uint8_t AsUint8() const { return static_cast<uint8_t>(uint_value_); } AsUint16()45 uint16_t AsUint16() const { return static_cast<uint16_t>(uint_value_); } AsUint32()46 uint32_t AsUint32() const { return static_cast<uint32_t>(uint_value_); } AsUint64()47 uint64_t AsUint64() const { return static_cast<uint64_t>(uint_value_); } 48 AsInt8()49 int8_t AsInt8() const { return static_cast<int8_t>(uint_value_); } AsInt16()50 int16_t AsInt16() const { return static_cast<int16_t>(uint_value_); } AsInt32()51 int32_t AsInt32() const { return static_cast<int32_t>(uint_value_); } AsInt64()52 int64_t AsInt64() const { return static_cast<int64_t>(uint_value_); } 53 AsFloat()54 float AsFloat() const { return static_cast<float>(double_value_); } AsDouble()55 double AsDouble() const { return double_value_; } 56 57 private: 58 enum Type { kValueTypeFloat, kValueTypeInteger }; 59 Type type_; 60 uint64_t uint_value_; 61 double double_value_; 62 }; 63 64 } // namespace amber 65 66 #endif // AMBER_VALUE_H_ 67