2 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
17 * @file descriptor-set.h
18 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
26 #include <dpl/exception.h>
28 #include <noncopyable.h>
35 // int is for descriptor, short is for revents,
36 typedef std::function<void(int, short)> Callback;
38 virtual void add(int fd, short events, Callback&& callback) = 0;
39 virtual void remove(int fd, bool close_fd = true) = 0;
41 // I don't want anyone to manage object lifetime via interface.
47 * @brief Wrapper for poll()
49 class DescriptorSet : public IDescriptorSet
53 virtual ~DescriptorSet();
55 NONCOPYABLE(DescriptorSet);
58 * Add descriptor fd to watched set. Watches for events. Takes ownership of fd (closes it). Will
59 * synchronously call supported callback when an event occurs on descriptor. If descriptor
60 * already exists in the set events and callback will be overwritten.
62 * @param fd descriptor to be watched
63 * @param events events to watch for
64 * @param callback callback to be called when an event on descriptor occurs
66 virtual void add(int fd, short events, Callback&& callback);
68 * Removes give descriptor from watched set and closes it.
70 * @param fd descriptor to be removed and closed
72 virtual void remove(int fd, bool close_fd = true);
75 * Wait for descriptor events using poll().
76 * Synchronously calls provided descriptor callbacks.
78 * @param timeout_ms timeout in ms. egative value means no timeout.
80 * @throws Timeout exception in case of timeout
81 * @throws InternalError in case of other error
83 void wait(int timeout_ms = 10000);
85 * Removes and closes all descriptors
89 DECLARE_EXCEPTION_TYPE(CKM::Exception, InternalError);
90 DECLARE_EXCEPTION_TYPE(CKM::Exception, Timeout);
93 // returns false if there are no descriptors to wait for
95 void notify(int descCount);
99 DescriptorData(short e, Callback&& c) : events(e), callback(std::move(c)) {}
105 std::map<int, DescriptorData> m_descriptors;
107 // true if pollfd needs update
112 } /* namespace CKM */