sl@0: // Copyright David Abrahams 2002. sl@0: // Distributed under the Boost Software License, Version 1.0. (See sl@0: // accompanying file LICENSE_1_0.txt or copy at sl@0: // http://www.boost.org/LICENSE_1_0.txt) sl@0: #ifndef DICT_20020706_HPP sl@0: #define DICT_20020706_HPP sl@0: sl@0: # include sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: namespace boost { namespace python { sl@0: sl@0: class dict; sl@0: sl@0: namespace detail sl@0: { sl@0: struct BOOST_PYTHON_DECL dict_base : object sl@0: { sl@0: // D.clear() -> None. Remove all items from D. sl@0: void clear(); sl@0: sl@0: // D.copy() -> a shallow copy of D sl@0: dict copy(); sl@0: sl@0: // D.get(k[,d]) -> D[k] if D.has_key(k), else d. d defaults to None. sl@0: object get(object_cref k) const; sl@0: sl@0: object get(object_cref k, object_cref d) const; sl@0: sl@0: // D.has_key(k) -> 1 if D has a key k, else 0 sl@0: bool has_key(object_cref k) const; sl@0: sl@0: // D.items() -> list of D's (key, value) pairs, as 2-tuples sl@0: list items() const; sl@0: sl@0: // D.iteritems() -> an iterator over the (key, value) items of D sl@0: object iteritems() const; sl@0: sl@0: // D.iterkeys() -> an iterator over the keys of D sl@0: object iterkeys() const; sl@0: sl@0: // D.itervalues() -> an iterator over the values of D sl@0: object itervalues() const; sl@0: sl@0: // D.keys() -> list of D's keys sl@0: list keys() const; sl@0: sl@0: // D.popitem() -> (k, v), remove and return some (key, value) pair as a sl@0: // 2-tuple; but raise KeyError if D is empty sl@0: tuple popitem(); sl@0: sl@0: // D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if not D.has_key(k) sl@0: object setdefault(object_cref k); sl@0: sl@0: object setdefault(object_cref k, object_cref d); sl@0: sl@0: // D.update(E) -> None. Update D from E: for k in E.keys(): D[k] = E[k] sl@0: void update(object_cref E); sl@0: sl@0: // D.values() -> list of D's values sl@0: list values() const; sl@0: sl@0: protected: sl@0: // dict() -> new empty dictionary. sl@0: // dict(mapping) -> new dictionary initialized from a mapping object's sl@0: // (key, value) pairs. sl@0: // dict(seq) -> new dictionary initialized as if via: sl@0: dict_base(); // new dict sl@0: explicit dict_base(object_cref data); sl@0: sl@0: BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(dict_base, object) sl@0: private: sl@0: static detail::new_reference call(object const&); sl@0: }; sl@0: } sl@0: sl@0: class dict : public detail::dict_base sl@0: { sl@0: typedef detail::dict_base base; sl@0: public: sl@0: // dict() -> new empty dictionary. sl@0: // dict(mapping) -> new dictionary initialized from a mapping object's sl@0: // (key, value) pairs. sl@0: // dict(seq) -> new dictionary initialized as if via: sl@0: dict() {} // new dict sl@0: sl@0: template sl@0: explicit dict(T const& data) sl@0: : base(object(data)) sl@0: { sl@0: } sl@0: sl@0: template sl@0: object get(T const& k) const sl@0: { sl@0: return base::get(object(k)); sl@0: } sl@0: sl@0: template sl@0: object get(T1 const& k, T2 const& d) const sl@0: { sl@0: return base::get(object(k),object(d)); sl@0: } sl@0: sl@0: template sl@0: bool has_key(T const& k) const sl@0: { sl@0: return base::has_key(object(k)); sl@0: } sl@0: sl@0: template sl@0: object setdefault(T const& k) sl@0: { sl@0: return base::setdefault(object(k)); sl@0: } sl@0: sl@0: template sl@0: object setdefault(T1 const& k, T2 const& d) sl@0: { sl@0: return base::setdefault(object(k),object(d)); sl@0: } sl@0: sl@0: template sl@0: void update(T const& E) sl@0: { sl@0: base::update(object(E)); sl@0: } sl@0: sl@0: public: // implementation detail -- for internal use only sl@0: BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(dict, base) sl@0: }; sl@0: sl@0: // sl@0: // Converter Specializations sl@0: // sl@0: namespace converter sl@0: { sl@0: template <> sl@0: struct object_manager_traits sl@0: : pytype_object_manager_traits<&PyDict_Type,dict> sl@0: { sl@0: }; sl@0: } sl@0: sl@0: }} // namespace boost::python sl@0: sl@0: #endif sl@0: