如何将整数数组从Tcl传递到C++
How to pass array of integers from Tcl to C++?
如何将Tcl数组发送到C++?我已经写了以下代码:
Tcl:
set ns [new Simulator]
set n [$ns node]
$n set X_ 100
$n set Y_ 30
$n set Z_ 0
set x [$n set X_]
set y [$n set Y_]
set z [$n set Z_]
#after 2000
set b {12 2 3 4 5}
set aa [new "Application/Trust/ITLeach"]
$aa set bufer_ 1
$aa set allnode_ $n
$aa set X_ $x
$aa set Y_ $y
$aa set Z_ $z
$aa set ClausterHeadID_ [array get b] **#send array to c++**
$ns at 0.0 "$aa start"
puts $b
$ns run
ITLEACH.h:
#ifndef ns_ITLeach_h
#define ns_ITLeach_h
#include "app.h"
#include "node.h"
#include "tcl.h"
#include "mobilenode.h"
#include <iostream>
#include <fstream>
class ITLeach;
#define TCL_OK 0
class ITLeach : public Application {
public:
ITLeach();
virtual int command(int argc, const char*const* argv);
protected:
// need to define recv and timeout
void start();
int Buffer;
MobileNode * node ;
ofstream nodeSetting;
double XPos ;
double YPos ;
double ZPos ;
int ClausterHeadID [] ; //int array that passed from tcl file
int ClausterID [] ;
int id_node;
};
#endif
ITLECH.cc:
/*
* ITLeach.cc
*
* Created on: Oct 29, 2013
* Author: root
*/
#include "ITLeach.h"
static class ITLeachClass : public TclClass {
public:
ITLeachClass() : TclClass("Application/Trust/ITLeach") {}
TclObject* create(int, const char*const*) {
return (new ITLeach());
}
} class_app_ITLeach;
ITLeach::ITLeach() : Application() {
Tcl_Obj *baObj = Tcl_NewObj();
bind("bufer_",&Buffer);
bind("allnode_",&node);
bind("X_",&XPos);
bind("Y_",&YPos);
bind("Z_",&ZPos);
bind("ClausterHeadID_",(int *) &ClausterHeadID); // call array from tcl
bind("ClausterID_",ClausterID);
bind("id_",&id_node);
}
int ITLeach::command(int argc, const char*const* argv) {
if (strcmp(argv[1], "start") == 0) {
ITLeach::start();
return(TCL_OK);
}
return(ITLeach::command(argc, argv));
}
void ITLeach::start()
{
//double x=0, y =0 , z =0;
nodeSetting.open("./leachnode.txt",fstream::app);
//node = (MobileNode*)Node::get_node_by_address(i);
//node->location()->getLocation(x,y,z);
//node->getLoc(&x,&y,&z);
nodeSetting << "id " << id_node << " x "<< XPos << " y " << YPos << " z " << ZPos <<"n";
nodeSetting.close();
printf(" claster head number : %d n" ,ClausterHeadID[1]);
printf("node number is : %d n",Buffer);
}
我用以下代码从Tcl发送数组:
$aa set ClausterHeadID_ [array get b] **#send array to c++**
并用以下代码从C++接收数组:
bind("ClausterHeadID_",(int *) &ClausterHeadID); // call array from tcl
但是它不起作用,请帮帮我。
如果已将该命令绑定到字符串接口(即,参数通过int argc, char **argv
到达(,则使用Tcl_SplitList()
分解相关参数(可能是argv[argc-1]
,即最后一个参数(,然后使用Tcl_GetInt()
从每个值中检索一个整数。这些整数是Tcl列表的成员。
int listc;
char **listv;
if (Tcl_SplitList(interp, argv[argc-1], &listc, &listv) != TCL_OK) {
// wasn't a valid list!
return TCL_ERROR;
}
std::vector<int> theArray(listc, 0);
for (int i=0 ; i<listc ; i++) {
if (Tcl_GetInt(interp, listv[i], &theArray[i]) != TCL_OK) {
// wasn't an int in the list!
return TCL_ERROR;
}
}
这不是很快!为了获得更快的方法,您需要使用基于Tcl_Obj
的API(Tcl_Obj
是基本的Tcl第一类值类型(,首先正确注册实现函数。之后,转换上述代码相当容易:
int listc;
Tcl_Obj **listv;
if (Tcl_ListObjGetElements(interp, argv[argc-1], &listc, &listv) != TCL_OK) {
// wasn't a valid list!
return TCL_ERROR;
}
std::vector<int> theArray(listc, 0);
for (int i=0 ; i<listc ; i++) {
if (Tcl_GetIntFromObj(interp, listv[i], &theArray[i]) != TCL_OK) {
// wasn't an int in the list!
return TCL_ERROR;
}
}
有什么不同?Tcl_Obj
知道它持有的是字符串还是整数(或浮点或任何其他东西(,因此Tcl运行时通常不需要重新分析或键入转换值,而如果所有东西都是字符串,则需要进行大量转换。(在Tcl中通常说">Everything is a string",但这是不准确的;正确的版本是">Everything has a perfect string serialization,or is a named entity,但这更为冗长。(
相关文章:
- 如何在C++代码上使用 Tcl-C API
- 函数 C++>Tcl 与 Critcl
- 将 Tcl 中的命令输出重定向到文件时,文件以 DOS 格式生成
- Tcl 文件事件挂在 64 位版本的 Tcl 上
- 如何将C 中的后端写入与TCL/TCK用户界面连接
- swig:std/multimap.i似乎打破了TCL包装
- TCL文件无法获取通过代码设置的环境变量
- 使用正在调用TCL脚本的CPP代码的GCC编译
- 使用 TCL 编写嵌入式 SQLite 脚本
- 协议缓冲区测试C++:期望/tcl/tk
- Vera TCL规则:列出所有本地变量
- 需要在 c++ 中从另一个文件调用 tcl proc
- 编译 TCL-DLL 包装器时出现的问题
- TCL高管的程序运行正常
- 为什么并行执行Tcl_ExprDouble的独立Tcl解释器需要互斥
- Swig:如何返回从C++到Tcl的指针列表
- C++应用程序使用TCL API启用程序包自动加载
- 改进make函数的Swig-tcl包装
- 获取在C++中实例化的TCL解释器中执行代码的行号
- Swig(Tcl):我可以在执行c++程序时调用Swig函数吗