Skip to content

Commit aae9224

Browse files
Fix up sequence accessor
1 parent 65916fa commit aae9224

3 files changed

Lines changed: 40 additions & 32 deletions

File tree

function.h

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -117,27 +117,27 @@ namespace classdesc
117117
#include "functiondb.h"
118118

119119
#if defined(__cplusplus) && __cplusplus>=201103L
120-
// for generic function objects
121-
template <class F>
122-
struct Arity
123-
{
124-
static const int V=Arity<decltype(&F::operator())>::V;
125-
static const int value=V;
126-
};
127-
128-
template <class F>
129-
struct Return
130-
{
131-
typedef typename Return<decltype(&F::operator())>::T T;
132-
typedef T type;
133-
};
134-
135-
template <class F, int i>
136-
struct Arg
137-
{
138-
typedef typename Arg<decltype(&F::operator()),i>::T T;
139-
typedef T type;
140-
};
120+
// // for generic function objects
121+
// template <class F>
122+
// struct Arity
123+
// {
124+
// static const int V=Arity<decltype(&F::operator())>::V;
125+
// static const int value=V;
126+
// };
127+
//
128+
// template <class F>
129+
// struct Return
130+
// {
131+
// typedef typename Return<decltype(&F::operator())>::T T;
132+
// typedef T type;
133+
// };
134+
//
135+
// template <class F, int i>
136+
// struct Arg
137+
// {
138+
// typedef typename Arg<decltype(&F::operator()),i>::T T;
139+
// typedef T type;
140+
// };
141141
#endif
142142

143143
template <class O, class M>

pythonExample/example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
assert len(r.bar.c)==len('\r hello & 123 ')
1212
assert len(r.bar.c1)==2
1313
# doesn't work???
14-
#for i in r.bar.c1:
15-
# assert i=='\r'
14+
for i in r.bar.c1:
15+
assert i=='\r'
1616
assert len(r.bar.d)==3
1717
for i in range(len(r.bar.d)):
1818
assert r.bar.d[i]==i

python_base.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ namespace classdesc
5656
{
5757
typedef boost::mpl::vector<typename Arg<F,Arity<F>::V>::T> T;
5858
};
59-
59+
6060
template <class F,int N> struct SigArg
6161
{
6262
typedef typename boost::mpl::push_front<
63-
typename Arg<F,Arity<F>::V-N+1>::T,
64-
typename SigArg<F,N-1>::T
63+
typename SigArg<F,N-1>::T,
64+
typename Arg<F,Arity<F>::V-N+1>::T
6565
>::type T;
6666
};
6767

@@ -84,13 +84,13 @@ namespace classdesc
8484
};
8585

8686
template <class T>
87-
Len<T> len(T& x) {return Len<T>(x);}
87+
size_t len(T& x) {return x.size();}
8888

8989
template <class T> struct GetItem
9090
{
9191
T& container;
9292
GetItem(T& container): container(container) {}
93-
typename T::value_type operator()(T&,size_t n) const {
93+
typename T::value_type operator()(T& c,size_t n) const {
9494
if (n>=container.size())
9595
throw std::out_of_range("index out of bounds");
9696
typename T::iterator i=container.begin();
@@ -100,7 +100,13 @@ namespace classdesc
100100
};
101101

102102
template <class T>
103-
GetItem<T> getItem(T& x) {return GetItem<T>(x);}
103+
typename T::value_type getItem(const T& c, size_t n) {
104+
if (n>=c.size())
105+
throw std::out_of_range("index out of bounds");
106+
typename T::const_iterator i=c.begin();
107+
std::advance(i,n);
108+
return *i;
109+
}
104110

105111
template <class U> struct Sig<Len<U>>
106112
{
@@ -617,9 +623,11 @@ namespace classdesc
617623
template <class T>
618624
typename enable_if<is_sequence<T>,void>::T
619625
python(python_t& p, const string& d, T& a) {
620-
boost::python::class_<T>((tail(d)+"_type").c_str()).
621-
def("__len__", detail::len(a)).
622-
def("__getitem__", detail::getItem(a));
626+
//boost::python::class_<T>((tail(d)+"_type").c_str()).
627+
auto& c=p.getClass<T>();
628+
if (!c.completed)
629+
c.def("__len__", &detail::len<T>).
630+
def("__getitem__", &detail::getItem<T>);
623631
p.addObject(d,a);
624632
}
625633

0 commit comments

Comments
 (0)