How to declare an array with macros in Lauterbach CMM script - trace32

I would like to create an array with Macros in Lauterbach CMM script.
I tried the following:
LOCAL &ARRAY_VAR[10]
This syntax doesn't work.
I know how to create array with Var.NEWLOCAL:
Var.NEWLOCAL int[3] \ARRAY_VAR
Is there any way to create macros with array?

Short answer: PRACTICE does not support macro arrays.
Longer answer: PRACTICE can do a recursive macro expansion, which allows you to do something similar than real arrays.
(See "PRACTICE Macros" in practice_user.pdf)
E.g.:
LOCAL &myarr0 &myarr1 &myarr2 &myarr3
LOCAL &data &i
&data="zero|one|two|three"
// Assign values to &myarra0 to &myarra3 in a loop
&i=0.
WHILE &i<4.
(
PRIVATE &cmd
&cmd="&"+"myarr"+FORMAT.Decimal(1,&i)+"=STRing.SPLIT(""&"+"data"",""|"",&i)"
&&cmd // << evaluate with recursive macro expansion
&i=&i+1.
)
// Print values saved in &myarra0 to &myarra3 in a loop
&i=0.
WHILE &i<4.
(
PRIVATE &val
&val="&"+"myarr"+FORMAT.Decimal(1,&i)
&&val="&val" // << assign with recursive macro expansion
ECHO "&val"
&i=&i+1.
)
If you don't care of having too many GLOBAL macros you can also create the pseudo-array in a loop like this:
LOCAL &i
&i=0
WHILE &i<4.
(
PRIVATE &cmd
&cmd="GLOBAL "+"&"+"myarr"+FORMAT.Decimal(1,&i)
&cmd
&i=&i+1
)
I wouldn't do that because I don't like GLOBAL macros.
Workaround: Create an array with Var.NEWLOCAL
E.g.:
Var.NEWLOCAL char[4][32] \myarr
LOCAL &i &data
&data="zero|one|two|three"
// Assign values to \myarr in a loop
&i=0.
WHILE &i<4
(
PRIVATE &val
&val=STRing.SPLIT("&data","|",&i)
Var.Assign \myarr[&i]="&val"
&i=&i+1.
)
// Print values saved in \myarr in a loop
&i=0.
WHILE &i<4
(
ECHO Var.STRing(\myarr[&i])
&i=&i+1.
)

Related

Iterating C array-type container class from Lua using LuaBridge

This may be a newbie question, but I have not been able to find an answer with web searching that will even help me get started. I have a container class that at heart is a C-style array. For simplicity, let's depict it as this:
int *myArray = new int[mySize];
With LuaBridge we can assume I have successfully registered it as my_array in the global namespace. I would like to iterate over it from Lua like this:
for n in each(my_array) do
... -- do something with n
end
I'm guessing I probably need to register a function each in the global namespace. The problem is, I don't know what that function should look like in C++.
<return-type> DoForEach (<function-signature that includes luabridge::LuaRef>)
{
// execute callback using luabridge::LuaRef, which I think I know how to do
return <return-type>; //what do I return here?
}
This would perhaps have been easier if the code had used std::vector but I am trying to create a Lua interface to an existing code base that is complicated to change.
I'm answering my own question, because I have figured out that the question makes some incorrect assumptions. The existing code I was working with was a true iterator class (which is what it is called in the Lua docs) implemented in c++. These cannot be used with for loops, but that's how you get a callback function in c++.
To do what I was originally asking, we'll assume we've made myArray available as table my_array in lua using LuaBridge or whichever interface you prefer. (This may require a wrapper class.) You implement what I was asking entirely in Lua as follows. (This is almost exactly an example in the Lua documentation, but somehow I missed it earlier.)
function each (t)
local i = 0
local n = table.getn(t)
return function ()
i = i + 1
if i <= n then return t[i] end
end
end
--my_array is a table linked to C++ myArray
--this can be done with a wrapper class if necessary
for n in each(my_array) do
... -- do something with n
end
If you want to provide the each function to every script you run, you add it directly from C++ as follows, before executing the script.
luaL_dostring(l,
"function each (t)" "\n"
"local i = 0" "\n"
"local n = table.getn(t)" "\n"
"return function ()" "\n"
" i = i + 1" "\n"
" if i <= n then return t[i] end" "\n"
"end" "\n"
"end"
);

Find a value within array of structures

