Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/boost/python/dict.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Copyright David Abrahams 2002.
0002 // Distributed under the Boost Software License, Version 1.0. (See
0003 // accompanying file LICENSE_1_0.txt or copy at
0004 // http://www.boost.org/LICENSE_1_0.txt)
0005 #ifndef DICT_20020706_HPP
0006 #define DICT_20020706_HPP
0007 
0008 # include <boost/python/detail/prefix.hpp>
0009 
0010 #include <boost/python/object.hpp>
0011 #include <boost/python/list.hpp>
0012 #include <boost/python/tuple.hpp>
0013 #include <boost/python/converter/pytype_object_mgr_traits.hpp>
0014 
0015 namespace boost { namespace python {
0016 
0017 class dict;
0018 
0019 namespace detail
0020 {
0021   struct BOOST_PYTHON_DECL dict_base : object
0022   {
0023       // D.clear() -> None.  Remove all items from D.
0024       void clear();
0025 
0026       // D.copy() -> a shallow copy of D
0027       dict copy();
0028 
0029       // D.get(k[,d]) -> D[k] if D.has_key(k), else d.  d defaults to None.
0030       object get(object_cref k) const;
0031     
0032       object get(object_cref k, object_cref d) const;
0033 
0034       // D.has_key(k) -> 1 if D has a key k, else 0
0035       bool has_key(object_cref k) const;
0036 
0037       // D.items() -> list of D's (key, value) pairs, as 2-tuples
0038       list items() const;
0039  
0040       // D.iteritems() -> an iterator over the (key, value) items of D
0041       object iteritems() const;
0042 
0043       // D.iterkeys() -> an iterator over the keys of D
0044       object iterkeys() const;
0045 
0046       // D.itervalues() -> an iterator over the values of D
0047       object itervalues() const;
0048  
0049       // D.keys() -> list of D's keys
0050       list keys() const;
0051  
0052       // D.popitem() -> (k, v), remove and return some (key, value) pair as a
0053       // 2-tuple; but raise KeyError if D is empty
0054       tuple popitem();
0055 
0056       // D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if not D.has_key(k)
0057       object setdefault(object_cref k);
0058 
0059       object setdefault(object_cref k, object_cref d);
0060 
0061       // D.update(E) -> None.  Update D from E: for k in E.keys(): D[k] = E[k]
0062       void update(object_cref E);
0063 
0064       // D.values() -> list of D's values
0065       list values() const;
0066 
0067    protected:
0068       // dict() -> new empty dictionary.
0069       // dict(mapping) -> new dictionary initialized from a mapping object's
0070       //     (key, value) pairs.
0071       // dict(seq) -> new dictionary initialized as if via:
0072       dict_base();   // new dict
0073       explicit dict_base(object_cref data);
0074 
0075       BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(dict_base, object)
0076    private:
0077       static detail::new_reference call(object const&);
0078   };
0079 }
0080 
0081 class dict : public detail::dict_base
0082 {
0083     typedef detail::dict_base base;
0084  public:
0085     // dict() -> new empty dictionary.
0086     // dict(mapping) -> new dictionary initialized from a mapping object's
0087     //     (key, value) pairs.
0088     // dict(seq) -> new dictionary initialized as if via:
0089     dict() {}   // new dict
0090 
0091     template <class T>
0092     explicit dict(T const& data)
0093         : base(object(data))
0094     {
0095     }
0096 
0097     template<class T>
0098     object get(T const& k) const 
0099     {
0100         return base::get(object(k));
0101     }
0102     
0103     template<class T1, class T2>
0104     object get(T1 const& k, T2 const& d) const 
0105     {
0106         return base::get(object(k),object(d));
0107     }
0108     
0109     template<class T>
0110     bool has_key(T const& k) const
0111     {
0112         return base::has_key(object(k));
0113     }
0114     
0115     template<class T>
0116     object setdefault(T const& k)
0117     {
0118         return base::setdefault(object(k));
0119     }
0120     
0121     template<class T1, class T2>
0122     object setdefault(T1 const& k, T2 const& d)
0123     {
0124         return base::setdefault(object(k),object(d));
0125     }
0126 
0127     template<class T>
0128     void update(T const& E)
0129     {
0130         base::update(object(E));
0131     }
0132 
0133  public: // implementation detail -- for internal use only
0134     BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(dict, base)
0135 };
0136 
0137 //
0138 // Converter Specializations
0139 //
0140 namespace converter
0141 {
0142   template <>
0143   struct object_manager_traits<dict>
0144       : pytype_object_manager_traits<&PyDict_Type,dict>
0145   {
0146   };
0147 }
0148 
0149 }}   // namespace boost::python
0150 
0151 #endif
0152