0

I have an external Parameter class I need to define as global objects (instances) inside different namespaces. I insert a pointer to each instance of the class to a map.

the problem:

not all parameter names are unique and I need a unique name as key to each instance, can I get the namespace hierarchy of the object name as prefix for his name?

RegisteredGlobalObject.cpp:

RegisteredGlobalObject::RegisteredGlobalObject(const string& uid, int val) {
    m_mapUidToParamPair[uid] = val;
}

ParamsDef.h:

namespace base {
namespace vpeservice {

    extern RegisteredGlobalObject int_64_param;

} // namespace vpeservice
namespace otherservice {

    extern RegisteredGlobalObject int_64_param;

} // namespace otherservice
} // namespace base

ParamsDef.cpp:

namespace base {
namespace vpeservice {

    RegisteredGlobalObject int_64_param(getInstatnceDefenitionPlaceStr()+"int_64_param", 1);

}
namespace otherservice {

    RegisteredGlobalObject int_64_param(getInstatnceDefenitionPlaceStr()+"int_64_param", 2);

}
}

can I implement the function getInstatnceDefenitionPlaceStr() which returns "base::vpeservice" for the first call and "base::otherservice" for the second?

Guy Sadoun
  • 427
  • 6
  • 17
  • Seems not possible according to [this question](https://stackoverflow.com/questions/21776142/how-to-get-string-for-current-namespace-in-macro). I guess you have to manually prepend the namespace strings. – Tobias Jun 12 '22 at 13:39
  • this question refer to compile time, I can get it in runtime if possible. – Guy Sadoun Jun 12 '22 at 13:55
  • How about `__FILE__`/`__LINE__` for a unique string? – HolyBlackCat Jun 12 '22 at 13:59
  • I need some better logic so I can get the key from .json file from other place. this is why I thought define the namespaces with the same hierarchy of the .json file. – Guy Sadoun Jun 12 '22 at 14:02
  • 1
    Perhaps the string returned by typeid(myObj).name() would help? – Jeremy Friesner Jun 12 '22 at 14:06
  • I'll try, thanks. I thought using it only on template types – Guy Sadoun Jun 12 '22 at 14:08
  • returns the same string for different namespaces – Guy Sadoun Jun 12 '22 at 14:10
  • 2
    Please use `@username` when replying, otherwise we don't get notifications. – HolyBlackCat Jun 12 '22 at 14:19
  • @HolyBlackCat will do – Guy Sadoun Jun 12 '22 at 14:21
  • I don't know of a way to get the full name of an object. Some compilers support getting a full name of a function, in the form of `__PRETTY_FUNCTION__` macro. You could arrange for the object to be defined within a function, e.g. `bool RegisterHelper() {static RegisteredGlobalObject int_64_param(ExtractNamepaceFromPrettyFunction(__PRETTY_FUNCTION__)+"int_64_param", 1); return true} bool register_helper = RegisterHelper();` This will be very much non-portable and compiler-dependent. – Igor Tandetnik Jun 12 '22 at 15:26
  • Can you generate the namespace (e.g. with a macro), then you know its name? Or is it fixed? – Sebastian Jun 13 '22 at 08:43

1 Answers1

1

OK, this is my solution to this case. hope it will help someone.

I defined a macro for every internal namespace that will define a function inside the namespace to get current __PRETTY_FUNCTION__ path.

then I used How to get a fully-qualified function name in C++ (gcc) with some modifications (you can see the f(__PRETTY_FUNCTION__,__func__) call inside the macro:

#define PARAMETERS_SECTION(name) namespace name {\
std::string getPath()\
{\
    return f(__PRETTY_FUNCTION__,__func__);\
}\
#define configureParam(type, name, val) RegisteredGlobalObject name(getPath()+#name, val);

then I changed ParamsDef.cpp to be:

namespace base {
PARAMETERS_SECTION(vpeservice) {

    configureParam(int64_t, int_64_param, 1)

}
PARAMETERS_SECTION(otherservice) {

    configureParam(int64_t, int_64_param, 2)

}
}

and got keyes:

base::vpeservice::int_64_param

base::otherservice::int_64_param

Guy Sadoun
  • 427
  • 6
  • 17