Using a variable as a method name

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Using a variable as a method name

LaViolette, Alan

Hello

 

I would like to call a method based on a variable name such as

 

set( FOO “123”)

BAR_${FOO}(arg1 arg2)

 

would do the same as

 

BAR_123(arg1 arg2)

 

Does any way exist to do this in CMake, such as a call() or eval() command?

 

 

--

Alan LaViolette

Software Engineering Consultant

OVERWATCH

An Operating Unit of Textron Systems

21660 Ridgetop Circle, Suite 110

Sterling, VA 20166

Office: (703) 437-7651 x 2419

Fax: (703) 437-0039

[hidden email]

www.overwatch.com

 

"WARNING: Documents that can be viewed, printed or retrieved from this E-Mail may contain technical data whose export is restricted by the Arms Export Control Act (Title 22, U.S.C., Sec 2751, et seq,) or the Export Administration Act of 1979, as amended, Title 50, U.S.C., App. 2401 et seq. and which may not be exported, released or disclosed to non-U.S. persons (i.e. persons who are not U.S. citizens or lawful permanent residents [“green card” holders]) inside or outside the United States, without first obtaining an export license.  Violations of these export laws are subject to severe civil, criminal and administrative penalties."

 


--

Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Follow this link to subscribe/unsubscribe:
http://www.cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Using a variable as a method name

David Cole
Not directly, but as a workaround, you could write that into a file, and then include the file.

i.e.:

file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/tmp.cmake" "BAR_${FOO}(arg1 arg2)")
include(${CMAKE_CURRENT_BINARY_DIR}/tmp.cmake)

You may need to do some quoting and/or escaping to get things working depending on how complex arg1 and arg2 are in reality.

This sort of "eval" functionality has been requested (and denied ;-) before:

HTH,
David


On Fri, Jun 1, 2012 at 11:02 AM, LaViolette, Alan <[hidden email]> wrote:

Hello

 

I would like to call a method based on a variable name such as

 

set( FOO “123”)

BAR_${FOO}(arg1 arg2)

 

would do the same as

 

BAR_123(arg1 arg2)

 

Does any way exist to do this in CMake, such as a call() or eval() command?

 

 

--

Alan LaViolette

Software Engineering Consultant

OVERWATCH

An Operating Unit of Textron Systems

21660 Ridgetop Circle, Suite 110

Sterling, VA 20166

Office: <a href="tel:%28703%29%20437-7651%20x%202419" value="+17034377651" target="_blank">(703) 437-7651 x 2419

Fax: <a href="tel:%28703%29%20437-0039" value="+17034370039" target="_blank">(703) 437-0039

[hidden email]

www.overwatch.com

 

"WARNING: Documents that can be viewed, printed or retrieved from this E-Mail may contain technical data whose export is restricted by the Arms Export Control Act (Title 22, U.S.C., Sec 2751, et seq,) or the Export Administration Act of 1979, as amended, Title 50, U.S.C., App. 2401 et seq. and which may not be exported, released or disclosed to non-U.S. persons (i.e. persons who are not U.S. citizens or lawful permanent residents [“green card” holders]) inside or outside the United States, without first obtaining an export license.  Violations of these export laws are subject to severe civil, criminal and administrative penalties."

 


--

Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Follow this link to subscribe/unsubscribe:
http://www.cmake.org/mailman/listinfo/cmake


--

Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Follow this link to subscribe/unsubscribe:
http://www.cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Using a variable as a method name

LaViolette, Alan

Thanks that works great.  I just wrote an eval macro that does that

 

From: David Cole [mailto:[hidden email]]
Sent: Friday, June 01, 2012 11:22 AM
To: LaViolette, Alan
Cc: [hidden email]
Subject: Re: [CMake] Using a variable as a method name

 

Not directly, but as a workaround, you could write that into a file, and then include the file.

 

i.e.:

 

file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/tmp.cmake" "BAR_${FOO}(arg1 arg2)")

include(${CMAKE_CURRENT_BINARY_DIR}/tmp.cmake)

 

You may need to do some quoting and/or escaping to get things working depending on how complex arg1 and arg2 are in reality.

 

This sort of "eval" functionality has been requested (and denied ;-) before:

 

HTH,

David

 

 

On Fri, Jun 1, 2012 at 11:02 AM, LaViolette, Alan <[hidden email]> wrote:

Hello

 

I would like to call a method based on a variable name such as

 

set( FOO “123”)

BAR_${FOO}(arg1 arg2)

 

would do the same as

 

