c++ - Is there a way to get Common/templated functionality with different function names? -
first of all, wasn't sure name question, hope it's enough.
essentially, have whole bunch of functions have common functionality differ types. sounds templates yeah? here catch: each function specific enough name each function differently.
for example, @ following code:
bool getfriendslist(friendslistrequestdata::callbacktype callback) { check(callback != nullptr); friendslistrequest* request = new friendslistrequest(this, getnewrequestid()); friendslistrequestdata* data = new friendslistrequestdata(request, callback); return storeandperformrequest(data); } bool getaccountinfo(accountinforequestdata::callbacktype callback) { check(callback != nullptr); accountinforequest* request = new accountinforequest(this, getnewrequestid()); accountinforequestdata* data = new accountinforequestdata(request, callback); return storeandperformrequest(data); } // many more routines...
the 2 functions identical. differ types , function names. templatize functions, have same name. implemented following macros, don't how unreadable make code:
#define implement_request_function(name, requesttype, requestdatatype) \ bool name(requestdatatype::callbacktype callback) \ { \ check(callback != nullptr); \ requesttype* request = new requesttype(this, getnewrequestid()); \ requestdatatype* data = new requestdatatype(request, callback); \ return storeandperformrequest(data); \ } class foo { public: // other stuff... implement_request_function(getfriendslist, friendslistrequest, friendslistrequestdata) implement_request_function(getaccountinfo, accountinforequest, accountinforequestdata) // other stuff... };
i macro better adding functions on , on in both class , source, is there way templated functionality while naming resulting functions differently wouldn't have use macro (or possibly use more friendly macro)?
thank you.
edit: may have put background information in , skewed asking about. essentially, i'm wondering if can functionality of above macro somehow without macro. i'm trying keep function names different though have same implementation.
what about:
template <typename r, typename d> bool get(typename d::callbacktype callback) { check(callback != nullptr); r* request = new r(this, getnewrequestid()); d* data = new d(request, callback); return storeandperformrequest(data); } inline bool getfriendslist(friendslistrequestdata::callbacktype callback) { return get<friendslistrequest, friendslistrequestdata>(callback); } inline bool getaccountinfo(accountinforequestdata::callbacktype callback) { return get<accountinforequest, accountinforequestdata>(callback); }
note may need templatize storeandperformrequest()
well.
Comments
Post a Comment