Skip to content

Commit

Permalink
add example bind_vector
Browse files Browse the repository at this point in the history
  • Loading branch information
peacalm committed Oct 22, 2024
1 parent 2c58b00 commit 0f8863e
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
54 changes: 54 additions & 0 deletions example/bind_vector/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2024 Li Shuangquan. All Rights Reserved.
//
// Licensed under the MIT License (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License
// at
//
// http://opensource.org/licenses/MIT
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

#include <peacalm/luaw.h>

#include <vector>

int main() {
peacalm::luaw l;

using VD = std::vector<double>;
using SharedVD = std::shared_ptr<VD>;

// Constructor
l.set("NewVD", []() { return std::make_shared<VD>(); });

l.register_member<const double& (VD::*)(size_t) const>("at", &VD::at);
// Fake member function: to stimulate the operator[]
l.register_member<void (VD::*)(int, double)>(
"set_at", [](VD* p, int idx, double v) { (*p)[idx] = v; });

l.register_member("size", &VD::size);
l.register_member("empty", &VD::empty);

l.register_member("clear", &VD::clear);
// Fake member function: to stimulate insert
l.register_member<void (VD::*)(int, double)>(
"insert_at", [](VD* p, int idx, double v) {
p->insert(std::next(p->begin(), idx), v);
});

l.register_member<void (VD::*)(const double&)>("push_back", &VD::push_back);
l.register_member("pop_back", &VD::pop_back);

l.register_member<void (VD::*)(size_t)>("resize", &VD::resize);

int retcode = l.dofile("test.lua");
if (retcode != LUA_OK) {
l.log_error_out();
return retcode;
}
return 0;
}
50 changes: 50 additions & 0 deletions example/bind_vector/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
printv = function(v, msg)
t = {'['}
for i = 0, v:size() - 1 do
if i > 0 then
table.insert(t, #t + 1, ', ')
end
table.insert(t, #t + 1, v:at(i))
end
table.insert(t, #t + 1, ']')
if msg ~= nil then
print(msg, table.concat(t))
else
print(table.concat(t))
end
end

v = NewVD();
assert(v:size() == 0)
assert(v:empty())
for i = 0, 4 do
v:push_back(i)
end
printv(v, 'init 0~4')

assert(v:size() == 5)
assert(not v:empty())
assert(v:at(2) == 2)

v:pop_back()
assert(v:size() == 4)
printv(v, 'pop back')

v:set_at(2, -1)
assert(v:at(2) == -1)
printv(v, 'v[2] = -1')

v:insert_at(0, 1);
printv(v, 'insert_at(0, 1)')

v:set_at(1, 2)
printv(v, 'v[1] = 2')

v:clear()
assert(v:empty())
printv(v, 'clear')

v:resize(3)
printv(v, 'resize(3)')

print('END')

0 comments on commit 0f8863e

Please sign in to comment.