howto share a list between C++ library and appl

P

pvbolhuis

I'm creating a shared library that provides some basic system facilities.

Suppose a global list of "system processes" needs to be shared between the
application and the library. And suppose I want this list completely
filled very early on (before main() starts).

I can do as follows:

#>cat -n lib.h
1 #include <map>
2 #include "myClass.h"
3
4 extern std::map<const char *, myClass *> processMap_;
5
6 int registerProcess(const char *, int, int, int);
7
8 #define SYSTEM_PROCESS(name, ip, pc, prio) \
9 static int dummy##ip##pc##prio = registerProcess(name, ip, pc, prio);
10

#>cat -n lib.c
1 #include <map>
2 #include "lib.h"
3
4 std::map<const char *, myClass *> processMap_;
5
6 int registerProcess(const char *name, int pi, int pc, int prio)
7 {
8 processMap_[name] = new myClass(name, pi, pc, prio);
9 }
10
11 // Own (LIB) proces (which LIB needs itself)
12 SYSTEM_PROCESS("LIB", 1, 11, 111);

#>cat -n appl.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include "lib.h"
4
5 // global scope
6 SYSTEM_PROCESS("AAAA", 2, 3, 4);
7 SYSTEM_PROCESS("BBB", 7, 8, 9);
8
9 int main(void)
10 {
11 // main scope works too, but better do it in global scope
12 SYSTEM_PROCESS("CCC", 5, 6, 7);
13
14 printf("number of system processes = %d\n", processMap_.size());
15 printf("sleep...\n");
16 sleep(3000);
17 printf("done...\n");
18 }

This works fine.
I assume any additional application has its own processMap_ (with "LIB" process in it and its
own "system processes"). This is fine.

My question is: what's the downside of this (apart from the extra required dummy int variable) ?

Can anyone think of a better way ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top