1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "utils/variant.h" 18 19 namespace libtextclassifier3 { 20 ToString() const21std::string Variant::ToString() const { 22 switch (GetType()) { 23 case Variant::TYPE_BOOL_VALUE: 24 if (Value<bool>()) { 25 return "true"; 26 } else { 27 return "false"; 28 } 29 break; 30 case Variant::TYPE_INT_VALUE: 31 return std::to_string(Value<int>()); 32 break; 33 case Variant::TYPE_INT64_VALUE: 34 return std::to_string(Value<int64>()); 35 break; 36 case Variant::TYPE_FLOAT_VALUE: 37 return std::to_string(Value<float>()); 38 break; 39 case Variant::TYPE_DOUBLE_VALUE: 40 return std::to_string(Value<double>()); 41 break; 42 case Variant::TYPE_STRING_VALUE: 43 return ConstRefValue<std::string>(); 44 break; 45 default: 46 TC3_LOG(FATAL) << "Unsupported variant type: " << GetType(); 47 return ""; 48 break; 49 } 50 } 51 operator <<(logging::LoggingStringStream & stream,const Variant & value)52logging::LoggingStringStream& operator<<(logging::LoggingStringStream& stream, 53 const Variant& value) { 54 return stream << "Variant(" << value.GetType() << ", " << value.ToString() 55 << ")"; 56 } 57 58 } // namespace libtextclassifier3 59