1+ #include " lpoplib.hpp"
2+ #include " math/point.hpp"
3+
4+ using namespace PopLib ;
5+
6+ template <typename T> struct TPointWrapper : public TPoint <T>
7+ {
8+ using TPoint<T>::TPoint; // inherit constructors
9+
10+ TPointWrapper () : TPoint<T>()
11+ {
12+ }
13+ TPointWrapper (T x, T y) : TPoint<T>(x, y)
14+ {
15+ }
16+ TPointWrapper (const TPoint<T> &p) : TPoint<T>(p)
17+ {
18+ }
19+
20+ TPointWrapper operator +(const TPointWrapper &other) const
21+ {
22+ return TPointWrapper (this ->mX + other.mX , this ->mY + other.mY );
23+ }
24+
25+ TPointWrapper operator -(const TPointWrapper &other) const
26+ {
27+ return TPointWrapper (this ->mX - other.mX , this ->mY - other.mY );
28+ }
29+
30+ TPointWrapper operator *(const TPointWrapper &other) const
31+ {
32+ return TPointWrapper (this ->mX * other.mX , this ->mY * other.mY );
33+ }
34+
35+ TPointWrapper operator /(const TPointWrapper &other) const
36+ {
37+ return TPointWrapper (this ->mX / other.mX , this ->mY / other.mY );
38+ }
39+
40+ TPointWrapper operator *(T s) const
41+ {
42+ return TPointWrapper (this ->mX * s, this ->mY * s);
43+ }
44+
45+ TPointWrapper operator /(T s) const
46+ {
47+ return TPointWrapper (this ->mX / s, this ->mY / s);
48+ }
49+
50+ bool operator ==(const TPointWrapper &other) const
51+ {
52+ return this ->mX == other.mX && this ->mY == other.mY ;
53+ }
54+ };
55+
56+ void open_point (sol::state_view lua)
57+ {
58+ sol::table poplib;
59+ if (lua[" PopLib" ].valid () && lua[" PopLib" ].get_type () == sol::type::table)
60+ {
61+ poplib = lua[" PopLib" ];
62+ }
63+ else
64+ {
65+ poplib = lua.create_table ();
66+ lua[" PopLib" ] = poplib;
67+ }
68+
69+ poplib.new_usertype <TPointWrapper<int >>(
70+ " Point" ,
71+ sol::constructors<TPointWrapper<int >(), TPointWrapper<int >(int , int ),
72+ TPointWrapper<int >(const TPoint<int > &)>(),
73+ " mX" , &TPointWrapper<int >::mX , " mY" , &TPointWrapper<int >::mY , sol::meta_function::equal_to,
74+ &TPointWrapper<int >::operator ==, sol::meta_function::addition, &TPointWrapper<int >::operator +,
75+ sol::meta_function::subtraction, &TPointWrapper<int >::operator -, sol::meta_function::multiplication,
76+ sol::overload (
77+ static_cast <TPointWrapper<int > (TPointWrapper<int >::*)(const TPointWrapper<int > &) const >(
78+ &TPointWrapper<int >::operator *),
79+ static_cast <TPointWrapper<int > (TPointWrapper<int >::*)(int ) const >(&TPointWrapper<int >::operator *)),
80+ sol::meta_function::division,
81+ sol::overload (
82+ static_cast <TPointWrapper<int > (TPointWrapper<int >::*)(const TPointWrapper<int > &) const >(
83+ &TPointWrapper<int >::operator /),
84+ static_cast <TPointWrapper<int > (TPointWrapper<int >::*)(int ) const >(&TPointWrapper<int >::operator /)),
85+ sol::meta_function::to_string, [](const TPointWrapper<int > &p) {
86+ return " Point(" + std::to_string (p.mX ) + " , " + std::to_string (p.mY ) + " )" ;
87+ });
88+
89+ poplib.new_usertype <TPointWrapper<double >>(
90+ " FPoint" ,
91+ sol::constructors<TPointWrapper<double >(), TPointWrapper<double >(double , double ),
92+ TPointWrapper<double >(const TPoint<double > &)>(),
93+ " mX" , &TPointWrapper<double >::mX , " mY" , &TPointWrapper<double >::mY , sol::meta_function::equal_to,
94+ &TPointWrapper<double >::operator ==, sol::meta_function::addition, &TPointWrapper<double >::operator +,
95+ sol::meta_function::subtraction, &TPointWrapper<double >::operator -, sol::meta_function::multiplication,
96+ sol::overload (
97+ static_cast <TPointWrapper<double > (TPointWrapper<double >::*)(const TPointWrapper<double > &) const >(
98+ &TPointWrapper<double >::operator *),
99+ static_cast <TPointWrapper<double > (TPointWrapper<double >::*)(double ) const >(
100+ &TPointWrapper<double >::operator *)),
101+ sol::meta_function::division,
102+ sol::overload (
103+ static_cast <TPointWrapper<double > (TPointWrapper<double >::*)(const TPointWrapper<double > &) const >(
104+ &TPointWrapper<double >::operator /),
105+ static_cast <TPointWrapper<double > (TPointWrapper<double >::*)(double ) const >(
106+ &TPointWrapper<double >::operator /)),
107+ sol::meta_function::to_string, [](const TPointWrapper<double > &p) {
108+ return " FPoint(" + std::to_string (p.mX ) + " , " + std::to_string (p.mY ) + " )" ;
109+ });
110+ }
0 commit comments