[CMake] Paired values in an iterable data structure like map or tuple

Alexander Neundorf a.neundorf-work at gmx.net
Wed Jul 25 15:53:08 EDT 2012


On Friday 13 July 2012, Ateljevich, Eli wrote:
> Hi,
> I have a series of tests I would like to perform, some of which are serial
> and some of which are mpi and should use np processors.
> 
> I would further prefer to be able to process this as a list of paired
> values. The following is nothing but pseudocode, but it attempts to convey
> the idea. The ability to pair attributes (either as tuples or as some sort
> of map or dictionary) is something I would love to learn to emulate. Am I
> missing an easy way to do this? Thanks -E
> 
> foreach(testname (test_serial_1,serial,na),
>                                   test_mpi_1,mpi,4),
>                                   test_mpi_2,mpi,2)      )
> maketest(${testname}[0], ${testname}[1],${testname}[2])
> endforeach()

A map in C++:

std::map<std::string, std::string> m;
std::string key;
key = "foo";
m[key] = "CMake";
key = "bar";
m[key] = "Rules";
key = "blub";
m[key] = "!";

if (m.find("blah") == m.end())
  printf("blah not found in map !");

std::string s = m["blub"];

The same in CMake:

set(key "foo")
set(FOO_${key} "CMake")

set(key "bar")
set(FOO_${key} "Rules")

set(key "blub")
set(FOO_${key} "!")

if(FOO_blah)
  message(STATUS "blah not found in map !")
endif()

set(s ${FOO_blub})



A list of structs in C++
struct Foo
{
  std::string s1;
  std::string s2;
};


std::vector<Foo> v;

Foo f;
f.s1="Alex";
f.s2 = "Neundorf";
v.push_back(f);

f.s1="Bill";
f.s2 = "Hoffman";
v.push_back(f);


for(std::vector<Foo>::const_iterator it = v.begin(); it!=v.end(); ++it)
{
  printf("s1: %s s2: %s\n", it->s1.c_str(), it->s2.c_str());
}


In CMake I can think of two ways to do this.
Once by using two lists in parallel

list(APPEND v_s1 "Alex")
list(APPEND v_s2 "Neundorf")

list(APPEND v_s1 "Bill")
list(APPEND v_s2 "Hoffman")

list(LENGTH v_s1 count)
math(EXPR count "${count}-1")
foreach(i RANGE ${count})
  list(GET v_s1 ${i} s1)
  list(GET v_s2 ${i} s2)
  message(STATUS "s1: ${s1} s2: ${s2}")
endforeach()

and alternatively again using specially named variables (as for the map, but 
using the index as suffix for the name), but this may be a bit ugly.

Alex
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20120725/ecdf3981/attachment.htm>


More information about the CMake mailing list