Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
73a50a04
Commit
73a50a04
authored
Sep 11, 2015
by
Wenzel Jakob
Browse files
Fix for radd/rsub/rmul/rdiv operator convenience wrappers
parent
38ffb523
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
16 additions
and
2 deletions
+16
-2
example/example3.cpp
example/example3.cpp
+10
-0
example/example3.py
example/example3.py
+4
-0
include/pybind/operators.h
include/pybind/operators.h
+2
-2
No files found.
example/example3.cpp
View file @
73a50a04
...
@@ -42,10 +42,16 @@ public:
...
@@ -42,10 +42,16 @@ public:
Vector2
&
operator
-=
(
const
Vector2
&
v
)
{
x
-=
v
.
x
;
y
-=
v
.
y
;
return
*
this
;
}
Vector2
&
operator
-=
(
const
Vector2
&
v
)
{
x
-=
v
.
x
;
y
-=
v
.
y
;
return
*
this
;
}
Vector2
&
operator
*=
(
float
v
)
{
x
*=
v
;
y
*=
v
;
return
*
this
;
}
Vector2
&
operator
*=
(
float
v
)
{
x
*=
v
;
y
*=
v
;
return
*
this
;
}
Vector2
&
operator
/=
(
float
v
)
{
x
/=
v
;
y
/=
v
;
return
*
this
;
}
Vector2
&
operator
/=
(
float
v
)
{
x
/=
v
;
y
/=
v
;
return
*
this
;
}
friend
Vector2
operator
+
(
float
f
,
const
Vector2
&
v
)
{
return
Vector2
(
f
+
v
.
x
,
f
+
v
.
y
);
}
friend
Vector2
operator
-
(
float
f
,
const
Vector2
&
v
)
{
return
Vector2
(
f
-
v
.
x
,
f
-
v
.
y
);
}
friend
Vector2
operator
*
(
float
f
,
const
Vector2
&
v
)
{
return
Vector2
(
f
*
v
.
x
,
f
*
v
.
y
);
}
friend
Vector2
operator
/
(
float
f
,
const
Vector2
&
v
)
{
return
Vector2
(
f
/
v
.
x
,
f
/
v
.
y
);
}
private:
private:
float
x
,
y
;
float
x
,
y
;
};
};
void
init_ex3
(
py
::
module
&
m
)
{
void
init_ex3
(
py
::
module
&
m
)
{
py
::
class_
<
Vector2
>
(
m
,
"Vector2"
)
py
::
class_
<
Vector2
>
(
m
,
"Vector2"
)
.
def
(
py
::
init
<
float
,
float
>
())
.
def
(
py
::
init
<
float
,
float
>
())
...
@@ -59,6 +65,10 @@ void init_ex3(py::module &m) {
...
@@ -59,6 +65,10 @@ void init_ex3(py::module &m) {
.
def
(
py
::
self
-=
py
::
self
)
.
def
(
py
::
self
-=
py
::
self
)
.
def
(
py
::
self
*=
float
())
.
def
(
py
::
self
*=
float
())
.
def
(
py
::
self
/=
float
())
.
def
(
py
::
self
/=
float
())
.
def
(
float
()
+
py
::
self
)
.
def
(
float
()
-
py
::
self
)
.
def
(
float
()
*
py
::
self
)
.
def
(
float
()
/
py
::
self
)
.
def
(
"__str__"
,
&
Vector2
::
toString
);
.
def
(
"__str__"
,
&
Vector2
::
toString
);
m
.
attr
(
"Vector"
)
=
m
.
attr
(
"Vector2"
);
m
.
attr
(
"Vector"
)
=
m
.
attr
(
"Vector2"
);
...
...
example/example3.py
View file @
73a50a04
...
@@ -16,6 +16,10 @@ print("v1-8 = " + str(v1-8))
...
@@ -16,6 +16,10 @@ print("v1-8 = " + str(v1-8))
print
(
"v1+8 = "
+
str
(
v1
+
8
))
print
(
"v1+8 = "
+
str
(
v1
+
8
))
print
(
"v1*8 = "
+
str
(
v1
*
8
))
print
(
"v1*8 = "
+
str
(
v1
*
8
))
print
(
"v1/8 = "
+
str
(
v1
/
8
))
print
(
"v1/8 = "
+
str
(
v1
/
8
))
print
(
"8-v1 = "
+
str
(
8
-
v1
))
print
(
"8+v1 = "
+
str
(
8
+
v1
))
print
(
"8*v1 = "
+
str
(
8
*
v1
))
print
(
"8/v1 = "
+
str
(
8
/
v1
))
v1
+=
v2
v1
+=
v2
v1
*=
2
v1
*=
2
...
...
include/pybind/operators.h
View file @
73a50a04
...
@@ -67,8 +67,8 @@ template <typename B, typename L, typename R> struct op_impl<op_##id, op_l, B, L
...
@@ -67,8 +67,8 @@ template <typename B, typename L, typename R> struct op_impl<op_##id, op_l, B, L
}; \
}; \
template <typename B, typename L, typename R> struct op_impl<op_##id, op_r, B, L, R> { \
template <typename B, typename L, typename R> struct op_impl<op_##id, op_r, B, L, R> { \
static char const* name() { return "__" #rid "__"; } \
static char const* name() { return "__" #rid "__"; } \
static auto execute(const
L
&
l
, const
R
&
r
) -> decltype(expr) { return (expr); } \
static auto execute(const
R
&
r
, const
L
&
l
) -> decltype(expr) { return (expr); } \
static B execute_cast(const
L
&
l
, const
R
&
r
) { return B(expr); } \
static B execute_cast(const
R
&
r
, const
L
&
l
) { return B(expr); } \
}; \
}; \
inline op_<op_##id, op_l, self_t, self_t> op(const self_t &, const self_t &) { \
inline op_<op_##id, op_l, self_t, self_t> op(const self_t &, const self_t &) { \
return op_<op_##id, op_l, self_t, self_t>(); \
return op_<op_##id, op_l, self_t, self_t>(); \
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment