Skip to content

Instantly share code, notes, and snippets.

@gogo40
Created March 1, 2017 04:04
Show Gist options
  • Save gogo40/e3eca56cdee121926ff8cbe9d90385af to your computer and use it in GitHub Desktop.
Save gogo40/e3eca56cdee121926ff8cbe9d90385af to your computer and use it in GitHub Desktop.
#include <utils/notification_manager.h>
#include <utils/ar2gems_base_object.h>
#include <utils/data_service.h>
#include <utils/string_manipulation.h>
using namespace ar2tech;
/* Object notifications */
std::string add_notification_signal() { return "add"; }
class Add_notification : public Notification {
public:
using Ptr = std::shared_ptr<Add_notification>;
Add_notification(Ar2gems_base_object::Ptr sender, int result)
: Notification(sender, add_notification_signal()), result_(result) {}
virtual ~Add_notification() {}
virtual std::string message() const
{
return "{ 'result': " + String_Op::to_string(result_) + "}";
}
int result() const { return result_; }
private:
int result_;
};
// Ar2gems object with notifications
AR2GEMS_OBJ_BEGIN(Test_emitter)
public:
Test_emitter(int v)
: Ar2gems_base_object(), v_(v) {
add_signal(add_notification_signal());
}
virtual void on_delete() { Ar2gems_base_object::release(); }
void add(int b) {
v_ += b;
notify<Add_notification>(v_);
}
private:
int v_;
AR2GEMS_OBJ_END(Test_emitter)
int main()
{
auto context = get_default_context();
memory_usage_log::memory_usage_log_guard memlog;
{
int r1 = 0;
Test_emitter::Ptr o = New<Test_emitter>(context, 10);
auto l = Listener::New(
[&r1](Notification::Ptr message) {
Add_notification::Ptr add_notification =
std::dynamic_pointer_cast<Add_notification>(message);
std::string signal = message->signal();
auto o = message->sender();
std::cout << "New notification received...\n";
std::cout << "sender: " << o->addr() << "\n";
std::cout << "signal: " << message->signal() << "\n";
std::cout << "message: " << message->message() << "\n";
if (add_notification != nullptr) {
std::cout << "Add method() called!\n";
Ar2gems_base_object::Ptr o = message->sender();
int result = add_notification->result();
std::cout << "The object "
<< o->addr()
<< " has a new result: '" << result << "'.\n";
r1 = result;
}
});
std::cout << "Connecting object " << o->addr() << "...\n";
o->connect(l);
o->name("new name");
o->add(10);
o->add(20);
}
memlog.print_live_obj();
int mem_diff = memlog.get_diff();
if (mem_diff != 0) {
std::cout << "MEMORY_LEAK!\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment