Skip to content

Commit fab1f18

Browse files
committed
added support for 'set'
1 parent 4f6d547 commit fab1f18

File tree

2 files changed

+176
-0
lines changed

2 files changed

+176
-0
lines changed

include/boost/python/set.hpp

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#ifndef SET_BOOST_PYTHON_HH
2+
#define SET_BOOST_PYTHON_HH
3+
4+
#include <boost/python.hpp>
5+
6+
namespace boost {
7+
namespace python {
8+
9+
namespace detail
10+
{
11+
struct BOOST_PYTHON_DECL set_base : object
12+
{
13+
void add(object_cref); // add object to set
14+
15+
object pop(); // remove and return item at top
16+
17+
void discard(object_cref x); // discard value from set
18+
19+
long __len__(); // length of set
20+
21+
void clear(); // empties set
22+
23+
protected:
24+
set_base();
25+
explicit set_base(object_cref sequence); // new set initialized from sequence's items
26+
27+
BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(set_base, object)
28+
private:
29+
static detail::new_non_null_reference call(object const&);
30+
};
31+
32+
33+
}
34+
35+
36+
37+
38+
39+
40+
class set : public detail::set_base
41+
{
42+
typedef detail::set_base base;
43+
public:
44+
set() {}
45+
46+
template <class T>
47+
explicit set(T const& sequence)
48+
: base(object(sequence))
49+
{
50+
}
51+
52+
template <class T>
53+
void add(T const& x)
54+
{
55+
base::add(object(x));
56+
}
57+
58+
object pop() { return base::pop(); }
59+
60+
template <class T>
61+
void discard(T const& value)
62+
{
63+
base::discard(object(value));
64+
}
65+
66+
void clear() {base::clear(); }
67+
68+
long __len__() { return base::__len__(); }
69+
70+
71+
72+
public:
73+
BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(set, base)
74+
};
75+
76+
77+
namespace converter
78+
{
79+
template <>
80+
struct object_manager_traits<set>
81+
: pytype_object_manager_traits<&PySet_Type, set>
82+
{
83+
};
84+
}
85+
86+
87+
}
88+
89+
}
90+
91+
92+
93+
94+
95+
#endif // !SET_BOOST_PYTHON_HH

src/set.cpp

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include "set.hpp"
2+
#include <boost/python/ssize_t.hpp>
3+
#include <boost/python.hpp>
4+
5+
namespace boost {
6+
namespace python {
7+
namespace detail {
8+
9+
10+
detail::new_non_null_reference set_base::call(object_cref arg_)
11+
{
12+
return (detail::new_non_null_reference)
13+
(expect_non_null)(
14+
PySet_New(arg_.ptr())
15+
);
16+
}
17+
18+
set_base::set_base()
19+
: object(detail::new_reference(PySet_New(NULL)))
20+
{}
21+
22+
set_base::set_base(object_cref sequence)
23+
: object(set_base::call(sequence))
24+
{}
25+
26+
void set_base::add(object_cref x)
27+
{
28+
if (PyAnySet_CheckExact(this->ptr()))
29+
{
30+
if (PySet_Add(this->ptr(), x.ptr()) == -1)
31+
throw_error_already_set();
32+
}
33+
else
34+
{
35+
this->attr("add")(x);
36+
}
37+
}
38+
39+
40+
void set_base::discard(object_cref x)
41+
{
42+
if (PyAnySet_CheckExact(this->ptr()))
43+
{
44+
if (PySet_Discard(this->ptr(), x.ptr()) == -1)
45+
throw_error_already_set();
46+
}
47+
else
48+
{
49+
this->attr("discrad")(x);
50+
}
51+
}
52+
53+
object set_base::pop()
54+
{
55+
return this->attr("pop")();
56+
}
57+
58+
void set_base::clear()
59+
{
60+
this->attr("clear")();
61+
}
62+
63+
long set_base::__len__()
64+
{
65+
return extract<long>(object(PySet_Size(this->ptr())))();
66+
}
67+
68+
69+
static struct register_set_pytype_ptr
70+
{
71+
register_set_pytype_ptr()
72+
{
73+
const_cast<converter::registration &>(
74+
converter::registry::lookup(boost::python::type_id<boost::python::set>())
75+
).m_class_object = &PySet_Type;
76+
}
77+
}register_set_pytype_ptr_;
78+
79+
}
80+
}
81+
} // namespace boost::python

0 commit comments

Comments
 (0)