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_FUNCTION_H
21 #define T_FUNCTION_H
22 
23 #include <string>
24 #include "thrift/parse/t_type.h"
25 #include "thrift/parse/t_struct.h"
26 #include "thrift/parse/t_doc.h"
27 
28 /**
29  * Representation of a function. Key parts are return type, function name,
30  * optional modifiers, and an argument list, which is implemented as a thrift
31  * struct.
32  *
33  */
34 class t_function : public t_doc {
35 public:
36   t_function(t_type* returntype, std::string name, t_struct* arglist, bool oneway = false)
returntype_(returntype)37     : returntype_(returntype),
38       name_(name),
39       arglist_(arglist),
40       xceptions_(new t_struct(nullptr)),
41       own_xceptions_(true),
42       oneway_(oneway) {
43     if (oneway_ && (!returntype_->is_void())) {
44       pwarning(1, "Oneway methods should return void.\n");
45     }
46   }
47 
48   t_function(t_type* returntype,
49              std::string name,
50              t_struct* arglist,
51              t_struct* xceptions,
52              bool oneway = false)
returntype_(returntype)53     : returntype_(returntype),
54       name_(name),
55       arglist_(arglist),
56       xceptions_(xceptions),
57       own_xceptions_(false),
58       oneway_(oneway) {
59     if (oneway_ && !xceptions_->get_members().empty()) {
60       throw std::string("Oneway methods can't throw exceptions.");
61     }
62     if (oneway_ && (!returntype_->is_void())) {
63       pwarning(1, "Oneway methods should return void.\n");
64     }
65   }
66 
~t_function()67   ~t_function() override {
68     if (own_xceptions_)
69       delete xceptions_;
70   }
71 
get_returntype()72   t_type* get_returntype() const { return returntype_; }
73 
get_name()74   const std::string& get_name() const { return name_; }
75 
get_arglist()76   t_struct* get_arglist() const { return arglist_; }
77 
get_xceptions()78   t_struct* get_xceptions() const { return xceptions_; }
79 
is_oneway()80   bool is_oneway() const { return oneway_; }
81 
82   std::map<std::string, std::string> annotations_;
83 
84 private:
85   t_type* returntype_;
86   std::string name_;
87   t_struct* arglist_;
88   t_struct* xceptions_;
89   bool own_xceptions_;
90   bool oneway_;
91 };
92 
93 #endif
94