1 #include <sdeventplus/internal/cexec.hpp>
2 #include <sdeventplus/internal/sdevent.hpp>
3 #include <sdeventplus/source/base.hpp>
4 #include <sdeventplus/types.hpp>
5
6 #include <functional>
7 #include <utility>
8
9 namespace sdeventplus
10 {
11 namespace source
12 {
13
get() const14 sd_event_source* Base::get() const
15 {
16 return source.value();
17 }
18
get_event() const19 const Event& Base::get_event() const
20 {
21 return event;
22 }
23
get_description() const24 const char* Base::get_description() const
25 {
26 const char* description;
27 SDEVENTPLUS_CHECK("sd_event_source_get_description",
28 event.getSdEvent()->sd_event_source_get_description(
29 get(), &description));
30 return description;
31 }
32
set_description(const char * description) const33 void Base::set_description(const char* description) const
34 {
35 SDEVENTPLUS_CHECK("sd_event_source_set_description",
36 event.getSdEvent()->sd_event_source_set_description(
37 get(), description));
38 }
39
set_prepare(Callback && callback)40 void Base::set_prepare(Callback&& callback)
41 {
42 try
43 {
44 SDEVENTPLUS_CHECK("sd_event_source_set_prepare",
45 event.getSdEvent()->sd_event_source_set_prepare(
46 get(), callback ? prepareCallback : nullptr));
47 get_userdata().prepare = std::move(callback);
48 }
49 catch (...)
50 {
51 get_userdata().prepare = nullptr;
52 throw;
53 }
54 }
55
get_pending() const56 bool Base::get_pending() const
57 {
58 return SDEVENTPLUS_CHECK(
59 "sd_event_source_get_pending",
60 event.getSdEvent()->sd_event_source_get_pending(get()));
61 }
62
get_priority() const63 int64_t Base::get_priority() const
64 {
65 int64_t priority;
66 SDEVENTPLUS_CHECK(
67 "sd_event_source_get_priority",
68 event.getSdEvent()->sd_event_source_get_priority(get(), &priority));
69 return priority;
70 }
71
set_priority(int64_t priority) const72 void Base::set_priority(int64_t priority) const
73 {
74 SDEVENTPLUS_CHECK(
75 "sd_event_source_set_priority",
76 event.getSdEvent()->sd_event_source_set_priority(get(), priority));
77 }
78
get_enabled() const79 Enabled Base::get_enabled() const
80 {
81 int enabled;
82 SDEVENTPLUS_CHECK(
83 "sd_event_source_get_enabled",
84 event.getSdEvent()->sd_event_source_get_enabled(get(), &enabled));
85 return static_cast<Enabled>(enabled);
86 }
87
set_enabled(Enabled enabled) const88 void Base::set_enabled(Enabled enabled) const
89 {
90 SDEVENTPLUS_CHECK("sd_event_source_set_enabled",
91 event.getSdEvent()->sd_event_source_set_enabled(
92 get(), static_cast<int>(enabled)));
93 }
94
get_floating() const95 bool Base::get_floating() const
96 {
97 return SDEVENTPLUS_CHECK(
98 "sd_event_source_get_floating",
99 event.getSdEvent()->sd_event_source_get_floating(get()));
100 }
101
set_floating(bool b) const102 void Base::set_floating(bool b) const
103 {
104 SDEVENTPLUS_CHECK("sd_event_source_set_floating",
105 event.getSdEvent()->sd_event_source_set_floating(
106 get(), static_cast<int>(b)));
107 }
108
Base(const Event & event,sd_event_source * source,std::false_type)109 Base::Base(const Event& event, sd_event_source* source, std::false_type) :
110 event(event), source(std::move(source), event.getSdEvent(), true)
111 {}
112
Base(const Base & other,sdeventplus::internal::NoOwn)113 Base::Base(const Base& other, sdeventplus::internal::NoOwn) :
114 event(other.get_event(), sdeventplus::internal::NoOwn()),
115 source(other.get(), event.getSdEvent(), false)
116 {}
117
set_userdata(std::unique_ptr<detail::BaseData> data) const118 void Base::set_userdata(std::unique_ptr<detail::BaseData> data) const
119 {
120 SDEVENTPLUS_CHECK("sd_event_source_set_destroy_callback",
121 event.getSdEvent()->sd_event_source_set_destroy_callback(
122 get(), &Base::destroy_userdata));
123 event.getSdEvent()->sd_event_source_set_userdata(get(), data.release());
124 }
125
get_userdata() const126 detail::BaseData& Base::get_userdata() const
127 {
128 return *reinterpret_cast<detail::BaseData*>(
129 event.getSdEvent()->sd_event_source_get_userdata(get()));
130 }
131
get_prepare()132 Base::Callback& Base::get_prepare()
133 {
134 return get_userdata().prepare;
135 }
136
ref(sd_event_source * const & source,const internal::SdEvent * & sdevent,bool & owned)137 sd_event_source* Base::ref(sd_event_source* const& source,
138 const internal::SdEvent*& sdevent, bool& owned)
139 {
140 owned = true;
141 return sdevent->sd_event_source_ref(source);
142 }
143
drop(sd_event_source * && source,const internal::SdEvent * & sdevent,bool & owned)144 void Base::drop(sd_event_source*&& source, const internal::SdEvent*& sdevent,
145 bool& owned)
146 {
147 if (owned)
148 {
149 sdevent->sd_event_source_unref(source);
150 }
151 }
152
destroy_userdata(void * userdata)153 void Base::destroy_userdata(void* userdata)
154 {
155 delete static_cast<Base*>(userdata);
156 }
157
prepareCallback(sd_event_source * source,void * userdata)158 int Base::prepareCallback(sd_event_source* source, void* userdata)
159 {
160 return sourceCallback<Callback, Base, &Base::get_prepare>(
161 "prepareCallback", source, userdata);
162 }
163
164 namespace detail
165 {
166
BaseData(const Base & base)167 BaseData::BaseData(const Base& base) :
168 Base(base, sdeventplus::internal::NoOwn())
169 {}
170
171 } // namespace detail
172
173 } // namespace source
174 } // namespace sdeventplus
175