function.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifndef FUNCTION_HPP
  2. #define FUNCTION_HPP
  3. #include "genbase.hpp"
  4. #include <iosfwd>
  5. #include <map>
  6. #include <set>
  7. #include <string>
  8. #include <vector>
  9. struct ElementFunction
  10. {
  11. // type of function; method, etc
  12. std::string kind;
  13. // GIR name
  14. std::string name;
  15. // otherwise descriptive name (e.g. original C name)
  16. std::string c_id;
  17. // expression defining function
  18. std::string functionexp;
  19. bool throws{};
  20. // represents symbol in lib
  21. // (which can be linked to or dlsym'ed)
  22. bool lib_symbol{};
  23. std::string shadows;
  24. };
  25. // parameter data
  26. constexpr static const int INDEX_DEFAULT = -10;
  27. struct FunctionParameter
  28. {
  29. std::string name;
  30. GeneratorBase::ArgInfo tinfo{};
  31. // deduced C type
  32. std::string ptype;
  33. bool instance{};
  34. std::string direction;
  35. std::string transfer{TRANSFER_NOTHING};
  36. // index
  37. int closure{INDEX_DEFAULT}, destroy{INDEX_DEFAULT};
  38. int callerallocates{};
  39. std::string scope;
  40. bool optional{};
  41. bool nullable{};
  42. };
  43. using Parameter = FunctionParameter;
  44. // info defining function to construct declaration/definition
  45. // (some parts not relevant for signal/callback)
  46. struct FunctionDefinition
  47. {
  48. // return (and optionally additional outputs)
  49. struct Output
  50. {
  51. // cpp type of output
  52. std::string type;
  53. // expression (value of output)
  54. std::string value;
  55. };
  56. struct ArgTrait
  57. {
  58. // transfer (original attribute)
  59. std::string transfer;
  60. // inout parameter
  61. bool inout{};
  62. // applicable/relevant arguments (indexed as usual; see below)
  63. // usually only 1 (to 1)
  64. // for callback; function, userdata[, destroy]
  65. // for sized arrays; data, size
  66. // (other containers only need 1 and handled as usual)
  67. std::vector<int> args;
  68. // custom type trait
  69. std::string custom{};
  70. };
  71. // GIR name (empty if not valid)
  72. std::string name;
  73. // statements preceding wrapped call (excluding final ;)
  74. std::vector<std::string> pre_call;
  75. // idem, post call
  76. std::vector<std::string> post_call;
  77. // assembled outputs (first one is return value, if any, could be empty)
  78. std::vector<Output> cpp_outputs;
  79. // parts that make up the C call (...)
  80. // indexed by param number (instance = -1)
  81. std::map<int, std::string> c_call;
  82. // parts that make up the ( ... ) decl/def
  83. // (similarly indexed/sorted)
  84. std::map<int, std::string> cpp_decl;
  85. // callee; trait info of (callback) parameters
  86. // (lowest one is for return; always present)
  87. // (except if fallback virtual method)
  88. std::map<int, ArgTrait> arg_traits;
  89. // extra parameters in a callee (= cb) declaration not present in callforward
  90. // (to deal with cb sized array output0
  91. std::map<int, std::string> cpp_decl_extra;
  92. // function (expression) to call
  93. std::string c_callee;
  94. // format with single placeholder for call (constructed based on all above)
  95. std::string ret_format;
  96. // parts used for callforward generation of callback type
  97. // (callforward = C++ signature which then calls a C function)
  98. // (callback = C signature which then calls a C++ function)
  99. // typedef of C function to call
  100. std::string cf_ctype;
  101. // (derived) C signature (without instance parameter if virtual method)
  102. std::string c_sig;
  103. };
  104. std::string make_arg_traits(
  105. const std::map<int, FunctionDefinition::ArgTrait> &traits,
  106. const std::string &c_sig);
  107. FunctionDefinition process_element_function(GeneratorContext &_ctx,
  108. const std::string _ns, const pt::ptree::value_type &entry,
  109. std::ostream &out, std::ostream &impl, const std::string &klass,
  110. const std::string &klasstype, std::set<std::string> &deps,
  111. bool allow_deprecated);
  112. FunctionDefinition process_element_function(GeneratorContext &_ctx,
  113. const std::string _ns, const ElementFunction &func,
  114. const std::vector<Parameter> &params, std::ostream &out, std::ostream &impl,
  115. const std::string &klass, const std::string &klasstype,
  116. std::set<std::string> &deps);
  117. #endif // FUNCTION_HPP