开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 0

electron/ChaiScript

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
develop
分支 (15)
标签 (61)
develop
update-catch
gitignore-build
release-6.x
master
best_practices
test_limits
attempt_converting_unique_ptr_through_base_class
release-5.x
attempt_global_object_caching
typed_reworks
attempt_register_function_simplification
Workaround_Thread_Local_MinGW
value_types
release-4.x
v6.1.0
v6.0.0
v5.8.6
v5.8.5
v5.8.4
v5.8.3
v5.8.2
v5.8.1
v5.8.0
Test_Release
v5.7.1
v5.7.0
v5.6.0
v5.5.1
v5.5.0
v5.4.0
v5.3.1
v4.3.1
Release-5.3.0
Release-4.3.0
develop
分支 (15)
标签 (61)
develop
update-catch
gitignore-build
release-6.x
master
best_practices
test_limits
attempt_converting_unique_ptr_through_base_class
release-5.x
attempt_global_object_caching
typed_reworks
attempt_register_function_simplification
Workaround_Thread_Local_MinGW
value_types
release-4.x
v6.1.0
v6.0.0
v5.8.6
v5.8.5
v5.8.4
v5.8.3
v5.8.2
v5.8.1
v5.8.0
Test_Release
v5.7.1
v5.7.0
v5.6.0
v5.5.1
v5.5.0
v5.4.0
v5.3.1
v4.3.1
Release-5.3.0
Release-4.3.0
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
develop
分支 (15)
标签 (61)
develop
update-catch
gitignore-build
release-6.x
master
best_practices
test_limits
attempt_converting_unique_ptr_through_base_class
release-5.x
attempt_global_object_caching
typed_reworks
attempt_register_function_simplification
Workaround_Thread_Local_MinGW
value_types
release-4.x
v6.1.0
v6.0.0
v5.8.6
v5.8.5
v5.8.4
v5.8.3
v5.8.2
v5.8.1
v5.8.0
Test_Release
v5.7.1
v5.7.0
v5.6.0
v5.5.1
v5.5.0
v5.4.0
v5.3.1
v4.3.1
Release-5.3.0
Release-4.3.0
ChaiScript
/
samples
/
factory.cpp
ChaiScript
/
samples
/
factory.cpp
factory.cpp 2.95 KB
一键复制 编辑 原始数据 按行查看 历史
#include <chaiscript/chaiscript.hpp>
class Entity {
public:
int width;
int height;
int x;
int y;
std::string name;
std::function<void(Entity &)> updater;
Entity(const int t_width, const int t_height, const int t_x, const int t_y, std::string t_name)
: width(t_width)
, height(t_height)
, x(t_x)
, y(t_y)
, name(std::move(t_name)) {
}
};
class Factory {
public:
// we may as well pass the parameters for the entity to the factory method, this does the initialization
// in one step.
Entity *make_entity(const int width, const int height, const int x, const int y, const std::string &name) {
auto entity = entities.insert({name, Entity{width, height, x, y, name}});
return &(entity.first->second);
}
Entity *get_entity(const std::string &name) { return &entities.at(name); }
// loop over all entities and all their updater function (if it exists)
void update_entities() {
for (auto &entity : entities) {
if (entity.second.updater) {
entity.second.updater(entity.second);
}
}
}
private:
// we cannot store the entities in a std::vector if we want to return a pointer to them,
// because a vector automatically resizing itself can invalidate the pointer that was returned.
// using a map guarantees that the memory assigned to the entity will never change, plus
// lets us easily look up an entity by name
std::map<std::string, Entity> entities;
};
int main() {
chaiscript::ChaiScript chai;
chai.add(chaiscript::fun(&Entity::width), "width");
chai.add(chaiscript::fun(&Entity::height), "height");
chai.add(chaiscript::fun(&Entity::x), "x");
chai.add(chaiscript::fun(&Entity::y), "y");
chai.add(chaiscript::fun(&Entity::name), "name");
chai.add(chaiscript::fun(&Entity::updater), "updater");
chai.add(chaiscript::user_type<Entity>(), "Entity"); // this isn't strictly necessary but makes error messages nicer
chai.add(chaiscript::fun(&Factory::make_entity), "make_entity");
chai.add(chaiscript::fun(&Factory::get_entity), "get_entity");
chai.add(chaiscript::fun(&Factory::update_entities), "update_entities");
chai.add(chaiscript::user_type<Factory>(), "Factory"); // this isn't strictly necessary but makes error messages nicer
Factory f;
chai.add(chaiscript::var(&f), "f");
std::string script = R""(
f.make_entity(10,10,1,1,"entity1").updater = fun(e){ e.x += 1; e.y += 1 };
f.make_entity(10,10,10,10,"entity2").updater = fun(e){ e.x += 2; e.y += 2 };
f.make_entity(10,10,20,20,"entity3");
print(f.get_entity("entity1").x == 1)
print(f.get_entity("entity2").x == 10)
print(f.get_entity("entity3").x == 20)
f.update_entities(); // this runs the function objects we set in the previous lines
// we should now see the updated values
print(f.get_entity("entity1").x == 2)
print(f.get_entity("entity2").x == 12)
print(f.get_entity("entity3").x == 20) // this one has no updater, so it stays the same
)"";
chai.eval(script);
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

ChaiScript是一个嵌入式脚本语言,专为与C++无缝集成而设计。
暂无标签
BSD-3-Clause
使用 BSD-3-Clause 开源许可协议
, BSD-3-Clause
使用 BSD-3-Clause 开源许可协议
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/electron/ChaiScript.git
git@gitee.com:electron/ChaiScript.git
electron
ChaiScript
ChaiScript
develop
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /