OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
t_type.h
Go to the documentation of this file.
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  * Copyright 2006-2017 The Apache Software Foundation.
20  * https://github.com/apache/thrift
21  */
22 
23 #ifndef T_TYPE_H
24 #define T_TYPE_H
25 
26 #include <string>
27 #include <map>
28 #ifdef SANDESH
29 #include <vector>
30 #endif // !SANDESH
31 #include <cstring>
32 #include <stdint.h>
33 #include "t_doc.h"
34 
35 class t_program;
36 #ifdef SANDESH
37 class t_field;
38 #endif // !SANDESH
39 
48 class t_type : public t_doc {
49  public:
50  virtual ~t_type() {}
51 
52  virtual void set_name(const std::string& name) {
53  name_ = name;
54  }
55 
56  virtual const std::string& get_name() const {
57  return name_;
58  }
59 
60  virtual bool is_void() const { return false; }
61  virtual bool is_base_type() const { return false; }
62  virtual bool is_string() const { return false; }
63 #ifdef SANDESH
64  virtual bool is_uuid() const { return false; }
65  virtual bool is_ipaddr() const { return false; }
66  virtual bool is_xml() const { return false; }
67  virtual bool is_static_const_string() const { return false; }
68  virtual bool is_sandesh() const { return false; }
69 #endif
70  virtual bool is_bool() const { return false; }
71  virtual bool is_typedef() const { return false; }
72  virtual bool is_enum() const { return false; }
73  virtual bool is_struct() const { return false; }
74  virtual bool is_xception() const { return false; }
75  virtual bool is_container() const { return false; }
76  virtual bool is_list() const { return false; }
77  virtual bool is_set() const { return false; }
78  virtual bool is_map() const { return false; }
79  virtual bool is_service() const { return false; }
80 
82  return program_;
83  }
84 
86 
87  // Return a string that uniquely identifies this type
88  // from any other thrift type in the world, as far as
89  // TDenseProtocol is concerned.
90  // We don't cache this, which is a little sloppy,
91  // but the compiler is so fast that it doesn't really matter.
92  virtual std::string get_fingerprint_material() const = 0;
93 
94  // Fingerprint should change whenever (and only when)
95  // the encoding via TDenseProtocol changes.
96  static const int fingerprint_len = 16;
97 
98  // Call this before trying get_*_fingerprint().
99  virtual void generate_fingerprint();
100 
101  bool has_fingerprint() const {
102  for (int i = 0; i < fingerprint_len; i++) {
103  if (fingerprint_[i] != 0) {
104  return true;
105  }
106  }
107  return false;
108  }
109 
110  const uint8_t* get_binary_fingerprint() const {
111  return fingerprint_;
112  }
113 
114  std::string get_ascii_fingerprint() const {
115  std::string rv;
116  const uint8_t* fp = get_binary_fingerprint();
117  for (int i = 0; i < fingerprint_len; i++) {
118  rv += byte_to_hex(fp[i]);
119  }
120  return rv;
121  }
122 
123  // This function will break (maybe badly) unless 0 <= num <= 16.
124  static char nybble_to_xdigit(int num) {
125  if (num < 10) {
126  return '0' + num;
127  } else {
128  return 'A' + num - 10;
129  }
130  }
131 
132  static std::string byte_to_hex(uint8_t byte) {
133  std::string rv;
134  rv += nybble_to_xdigit(byte >> 4);
135  rv += nybble_to_xdigit(byte & 0x0f);
136  return rv;
137  }
138 
139  const uint32_t get_4byte_fingerprint() const {
140  uint32_t rv = 0;
141  const uint8_t* fp = get_binary_fingerprint();
142 
143  for (int i = 0; i < fingerprint_len/4; i +=4) {
144  rv |= *(uint32_t*)&fp[i];
145  }
146 
147  return rv;
148  }
149 
150 #ifdef SANDESH
151  virtual bool has_key_annotation() const {
152  std::map<std::string, std::string>::const_iterator it;
153  it = annotations_.find("key");
154  if (it != annotations_.end()) {
155  return true;
156  }
157  return false;
158  }
159  virtual bool has_hidden_annotation() const {
160  std::map<std::string, std::string>::const_iterator it;
161  it = annotations_.find("hidden");
162  if (it != annotations_.end()) {
163  return true;
164  }
165  return false;
166  }
167  bool has_annotation(bool check_self, const std::vector<t_field*> &members,
168  const t_program *program) const;
169 #endif
170 
171  std::map<std::string, std::string> annotations_;
172 
173  protected:
174  t_type() :
175  program_(NULL)
176  {
177  memset(fingerprint_, 0, sizeof(fingerprint_));
178  }
179 
180  t_type(t_program* program) :
181  program_(program)
182  {
183  memset(fingerprint_, 0, sizeof(fingerprint_));
184  }
185 
186  t_type(t_program* program, std::string name) :
187  program_(program),
188  name_(name)
189  {
190  memset(fingerprint_, 0, sizeof(fingerprint_));
191  }
192 
193  t_type(std::string name) :
194  program_(NULL),
195  name_(name)
196  {
197  memset(fingerprint_, 0, sizeof(fingerprint_));
198  }
199 
201  std::string name_;
202 
204 };
205 
206 
211 struct t_annotation {
212  std::string key;
213  std::string val;
214 };
215 
216 #endif
virtual bool is_xception() const
Definition: t_type.h:74
std::string val
Definition: t_type.h:213
Definition: t_type.h:48
virtual bool is_bool() const
Definition: t_type.h:70
uint8_t fingerprint_[fingerprint_len]
Definition: t_type.h:203
static char nybble_to_xdigit(int num)
Definition: t_type.h:124
const uint32_t get_4byte_fingerprint() const
Definition: t_type.h:139
virtual bool is_map() const
Definition: t_type.h:78
bool has_fingerprint() const
Definition: t_type.h:101
virtual std::string get_fingerprint_material() const =0
t_type(t_program *program)
Definition: t_type.h:180
static const int fingerprint_len
Definition: t_type.h:96
virtual bool is_enum() const
Definition: t_type.h:72
virtual bool is_base_type() const
Definition: t_type.h:61
virtual ~t_type()
Definition: t_type.h:50
t_type(std::string name)
Definition: t_type.h:193
std::string key
Definition: t_type.h:212
virtual bool is_container() const
Definition: t_type.h:75
virtual bool is_typedef() const
Definition: t_type.h:71
Definition: t_doc.h:27
t_type * get_true_type()
Definition: parse.cc:21
virtual bool is_set() const
Definition: t_type.h:77
virtual void set_name(const std::string &name)
Definition: t_type.h:52
virtual bool is_void() const
Definition: t_type.h:60
t_type(t_program *program, std::string name)
Definition: t_type.h:186
virtual void generate_fingerprint()
Definition: parse.cc:13
virtual const std::string & get_name() const
Definition: t_type.h:56
virtual bool is_list() const
Definition: t_type.h:76
static std::string byte_to_hex(uint8_t byte)
Definition: t_type.h:132
std::string get_ascii_fingerprint() const
Definition: t_type.h:114
t_program * get_program()
Definition: t_type.h:81
virtual bool is_struct() const
Definition: t_type.h:73
t_type()
Definition: t_type.h:174
std::map< std::string, std::string > annotations_
Definition: t_type.h:171
std::string name_
Definition: t_type.h:201
const uint8_t * get_binary_fingerprint() const
Definition: t_type.h:110
virtual bool is_string() const
Definition: t_type.h:62
t_program * program_
Definition: t_type.h:200
virtual bool is_service() const
Definition: t_type.h:79