Skip to content

Commit 0f8863e

Browse files
committed
add example bind_vector
1 parent 2c58b00 commit 0f8863e

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

example/bind_vector/test.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) 2024 Li Shuangquan. All Rights Reserved.
2+
//
3+
// Licensed under the MIT License (the "License"); you may not use this file
4+
// except in compliance with the License. You may obtain a copy of the License
5+
// at
6+
//
7+
// http://opensource.org/licenses/MIT
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
// License for the specific language governing permissions and limitations
13+
// under the License.
14+
15+
#include <peacalm/luaw.h>
16+
17+
#include <vector>
18+
19+
int main() {
20+
peacalm::luaw l;
21+
22+
using VD = std::vector<double>;
23+
using SharedVD = std::shared_ptr<VD>;
24+
25+
// Constructor
26+
l.set("NewVD", []() { return std::make_shared<VD>(); });
27+
28+
l.register_member<const double& (VD::*)(size_t) const>("at", &VD::at);
29+
// Fake member function: to stimulate the operator[]
30+
l.register_member<void (VD::*)(int, double)>(
31+
"set_at", [](VD* p, int idx, double v) { (*p)[idx] = v; });
32+
33+
l.register_member("size", &VD::size);
34+
l.register_member("empty", &VD::empty);
35+
36+
l.register_member("clear", &VD::clear);
37+
// Fake member function: to stimulate insert
38+
l.register_member<void (VD::*)(int, double)>(
39+
"insert_at", [](VD* p, int idx, double v) {
40+
p->insert(std::next(p->begin(), idx), v);
41+
});
42+
43+
l.register_member<void (VD::*)(const double&)>("push_back", &VD::push_back);
44+
l.register_member("pop_back", &VD::pop_back);
45+
46+
l.register_member<void (VD::*)(size_t)>("resize", &VD::resize);
47+
48+
int retcode = l.dofile("test.lua");
49+
if (retcode != LUA_OK) {
50+
l.log_error_out();
51+
return retcode;
52+
}
53+
return 0;
54+
}

example/bind_vector/test.lua

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
printv = function(v, msg)
2+
t = {'['}
3+
for i = 0, v:size() - 1 do
4+
if i > 0 then
5+
table.insert(t, #t + 1, ', ')
6+
end
7+
table.insert(t, #t + 1, v:at(i))
8+
end
9+
table.insert(t, #t + 1, ']')
10+
if msg ~= nil then
11+
print(msg, table.concat(t))
12+
else
13+
print(table.concat(t))
14+
end
15+
end
16+
17+
v = NewVD();
18+
assert(v:size() == 0)
19+
assert(v:empty())
20+
for i = 0, 4 do
21+
v:push_back(i)
22+
end
23+
printv(v, 'init 0~4')
24+
25+
assert(v:size() == 5)
26+
assert(not v:empty())
27+
assert(v:at(2) == 2)
28+
29+
v:pop_back()
30+
assert(v:size() == 4)
31+
printv(v, 'pop back')
32+
33+
v:set_at(2, -1)
34+
assert(v:at(2) == -1)
35+
printv(v, 'v[2] = -1')
36+
37+
v:insert_at(0, 1);
38+
printv(v, 'insert_at(0, 1)')
39+
40+
v:set_at(1, 2)
41+
printv(v, 'v[1] = 2')
42+
43+
v:clear()
44+
assert(v:empty())
45+
printv(v, 'clear')
46+
47+
v:resize(3)
48+
printv(v, 'resize(3)')
49+
50+
print('END')

0 commit comments

Comments
 (0)