v8: Array of objects - c++

I'm transforming a parser for v8 in NodeJS. Currently I have the following structure
struct Node {
short tag;
std::string data;
Node(std::string input, short tagId) {
tag = tagId;
data = input;
}
};
std::vector<Node> elems;
And I'm populating the vector from loop like this:
elems.push_back(Node(STRING, 3));
My goal is return a javascript object like this:
[
{ tag: 2, data: "asdsad" },
{ tag: 2, data: "asdsad" },
{ tag: 2, data: "asdsad" }
]
But since the V8 documentation is crappy, I couldn't figure out how to do it. My best shot was to make
Local<Value> data[2] = {
Local<Value>::New(Integer::New(2)),
String::New("test")
};
but I can't figure out how to make it an array and return it.
I'm using this example as template.

Here's what you might try (node v0.10.x):
// in globals
static Persistent<String> data_symbol;
static Persistent<String> tag_symbol;
// in addon initialization function
data_symbol = NODE_PSYMBOL("data");
tag_symbol = NODE_PSYMBOL("tag");
// in some function somewhere
HandleScope scope;
Local<Array> nodes = Array::New();
for (unsigned int i = 0; i < elems.length; ++i) {
HandleScope scope;
Local<Object> node_obj = Object::New();
node_obj->Set(data_symbol, String::New(elems[i].data.c_str()));
node_obj->Set(tag_symbol, Integer::New(elems[i].tag));
nodes->Set(i, node_obj);
}
return scope.Close(nodes);

I was looking for a solution for Node 5, and it seems mscdex's answer is outdated now. Hope this helps someone.
HandleScope scope(isolate);
Local<Array> nodes = Array::New(isolate);
for (unsigned int i = 0; i < elems.length; ++i) {
HandleScope scope(isolate);
Local<Object> node = Object::New(isolate);
node->Set(String::NewFromUtf8(isolate, "data"), String::New(isolate, elems[i].data.c_str()));
node->Set(String::NewFromUtf8(isolate, "tag"), Integer::New(isolate, elems[i].tag));
nodes->Set(i, node);
}
args.GetReturnValue().Set(nodes);

Related

How to handle a map argument in a nodejs C++ module

I have a C++ module for nodejs. I need to accept a key/value pair as argument for a method.
var my_map = {'key1': 'value1','key2': 'value2'};
Not sure what to do after this:
void MyClient::AcceptData(const FunctionCallbackInfo<Value>& args)
{
Isolate* isolate = args.GetIsolate();
if (args.Length() != 1)
{
isolate->ThrowException(v8::Exception::TypeError(String::NewFromUtf8(isolate,
"Usage: Key/Value Map")));
return;
}
// It returns true for args[0]->isObject(), but not for args[0]->IsMap()
// Now what? How do I get a C++ map out of args[0] ?
// What do I cast it into?
}
If you are sure it is a Map object, you can retrieve it through this code:
Handle<Map> map = Handle<Map>::cast(args[0]);
And then use the properties and attributes of the map.
Hope this helps.
Ok, I found the answer...
v8::Local<v8::Object> obj = args[0]->ToObject();
v8::Local<v8::Array> props = obj->GetPropertyNames();
std::map<std::string, std::string> theMap;
for (unsigned int i = 0; i < props->Length(); i++)
{
char key[1000], value[1000];
props->Get(i)->ToString()->WriteUtf8(key, 1000);
obj->Get(props->Get(i))->ToString()->WriteUtf8(value, 1000);
theMap.insert(std::make_pair(key, value));
}

How to export array data through node addon

I'm using node 0.12.x, I want to return some array data from node addon written by c++
Isolate* isolate = args.GetIsolate();
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
obj->value_ += 1;
args.GetReturnValue().Set(Number::New(isolate, obj->value_));
This is a sample for returning Number data.
using namespace v8;
Create an array:
Local<Array> myArray = Array::New(isolate);
You can then create objects with properties (or just integers) and push them into the array:
for (int i = 0; i < n; i++) {
Local<Object> obj = Object::New(isolate);
obj->Set(String::NewFromUtf8(isolate, "tag1"), "test");
myArray->Set(i, obj);
}
args.GetReturnValue().Set(myArray);
If you're writing native code for node.js I highly recommend using nan:
https://github.com/nodejs/nan

tinyxml parsing xml file

I have a xml file like this:
<?xml version="1.0"?>
<ApplicationSettings>
<BeamGeometry
Dimension="2"
Type="fan"
Shape="arc"
LengthFocalPointToISOCenter="558"
LengthISOCenterToDetector="394"
LengthDetectorSeperation="0.98"
LengthModuleSeperation="0.04"
NumberModules="57"
NumberDetectorsPerModule="16"
NumberISOCenterShift="3.25" />
</ApplicationSettings>
And I'd like to use tinyxml retrieving all the values (such as 558) based on the entry name such as (LengthFocalPointToISOCenter). Here is my code, not successful yet.
int SetFanbeamGeometry(const char* filename)
{
int ret = TRUE;
TiXmlDocument doc("E:\\Projects\\iterativeRecon\\ProjectPackage\\ApplicationSettings\\ApplicationSettings.xml");
int LengthFocalPointToISOCenter;
if( doc.LoadFile())
{
TiXmlHandle hDoc(&doc);
TiXmlElement *pRoot, *pParm;
pRoot = doc.FirstChildElement("ApplicationSettings");
if(pRoot)
{
pParm = pRoot->FirstChildElement("BeamGeometry");
int i = 0; // for sorting the entries
while(pParm)
{
pParm = pParm->NextSiblingElement("BeamGeometry");
i++;
}
}
}
else
{
printf("Warning: ApplicationSettings is not loaded!");
ret = FALSE;
}
return ret;
}
I am wondering how can I use tinyxml to do that? Sorry I am a first time user. and it looks confusing to me. Thanks.
There's only one BeamGeometry child element in the snippet you've shown; the information you're trying to access are its attributes - they're not individual elements.
So you need something like this:
// ...
pParm = pRoot->FirstChildElement("BeamGeometry");
if (pParm)
{
const char* pAttr = pParm->Attribute("LengthFocalPointToISOCenter");
if (pAttr)
{
int iLengthFocalPointToISOCenter = strtoul(pAttr, NULL, 10);
// do something with the value
}
}
If you want to loop through all attributes, it's quite simple:
const TiXmlAttribute* pAttr = pParm->FirstAttribute();
while (pAttr)
{
const char* name = pAttr->Name(); // attribute name
const char* value = pAttr->Value(); // attribute value
// do something
pAttr = pAttr->Next();
}

