repository.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #ifndef REPOSITORY_HPP
  2. #define REPOSITORY_HPP
  3. #include "common.hpp"
  4. #include <memory>
  5. #include <string>
  6. #include <boost/property_tree/ptree.hpp>
  7. namespace pt = boost::property_tree;
  8. // ptree helpers
  9. template<typename T = std::string, typename... Args>
  10. static T
  11. get_attribute(const pt::ptree &node, const std::string &attr, Args... args)
  12. {
  13. static auto prefix = PT_ATTR + '.';
  14. return node.get<T>(prefix + attr, args...);
  15. }
  16. inline std::string
  17. get_name(const pt::ptree &node)
  18. {
  19. return get_attribute(node, AT_NAME);
  20. }
  21. inline std::string
  22. get_name(const pt::ptree &node, std::nothrow_t)
  23. {
  24. return get_attribute(node, AT_NAME, "");
  25. }
  26. enum TYPE_TRAITS {
  27. // basic/fundamental glib defined type
  28. TYPE_BASIC = 1 << 0,
  29. // type passed by value (integral, enum, etc)
  30. TYPE_VALUE = 1 << 1,
  31. // enum, bitfield type
  32. TYPE_ENUM = 1 << 2,
  33. // class type (string, object, record)
  34. TYPE_CLASS = 1 << 3,
  35. // gobject
  36. TYPE_OBJECT = 1 << 4,
  37. // boxed
  38. TYPE_BOXED = 1 << 5,
  39. // callback
  40. TYPE_CALLBACK = 1 << 6,
  41. // containers
  42. TYPE_ARRAY = 1 << 7,
  43. TYPE_LIST = 1 << 8,
  44. TYPE_MAP = 1 << 9,
  45. TYPE_CONTAINER = TYPE_ARRAY | TYPE_LIST | TYPE_MAP,
  46. TYPE_VARARGS = 1 << 10,
  47. // predefined
  48. TYPE_PREDEFINED = 1 << 11,
  49. // typedef (no forward class declare)
  50. TYPE_TYPEDEF = 1 << 12
  51. };
  52. // some info on (argument) type
  53. struct TypeInfo
  54. {
  55. TypeInfo() = default;
  56. TypeInfo(const std::string &_gir, const std::string &_cpp,
  57. const std::string &_c, const std::string &_argtype, int _flags)
  58. : girname(_gir), cpptype(_cpp), dtype(_c), argtype(_argtype),
  59. flags(_flags), pdepth(flags & (TYPE_CLASS | TYPE_CONTAINER) ? 1 : 0)
  60. {}
  61. // always qualified (if not predefined glib type)
  62. std::string girname;
  63. // always qualified (see below) as it might be used in class context
  64. // (so to avoid lookup conflicts with parent classes in other ns,
  65. // e.g. injected-class-name)
  66. std::string cpptype;
  67. // c:type taken from class/record definition
  68. std::string dtype;
  69. // c type as used in argument (no cv qualification)
  70. std::string argtype;
  71. // combination of flags above
  72. int flags = 0;
  73. // number of pointer indirections
  74. int pdepth = 0;
  75. };
  76. class Repository
  77. {
  78. Repository() = default;
  79. friend class RepositoryPriv;
  80. Repository(const Repository &other) = delete;
  81. Repository &operator=(const Repository &other) = delete;
  82. public:
  83. typedef std::string key_type;
  84. struct mapped_type
  85. {
  86. typedef pt::ptree::value_type tree_type;
  87. std::unique_ptr<tree_type> tree;
  88. std::unique_ptr<TypeInfo> info;
  89. };
  90. static std::shared_ptr<Repository> new_();
  91. // set namespace used for unqualified girname
  92. void set_ns(const std::string _ns);
  93. // qualify girname, optionally wrt relative base
  94. std::string qualify(
  95. const std::string &girname, const std::string &base = "") const;
  96. void add(const key_type &girname, const mapped_type::tree_type &n);
  97. void discard(const key_type &girname);
  98. const mapped_type::tree_type &tree(const std::string &girname) const;
  99. const mapped_type *lookup(const std::string &girname) const;
  100. // check for duplicate definition for ctype
  101. // if ctype already claimed, returns non-empty claiming cpptype
  102. std::string check_odr(const std::string &cpptype, const std::string &ctype);
  103. };
  104. #endif // REPOSITORY_HPP