2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-23 05:42:30 +00:00

str, dict, and tuple!

[SVN r14517]
This commit is contained in:
Dave Abrahams
2002-07-18 15:17:08 +00:00
parent 94edc13393
commit dfd85da9d7
18 changed files with 1419 additions and 537 deletions

View File

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

View File

@@ -10,8 +10,7 @@
# define OBJECTS_DWA051100_H_
# ifdef BOOST_PYTHON_V2
# include <boost/python/objects2.hpp>
# include <boost/python/list.hpp>
# error obsolete
# else
# include <boost/python/detail/wrap_python.hpp>
# include <boost/python/detail/config.hpp>

View File

@@ -1,252 +0,0 @@
// (C) Copyright David Abrahams 2002. Permission to copy, use, modify, sell and
// distribute this software is granted provided this copyright notice appears
// in all copies. This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
#ifndef OBJECTS_DWA20020611_H
# define OBJECTS_DWA20020611_H
# include <boost/python/detail/wrap_python.hpp>
# include <boost/python/detail/config.hpp>
# include <boost/python/handle.hpp>
# include "boost/operators.hpp"
# include <utility>
namespace boost { namespace python {
class list;
class BOOST_PYTHON_DECL objects_base
{
public:
explicit objects_base(handle<> const& p);
// Return a reference to the held object
handle<> reference() const;
// Return a raw pointer to the held object
PyObject* get() const;
private:
handle<> m_p;
};
class tuple;
class BOOST_PYTHON_DECL tuple_base : public objects_base
{
public:
explicit tuple_base(std::size_t n = 0);
explicit tuple_base(handle<> p);
static PyTypeObject* type_obj();
static bool accepts(handle<> p);
std::size_t size() const;
handle<> operator[](std::size_t pos) const;
void set_item(std::size_t pos, const handle<>& rhs);
tuple slice(int low, int high) const;
friend BOOST_PYTHON_DECL tuple operator+(const tuple&, const tuple&);
friend BOOST_PYTHON_DECL tuple& operator+=(tuple&, const tuple&);
};
class tuple : public tuple_base
{
public:
explicit tuple(std::size_t n = 0) : tuple_base(n) {}
explicit tuple(handle<> p) : tuple_base(p) {}
template <class First, class Second>
tuple(const std::pair<First,Second>& x)
: tuple_base(handle<>(PyTuple_New(2)))
{
set_item(0, x.first);
set_item(1, x.second);
}
template <class First, class Second>
tuple(const First& first, const Second& second)
: tuple_base(handle<>(PyTuple_New(2)))
{
set_item(0, first);
set_item(1, second);
}
template <class First, class Second, class Third>
tuple(const First& first, const Second& second, const Third& third)
: tuple_base(handle<>(PyTuple_New(3)))
{
set_item(0, first);
set_item(1, second);
set_item(2, third);
}
template <class First, class Second, class Third, class Fourth>
tuple(const First& first, const Second& second, const Third& third, const Fourth& fourth)
: tuple_base(handle<>(PyTuple_New(4)))
{
set_item(0, first);
set_item(1, second);
set_item(2, third);
set_item(3, fourth);
}
void set_item(std::size_t pos, const handle<>& rhs)
{
tuple_base::set_item(pos, rhs);
}
};
class BOOST_PYTHON_DECL string
: public objects_base, public boost::multipliable2<string, unsigned int>
{
public:
// Construct from an owned PyObject*.
// Precondition: p must point to a python string.
explicit string(handle<> p);
explicit string(const char* s);
string(const char* s, std::size_t length);
string(const string& rhs);
enum interned_t { interned };
string(const char* s, interned_t);
// Get the type object for Strings
static PyTypeObject* type_obj();
// Return true if the given object is a python string
static bool accepts(handle<> o);
// Return the length of the string.
std::size_t size() const;
// Returns a null-terminated representation of the contents of string.
// The pointer refers to the internal buffer of string, not a copy.
// The data must not be modified in any way. It must not be de-allocated.
const char* c_str() const;
string& operator*=(unsigned int repeat_count);
string& operator+=(const string& rhs);
friend string operator+(string x, string y);
string& operator+=(const char* rhs);
friend string operator+(string x, const char* y);
friend string operator+(const char* x, string y);
void intern();
friend string operator%(const string& format, const tuple& args);
};
class dictionary;
struct BOOST_PYTHON_DECL dictionary_proxy;
class BOOST_PYTHON_DECL dictionary_base : public objects_base
{
protected:
typedef dictionary_proxy proxy;
public:
explicit dictionary_base(handle<> p);
dictionary_base();
void clear();
static PyTypeObject* type_obj();
static bool accepts(handle<> p);
public:
proxy operator[](handle<> key);
handle<> operator[](handle<> key) const;
handle<> get_item(const handle<>& key) const;
handle<> get_item(const handle<>& key, const handle<>& default_) const;
void set_item(const handle<>& key, const handle<>& value);
void erase(handle<> key);
// proxy operator[](const object& key);
// ref operator[](const object& key) const;
// ref get_item(const object& key, ref default_ = ref()) const;
// void set_item(const object& key, const ref& value);
// void erase(const object& key);
list items() const;
list keys() const;
list values() const;
std::size_t size() const;
// TODO: iterator support
};
struct BOOST_PYTHON_DECL dictionary_proxy
{
template <class T>
const handle<>& operator=(const T& rhs)
{ return (*this) = make_ref(rhs); }
const handle<>& operator=(const handle<>& rhs);
operator handle<>() const;
private:
friend class dictionary_base;
dictionary_proxy(const handle<>& dict, const handle<>& key);
// This is needed to work around the very strange MSVC error report that the
// return type of the built-in operator= differs from that of the ones
// defined above. Couldn't hurt to make these un-assignable anyway, though.
const handle<>& operator=(const dictionary_proxy&); // Not actually implemented
private:
handle<> m_dict;
handle<> m_key;
};
class dictionary : public dictionary_base
{
typedef dictionary_proxy proxy;
public:
explicit dictionary(handle<> p) : dictionary_base(p) {}
dictionary() : dictionary_base() {}
template <class Key>
proxy operator[](const Key& key)
{ return this->operator[](make_ref(key)); }
proxy operator[](handle<> key)
{ return dictionary_base::operator[](key); }
template <class Key>
handle<> operator[](const Key& key) const
{ return this->operator[](make_ref(key)); }
handle<> operator[](handle<> key) const
{ return dictionary_base::operator[](key); }
template <class Key>
handle<> get_item(const Key& key) const
{ return this->get_item(make_ref(key)); }
handle<> get_item(const handle<>& key) const
{ return dictionary_base::get_item(key); }
template <class Key, class Default>
handle<> get_item(const Key& key, const Default& default_) const
{ return this->get_item(make_ref(key), make_ref(default_)); }
handle<> get_item(const handle<>& key, const handle<>& default_) const
{ return dictionary_base::get_item(key, default_); }
template <class Key, class Value>
void set_item(const Key& key, const Value& value)
{ this->set_item(make_ref(key), make_ref(value)); }
void set_item(const handle<>& key, const handle<>& value)
{ dictionary_base::set_item(key, value); }
template <class Key>
void erase(const Key& key)
{ this->erase(make_ref(key)); }
void erase(handle<> key)
{ dictionary_base::erase(key); }
};
}} // namespace boost::python
#endif // OBJECTS_DWA20020611_H