Making an array for a static casted indexed array

I don't know if my title is right but I am trying to eliminate duplicate so I think I should put this definitions in an array. Can someone suggest me how I could put the pButtons in array? I am thinking something like pButton[EButtonHost], pButton[EButtonUsername] etc.
#define pButtonHost static_cast<XQtMultipleStringInputButton*>(m_pButtonList[EButtonHost])
#define pButtonUsername static_cast<XQtMultipleStringInputButton*>(m_pButtonList[EButtonUsername])
#define pButtonPassword static_cast<XQtMultipleStringInputButton*>(m_pButtonList[EButtonPassword])
I have a method below like this.
XIniFile readIniFile;
readIniFile.open(k_systemIniFile, EIniReadOnly);
string data;
readIniFile.readString("Server", "host", data);
pButtonHost->setString(data);
m_host = pButtonHost->getString();
readIniFile.readString("Server", "username", data);
pButtonUsername->setString(data);
m_username = pButtonUsername->getString();
readIniFile.readString("Server", "password", data);
pButtonPassword->setString(data);
m_password = pButtonPassword->getString();
They look like duplicates so I am trying to optimize it. Thanks!
Update:
I have something like this now. Would this be right? or do you have any better suggestions?
for (int i = 0; i < 3; ++i) {
readIniFile.readString("Server", k_dataList[i], data);
static_cast<XQtMultipleStringInputButton*>(m_pButtonList[i])->setString(data);
m_pData[i] = static_cast<XQtMultipleStringInputButton*>(m_pButtonList[i])->getString();
}
A below code looks clear by using auto and `lambda',
auto GetConfigInfo = [&](string section_name, ButtonType btn_type)-> string
{
readIniFile.readString("Server", section_name, data);
m_pButtonList[btn_type_]->setString(data);
return m_pButtonList->getString();
};
m_host = GetConfigInfo("host", EButtonHost);
m_username = GetConfigInfo("username", EButtonUserName);
m_password = GetConfigInfo("password", EButtonPassword);
and a define data type by using struct can be other method.
struct ConfigDefine
{
string section_;
ButtonType btn_type_; //your enum button type, such as EButtonHost, EButtonUserName
string& result_;
}configs[] =
{
{"host", EButtonHost, m_host},
{"username", EButtonUserName, m_username},
{"password", EButtonPassword, m_password}
};
for_each(std::begin(configs), std::end(configs), [&](ConfigDefine& config)
{
readIniFile.readString("Server", config.section_, data);
m_pButtonList[config.btn_type_]->setString(data);
config.result_ = pButtonHost->getString();
});
Do you intend to have something like this one?
#define ASIZE 3
int indx[ASIZE] = {EButtonHost, EButtonUsername, EButtonPassword};
string s[ASIZE] = {"host", "username", "password"};
string *m[ASIZE] = {&m_host, &m_username, &m_password};
for (int i = 0; i < ASIZE; i++) {
readIniFile.readString("Server", s[i].c_str(), data);
pButtonHost->setString(data);
*m[i] = pButtonHost->getString();
}

Is it possible to create a std::map of *CCAnimation objects?

They seem to all get autoreleased the moment I create them =s
void SceneView::createAnimation(KillerRabbit* killerRabbit, std::string animation) {
CCArray* animFrames = CCArray::createWithCapacity(15);
int first = std::stoi(killerRabbit->spriteSheetMap[animation]["FIRST"]);
int last = std::stoi(killerRabbit->spriteSheetMap[animation]["LAST"]);
char str[100] = {0};
for (int i = first; i <= last; i++) {
// Obtain frames by alias name
sprintf(str, (killerRabbit->spriteSheetMap[animation]["KEY"]+"[%d].png").c_str(), i);
CCSpriteFrame* frame = sharedSpriteFrameCache->spriteFrameByName(str);
animFrames->addObject(frame);
}
spriteAnimationsMap[killerRabbit->spriteName][animation] = CCAnimation::createWithSpriteFrames(animFrames, 0.1f);
// 14 frames * 1sec = 14 seconds
rabbitSprites[killerRabbit->spriteName][animation]->
runAction(CCRepeatForever::create(CCAnimate::create(spriteAnimationsMap[killerRabbit->spriteName][animation])));
}
If I omit this part of the code:
rabbitSprites[killerRabbit->spriteName][animation]->
runAction(CCRepeatForever::create(CCAnimate::create(spriteAnimationsMap[killerRabbit->spriteName][animation])));
And try to access the object in:
spriteAnimationsMap[killerRabbit->spriteName][animation]
In a later part of the code with another method, the object inside that map would have been autoreleased, how can I retain it so I can use the different animations stored in it at a later time?
Oh, silly me, I had to do this:
spriteAnimationsMap[killerRabbit->spriteName][animation]->retain();