gpt4 book ai didi

c++ - 如何在 C++ 代码上使用 Tcl-C API

转载 作者:行者123 更新时间:2023-11-28 04:34:11 27 4
gpt4 key购买 nike

<分区>

我目前正在学习如何使用 Tcl-C api,我正在阅读“Tcl 和 Tk 中的实用编程 - 第四版”,但是当我有一个 C++ 程序时,我发现很难理解 API 的实际工作方式我想使用此 API 执行包装函数,这将为我提供一个将由我的脚本加载的共享对象。

我会尽量说得更清楚。
假设我有以下 C++ 文件:

/* example.h */
Class A {
private:
std::vector<int> data;
public:
A(){}
}

和 C 包装器文件:

/* match.c */

#include <tcl.h>
#include "example.h"

// should create an instance of an A object for future use.
int Tcl_ACmd (clientData, interp, argc, argv)
// should take an instance of an A object that was created and return it's data vector.
int Tcl_AGetDataCmd(clientData, interp, argc, argv)

int Match_Init(Tcl_Interp *interp)
{
if (NULL == Tcl_InitStubs (interp, TCL_VERSION, 0)) {
return TCL_ERROR;
}

Tcl_CreateCommand (interp, "A", Tcl_MaxCmd, (ClientData)NULL,(void (*)())NULL);
Tcl_CreateCommand (interp, "get", Tcl_MaxCmd, (ClientData)client_data,(void (*)())NULL);

return Tcl_PkgProvide(interp, "Match", "1.0");
}

最后是脚本:

 # script.tcl
$> load match.o
$> A (This will create an A instance)
$> A get (this will return the data of A)

现在,我明白了,当我调用 AGetData 命令时,ClientData 实际上是 void* 类型,应该转换为 A 类。
但我仍然不太明白如何将参数传递给包装函数(我应该用 Tcl_Obj 更改 argv 吗?)以及对象的创建位置以及如何跟踪它们以便稍后通过脚本调用那里的方法。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com