mirror of
https://github.com/boostorg/python.git
synced 2026-01-23 05:42:30 +00:00
Add simple_map_traits example and section on iterator_pair
[SVN r20349]
This commit is contained in:
@@ -72,14 +72,24 @@
|
||||
</dl>
|
||||
</dd>
|
||||
<dt>
|
||||
<a href="#container_proxy">container_proxy</a>
|
||||
<a href="#extending">Container adapters</a>
|
||||
</dt>
|
||||
<dd>
|
||||
<dl class="page-index">
|
||||
<dt>
|
||||
<a href="#container_proxy">container_proxy</a>
|
||||
</dt>
|
||||
<dt>
|
||||
<a href="#iterator_pair">iterator_pair</a>
|
||||
</dt>
|
||||
</dl>
|
||||
</dd>
|
||||
<dt>
|
||||
<a href="#workarounds">Compiler workarounds</a>
|
||||
</dt>
|
||||
<dt>
|
||||
<a href="#limitations">Known limitations</a>
|
||||
</dt>
|
||||
<dt>
|
||||
<a href="#workarounds">Compiler workarounds</a>
|
||||
</dt>
|
||||
</dl>
|
||||
</dd>
|
||||
<dt>
|
||||
@@ -1001,6 +1011,97 @@ namespace our_namespace {
|
||||
|
||||
</p>
|
||||
|
||||
<h3><a name="simple_ctraits">Simplistic ContainerTraits example</a></h3>
|
||||
|
||||
<p>
|
||||
|
||||
The following block of code shows a simplistic implementation of
|
||||
<code>ContainerTraits</code> for the container
|
||||
<code>std::set<std::string, int></code>. The actual
|
||||
implementation used by the suite relies on template
|
||||
metaprogramming techniques, whereas this example is designed to
|
||||
show only the essential elements of a
|
||||
<code>ContainerTraits</code> implementation.
|
||||
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<pre>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <boost/python/suite/indexing/iterator_traits.hpp>
|
||||
// Include iterator_traits to get IndexStyle
|
||||
|
||||
struct simple_map_traits {
|
||||
// Traits information for std::map<std::string, int>
|
||||
|
||||
typedef std::map<std::string, int> container;
|
||||
typedef container::size_type size_type;
|
||||
typedef container::iterator iterator;
|
||||
|
||||
typedef int value_type;
|
||||
typedef int & reference;
|
||||
typedef std::string key_type;
|
||||
typedef std::string index_type;
|
||||
|
||||
typedef int value_param;
|
||||
typedef std::string const & key_param;
|
||||
typedef std::string const & index_param;
|
||||
|
||||
static bool const has_copyable_iter = true;
|
||||
static bool const has_mutable_ref = true;
|
||||
static bool const has_find = true;
|
||||
static bool const has_insert = true;
|
||||
static bool const has_erase = true;
|
||||
static bool const has_pop_back = false;
|
||||
static bool const has_push_back = false;
|
||||
static bool const is_reorderable = false;
|
||||
|
||||
static boost::python::indexing::IndexStyle const index_style
|
||||
= boost::python::indexing::index_style_nonlinear;
|
||||
|
||||
struct value_traits_ {
|
||||
// Traits information for our value_type
|
||||
static bool const equality_comparable = true;
|
||||
static bool const lessthan_comparable = true;
|
||||
};
|
||||
|
||||
template<typename PythonClass, typename Policy>
|
||||
static void visitor_helper (PythonClass &, Policy const &)
|
||||
{
|
||||
// Empty
|
||||
}
|
||||
};
|
||||
</pre>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
|
||||
Example usage of the <code>simple_map_traits</code>:
|
||||
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<pre>
|
||||
#include "simple_map_traits.hpp"
|
||||
|
||||
#include <boost/python/suite/indexing/container_suite.hpp>
|
||||
|
||||
#include <boost/python/module.hpp>
|
||||
#include <boost/python/class.hpp>
|
||||
|
||||
BOOST_PYTHON_MODULE(test_simple) {
|
||||
using namespace boost::python;
|
||||
|
||||
typedef std::map<std::string, int> Container;
|
||||
typedef indexing::map_algorithms<simple_map_traits> Algorithms;
|
||||
|
||||
class_<Container> ("map")
|
||||
.def (indexing::container_suite<Container, Algorithms>());
|
||||
}
|
||||
</pre>
|
||||
</p>
|
||||
|
||||
<h2><a name="Algorithms">Algorithms</a></h2>
|
||||
|
||||
<p>
|
||||
@@ -1463,6 +1564,67 @@ template<typename P> struct deref {
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<h2><a name="iterator_pair">iterator_pair</a></h2>
|
||||
|
||||
<p>
|
||||
|
||||
The <code>iterator_pair</code> template provides a
|
||||
container-like interface to a range defined by two iterators.
|
||||
The interface is complete enough to allow the container suite to
|
||||
expose an iterator-defined range as a Python sequence type, with
|
||||
support for operations that do not require insertion or
|
||||
deletion. This can be used to expose a C++ array to Python, or
|
||||
with the result of an <code>equal_range</code> function, or any
|
||||
other source of two iterators marking out a range of values. See
|
||||
the <code>getArray</code> function in
|
||||
libs/python/test/testarray.cpp for an example usage.
|
||||
|
||||
</p>
|
||||
<p>
|
||||
|
||||
<code>iterator_pair</code> should work with any
|
||||
<code>ForwardIterator</code> type.
|
||||
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<pre>
|
||||
namespace boost { namespace python { namespace indexing {
|
||||
template<typename Iterator>
|
||||
class iterator_pair
|
||||
{
|
||||
private:
|
||||
typedef typename boost::call_traits<Iterator>::param_type iterator_param;
|
||||
typedef std::iterator_traits<Iterator> std_traits;
|
||||
|
||||
public:
|
||||
typedef typename std_traits::reference reference;
|
||||
typedef Iterator iterator;
|
||||
typedef typename std_traits::difference_type size_type;
|
||||
typedef typename std_traits::difference_type difference_type;
|
||||
typedef typename std_traits::value_type value_type;
|
||||
typedef typename std_traits::pointer pointer;
|
||||
|
||||
iterator_pair (iterator_param, iterator_param);
|
||||
iterator_pair (std::pair<iterator, iterator> const &);
|
||||
|
||||
iterator begin() const;
|
||||
iterator end() const;
|
||||
|
||||
size_type size () const;
|
||||
reference operator[] (size_type) const;
|
||||
reference at (size_type) const;
|
||||
|
||||
private:
|
||||
// Member variables
|
||||
};
|
||||
|
||||
template<typename T, std::size_t N> T *begin (T (&array)[N]);
|
||||
template<typename T, std::size_t N> T *end (T (&array)[N]);
|
||||
|
||||
} } }
|
||||
</pre>
|
||||
</p>
|
||||
|
||||
<h2><a name="workarounds">Compiler workarounds</a></h2>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user