types.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #pragma once
  2. #include "object.h"
  3. namespace pybind11 {
  4. class type : public object {
  5. public:
  6. using object::object;
  7. template <typename T>
  8. static handle handle_of();
  9. };
  10. class iterable : public object {
  11. public:
  12. using object::object;
  13. iterable() = delete;
  14. };
  15. class iterator : public object {
  16. public:
  17. using object::object;
  18. iterator() = delete;
  19. };
  20. class list : public object {
  21. public:
  22. using object::object;
  23. list() : object(vm->new_object<pkpy::List>(pkpy::VM::tp_list), true) {}
  24. };
  25. class tuple : public object {
  26. public:
  27. using object::object;
  28. tuple(int n) : object(vm->new_object<pkpy::Tuple>(pkpy::VM::tp_tuple, n), true) {}
  29. //& operator[](int i){ return _args[i]; }
  30. // PyVar operator[](int i) const { return _args[i]; }
  31. };
  32. class set : public object {
  33. public:
  34. using object::object;
  35. // set() : object(vm->new_object<pkpy::Se>(pkpy::VM::tp_set), true) {}
  36. };
  37. class dict : public object {
  38. public:
  39. using object::object;
  40. dict() : object(vm->new_object<pkpy::Dict>(pkpy::VM::tp_dict), true) {}
  41. };
  42. class str : public object {
  43. public:
  44. using object::object;
  45. str(const char* c, int len) :
  46. object(vm->new_object<pkpy::Str>(pkpy::VM::tp_str, c, len), true) {
  47. };
  48. str(const char* c = "") : str(c, strlen(c)) {}
  49. str(const std::string& s) : str(s.data(), s.size()) {}
  50. str(std::string_view sv) : str(sv.data(), sv.size()) {}
  51. explicit str(const bytes& b);
  52. explicit str(handle h);
  53. operator std::string () const;
  54. template <typename... Args>
  55. str format(Args&&... args) const;
  56. };
  57. class int_ : public object {
  58. public:
  59. using object::object;
  60. int_(int64_t value) : object(pkpy::py_var(vm, value), true) {}
  61. };
  62. class float_ : public object {
  63. public:
  64. using object::object;
  65. float_(double value) : object(pkpy::py_var(vm, value), true) {}
  66. };
  67. class bool_ : public object {
  68. public:
  69. using object::object;
  70. bool_(bool value) : object(pkpy::py_var(vm, value), true) {}
  71. };
  72. class function : public object {
  73. public:
  74. using object::object;
  75. };
  76. class attr_accessor : public object {
  77. private:
  78. object key;
  79. public:
  80. template <typename T>
  81. attr_accessor(const object& obj, T&& key) : object(obj), key(std::forward<T>(key)){};
  82. template <typename T>
  83. attr_accessor& operator= (T&& value) & {
  84. static_assert(std::is_base_of_v<object, std::decay_t<T>>,
  85. "T must be derived from object");
  86. m_ptr = std::forward<T>(value);
  87. return *this;
  88. }
  89. template <typename T>
  90. attr_accessor& operator= (T&& value) && {
  91. static_assert(std::is_base_of_v<object, std::decay_t<T>>,
  92. "T must be derived from object");
  93. setattr(*this, key, std::forward<T>(value));
  94. return *this;
  95. }
  96. };
  97. inline attr_accessor handle::attr(const char* name) const {
  98. return attr_accessor(reinterpret_borrow<object>(*this), str(name));
  99. }
  100. inline attr_accessor handle::attr(const handle& name) const {
  101. return attr_accessor(reinterpret_borrow<object>(*this), reinterpret_borrow<object>(name));
  102. }
  103. inline attr_accessor handle::attr(object&& name) const {
  104. return attr_accessor(reinterpret_borrow<object>(*this), std::move(name));
  105. }
  106. class item_accessor : public object {
  107. public:
  108. object key;
  109. public:
  110. template <typename T>
  111. item_accessor(const object& obj, T&& key) : object(obj), key(std::forward<T>(key)){};
  112. template <typename T>
  113. item_accessor& operator= (T&& value) & {
  114. static_assert(std::is_base_of_v<object, std::decay_t<T>>,
  115. "T must be derived from object");
  116. m_ptr = std::forward<T>(value);
  117. }
  118. template <typename T>
  119. item_accessor& operator= (object&& value) && {
  120. static_assert(std::is_base_of_v<object, std::decay_t<T>>,
  121. "T must be derived from object");
  122. setitem(*this, key, std::forward<T>(value));
  123. }
  124. };
  125. inline item_accessor handle::operator[] (int64_t key) const {
  126. return item_accessor(reinterpret_borrow<object>(*this), int_(key));
  127. }
  128. inline item_accessor handle::operator[] (const char* key) const {
  129. return item_accessor(reinterpret_borrow<object>(*this), str(key));
  130. }
  131. inline item_accessor handle::operator[] (const handle& key) const {
  132. return item_accessor(reinterpret_borrow<object>(*this), reinterpret_borrow<object>(key));
  133. }
  134. inline item_accessor handle::operator[] (object&& key) const {
  135. return item_accessor(reinterpret_borrow<object>(*this), std::move(key));
  136. }
  137. class args : public tuple {
  138. using tuple::tuple;
  139. };
  140. class kwargs : public dict {
  141. using dict::dict;
  142. };
  143. template <typename T>
  144. handle type::handle_of() {
  145. if constexpr(std::is_same_v<T, object>) {
  146. return vm->_t(vm->tp_object);
  147. }
  148. #define PYBIND11_TYPE_MAPPER(type, tp) \
  149. else if constexpr(std::is_same_v<T, type>) { \
  150. return vm->_t(vm->tp); \
  151. }
  152. PYBIND11_TYPE_MAPPER(type, tp_type)
  153. PYBIND11_TYPE_MAPPER(str, tp_str)
  154. PYBIND11_TYPE_MAPPER(int_, tp_int)
  155. PYBIND11_TYPE_MAPPER(float_, tp_float)
  156. PYBIND11_TYPE_MAPPER(bool_, tp_bool)
  157. PYBIND11_TYPE_MAPPER(list, tp_list)
  158. PYBIND11_TYPE_MAPPER(tuple, tp_tuple)
  159. PYBIND11_TYPE_MAPPER(args, tp_tuple)
  160. PYBIND11_TYPE_MAPPER(dict, tp_dict)
  161. PYBIND11_TYPE_MAPPER(kwargs, tp_dict)
  162. #undef PYBIND11_TYPE_MAPPER
  163. else {
  164. auto result = vm->_cxx_typeid_map.find(typeid(T));
  165. if(result != vm->_cxx_typeid_map.end()) {
  166. return vm->_t(result->second);
  167. }
  168. vm->TypeError("Type not registered");
  169. }
  170. }
  171. } // namespace pybind11