From 6f940bcaafa9b1aa616c477bfb216df60b240a0b Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Thu, 23 Jul 2015 01:02:53 -0700 Subject: [PATCH] Add immediate_notify and is_new to entry listener. On the callback function, is_new indicates the value is newly added. On adding a callback function, immediate_notify indicates the callback should be called once (with is_new=true) for each matching entry that already exists. --- include/ntcore_c.h | 5 +++-- include/ntcore_cpp.h | 6 ++++-- src/ntcore_c.cpp | 11 +++++++---- src/ntcore_cpp.cpp | 4 ++-- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/include/ntcore_c.h b/include/ntcore_c.h index eefd35efa3..c9e35bea8c 100644 --- a/include/ntcore_c.h +++ b/include/ntcore_c.h @@ -237,14 +237,15 @@ void NT_Flush(void); typedef void (*NT_EntryListenerCallback)( unsigned int uid, void *data, const char *name, size_t name_len, - const struct NT_Value *value); + const struct NT_Value *value, int is_new); typedef void (*NT_ConnectionListenerCallback)( unsigned int uid, void *data, int connected, const struct NT_ConnectionInfo *conn); unsigned int NT_AddEntryListener(const char *prefix, size_t prefix_len, - void *data, NT_EntryListenerCallback callback); + void *data, NT_EntryListenerCallback callback, + int immediate_notify); void NT_RemoveEntryListener(unsigned int entry_listener_uid); unsigned int NT_AddConnectionListener(void *data, NT_ConnectionListenerCallback callback); diff --git a/include/ntcore_cpp.h b/include/ntcore_cpp.h index 080029f6c1..32648ee6b0 100644 --- a/include/ntcore_cpp.h +++ b/include/ntcore_cpp.h @@ -166,13 +166,15 @@ void Flush(); */ typedef std::function value)> EntryListenerCallback; + std::shared_ptr value, bool is_new)> + EntryListenerCallback; typedef std::function ConnectionListenerCallback; -unsigned int AddEntryListener(StringRef prefix, EntryListenerCallback callback); +unsigned int AddEntryListener(StringRef prefix, EntryListenerCallback callback, + bool immediate_notify); void RemoveEntryListener(unsigned int entry_listener_uid); unsigned int AddConnectionListener(ConnectionListenerCallback callback); void RemoveConnectionListener(unsigned int conn_listener_uid); diff --git a/src/ntcore_c.cpp b/src/ntcore_c.cpp index dfdab0f34e..472950d863 100644 --- a/src/ntcore_c.cpp +++ b/src/ntcore_c.cpp @@ -130,12 +130,15 @@ void NT_Flush(void) { nt::Flush(); } unsigned int NT_AddEntryListener(const char *prefix, size_t prefix_len, void *data, - NT_EntryListenerCallback callback) { + NT_EntryListenerCallback callback, + int immediate_notify) { return nt::AddEntryListener( StringRef(prefix, prefix_len), - [=](unsigned int uid, StringRef name, std::shared_ptr value) { - callback(uid, data, name.data(), name.size(), &value->value()); - }); + [=](unsigned int uid, StringRef name, std::shared_ptr value, + bool is_new) { + callback(uid, data, name.data(), name.size(), &value->value(), is_new); + }, + immediate_notify != 0); } void NT_RemoveEntryListener(unsigned int entry_listener_uid) { diff --git a/src/ntcore_cpp.cpp b/src/ntcore_cpp.cpp index a634254234..988b42f65c 100644 --- a/src/ntcore_cpp.cpp +++ b/src/ntcore_cpp.cpp @@ -61,8 +61,8 @@ void Flush() { * Callback Creation Functions */ -unsigned int AddEntryListener(StringRef prefix, - EntryListenerCallback callback) { +unsigned int AddEntryListener(StringRef prefix, EntryListenerCallback callback, + bool immediate_notify) { return 0; }