Home | History | Annotate | Download | only in utils
      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 #ifndef LIBTEXTCLASSIFIER_UTILS_VARIANT_H_
     18 #define LIBTEXTCLASSIFIER_UTILS_VARIANT_H_
     19 
     20 #include <map>
     21 #include <string>
     22 
     23 #include "utils/base/integral_types.h"
     24 #include "utils/base/logging.h"
     25 #include "utils/strings/stringpiece.h"
     26 
     27 namespace libtextclassifier3 {
     28 
     29 // Represents a type-tagged union of different basic types.
     30 class Variant {
     31  public:
     32   enum Type {
     33     TYPE_EMPTY = 0,
     34     TYPE_INT_VALUE = 1,
     35     TYPE_INT64_VALUE = 2,
     36     TYPE_FLOAT_VALUE = 3,
     37     TYPE_DOUBLE_VALUE = 4,
     38     TYPE_BOOL_VALUE = 5,
     39     TYPE_STRING_VALUE = 6,
     40   };
     41 
     42   Variant() : type_(TYPE_EMPTY) {}
     43   explicit Variant(const int value)
     44       : type_(TYPE_INT_VALUE), int_value_(value) {}
     45   explicit Variant(const int64 value)
     46       : type_(TYPE_INT64_VALUE), long_value_(value) {}
     47   explicit Variant(const float value)
     48       : type_(TYPE_FLOAT_VALUE), float_value_(value) {}
     49   explicit Variant(const double value)
     50       : type_(TYPE_DOUBLE_VALUE), double_value_(value) {}
     51   explicit Variant(const StringPiece value)
     52       : type_(TYPE_STRING_VALUE), string_value_(value.ToString()) {}
     53   explicit Variant(const std::string value)
     54       : type_(TYPE_STRING_VALUE), string_value_(value) {}
     55   explicit Variant(const char* value)
     56       : type_(TYPE_STRING_VALUE), string_value_(value) {}
     57   explicit Variant(const bool value)
     58       : type_(TYPE_BOOL_VALUE), bool_value_(value) {}
     59 
     60   Variant& operator=(const Variant&) = default;
     61 
     62   int IntValue() const {
     63     TC3_CHECK(HasInt());
     64     return int_value_;
     65   }
     66 
     67   int64 Int64Value() const {
     68     TC3_CHECK(HasInt64());
     69     return long_value_;
     70   }
     71 
     72   float FloatValue() const {
     73     TC3_CHECK(HasFloat());
     74     return float_value_;
     75   }
     76 
     77   double DoubleValue() const {
     78     TC3_CHECK(HasDouble());
     79     return double_value_;
     80   }
     81 
     82   bool BoolValue() const {
     83     TC3_CHECK(HasBool());
     84     return bool_value_;
     85   }
     86 
     87   const std::string& StringValue() const {
     88     TC3_CHECK(HasString());
     89     return string_value_;
     90   }
     91 
     92   bool HasInt() const { return type_ == TYPE_INT_VALUE; }
     93 
     94   bool HasInt64() const { return type_ == TYPE_INT64_VALUE; }
     95 
     96   bool HasFloat() const { return type_ == TYPE_FLOAT_VALUE; }
     97 
     98   bool HasDouble() const { return type_ == TYPE_DOUBLE_VALUE; }
     99 
    100   bool HasBool() const { return type_ == TYPE_BOOL_VALUE; }
    101 
    102   bool HasString() const { return type_ == TYPE_STRING_VALUE; }
    103 
    104   Type GetType() const { return type_; }
    105 
    106   bool HasValue() const { return type_ != TYPE_EMPTY; }
    107 
    108  private:
    109   Type type_;
    110   union {
    111     int int_value_;
    112     int64 long_value_;
    113     float float_value_;
    114     double double_value_;
    115     bool bool_value_;
    116   };
    117   std::string string_value_;
    118 };
    119 
    120 }  // namespace libtextclassifier3
    121 
    122 #endif  // LIBTEXTCLASSIFIER_UTILS_VARIANT_H_
    123