callback - C++ Java Handler equivilent -


i'm coming java/c# background , new c++. writing library , trying accomplish same functionality handler in java. scenario follows:

i building winrt library user put mobile/desktop app. @ point, after network communication happens, piece of data come in want pass along user. in java, declare public static handler (but not initialize it) user can initialize. when data comes in, send message through handler, , end user's declaration within app gets called, receiving message , grabbing data it.

i can't seem find way same in c++. have looked @ kinds of posts/documentation on delegates/function pointers, utilizing mighty google keywords "callback", "handler", , "delegate". think have half-formed understanding work differently in c++, nothing bearing fruit.

could kindly point me in right direction?

pretty same you'd in java. in library, define pure virtual class (more-or-less analogous java interface) of handler.

class somethinghandler { public:     virtual void somethinghappened(<stuff happened>) = 0; } 

and define , implement class watch event , call function defined in handler. class contains pointer, or list of pointers, objects installed client , methods add , remove clients. if possible, don't use pointers @ let something of containing. life cleaner way. if can't, guard pointers shared_ptr cannot destroyed before removed watcher.

class somethingwatcher { public:     bool addclient(std::shared_ptr<somethinghandler> clienttoadd);     bool removeclient(std::shared_ptr<somethinghandler> clienttoremove);     void applogic();  private:     std::shared_ptr<somethinghandler> client;     // or std::vector<std::shared_ptr<somethinghandler>> clients; } 

finally, somewhere in somethingwatcher's application logic detect happened , call client.

void somethingwatcher::applogic() {     // stuff     if(<something happened>)     {         // or iterate through list of clients , doing each:         if (client != nullptr)         {             client->somethinghappened(<stuff happened>);         }     } } 

the library doesn't need know client other exists , implements somethinghappened function defined in somethinghandler. long have valid pointer, magic happens. well, not isn't right place go how "magic" works.

outside library in client land...

the client code defines , implements class implements somethinghandler receive calls somethinghappened.

class somethingclient: public somethinghandler { public:     void somethinghappened(<stuff happened>);     \\ handles happening. } 

assuming client code has instantiated somethingwatcher named watcher, initialization routine creates , adds somethingclient watcher.

std::shared_ptr<somethinghandler> handler = std::make_shared<somethingclient>(constructor arguments); watcher.addclient(handler); 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -