CMake: Howto modify a variable from inside a function - function

What is the best practice to modify a caller's variable from inside a CMake-function.
Assume
function(MyFunction IN_OUT_NAME)
... what to do here ...
string(APPEND ${IN_OUT_NAME} " and that")
... what to do here ...
endfunction()
What needs to be done such that the following code fragment
set(MY_MESSAGE "this")
MyFunction(MY_MESSAGE)
message(${MY_MESSAGE})
delivers
this and that
Not-a-duplicate-remarks:
Modify a variable inside a function is about JavaScript not CMake
Is JavaScript a pass-by-reference or pass-by-value language? is about JavaScript not CMake

Just use PARENT_SCOPE to export the value to parent scope.
function(MyFunction IN_OUT_NAME)
string(APPEND ${IN_OUT_NAME} " and that")
set(${IN_OUT_NAME} "${${IN_OUT_NAME}}" PARENT_SCOPE)
endfunction()
set(MY_MESSAGE "this")
MyFunction(MY_MESSAGE)
message(${MY_MESSAGE})

Related

How to find out what cause cmake function does not work? [duplicate]

I would like to create a CMake function as:
function(test src_list dst_list)
# do something
endfunction()
usage:
test(${my_list} chg_list)
It means, my_list is a list with several fields, and chg_list will receive a list modified inside test function.
How can I create a function in CMake to do that?
In CMake, functions have their own scope, and by default, all modification of variables are local, unless you pass CACHE or PARENT_SCOPE as parameter to set. Inside a function, if you want to modify a variable in the scope of the caller, you should use:
set(${dst_list} <something> PARENT_SCOPE)
See documentation:
A function opens a new scope: see set(var PARENT_SCOPE) for details.
Check that inside your function you are set()ing not dst_list but ${dst_list}. You need this to return data to the parent scope.
Here's how I solved a similar problem which was marked a duplicate of this question. This function takes a string BinaryName and adds it to the list OutVariable which is available in the parent scope. If the list is not defined it is created. I use this strategy for creating new test targets and adding the name of these targets to a list of targets in the main scope.
Thanks to #Tsyvarev for the comments that lead me to figure this out.
function(AddToListFromFunction BinaryName OutVariable)
if ("${${OutVariable}}" STREQUAL "")
message(STATUS "1")
set(${OutVariable} ${BinaryName} PARENT_SCOPE)
message(STATUS "OutVariable: ${OutVariable} ${${OutVariable}}")
else ()
message(STATUS "2")
set(${OutVariable} "${${OutVariable}}" "${BinaryName}" PARENT_SCOPE)
endif ()
endfunction()
AddToListFromFunction(MyBinary1 MyTests)
AddToListFromFunction(MyBinary2 MyTests)
message(STATUS "MyTests Variable: ${MyTests}")

Data Bindings across template tags

I'm wondering, is there a possibility to have databindings "out of" a template? Say I have a <template/>-Tag somewhere which I put into the slot of a different component - that component stamps it to its context. Then I want to bind data from the root element to the <template/>-Tag. Also, event bindings (on-x-changed) don't work, because you can't assign a function which is defined in the hosting component. Any ideas?
Example:
... host
{{boundData}}
<binding-component>
<template>
{{boundData}}
</template>
</binding-component>
I don't see changes when I observe boundData in the hosting component. Is there a way to get around this? Or is firing a custom event my only chance?
If you are looking for binding a property outside of polymer something like from index.html you may bind value with element. an example ; index.html
<dom-bind>
<template>
<binding-component bound-data="{{boundData}}"></binding-component>
</template>
</dom-bind>
<script>
// set a value a string, Number or Object etc.
// Optionally wrap this code into a listener ie;
// window.addEventListener('load', e=> { ...below code ... })
var boundData= document.querySelector('dom-bind');
boundData = {} //
</script>
Now in your binding-component element has a property as boundData
hope its helps or provide more code to understand better.
I've made it work the way dom-if does it, too. Like in dom-if (reference), I'm creating a Templatize-instance which then uses forwardHostProp to handle the "inside"-properties
this.__ctor = Templatize.templatize(template, this, {
mutableData: true,
forwardHostProp(prop, value) {
// handling item updates, item being the only property
// from within the binding component
// everything else is automatically bound by templatize
this.set(prop, value);
this.update(this.item);
},
});
this.__instance = new this.__ctor();
this.root.appendChild(this.__instance.root);
This all happens in connectedCallback.
Because the Templatize-instance is passed this, it's bound to the current context as well.
Good luck!

Create Html local variable programmatically with Angular2

I need to know if there is a way to create HTML local variables programmatically.
I am developing a web app where I have an NgFor loop and I want to be able to assign a local variable to each sub element created by the NgFor.
ie :
<div *ngFor="#elt of eltList" >
<span #setLocalVariable(elt.title)></span>
</div>
setLocalVariable(_title : string){
let var = do some stuff to _title;
return var;
}
The exemple above shows you what I am trying to accomplish and obviously does not work.
Is there a way to achieve this ?
Thank you in advance.
Edit:
After seeing the answers I got (and i thank everyone who took the time to read my question and tried to answer it) i'll explain a bit more why i want it that way.
I will be using : loadIntoLocation() from the DynamicComponentLoader.
That function got as a 3rd parameter a string that refers to an anchors (ie : #test in an html element). Thats why i need to create those local variables with a name equal to the one of my elt.title.
I think local variables (defined with the # character) don't apply for your use case.
In fact, when you define a local variable on an HTML element it corresponds to the component if any. When there is no component on the element, the variable refers to the element itself.
Specifying a value for a local variable allows you to select a specific directive associated with the current element. For example:
<input #name="ngForm" ngControl="name" [(ngModel)]="company.name"/>
will set the instance of the ngForm directive associated with the current in the name variable.
So local variables don't target what you want, i.e. setting a value created for the current element of a loop.
If you try to do something like that:
<div *ngFor="#elt of eltList" >
<span #localVariable="elt.title"></span>
{{localVariable}}
</div>
You will have this following error:
Error: Template parse errors:
There is no directive with "exportAs" set to "elt.title" ("
<div *ngFor="#elt of eltList" >
<span [ERROR ->]#localVariable="elt.title"></span>
{{localVariable}}
</div>
"): AppComponent#2:10
Angular2 actually looks for a directive matching the provided name elt.title here)... See this plunkr to reproduce the error: https://plnkr.co/edit/qcMGr9FS7yQD8LbX18uY?p=preview
See this link: http://victorsavkin.com/post/119943127151/angular-2-template-syntax, section "Local variables" for more details.
In addition to the current element of the iteration, ngForm only provides a set of exported values that can be aliased to local variables: index, last, even and odd.
See this link: https://angular.io/docs/ts/latest/api/common/NgFor-directive.html
What you could do is to create a sub component to display elements in the loop. It will accept the current element as parameter and create your "local variable" as attribute of the component. You will be able then to use this attribute in the template of the component so it will be created once per element in the loop. Here is a sample:
#Component({
selector: 'elt',
template: `
<div>{{attr}}</div>
`
})
export class ElementComponent {
#Input() element;
constructor() {
// Your old "localVariable"
this.attr = createAttribute(element.title);
}
createAttribute(_title:string) {
// Do some processing
return somethingFromTitle;
}
}
and the way to use it:
<div *ngFor="#elt of eltList" >
<elt [element]="elt"></elt>
</div>
Edit
After your comment, I think that you try the approach described in this answer. Here are more details: create dynamic anchorName/Components with ComponentResolver and ngFor in Angular2.
Hope it helps you,
Thierry
You could stick it into the template interpolation since it handles expressions.
<div *ngFor="#elt of eltList" >
<span>{{setLocalVariable(#elt)}}</span>
</div>
setLocalVariable(_title : string){
let var = do some stuff to _title;
return var;
}

Creating namespace free Element in xquery

I have created an element as mentioned here XQuery: Create a new element with a given name?.
The code for creating element is below
declare function local:remove-tag-if-empty($elem as element(), $name as xs:string)
{
if(not($elem/node()))
then ()
else
(
element {$name} {$elem/text()}
)
};
Now what I am getting from the generated XML is something like this (I am using the above function to create DOB tag)
<DOB xmlns = "">2012-10-14+03:00</DOB>
I don't want xmlns="" to be the part of the generated xml. Can anyone tell me that how to create element without namespace? Thanks in advance.
Why do you want to create:
<DOB>2012-10-14+03:00</DOB>
It is functionally the same as below:
<DOB xmlns="">2012-10-14+03:00</DOB>

'Global' object in node.js

I am using 0.3.1-pre Node.js
Doing this:
typeof global.parseInt
results in
'undefined'
However when pressing [Tab] in the console after typing 'global.' gives a list of functions, including parseInt.
So is parseInt a member of the global namespace or not?
As of NodeJS v0.8.14 global seems to work across modules like the window object does in the browser.
Test:
a.js:
a1 = console.log; // Will be accessed from b.js
global.a2 = console.log; // Will be accessed from b.js
require('./b.js');
b1('a: b1');
b2('a: b2');
global.b1('a: global.b1');
global.b2('a: global.b2');
b.js:
a1('b: a1');
a2('b: a2');
global.a1('b: global.a1');
global.a2('b: global.a2');
b1 = console.log; // Will be accessed from a.js
global.b2 = console.log; // Will be accessed from a.js
Running a.js outputs:
b: a1
b: a2
b: global.a1
b: global.a2
a: b1
a: b2
a: global.b1
a: global.b2
Apparently, the global object isn't the global object as window is in the browser. It's (according to micheil in #nodejs # freenode) really only used internally. Something about global closures and whatnot.
parseInt and setTimeout and all those buddies are globals on their own. Not part of any visible global object.
Defining variable in app.js without var, just like myvar='someval' makes it visible inside every .js in your project
FAILS:
if( global[ some_object_i_want_to_exist ] ){ ... }
WORKS:
//: outside of all functions, including IIFE.
const THE_GLOBAL_YOU_PROBABLY_WANT_IS_THIS=( this );
//: Within a function:
const G = THE_GLOBAL_YOU_PROBABLY_WANT_IS_THIS;
if( G[ some_object_i_want_to_exist ] ){ ... }
I am assuming you got to this page about "global" in node.js because you wanted the equivalent of "window" in order to check for globally declared variables. bFunc's solution didn't work for me, as it seems to require that one explicitly does something like:
global.some_object_i_want_to_exist = whatever;
as a pre-requisit to using
global[ some_object_i_want_to_exist ]
EDIT: Looking at my code it seems that the only reason my solution worked is because I used "exports.some_object_i_want_to_exist" somewhere else in the
file. Without that, my solution fails. So... I have no clue how to reliable determine if an object exists in a given scope in Node.js.
Here is the documentation on global object:
https://nodejs.org/api/globals.html
I am going to leave my answer here because I hear people are more likely to correct you when you are wrong, so maybe someone will correct me with the answer to the problem.