summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorWilliam A. Kennington III <wak@google.com>2018-07-17 14:40:14 -0700
committerWilliam A. Kennington III <wak@google.com>2018-07-17 14:40:14 -0700
commit2d943ead4b1c160a163f284fb5b639f1f076ce49 (patch)
tree98ebe31bcb2649639ccdcb43fb3b27c4d86967e7 /src
parente3e1df0b549facf175b9201c4e945188eecaa700 (diff)
downloadsdeventplus-2d943ead4b1c160a163f284fb5b639f1f076ce49.tar.gz
sdeventplus-2d943ead4b1c160a163f284fb5b639f1f076ce49.zip
source/base: Support automatically configuring userdata
This makes callbacks work correctly as the systemd callback functions need to know where to look for the object storing the real callback.
Diffstat (limited to 'src')
-rw-r--r--src/sdeventplus/internal/sdevent.hpp9
-rw-r--r--src/sdeventplus/source/base.cpp16
-rw-r--r--src/sdeventplus/source/base.hpp4
-rw-r--r--src/sdeventplus/test/sdevent.hpp3
4 files changed, 31 insertions, 1 deletions
diff --git a/src/sdeventplus/internal/sdevent.hpp b/src/sdeventplus/internal/sdevent.hpp
index be8e0a8..7713cb8 100644
--- a/src/sdeventplus/internal/sdevent.hpp
+++ b/src/sdeventplus/internal/sdevent.hpp
@@ -36,6 +36,9 @@ class SdEvent
virtual sd_event_source*
sd_event_source_unref(sd_event_source* source) const = 0;
+ virtual void* sd_event_source_set_userdata(sd_event_source* source,
+ void* userdata) const = 0;
+
virtual int
sd_event_source_get_description(sd_event_source* source,
const char** description) const = 0;
@@ -141,6 +144,12 @@ class SdEventImpl : public SdEvent
return ::sd_event_source_unref(source);
}
+ void* sd_event_source_set_userdata(sd_event_source* source,
+ void* userdata) const override
+ {
+ return ::sd_event_source_set_userdata(source, userdata);
+ }
+
int sd_event_source_get_description(sd_event_source* source,
const char** description) const override
{
diff --git a/src/sdeventplus/source/base.cpp b/src/sdeventplus/source/base.cpp
index 94a5628..83908d1 100644
--- a/src/sdeventplus/source/base.cpp
+++ b/src/sdeventplus/source/base.cpp
@@ -163,6 +163,7 @@ Base::Base(const Event& event, sd_event_source* source) :
source(source, &internal::SdEvent::sd_event_source_ref,
&internal::SdEvent::sd_event_source_unref, event.getSdEvent())
{
+ set_userdata();
}
Base::Base(const Event& event, sd_event_source* source, std::false_type) :
@@ -170,6 +171,14 @@ Base::Base(const Event& event, sd_event_source* source, std::false_type) :
&internal::SdEvent::sd_event_source_unref,
std::false_type(), event.getSdEvent())
{
+ set_userdata();
+}
+
+Base::Base(Base&& other) :
+ event(std::move(other.event)), source(std::move(other.source)),
+ prepare(std::move(other.prepare))
+{
+ set_userdata();
}
Base& Base::operator=(Base&& other)
@@ -186,9 +195,16 @@ Base& Base::operator=(Base&& other)
event = std::move(other.event);
source = std::move(other.source);
prepare = std::move(other.prepare);
+
+ set_userdata();
}
return *this;
}
+void Base::set_userdata()
+{
+ event.getSdEvent()->sd_event_source_set_userdata(source.get(), this);
+}
+
} // namespace source
} // namespace sdeventplus
diff --git a/src/sdeventplus/source/base.hpp b/src/sdeventplus/source/base.hpp
index d576b24..ed30016 100644
--- a/src/sdeventplus/source/base.hpp
+++ b/src/sdeventplus/source/base.hpp
@@ -47,11 +47,13 @@ class Base
Base(const Base& other) = delete;
Base& operator=(const Base& other) = delete;
// We don't want to allow any kind of slicing.
- Base(Base&& other) = default;
+ Base(Base&& other);
Base& operator=(Base&& other);
private:
Callback prepare;
+
+ void set_userdata();
};
} // namespace source
diff --git a/src/sdeventplus/test/sdevent.hpp b/src/sdeventplus/test/sdevent.hpp
index fd05b30..9b39d50 100644
--- a/src/sdeventplus/test/sdevent.hpp
+++ b/src/sdeventplus/test/sdevent.hpp
@@ -34,6 +34,9 @@ class SdEventMock : public internal::SdEvent
MOCK_CONST_METHOD1(sd_event_source_unref,
sd_event_source*(sd_event_source*));
+ MOCK_CONST_METHOD2(sd_event_source_set_userdata,
+ void*(sd_event_source*, void*));
+
MOCK_CONST_METHOD2(sd_event_source_get_description,
int(sd_event_source*, const char**));
MOCK_CONST_METHOD2(sd_event_source_set_description,
OpenPOWER on IntegriCloud