forb
method.hpp
1 //
2 // Created by gabriele on 14/11/18.
3 //
4 
5 #ifndef FORBCC_METHOD_H
6 #define FORBCC_METHOD_H
7 
8 #include <entity.hpp>
9 #include <templates/ordered_unique_list.hpp>
10 
11 #include <interface.hpp>
12 #include <parameter.hpp>
13 
14 namespace forbcc {
15  // Forward declarations
16  class code_ostream;
17 
19  extern const std::string res_code_type;
20 
23  class method : public entity, public ordered_unique_list<parameter> {
24 
25  /* *********************************************** ATTRIBUTES *********************************************** */
26  private:
28  int _counters[3] = {0, 0, 0};
29 
31  std::shared_ptr<const type> _return_type;
32 
33  public:
34  /* ********************************************** CONSTRUCTORS ********************************************** */
35 
37  method() : entity(), _return_type(nullptr) {};
38 
40  method(const std::shared_ptr<interface> &parent, const std::string &name,
41  const std::shared_ptr<const type> &return_type)
42  : entity(parent, name),
43  _return_type(return_type) {};
44 
45  /**************************************************************************************************************/
46 
48  ~method() override = default;
49 
51  method(method &&) = default;
52 
54  method &operator=(method &&) = default;
55 
57  method(const method &) = default;
58 
60  method &operator=(const method &) = default;
61 
62  /**************************************************************************************************************/
63 
65  void print_declaration(code_ostream &out) const override;
66 
71  void print_definition(code_ostream &) const override {};
72 
74  void print_virtual_declaration(code_ostream &out) const;
75 
77  void print_stub_definition(code_ostream &out, const std::string &scope, const std::string &enumname) const;
78 
81  void print_skeleton_definition(code_ostream &out) const;
82 
85  bool insert(std::string key, const parameter &param) override {
86  bool success = ordered_unique_list::insert(key, param);
87 
88  if (success) {
89  ++_counters[static_cast<int>(param.dir())];
90  }
91 
92  return success;
93  };
94 
98  std::string id() const;
99 
100  private:
105  void print_prototype(forbcc::code_ostream &out, const std::string &thename) const;
106 
109  std::string get_unused_variable_name(std::string name) const;
110  };
111 }
112 
113 
114 #endif //FORBCC_METHOD_H
Defines a method of a forbcc::interface, which can have an arbitrary number of input, output or inout parameters.
Definition: method.hpp:23
void print_definition(code_ostream &) const override
THIS FUNCTION DOES NOTHING, use the print_stub_definition and print_skeleton_definition methods respe...
Definition: method.hpp:71
void print_stub_definition(code_ostream &out, const std::string &scope, const std::string &enumname) const
Prints the definition of the method of the stub.
Definition: method.cpp:49
std::string id() const
Returns the method id, which will be stored within an enum class defined for each interface...
Definition: method.cpp:297
~method() override=default
This class is virtual, so it requires a virtual destructor.
Represents the parameter of a forbcc::method, each associated with a direction between either input...
Definition: parameter.hpp:22
virtual bool insert(std::string key, const T &value)
Inserts a new value within the list if the corrisponding key is not present.
Definition: ordered_unique_list.hpp:44
direction dir() const
Returns the direction of the parameter.
Definition: parameter.hpp:60
A template that can be used to declare a list of unique ordered items.
Definition: ordered_unique_list.hpp:16
void print_virtual_declaration(code_ostream &out) const
Prints the virtual declaration of a method.
Definition: method.cpp:34
method()
Empty method, used to preallocate variables in arrays or to use later assignment operator.
Definition: method.hpp:37
std::shared_ptr< entity > parent() const
Returns the parent of the given entity.
Definition: entity.hpp:63
std::string name() const
Returns the name of the given entity.
Definition: entity.hpp:68
void print_skeleton_definition(code_ostream &out) const
Prints the definition of the skeleton code wrapping the virtual call to the method, defined by subclasses of the generated skeleton class.
Definition: method.cpp:166
This output stream supports indentation of code lines, which are formatted at each std::endl...
Definition: code_ostream.hpp:14
bool insert(std::string key, const parameter &param) override
Proxy to ordered_unique_list<forbcc::parameter>, which counts the number of parameters of each succes...
Definition: method.hpp:85
Definition: code_ostream.hpp:11
Any code entity that should be generated by the compiler: namespaces, classes, structures, methods, variables.
Definition: entity.hpp:18
method & operator=(method &&)=default
This class supports moving.
method(const std::shared_ptr< interface > &parent, const std::string &name, const std::shared_ptr< const type > &return_type)
Constructs a new method for the given parent interface with a name and a return type.
Definition: method.hpp:40
void print_declaration(code_ostream &out) const override
Prints the method declaration.
Definition: method.cpp:29