BAR_123(arg1 arg2)

 

Does any way exist to do this in CMake, such as a call() or eval() command?

 

 

--

Alan LaViolette

Software Engineering Consultant

OVERWATCH

An Operating Unit of Textron Systems

21660 Ridgetop Circle, Suite 110

Sterling, VA 20166

Office: <a href="tel:%28703%29%20437-7651%20x%202419" target="_blank">(703) 437-7651 x 2419

Fax: <a href="tel:%28703%29%20437-0039" target="_blank">(703) 437-0039

[hidden email]

www.overwatch.com

 

"WARNING: Documents that can be viewed, printed or retrieved from this E-Mail may contain technical data whose export is restricted by the Arms Export Control Act (Title 22, U.S.C., Sec 2751, et seq,) or the Export Administration Act of 1979, as amended, Title 50, U.S.C., App. 2401 et seq. and which may not be exported, released or disclosed to non-U.S. persons (i.e. persons who are not U.S. citizens or lawful permanent residents [“green card” holders]) inside or outside the United States, without first obtaining an export license.  Violations of these export laws are subject to severe civil, criminal and administrative penalties."

 


--

Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Follow this link to subscribe/unsubscribe:
http://www.cmake.org/mailman/listinfo/cmake

 


--

Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Follow this link to subscribe/unsubscribe:
http://www.cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Using a variable as a method name

Eric Noulard
In reply to this post by LaViolette, Alan
2012/6/1 LaViolette, Alan <[hidden email]>:

> Hello
>
> I would like to call a method based on a variable name such as
>
> set( FOO “123”)
> BAR_${FOO}(arg1 arg2)
>
> would do the same as
>
> BAR_123(arg1 arg2)
>
> Does any way exist to do this in CMake, such as a call() or eval() command?

No there is no such thing, however you can create a cmake script file
and then use something like
"cmake -DFOO=123 -P yourscript.cmake"
 to launch a "second" CMake interpreter that will do this evaluation.

This is kind of heavy weight but should work.

You may use
execute_process to launch it at CMake-time
add_custom_command to launch it at Build time.


> "WARNING: Documents that can be viewed, printed or retrieved from this
> E-Mail may contain technical data whose export is restricted by the Arms
> Export Control Act (Title 22, U.S.C., Sec 2751, et seq,) or the Export
> Administration Act of 1979, as amended, Title 50, U.S.C., App. 2401 et seq.
> and which may not be exported, released or disclosed to non-U.S. persons
> (i.e. persons who are not U.S. citizens or lawful permanent residents
> [“green card” holders]) inside or outside the United States, without first
> obtaining an export license.  Violations of these export laws are subject to
> severe civil, criminal and administrative penalties."

I'm not sure such footer is appropriate on a pubic, archived and
international mailing list...



--
Erk
Le gouvernement représentatif n'est pas la démocratie --
http://www.le-message.org
--

Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Follow this link to subscribe/unsubscribe:
http://www.cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Using a variable as a method name

Damien R
In reply to this post by David Cole
On 1 June 2012 17:22, David Cole <[hidden email]> wrote:
Not directly, but as a workaround, you could write that into a file, and then include the file.

i.e.:

file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/tmp.cmake" "BAR_${FOO}(arg1 arg2)")
include(${CMAKE_CURRENT_BINARY_DIR}/tmp.cmake)

Hi, there is also a variation based on the workaround given by David:

suppose you want to call:
bar_${foo1}(arg1 arg2)
bar_${foo2}(arg1 arg2)

you can create 2 files:
- bar1.cmake
macro(bar)
...
endmacro()

- bar2.cmake
macro(bar)
...
endmacro()

After that you can replace:
bar_${foo1}(arg1 arg2)
bar_${foo2}(arg1 arg2)
by:
include(${foo1}.cmake)
bar(...)
include(${foo2}.cmake)
bar(...)

where:
${foo1} = bar1
${foo2} = bar2

Regards,
Damien R.

--

Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Follow this link to subscribe/unsubscribe:
http://www.cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Using a variable as a method name

Damien R
On 2 June 2012 12:16, Damien R <[hidden email]> wrote:
Hi, there is also a variation based on the workaround given by David:
 ...

Sorry, this workaround was already found in the bug mentioned by David in his last post (http://public.kitware.com/Bug/view.php?id=4034).
The problem is that the title of this bug make it difficult to find it in a search engine ...

Regards,
Damien R.

--

Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Follow this link to subscribe/unsubscribe:
http://www.cmake.org/mailman/listinfo/cmake