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