View File

@@ -0,0 +1,365 @@
#ifndef STR_20020703_HPP
#define STR_20020703_HPP
#include <boost/python/object.hpp>
#include <boost/python/list.hpp>
#include <boost/python/converter/pytype_object_manager_traits.hpp>
namespace boost { namespace python {
class str : public object
{
public:
BOOST_PYTHON_DECL str(const char* s); // new str
explicit BOOST_PYTHON_DECL str(object_cref other);
template <class T>
explicit str(T const& other)
: object(str::call(object(other)))
{
}
BOOST_PYTHON_DECL str capitalize() const ;
BOOST_PYTHON_DECL str center(object_cref width) const ;
template <class T>
str center(T const& width) const
{
return this->center(object(width));
}
BOOST_PYTHON_DECL long count(object_cref sub) const;
template<class T>
long count(T const& sub) const
{
return this->count(object(sub));
}
BOOST_PYTHON_DECL long count(object_cref sub, object_cref start) const;
template<class T1, class T2>
str count(T1 const& sub,T2 const& start) const
{
return this->count(object(sub), object(start));
}
BOOST_PYTHON_DECL long count(object_cref sub, object_cref start, object_cref end) const;
template<class T1, class T2, class T3>
str count(T1 const& sub,T2 const& start, T3 const& end) const
{
return this->count(object(sub), object(start));
}
BOOST_PYTHON_DECL str decode() const;
BOOST_PYTHON_DECL str decode(object_cref encoding) const;
template<class T>
str decode(T const& encoding) const
{
return this->decode(object(encoding));
}
BOOST_PYTHON_DECL str decode(object_cref encoding, object_cref errors) const;
template<class T1, class T2>
str decode(T1 const& encoding, T2 const& errors) const
{
return this->decode(object(encoding),object(errors));
}
BOOST_PYTHON_DECL str encode() const;
BOOST_PYTHON_DECL str encode(object_cref encoding) const;
template <class T>
str encode(T const& encoding) const
{
return this->encode(object(encoding));
}
BOOST_PYTHON_DECL str encode(object_cref encoding, object_cref errors) const;
template <class T1, class T2>
str encode(T1 const& encoding, T2 const& errors) const
{
return this->encode(object(encoding),object(errors));
}
BOOST_PYTHON_DECL bool endswith(object_cref suffix) const;
template <class T>
bool endswith(T const& suffix) const
{
return this->endswith(object(suffix));
}
BOOST_PYTHON_DECL bool endswith(object_cref suffix, object_cref start) const;
template <class T1, class T2>
bool endswith(T1 const& suffix, T2 const& start) const
{
return this->endswith(object(suffix), object(start));
}
BOOST_PYTHON_DECL bool endswith(object_cref suffix, object_cref start, object_cref end) const;
template <class T1, class T2, class T3>
bool endswith(T1 const& suffix, T2 const& start, T3 const& end) const
{
return this->endswith(object(suffix), object(start), object(end));
}
BOOST_PYTHON_DECL str expandtabs() const;
BOOST_PYTHON_DECL str expandtabs(object_cref tabsize) const;
template <class T>
str expandtabs(T const& tabsize) const
{
return this->expandtabs(object(tabsize));
}
BOOST_PYTHON_DECL long find(object_cref sub) const;
template <class T>
long find(T const& sub) const
{
return this->find(object(sub));
}
BOOST_PYTHON_DECL long find(object_cref sub, object_cref start) const;
template <class T1, class T2>
long find(T1 const& sub, T2 const& start) const
{
return this->find(object(sub), object(start));
}
BOOST_PYTHON_DECL long find(object_cref sub, object_cref start, object_cref end) const;
template <class T1, class T2, class T3>
long find(T1 const& sub, T2 const& start, T3 const& end) const
{
return this->find(object(sub), object(start), object(end));
}
BOOST_PYTHON_DECL long index(object_cref sub) const;
template <class T>
long index(T const& sub) const
{
return this->index(object(sub));
}
BOOST_PYTHON_DECL long index(object_cref sub, object_cref start) const;
template <class T1, class T2>
long index(T1 const& sub, T2 const& start) const
{
return this->index(object(sub), object(start));
}
BOOST_PYTHON_DECL long index(object_cref sub, object_cref start, object_cref end) const;
template <class T1, class T2, class T3>
long index(T1 const& sub, T2 const& start, T3 const& end) const
{
return this->index(object(sub), object(start), object(end));
}
BOOST_PYTHON_DECL bool isalnum() const;
BOOST_PYTHON_DECL bool isalpha() const;
BOOST_PYTHON_DECL bool isdigit() const;
BOOST_PYTHON_DECL bool islower() const;
BOOST_PYTHON_DECL bool isspace() const;
BOOST_PYTHON_DECL bool istitle() const;
BOOST_PYTHON_DECL bool isupper() const;
BOOST_PYTHON_DECL str join(object_cref sequence) const;
template <class T>
str join(T const& sequence) const
{
return this->join(object(sequence));
}
BOOST_PYTHON_DECL str ljust(object_cref width) const;
template <class T>
str ljust(T const& width) const
{
return this->ljust(object(width));
}
BOOST_PYTHON_DECL str lower() const;
BOOST_PYTHON_DECL str lstrip() const;
BOOST_PYTHON_DECL str replace(object_cref old, object_cref new_) const ;
template <class T1, class T2>
str replace(T1 const& old, T2 const& new_) const
{
return this->replace(object(old),object(new_));
}
BOOST_PYTHON_DECL str replace(object_cref old, object_cref new_, object_cref maxsplit) const ;
template <class T1, class T2, class T3>
str replace(T1 const& old, T2 const& new_, T3 const& maxsplit) const
{
return this->replace(object(old),object(new_),object(maxsplit));
}
BOOST_PYTHON_DECL long rfind(object_cref sub) const;
template <class T>
long rfind(T const& sub) const
{
return this->rfind(object(sub));
}
BOOST_PYTHON_DECL long rfind(object_cref sub, object_cref start) const;
template <class T1, class T2>
long rfind(T1 const& sub, T2 const& start) const
{
return this->rfind(object(sub), object(start));
}
BOOST_PYTHON_DECL long rfind(object_cref sub, object_cref start, object_cref end) const;
template <class T1, class T2, class T3>
long rfind(T1 const& sub, T2 const& start, T3 const& end) const
{
return this->rfind(object(sub), object(start), object(end));
}
BOOST_PYTHON_DECL long rindex(object_cref sub) const;
template <class T>
long rindex(T const& sub) const
{
return this->rindex(object(sub));
}
BOOST_PYTHON_DECL long rindex(object_cref sub, object_cref start) const;
template <class T1, class T2>
long rindex(T1 const& sub, T2 const& start) const
{
return this->rindex(object(sub), object(start));
}
BOOST_PYTHON_DECL long rindex(object_cref sub, object_cref start, object_cref end) const;
template <class T1, class T2, class T3>
long rindex(T1 const& sub, T2 const& start, T3 const& end) const
{
return this->rindex(object(sub), object(start), object(end));
}
BOOST_PYTHON_DECL str rjust(object_cref width) const;
template <class T>
str rjust(T const& width) const
{
return this->rjust(object(width));
}
BOOST_PYTHON_DECL str rstrip() const;
BOOST_PYTHON_DECL list split() const;
BOOST_PYTHON_DECL list split(object_cref sep) const;
template <class T>
list split(T const& sep) const
{
return this->split(object(sep));
}
BOOST_PYTHON_DECL list split(object_cref sep, object_cref maxsplit) const;
template <class T1, class T2>
list split(T1 const& sep, T2 const& maxsplit) const
{
return this->split(object(sep), object(maxsplit));
}
BOOST_PYTHON_DECL list splitlines() const;
BOOST_PYTHON_DECL list splitlines(object_cref keepends) const;
template <class T>
list splitlines(T const& keepends) const
{
return this->splitlines(object(keepends));
}
BOOST_PYTHON_DECL bool startswith(object_cref prefix) const ;
template <class T>
bool startswith(T const& prefix) const
{
return this->startswith(object(prefix));
}
BOOST_PYTHON_DECL bool startswith(object_cref prefix, object_cref start) const ;
template <class T1, class T2>
bool startswidth(T1 const& prefix, T2 const& start) const
{
return this->startswidth(object(prefix), object(start));
}
BOOST_PYTHON_DECL bool startswith(object_cref prefix, object_cref start, object_cref end) const ;
template <class T1, class T2, class T3>
bool startswidth(T1 const& prefix, T2 const& start, T3 const& end) const
{
return this->startswidth(object(prefix), object(start), object(end));
}
BOOST_PYTHON_DECL str strip() const ;
BOOST_PYTHON_DECL str swapcase() const ;
BOOST_PYTHON_DECL str title() const ;
BOOST_PYTHON_DECL str translate(object_cref table) const;
template <class T>
str translate(T const& table) const
{
return this->translate(object(table));
}
BOOST_PYTHON_DECL str translate(object_cref table, object_cref deletechars) const;
template <class T1, class T2>
str translate(T1 const& table, T2 const& deletechars) const
{
return this->translate(object(table), object(deletechars));
}
BOOST_PYTHON_DECL str upper() const;
public: // implementation detail -- for internal use only
BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(str)
private:
static BOOST_PYTHON_DECL detail::new_reference call(object const&);
};
//
// Converter Specializations
//
namespace converter
{
template <>
struct object_manager_traits<str>
: pytype_object_manager_traits<&PyString_Type,str>
{
};
}
}} // namespace boost::python
#endif // STR_20020703_HPP

View File

@@ -0,0 +1,47 @@
#ifndef TUPLE_20020706_HPP
#define TUPLE_20020706_HPP
#include <boost/python/object.hpp>
#include <boost/python/converter/pytype_object_manager_traits.hpp>
namespace boost { namespace python {
class tuple : public object
{
public:
// tuple() -> an empty tuple
BOOST_PYTHON_DECL tuple();
// tuple(sequence) -> tuple initialized from sequence's items
BOOST_PYTHON_DECL tuple(object_cref sequence);
template <class T>
explicit tuple(T const& sequence)
: object(tuple::call(object(sequence)))
{
}
public: // implementation detail -- for internal use only
BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(tuple)
private:
static BOOST_PYTHON_DECL detail::new_reference call(object const&);
};
//
// Converter Specializations
//
namespace converter
{
template <>
struct object_manager_traits<tuple>
: pytype_object_manager_traits<&PyTuple_Type,tuple>
{
};
}
}} // namespace boost::python
#endif