dict.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright David Abrahams 2002.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef DICT_20020706_HPP
  6. #define DICT_20020706_HPP
  7. # include <boost/python/detail/prefix.hpp>
  8. #include <boost/python/object.hpp>
  9. #include <boost/python/list.hpp>
  10. #include <boost/python/tuple.hpp>
  11. #include <boost/python/converter/pytype_object_mgr_traits.hpp>
  12. namespace boost { namespace python {
  13. class dict;
  14. namespace detail
  15. {
  16. struct BOOST_PYTHON_DECL dict_base : object
  17. {
  18. // D.clear() -> None. Remove all items from D.
  19. void clear();
  20. // D.copy() -> a shallow copy of D
  21. dict copy();
  22. // D.get(k[,d]) -> D[k] if D.has_key(k), else d. d defaults to None.
  23. object get(object_cref k) const;
  24. object get(object_cref k, object_cref d) const;
  25. // D.has_key(k) -> 1 if D has a key k, else 0
  26. bool has_key(object_cref k) const;
  27. // D.items() -> list of D's (key, value) pairs, as 2-tuples
  28. list items() const;
  29. // D.iteritems() -> an iterator over the (key, value) items of D
  30. object iteritems() const;
  31. // D.iterkeys() -> an iterator over the keys of D
  32. object iterkeys() const;
  33. // D.itervalues() -> an iterator over the values of D
  34. object itervalues() const;
  35. // D.keys() -> list of D's keys
  36. list keys() const;
  37. // D.popitem() -> (k, v), remove and return some (key, value) pair as a
  38. // 2-tuple; but raise KeyError if D is empty
  39. tuple popitem();
  40. // D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if not D.has_key(k)
  41. object setdefault(object_cref k);
  42. object setdefault(object_cref k, object_cref d);
  43. // D.update(E) -> None. Update D from E: for k in E.keys(): D[k] = E[k]
  44. void update(object_cref E);
  45. // D.values() -> list of D's values
  46. list values() const;
  47. protected:
  48. // dict() -> new empty dictionary.
  49. // dict(mapping) -> new dictionary initialized from a mapping object's
  50. // (key, value) pairs.
  51. // dict(seq) -> new dictionary initialized as if via:
  52. dict_base(); // new dict
  53. explicit dict_base(object_cref data);
  54. BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(dict_base, object)
  55. private:
  56. static detail::new_reference call(object const&);
  57. };
  58. }
  59. class dict : public detail::dict_base
  60. {
  61. typedef detail::dict_base base;
  62. public:
  63. // dict() -> new empty dictionary.
  64. // dict(mapping) -> new dictionary initialized from a mapping object's
  65. // (key, value) pairs.
  66. // dict(seq) -> new dictionary initialized as if via:
  67. dict() {} // new dict
  68. template <class T>
  69. explicit dict(T const& data)
  70. : base(object(data))
  71. {
  72. }
  73. template<class T>
  74. object get(T const& k) const
  75. {
  76. return base::get(object(k));
  77. }
  78. template<class T1, class T2>
  79. object get(T1 const& k, T2 const& d) const
  80. {
  81. return base::get(object(k),object(d));
  82. }
  83. template<class T>
  84. bool has_key(T const& k) const
  85. {
  86. return base::has_key(object(k));
  87. }
  88. template<class T>
  89. object setdefault(T const& k)
  90. {
  91. return base::setdefault(object(k));
  92. }
  93. template<class T1, class T2>
  94. object setdefault(T1 const& k, T2 const& d)
  95. {
  96. return base::setdefault(object(k),object(d));
  97. }
  98. template<class T>
  99. void update(T const& E)
  100. {
  101. base::update(object(E));
  102. }
  103. public: // implementation detail -- for internal use only
  104. BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(dict, base)
  105. };
  106. //
  107. // Converter Specializations
  108. //
  109. namespace converter
  110. {
  111. template <>
  112. struct object_manager_traits<dict>
  113. : pytype_object_manager_traits<&PyDict_Type,dict>
  114. {
  115. };
  116. }
  117. }} // namespace boost::python
  118. #endif