object.hpp 842 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #pragma once
  2. #include "pocketpy/common/str.hpp"
  3. #include "pocketpy/common/config.h"
  4. #include "pocketpy/objects/base.hpp"
  5. #include "pocketpy/objects/object.h"
  6. namespace pkpy {
  7. struct NameDict;
  8. struct PyObject final: ::PyObject {
  9. bool is_attr_valid() const noexcept { return _attr != nullptr; }
  10. void* _value_ptr() noexcept { return PyObject__value_ptr(this); }
  11. NameDict& attr() const{
  12. assert(is_attr_valid());
  13. return *(NameDict*)_attr;
  14. }
  15. PyVar attr(StrName name) const;
  16. template <typename T>
  17. T& as() noexcept {
  18. static_assert(std::is_same_v<T, std::decay_t<T>>);
  19. return *reinterpret_cast<T*>(_value_ptr());
  20. }
  21. PyObject(Type type, bool gc_is_large){
  22. PyObject__ctor(this, type, gc_is_large);
  23. }
  24. };
  25. static_assert(sizeof(PyObject) <= 16);
  26. } // namespace pkpy