1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #ifndef T_BASE_TYPE_H
21 #define T_BASE_TYPE_H
22 
23 #include <cstdlib>
24 #include "thrift/parse/t_type.h"
25 
26 /**
27  * A thrift base type, which must be one of the defined enumerated types inside
28  * this definition.
29  *
30  */
31 class t_base_type : public t_type {
32 public:
33   /**
34    * Enumeration of thrift base types
35    */
36   enum t_base {
37     TYPE_VOID,
38     TYPE_STRING,
39     TYPE_BOOL,
40     TYPE_I8,
41     TYPE_I16,
42     TYPE_I32,
43     TYPE_I64,
44     TYPE_DOUBLE
45   };
46 
t_base_type(std::string name,t_base base)47   t_base_type(std::string name, t_base base)
48     : t_type(name), base_(base), string_list_(false), binary_(false), string_enum_(false) {}
49 
get_base()50   t_base get_base() const { return base_; }
51 
is_void()52   bool is_void() const override { return base_ == TYPE_VOID; }
53 
is_string()54   bool is_string() const override { return base_ == TYPE_STRING; }
55 
is_bool()56   bool is_bool() const override { return base_ == TYPE_BOOL; }
57 
set_string_list(bool val)58   void set_string_list(bool val) { string_list_ = val; }
59 
is_string_list()60   bool is_string_list() const { return string_list_ && (base_ == TYPE_STRING); }
61 
set_binary(bool val)62   void set_binary(bool val) { binary_ = val; }
63 
is_binary()64   bool is_binary() const override { return binary_ && (base_ == TYPE_STRING); }
65 
set_string_enum(bool val)66   void set_string_enum(bool val) { string_enum_ = val; }
67 
is_string_enum()68   bool is_string_enum() const { return string_enum_ && base_ == TYPE_STRING; }
69 
add_string_enum_val(std::string val)70   void add_string_enum_val(std::string val) { string_enum_vals_.push_back(val); }
71 
get_string_enum_vals()72   const std::vector<std::string>& get_string_enum_vals() const { return string_enum_vals_; }
73 
is_base_type()74   bool is_base_type() const override { return true; }
75 
t_base_name(t_base tbase)76   static std::string t_base_name(t_base tbase) {
77     switch (tbase) {
78     case TYPE_VOID:
79       return "void";
80       break;
81     case TYPE_STRING:
82       return "string";
83       break;
84     case TYPE_BOOL:
85       return "bool";
86       break;
87     case TYPE_I8:
88       return "i8";
89       break;
90     case TYPE_I16:
91       return "i16";
92       break;
93     case TYPE_I32:
94       return "i32";
95       break;
96     case TYPE_I64:
97       return "i64";
98       break;
99     case TYPE_DOUBLE:
100       return "double";
101       break;
102     default:
103       return "(unknown)";
104       break;
105     }
106   }
107 
108 private:
109   t_base base_;
110 
111   bool string_list_;
112   bool binary_;
113   bool string_enum_;
114   std::vector<std::string> string_enum_vals_;
115 };
116 
117 #endif
118