object.qbk 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. [section boost/python/object.hpp]
  2. [section Introduction]
  3. Exposes the generic Python object wrapper class object, and related classes. In order to avoid some potenential problems with argument-dependent lookup and the generalized operators defined on object, all these facilities are defined in namespace boost::python::api, and object is imported into namespace boost::python with a using-declaration.
  4. [endsect]
  5. [section Class `slice_nil`]
  6. ``
  7. class slice_nil;
  8. static const _ = slice_nil();
  9. ``
  10. A type that can be used to get the effect of leaving out an index in a Python slice expression:
  11. ``
  12. >>> x[:-1]
  13. >>> x[::-1]
  14. ``
  15. C++ equivalent:
  16. ``
  17. x.slice(_,-1)
  18. x[slice(_,_,-1)]
  19. ``
  20. [endsect]
  21. [section Class `const_attribute_policies`]
  22. The policies which are used for proxies representing an attribute access to a const object.
  23. ``
  24. namespace boost { namespace python { namespace api
  25. {
  26. struct const_attribute_policies
  27. {
  28. typedef char const* key_type;
  29. static object get(object const& target, char const* key);
  30. };
  31. }}}
  32. ``
  33. [endsect]
  34. [section Class `const_attribute_policies` static functions]
  35. ``
  36. static object get(object const& target, char const* key);
  37. ``
  38. [variablelist
  39. [[Requires][key is an [link ntbs].]]
  40. [[Effects][accesses the attribute of target named by key.]]
  41. [[Returns][An object managing the result of the attribute access.]]
  42. [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] if a Python exception is raised.]]
  43. ]
  44. [endsect]
  45. [section Class `attribute_policies`]
  46. The policies which are used for proxies representing an attribute access to a mutable object.
  47. ``
  48. namespace boost { namespace python { namespace api
  49. {
  50. struct attribute_policies : const_attribute_policies
  51. {
  52. static object const& set(object const& target, char const* key, object const& value);
  53. static void del(object const&target, char const* key);
  54. };
  55. }}}
  56. ``
  57. [endsect]
  58. [section Class `attribute_policies` static functions]
  59. ``
  60. static object const& set(object const& target, char const* key, object const& value);
  61. ``
  62. [variablelist
  63. [[Requires][key is an [link ntbs].]]
  64. [[Effects][sets the attribute of target named by key to value.]]
  65. [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] if a Python exception is raised.]]
  66. ]
  67. ``
  68. static void del(object const&target, char const* key);
  69. ``
  70. [variablelist
  71. [[Requires][key is an [link ntbs].]]
  72. [[Effects][deletes the attribute of target named by key.]]
  73. [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] if a Python exception is raised.]]
  74. ]
  75. [endsect]
  76. [section Class `const_objattribute_policies`]
  77. The policies which are used for proxies representing an attribute access to a const object when the attribute name is given as a const object.
  78. ``
  79. namespace boost { namespace python { namespace api
  80. {
  81. struct const_objattribute_policies
  82. {
  83. typedef object const& key_type;
  84. static object get(object const& target, object const& key);
  85. };
  86. }}}
  87. ``
  88. [endsect]
  89. [section Class `const_objattribute_policies` static functions]
  90. ``
  91. static object get(object const& target, object const& key);
  92. ``
  93. [variablelist
  94. [[Requires][key is an object holding a string.]]
  95. [[Effects][accesses the attribute of target named by key.]]
  96. [[Returns][An object managing the result of the attribute access.]]
  97. [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] if a Python exception is raised.]]
  98. ]
  99. [endsect]
  100. [section Class `objattribute_policies`]
  101. The policies which are used for proxies representing an attribute access to a mutable object when the attribute name is given as a const object.
  102. ``
  103. namespace boost { namespace python { namespace api
  104. {
  105. struct objattribute_policies : const_objattribute_policies
  106. {
  107. static object const& set(object const& target, object const& key, object const& value);
  108. static void del(object const&target, object const& key);
  109. };
  110. }}}
  111. ``
  112. [endsect]
  113. [section Class `objattribute_policies` static functions]
  114. ``
  115. static object const& set(object const& target, object const& key, object const& value);
  116. ``
  117. [variablelist
  118. [[Requires][key is an object holding a string.]]
  119. [[Effects][sets the attribute of target named by key to value.]]
  120. [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] if a Python exception is raised.]]
  121. ]
  122. ``
  123. static void del(object const&target, object const& key);
  124. ``
  125. [variablelist
  126. [[Requires][key is an object holding a string.]]
  127. [[Effects][deletes the attribute of target named by key.]]
  128. [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] if a Python exception is raised.]]
  129. ]
  130. [endsect]
  131. [section Class `const_item_policies`]
  132. The policies which are used for proxies representing an item access (via the Python bracket operators []) to a const object.
  133. ``
  134. namespace boost { namespace python { namespace api
  135. {
  136. struct const_item_policies
  137. {
  138. typedef object key_type;
  139. static object get(object const& target, object const& key);
  140. };
  141. }}}
  142. ``
  143. [endsect]
  144. [section Class `const_item_policies` static functions]
  145. ``
  146. static object get(object const& target, object const& key);
  147. ``
  148. [variablelist
  149. [[Effects][accesses the item of target specified by key.]]
  150. [[Returns][An object managing the result of the item access.]]
  151. [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] if a Python exception is raised.]]
  152. ]
  153. [endsect]
  154. [section Class `item_policies`]
  155. The policies which are used for proxies representing an item access (via the Python bracket operators []) to a mutable object.
  156. ``
  157. namespace boost { namespace python { namespace api
  158. {
  159. struct item_policies : const_item_policies
  160. {
  161. static object const& set(object const& target, object const& key, object const& value);
  162. static void del(object const& target, object const& key);
  163. };
  164. }}}
  165. ``
  166. [endsect]
  167. [section Class `item_policies` static functions]
  168. ``
  169. static object const& set(object const& target, object const& key, object const& value);
  170. ``
  171. [variablelist
  172. [[Effects][sets the item of target specified by key to value.]]
  173. [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] if a Python exception is raised.]]
  174. ]
  175. ``
  176. static void del(object const& target, object const& key);
  177. ``
  178. [variablelist
  179. [[Effects][deletes the item of target specified by key.]]
  180. [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] if a Python exception is raised.]]
  181. ]
  182. [endsect]
  183. [section Class `const_slice_policies`]
  184. The policies which are used for proxies representing an slice access (via the Python slice notation [x:y]) to a const object.
  185. ``
  186. namespace boost { namespace python { namespace api
  187. {
  188. struct const_slice_policies
  189. {
  190. typedef std::pair<handle<>, handle<> > key_type;
  191. static object get(object const& target, key_type const& key);
  192. };
  193. }}}
  194. ``
  195. [endsect]
  196. [section Class `const_slice_policies` static functions]
  197. ``
  198. static object get(object const& target, key_type const& key);
  199. ``
  200. [variablelist
  201. [[Effects][accesses the slice of target specified by key.]]
  202. [[Returns][An object managing the result of the slice access.]]
  203. [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] if a Python exception is raised.]]
  204. ]
  205. [endsect]
  206. [section Class `slice_policies`]
  207. The policies which are used for proxies representing an slice access to a mutable object.
  208. ``
  209. namespace boost { namespace python { namespace api
  210. {
  211. struct slice_policies : const_slice_policies
  212. {
  213. static object const& set(object const& target, key_type const& key, object const& value);
  214. static void del(object const& target, key_type const& key);
  215. };
  216. }}}
  217. ``
  218. [endsect]
  219. [section Class `slice_policies` static functions]
  220. ``
  221. static object const& set(object const& target, key_type const& key, object const& value);
  222. ``
  223. [variablelist
  224. [[Effects][sets the slice of target specified by key to value.]]
  225. [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] if a Python exception is raised.]]
  226. ]
  227. ``
  228. static void del(object const& target, key_type const& key);
  229. ``
  230. [variablelist
  231. [[Effects][deletes the slice of target specified by key.]]
  232. [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] if a Python exception is raised.]]
  233. ]
  234. [endsect]
  235. [section Class template `object_operators`]
  236. This is the base class of object and its proxy template used to supply common interface: member functions, and operators which must be defined within the class body. Its template parameter U is expected to be a class derived from object_operators<U>. In practice users should never use this class directly, but it is documented here because it supplies important interface to object and its proxies.
  237. ``
  238. namespace boost { namespace python { namespace api
  239. {
  240. template <class U>
  241. class object_operators
  242. {
  243. public:
  244. // function call
  245. //
  246. object operator()() const;
  247. template <class A0>
  248. object operator()(A0 const&) const;
  249. template <class A0, class A1>
  250. object operator()(A0 const&, A1 const&) const;
  251. ...
  252. template <class A0, class A1,...class An>
  253. object operator()(A0 const&, A1 const&,...An const&) const;
  254. detail::args_proxy operator* () const;
  255. object operator()(detail::args_proxy const &args) const;
  256. object operator()(detail::args_proxy const &args,
  257. detail::kwds_proxy const &kwds) const;
  258. // truth value testing
  259. //
  260. typedef unspecified bool_type;
  261. operator bool_type() const;
  262. // Attribute access
  263. //
  264. proxy<const_object_attribute> attr(char const*) const;
  265. proxy<object_attribute> attr(char const*);
  266. proxy<const_object_objattribute> attr(object const&) const;
  267. proxy<object_objattribute> attr(object const&);
  268. // item access
  269. //
  270. template <class T>
  271. proxy<const_object_item> operator[](T const& key) const;
  272. template <class T>
  273. proxy<object_item> operator[](T const& key);
  274. // slicing
  275. //
  276. template <class T, class V>
  277. proxy<const_object_slice> slice(T const& start, V const& end) const
  278. template <class T, class V>
  279. proxy<object_slice> slice(T const& start, V const& end);
  280. };
  281. }}}
  282. ``
  283. [endsect]
  284. [section Class template `object_operators` observer functions]
  285. ``
  286. object operator()() const;
  287. template <class A0>
  288. object operator()(A0 const&) const;
  289. template <class A0, class A1>
  290. object operator()(A0 const&, A1 const&) const;
  291. ...
  292. template <class A0, class A1,...class An>
  293. object operator()(A0 const& a1, A1 const& a2,...An const& aN) const;
  294. ``
  295. [variablelist
  296. [[Effects][`call<object>(object(*static_cast<U*>(this)).ptr(), a1, a2,...aN)`]]
  297. ]
  298. ``object operator()(detail::args_proxy const &args) const; ``
  299. [variablelist
  300. [[Effects][`call object with arguments given by the tuple args`]]
  301. ]
  302. ``object operator()(detail::args_proxy const &args,
  303. detail::kwds_proxy const &kwds) const;
  304. ``
  305. [variablelist
  306. [[Effects][`call object with arguments given by the tuple args, and named arguments given by the dictionary kwds`]]
  307. ]
  308. ``operator bool_type() const;``
  309. [variablelist
  310. [[Effects][Tests truth value of `*this`.]]
  311. [[Returns][`call<object>(object(*static_cast<U*>(this)).ptr(), a1, a2,...aN)`]]
  312. ]
  313. ``
  314. proxy<const_object_attribute> attr(char const* name) const;
  315. proxy<object_attribute> attr(char const* name);
  316. ``
  317. [variablelist
  318. [[Requires][name is an [link ntbs].]]
  319. [[Effects][accesses the named attribute of *this.]]
  320. [[Returns][a proxy object which binds `object(*static_cast<U*>(this))` as its target, and name as its key.]]
  321. ]
  322. ``
  323. proxy<const_object_objattribute> attr(const object& name) const;
  324. proxy<object_objattribute> attr(const object& name);
  325. ``
  326. [variablelist
  327. [[Requires][name is a object holding a string.]]
  328. [[Effects][accesses the named attribute of `*this`.]]
  329. [[Returns][a proxy object which binds `object(*static_cast<U*>(this))` as its target, and name as its key.]]
  330. ]
  331. ``
  332. template <class T>
  333. proxy<const_object_item> operator[](T const& key) const;
  334. template <class T>
  335. proxy<object_item> operator[](T const& key);
  336. ``
  337. [variablelist
  338. [[Effects][accesses the item of `*this` indicated by key.]]
  339. [[Returns][a proxy object which binds `object(*static_cast<U*>(this))` as its target, and object(key) as its key.]]
  340. ]
  341. ``
  342. template <class T, class V>
  343. proxy<const_object_slice> slice(T const& start; start, V const& finish) const
  344. template <class T, class V>
  345. proxy<object_slice> slice(T const& start; start, V const& finish);
  346. ``
  347. [variablelist
  348. [[Effects][accesses the slice of `*this` indicated by `std::make_pair(object(start), object(finish))`.]]
  349. [[Returns][a proxy object which binds `object(*static_cast<U*>(this))` as its target, and `std::make_pair(object(start), object(finish))` as its key.]]
  350. ]
  351. [endsect]
  352. [section Class `object`]
  353. The intention is that object acts as much like a Python variable as possible. Thus expressions you'd expect to work in Python should generally work in the same way from C++. Most of object's interface is provided by its base class `object_operators<object>`, and the free functions defined in this header.
  354. ``
  355. namespace boost { namespace python { namespace api
  356. {
  357. class object : public object_operators<object>
  358. {
  359. public:
  360. object();
  361. object(object const&);
  362. template <class T>
  363. explicit object(T const& x);
  364. ~object();
  365. object& operator=(object const&);
  366. PyObject* ptr() const;
  367. bool is_none() const;
  368. };
  369. }}}
  370. ``
  371. [endsect]
  372. [section Class `object` constructors and destructor]
  373. ``object();``
  374. [variablelist
  375. [[Effects][Constructs an object managing a reference to the Python None object.]]
  376. [[Throws][nothing.]]
  377. ]
  378. ``template <class T>
  379. explicit object(T const& x);
  380. ``
  381. [variablelist
  382. [[Effects][converts x to python and manages a reference to it.]]
  383. [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] and sets a Python TypeError exception if no such conversion is possible.]]
  384. ]
  385. ``
  386. ~object();
  387. ``
  388. [variablelist
  389. [[Effects][decrements the reference count of the internally-held object.]]
  390. ]
  391. [endsect]
  392. [section Class `object` modifiers]
  393. ``PyObject* ptr() const;``
  394. [variablelist
  395. [[Returns] [a pointer to the internally-held Python object.]]
  396. ]
  397. ``bool is_none() const;``
  398. [variablelist
  399. [[Returns] [result of `(ptr() == Py_None)`]]
  400. ]
  401. [endsect]
  402. [section Class template `proxy`]
  403. This template is instantiated with various Policies described in this document in order to implement attribute, item, and slice access for object. It stores an object of type Policies::key_type.
  404. ``
  405. namespace boost { namespace python { namespace api
  406. {
  407. template <class Policies>
  408. class proxy : public object_operators<proxy<Policies> >
  409. {
  410. public:
  411. operator object() const;
  412. proxy const& operator=(proxy const&) const;
  413. template <class T>
  414. inline proxy const& operator=(T const& rhs) const;
  415. void del() const;
  416. template <class R>
  417. proxy operator+=(R const& rhs);
  418. template <class R>
  419. proxy operator-=(R const& rhs);
  420. template <class R>
  421. proxy operator*=(R const& rhs);
  422. template <class R>
  423. proxy operator/=(R const& rhs);
  424. template <class R>
  425. proxy operator%=(R const& rhs);
  426. template <class R>
  427. proxy operator<<=(R const& rhs);
  428. template <class R>
  429. proxy operator>>=(R const& rhs);
  430. template <class R>
  431. proxy operator&=(R const& rhs);
  432. template <class R>
  433. proxy operator|=(R const& rhs);
  434. };
  435. }}}
  436. ``
  437. [endsect]
  438. [section Class template `proxy` observer functions]
  439. ``operator object() const;``
  440. [variablelist
  441. [[Effects][applies `Policies::get(target, key)` with the proxy's target and key objects.]]
  442. ]
  443. [endsect]
  444. [section Class template `proxy` modifier functions]
  445. ``
  446. proxy const& operator=(proxy const& rhs) const;
  447. template <class T>
  448. inline proxy const& operator=(T const& rhs) const;
  449. ``
  450. [variablelist
  451. [[Effects][ `Policies::set(target, key , object(rhs))` with the proxy's target and key objects.]]
  452. ]
  453. ``
  454. template <class R>
  455. proxy operator+=(R const& rhs);
  456. template <class R>
  457. proxy operator-=(R const& rhs);
  458. template <class R>
  459. proxy operator*=(R const& rhs);
  460. template <class R>
  461. proxy operator/=(R const& rhs);
  462. template <class R>
  463. proxy operator%=(R const& rhs);
  464. template <class R>
  465. proxy operator<<=(R const& rhs);
  466. template <class R>
  467. proxy operator>>=(R const& rhs);
  468. template <class R>
  469. proxy operator&=(R const& rhs);
  470. template <class R>
  471. proxy operator|=(R const& rhs);
  472. ``
  473. [variablelist
  474. [[Effects][for a given `operator@=`, `object(*this) @= rhs;`]]
  475. [[Returns][`*this`]]
  476. ]
  477. ``void del() const;``
  478. [variablelist
  479. [[Effects][Policies::del(target, key ) with the proxy's target and key objects.]]
  480. ]
  481. [endsect]
  482. [section Functions]
  483. ``
  484. template <class T>
  485. void del(proxy<T> const& x);
  486. ``
  487. [variablelist
  488. [[Effects][`x.del()`]]
  489. ]
  490. ``
  491. template<class L,class R> object operator>(L const&l,R const&r);
  492. template<class L,class R> object operator>=(L const&l,R const&r);
  493. template<class L,class R> object operator<(L const&l,R const&r);
  494. template<class L,class R> object operator<=(L const&l,R const&r);
  495. template<class L,class R> object operator==(L const&l,R const&r);
  496. template<class L,class R> object operator!=(L const&l,R const&r);
  497. ``
  498. [variablelist
  499. [[Effects][returns the result of applying the operator to `object(l)` and `object(r)`, respectively, in Python.]]
  500. ]
  501. ``
  502. template<class L,class R> object operator+(L const&l,R const&r);
  503. template<class L,class R> object operator-(L const&l,R const&r);
  504. template<class L,class R> object operator*(L const&l,R const&r);
  505. template<class L,class R> object operator/(L const&l,R const&r);
  506. template<class L,class R> object operator%(L const&l,R const&r);
  507. template<class L,class R> object operator<<(L const&l,R const&r);
  508. template<class L,class R> object operator>>(L const&l,R const&r);
  509. template<class L,class R> object operator&(L const&l,R const&r);
  510. template<class L,class R> object operator^(L const&l,R const&r);
  511. template<class L,class R> object operator|(L const&l,R const&r);
  512. ``
  513. [variablelist
  514. [[Effects][returns the result of applying the operator to `object(l)` and `object(r)`, respectively, in Python.]]
  515. ]
  516. ``
  517. template<class R> object& operator+=(object&l,R const&r);
  518. template<class R> object& operator-=(object&l,R const&r);
  519. template<class R> object& operator*=(object&l,R const&r);
  520. template<class R> object& operator/=(object&l,R const&r);
  521. template<class R> object& operator%=(object&l,R const&r);
  522. template<class R> object& operator<<=(object&l,R const&r)
  523. template<class R> object& operator>>=(object&l,R const&r);
  524. template<class R> object& operator&=(object&l,R const&r);
  525. template<class R> object& operator^=(object&l,R const&r);
  526. template<class R> object& operator|=(object&l,R const&r);
  527. ``
  528. [variablelist
  529. [[Effects][assigns to `l` the result of applying the corresponding Python inplace operator to `l` and `object(r)`, respectively.]]
  530. [[Returns][l]]
  531. ]
  532. ``long len(object const& obj);``
  533. [variablelist
  534. [[Effects][`PyObject_Length(obj.ptr())`]]
  535. [[Returns][`len()` of object.]]
  536. ]
  537. [endsect]
  538. [section Example]
  539. Python code:
  540. ``
  541. def sum_items(seq):
  542. result = 0
  543. for x in seq:
  544. result += x
  545. return result
  546. ``
  547. C++ version
  548. ``
  549. object sum_items(object seq)
  550. {
  551. object result = object(0);
  552. for (int i = 0; i < len(seq); ++i)
  553. result += seq[i];
  554. return result;
  555. }
  556. ``
  557. [endsect]
  558. [endsect]