c++ - 如何将整数数组从 Tcl 传递到 C++?

标签 c++ arrays tcl ns2

<分区>

如何将 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

ITLEACH.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

但是它不起作用,请帮助我。

最佳答案

如果您将该命令绑定(bind)到字符串接口(interface)(即,参数通过 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 知道它保存的是字符串还是整数(或 float 或任何数量的其他东西),因此 Tcl 运行时通常不需要重新解析或类型转换值,而如果一切都是一个字符串,你做了很多转换。 (在 Tcl 中常说“Everything is a string”,但这是不准确的;正确的说法是“Everything 都有一个完美的字符串序列化,或者是一个命名实体”但这更冗长。)

关于c++ - 如何将整数数组从 Tcl 传递到 C++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19751216/

相关文章:

javascript - 如何观察对象数组的变化

C++:根据模板参数填充数组

c++ - 从 Turbo C++(旧)切换到较新的类似编译器

c++ - "Cannot overload functions distinguished by return type alone"是什么意思?

c++ - 使用 <random> 将随机数生成限制在一个范围内

if-statement - TCL 代码的 "else"命令中 "if"子句后的额外字

sqlite - 将字符串分成相等长度的段的有效方法?

c++ - 回调的缺陷

c - 数组的C数组

scripting - 期望-根据行和列从屏幕区域获取变量