I have a program with a set of input and a set of out variables that I exchange with an Apache/PHP webpage using XML. The webpage is where users can see what is configured/going on and can change settings.
When I pass the XML to the PHP program (we use Unix_domain Sockets) I add a lot of meta-information about the settings and indicators I’m passing to him. Things like datatype, min, max, default, read/write privileges, etc.
<temperature datatype="INT32" min=-"40" max="150" permissions="R/O">25</temperature>
I need to know the metadata about each of my variables as I build the outbound xml. I though a good way to do this wouldbe to create a class VarInfo which I’d associate to each variable. Insted of storing the variable name as the key in the map (not sure I can always know the variable name) I thought of making the key be the pointer to the variable and the value be the pointer to a VarInfo object for that variable. Only way I know to store mixed pointer types is to cast them all to void*. So, something like:
bool cmpr( void* a, void*b) { return (long)a < (long)b;};
std::map<void*,VarInfo*,cmpr> VarMap;
int temperature;
VarInfo vi_temperature;
VarMap[(void*)&temperature] = &vi_temperature;
Doing this create a bunch of errors about declaring the map.
expected a type, got ‘cmpr’ ACT_iod.cpp
Invalid template arguments ACT_iod.cpp line 40 Semantic Error
invalid type in declaration before ‘;’ token
type/value mismatch at argument 3 in template parameter list for
‘template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map’
So, can someone suggest how I might get this working and probably also suggest a better way to store the metainfo? Still thinking about changing to std::string’s as the key.
Thanks.
The third parameter of the
std::mapshould be the type of the comparator. As one fix you could makecmpra functor (which is often the preferred aproach due to its inlinablility):The other alternative is to use the type of the function pointer (
bool(*)(void*, void*)iirc) and give the pointer to the actually used function as a parameter of the constructor:Edit: now that I think about it: The default comparison for pointers is
<anyways, so you could just usestd::map<void*, VarInfo*>which has the benefit of being more portable. Generally you should avoid casting pointers tolong. Avoid pointer to integral casts when possible, otherwise usingintptr_t,uintptr_torsize_tis a much better idea, sincelongis not guaranteed to be big enough to hold a pointer (and long is indeed 32bit on some 64bit platforms).