xref: /aosp_15_r20/external/libtextclassifier/native/utils/variant.cc (revision 993b0882672172b81d12fad7a7ac0c3e5c824a12)
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() const21 std::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_UINT_VALUE:
34       return std::to_string(Value<unsigned int>());
35       break;
36     case Variant::TYPE_INT64_VALUE:
37       return std::to_string(Value<int64>());
38       break;
39     case Variant::TYPE_FLOAT_VALUE:
40       return std::to_string(Value<float>());
41       break;
42     case Variant::TYPE_DOUBLE_VALUE:
43       return std::to_string(Value<double>());
44       break;
45     case Variant::TYPE_STRING_VALUE:
46       return ConstRefValue<std::string>();
47       break;
48     default:
49       TC3_LOG(FATAL) << "Unsupported variant type: " << GetType();
50       return "";
51       break;
52   }
53 }
54 
operator <<(logging::LoggingStringStream & stream,const Variant & value)55 logging::LoggingStringStream& operator<<(logging::LoggingStringStream& stream,
56                                          const Variant& value) {
57   return stream << "Variant(" << value.GetType() << ", " << value.ToString()
58                 << ")";
59 }
60 
61 }  // namespace libtextclassifier3
62