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 | |
---|
8 | # include <boost/python/detail/prefix.hpp> |
---|
9 | |
---|
10 | #include <boost/python/object.hpp> |
---|
11 | #include <boost/python/list.hpp> |
---|
12 | #include <boost/python/tuple.hpp> |
---|
13 | #include <boost/python/converter/pytype_object_mgr_traits.hpp> |
---|
14 | |
---|
15 | namespace boost { namespace python { |
---|
16 | |
---|
17 | class dict; |
---|
18 | |
---|
19 | namespace detail |
---|
20 | { |
---|
21 | struct BOOST_PYTHON_DECL dict_base : object |
---|
22 | { |
---|
23 | // D.clear() -> None. Remove all items from D. |
---|
24 | void clear(); |
---|
25 | |
---|
26 | // D.copy() -> a shallow copy of D |
---|
27 | dict copy(); |
---|
28 | |
---|
29 | // D.get(k[,d]) -> D[k] if D.has_key(k), else d. d defaults to None. |
---|
30 | object get(object_cref k) const; |
---|
31 | |
---|
32 | object get(object_cref k, object_cref d) const; |
---|
33 | |
---|
34 | // D.has_key(k) -> 1 if D has a key k, else 0 |
---|
35 | bool has_key(object_cref k) const; |
---|
36 | |
---|
37 | // D.items() -> list of D's (key, value) pairs, as 2-tuples |
---|
38 | list items() const; |
---|
39 | |
---|
40 | // D.iteritems() -> an iterator over the (key, value) items of D |
---|
41 | object iteritems() const; |
---|
42 | |
---|
43 | // D.iterkeys() -> an iterator over the keys of D |
---|
44 | object iterkeys() const; |
---|
45 | |
---|
46 | // D.itervalues() -> an iterator over the values of D |
---|
47 | object itervalues() const; |
---|
48 | |
---|
49 | // D.keys() -> list of D's keys |
---|
50 | list keys() const; |
---|
51 | |
---|
52 | // D.popitem() -> (k, v), remove and return some (key, value) pair as a |
---|
53 | // 2-tuple; but raise KeyError if D is empty |
---|
54 | tuple popitem(); |
---|
55 | |
---|
56 | // D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if not D.has_key(k) |
---|
57 | object setdefault(object_cref k); |
---|
58 | |
---|
59 | object setdefault(object_cref k, object_cref d); |
---|
60 | |
---|
61 | // D.update(E) -> None. Update D from E: for k in E.keys(): D[k] = E[k] |
---|
62 | void update(object_cref E); |
---|
63 | |
---|
64 | // D.values() -> list of D's values |
---|
65 | list values() const; |
---|
66 | |
---|
67 | protected: |
---|
68 | // dict() -> new empty dictionary. |
---|
69 | // dict(mapping) -> new dictionary initialized from a mapping object's |
---|
70 | // (key, value) pairs. |
---|
71 | // dict(seq) -> new dictionary initialized as if via: |
---|
72 | dict_base(); // new dict |
---|
73 | explicit dict_base(object_cref data); |
---|
74 | |
---|
75 | BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(dict_base, object) |
---|
76 | private: |
---|
77 | static detail::new_reference call(object const&); |
---|
78 | }; |
---|
79 | } |
---|
80 | |
---|
81 | class dict : public detail::dict_base |
---|
82 | { |
---|
83 | typedef detail::dict_base base; |
---|
84 | public: |
---|
85 | // dict() -> new empty dictionary. |
---|
86 | // dict(mapping) -> new dictionary initialized from a mapping object's |
---|
87 | // (key, value) pairs. |
---|
88 | // dict(seq) -> new dictionary initialized as if via: |
---|
89 | dict() {} // new dict |
---|
90 | |
---|
91 | template <class T> |
---|
92 | explicit dict(T const& data) |
---|
93 | : base(object(data)) |
---|
94 | { |
---|
95 | } |
---|
96 | |
---|
97 | template<class T> |
---|
98 | object get(T const& k) const |
---|
99 | { |
---|
100 | return base::get(object(k)); |
---|
101 | } |
---|
102 | |
---|
103 | template<class T1, class T2> |
---|
104 | object get(T1 const& k, T2 const& d) const |
---|
105 | { |
---|
106 | return base::get(object(k),object(d)); |
---|
107 | } |
---|
108 | |
---|
109 | template<class T> |
---|
110 | bool has_key(T const& k) const |
---|
111 | { |
---|
112 | return base::has_key(object(k)); |
---|
113 | } |
---|
114 | |
---|
115 | template<class T> |
---|
116 | object setdefault(T const& k) |
---|
117 | { |
---|
118 | return base::setdefault(object(k)); |
---|
119 | } |
---|
120 | |
---|
121 | template<class T1, class T2> |
---|
122 | object setdefault(T1 const& k, T2 const& d) |
---|
123 | { |
---|
124 | return base::setdefault(object(k),object(d)); |
---|
125 | } |
---|
126 | |
---|
127 | template<class T> |
---|
128 | void update(T const& E) |
---|
129 | { |
---|
130 | base::update(object(E)); |
---|
131 | } |
---|
132 | |
---|
133 | public: // implementation detail -- for internal use only |
---|
134 | BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(dict, base) |
---|
135 | }; |
---|
136 | |
---|
137 | // |
---|
138 | // Converter Specializations |
---|
139 | // |
---|
140 | namespace converter |
---|
141 | { |
---|
142 | template <> |
---|
143 | struct object_manager_traits<dict> |
---|
144 | : pytype_object_manager_traits<&PyDict_Type,dict> |
---|
145 | { |
---|
146 | }; |
---|
147 | } |
---|
148 | |
---|
149 | }} // namespace boost::python |
---|
150 | |
---|
151 | #endif |
---|
152 | |
---|