types.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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>>, "T must be derived from object");
  85. m_ptr = std::forward<T>(value);
  86. return *this;
  87. }
  88. template <typename T>
  89. attr_accessor& operator= (T&& value) && {
  90. static_assert(std::is_base_of_v<object, std::decay_t<T>>, "T must be derived from object");
  91. setattr(*this, key, std::forward<T>(value));
  92. return *this;
  93. }
  94. };
  95. inline attr_accessor handle::attr(const char* name) const {
  96. return attr_accessor(reinterpret_borrow<object>(*this), str(name));
  97. }
  98. inline attr_accessor handle::attr(const handle& name) const {
  99. return attr_accessor(reinterpret_borrow<object>(*this), reinterpret_borrow<object>(name));
  100. }
  101. inline attr_accessor handle::attr(object&& name) const {
  102. return attr_accessor(reinterpret_borrow<object>(*this), std::move(name));
  103. }
  104. class item_accessor : public object {
  105. public:
  106. object key;
  107. public:
  108. template <typename T>
  109. item_accessor(const object& obj, T&& key) : object(obj), key(std::forward<T>(key)){};
  110. template <typename T>
  111. item_accessor& operator= (T&& value) & {
  112. static_assert(std::is_base_of_v<object, std::decay_t<T>>, "T must be derived from object");
  113. m_ptr = std::forward<T>(value);
  114. }
  115. template <typename T>
  116. item_accessor& operator= (object&& value) && {
  117. static_assert(std::is_base_of_v<object, std::decay_t<T>>, "T must be derived from object");
  118. setitem(*this, key, std::forward<T>(value));
  119. }
  120. };
  121. inline item_accessor handle::operator[] (int64_t key) const {
  122. return item_accessor(reinterpret_borrow<object>(*this), int_(key));
  123. }
  124. inline item_accessor handle::operator[] (const char* key) const {
  125. return item_accessor(reinterpret_borrow<object>(*this), str(key));
  126. }
  127. inline item_accessor handle::operator[] (const handle& key) const {
  128. return item_accessor(reinterpret_borrow<object>(*this), reinterpret_borrow<object>(key));
  129. }
  130. inline item_accessor handle::operator[] (object&& key) const {
  131. return item_accessor(reinterpret_borrow<object>(*this), std::move(key));
  132. }
  133. class args : public tuple {
  134. using tuple::tuple;
  135. };
  136. class kwargs : public dict {
  137. using dict::dict;
  138. };
  139. template <typename T>
  140. handle type::handle_of() {
  141. if constexpr(std::is_same_v<T, object>) { return vm->_t(vm->tp_object); }
  142. #define PYBIND11_TYPE_MAPPER(type, tp) \
  143. else if constexpr(std::is_same_v<T, type>) { return vm->_t(vm->tp); }
  144. PYBIND11_TYPE_MAPPER(type, tp_type)
  145. PYBIND11_TYPE_MAPPER(str, tp_str)
  146. PYBIND11_TYPE_MAPPER(int_, tp_int)
  147. PYBIND11_TYPE_MAPPER(float_, tp_float)
  148. PYBIND11_TYPE_MAPPER(bool_, tp_bool)
  149. PYBIND11_TYPE_MAPPER(list, tp_list)
  150. PYBIND11_TYPE_MAPPER(tuple, tp_tuple)
  151. PYBIND11_TYPE_MAPPER(args, tp_tuple)
  152. PYBIND11_TYPE_MAPPER(dict, tp_dict)
  153. PYBIND11_TYPE_MAPPER(kwargs, tp_dict)
  154. #undef PYBIND11_TYPE_MAPPER
  155. else {
  156. auto result = vm->_cxx_typeid_map.find(typeid(T));
  157. if(result != vm->_cxx_typeid_map.end()) { return vm->_t(result->second); }
  158. vm->TypeError("Type not registered");
  159. }
  160. }
  161. } // namespace pybind11