this might have been asked before, but didn't quite find... or what I found was old.
I have an array of structures like so:
Isn't there a CFML function that will allow me to check if "test#email.com" already exists under any of the structures "toAddress" ? I can obviously loop my array, check and break if found, but wondering if something already exists for this ?
Thank you.
Pat
You can use arrayFind() with a callback function. Using your data structure above and assume the array is named myArray
if(
!arrayFind( myArray, function( item ){
return item.toAddress == 'test#email.com'
})
){
// do stuff if address is not used.
}

How to treated pair integer as separated variable?

Is there any way to separate paired integer?
first i declare queue in a way:
typedef pair<int,int>pr;
queue<pr>que;
i can easily push separate variable in it. e.g.
que.push(make_pair(c,p));
now when i take value from queue. i have to take in any paired variable like myp.
pair<int , int> myp = que.front();
Now, is there any way to take value in two separate variable from myp or directly take value in separate variable from queue?
is there any way to take value in two separate variable from myp
Yes:
auto [c, p] = que.front();
Those are called Structured Bindings and have been part of the language since C++17.
is there any way in C++98?
Yes. If you take a look at the documentation of std::pair, you'll find that it has two members, first, and second.
int a = myp.first;
int b = myp.second;

PyTuple_Pack which takes in variable number of arguments

I am on Python 2.6.6 & calling python from c++ .
How can i write PyTuple_Pack which takes in variable number of arguments .
I have variable arguments coming which I use a for loop and populate my array . My array has max of 1000 elements but i populate only top no_of_argument elements using
python_obj[i] = PyString_FromString(arg[i])
I DO NOT want to write code like
if(no_of arg ==1 )
return PyTuple_Pack(1, python_obj[0]);
if(no_of arg ==2 )
return PyTuple_Pack(2, python_obj[0],python_obj[1]);
I want something like
return PyTuple_Pack(no_of_args, followed by my array where pyobjects exist ;
On net i can see http://docs.python.org/release/2.4.4/whatsnew/node14.html
A new function, PyTuple_Pack(N, obj1, obj2, ..., objN), constructs tuples from a variable length argument list of Python objects. (Contributed by Raymond Hettinger.)
However example to write good code using above is not found by my search for which i need your help pls..
Use PyTuple_New to make a tuple with the right size, then iterate over your array and use PyTuple_SetItem to fill the tuple with values.

set variable depending on other variable

header1.h
#define MaxNum 10
#define MinNum 1
//similar 100 other variables defined
main.cpp
#include header1.h
main()
{
int size;
string first = "MaxNum"; //could be any of the variable name defined in header file
size = MaxNum ;
I have certain variables defined in header file.
In main, depending on the value of "first" i need to set the value of "size". Is this do-able ?
I guess you want to have
size = MaxNum
if first == "MaxNum" and
size = MinNum
if first == "MinNum". If the set of possible values for first (the set of variables to choose from) is only small, you can simply put the assignment around an if, else if series of statements. Finally, put an else statement to write an error message.
However, you have to hardcode every case:
if (first == "MaxNum") {
size = MaxNum;
}
else if (first == "MinNum") {
size = MinNum;
}
//...
So your code to decide the assignment to size becomes larger as the number of variable grows. This is considered bad style and very unmaintainable as well as error-prone.
If you don't want to do this, don't use several variables in your program, but one container variable containing all these possible keys with their values. It's called associate container, and in C++ there is the type std::map which implements such a data structure.
// Your container for the sizes (or whatever you want to call it)
std::map<std::string, int> sizes;
sizes["MinNum"] = 1;
sizes["MaxNum"] = 100;
// ... more values ...
// Accessing this container with a variable as the key:
size = sizes[first];
As you can see, accessing this container using a string variable as the key is very easy. sizes[first] simply gives you the value in the container of which the key equals the value of the current contents of the variable first.
A very important fact about C++ (and C) source code is that during runtime you don't have access to the names of variables. So essentially, they can be renamed without affecting your program. What you want to have (querying names of variables, enums, classes, functions, their parameters, etc.) is known as introspection or meta-programming, as you write code which operates on your code ("meta-code").
C++ doesn't provide meta-programming facilities per default (only exception I know of: typeid, but nothing for variable names / defines). By default means, that you could hack around this limitation and include some additional step in your build process which parses your header file for these definitions and stores them in a different way accessible during runtime. But the map is the better way, believe me.