File indexing completed on 2024-05-05 17:50:04

0001 /*
0002    Copyright (C) 2018 Andreas Hartmetz <ahartmetz@gmail.com>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LGPL.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 
0019    Alternatively, this file is available under the Mozilla Public License
0020    Version 1.1.  You may obtain a copy of the License at
0021    http://www.mozilla.org/MPL/
0022 */
0023 
0024 #ifndef IIOEVENTSOURCE_H
0025 #define IIOEVENTSOURCE_H
0026 
0027 #include "iovaluetypes.h"
0028 #include "types.h"
0029 
0030 class IIoEventListener;
0031 
0032 // TODO: explain the context for this - layered sources and listeners and such
0033 
0034 // There is one subtle problem that needs to be solved regarding IIoEventListener::fileDescriptor().
0035 // Event sources are expected to use some kind of hash / map FileDescriptor -> IIoEventListener, so
0036 // an IIoEventListener cannot unregister after closing the file descriptor. Even keeping the old
0037 // value around doesn't help - the file descriptor could be reused very soon by any other part of the
0038 // current process.
0039 // So the lowest level implementation of close() must immediately unregister the IIoEventListener
0040 // leading to the I/O operation leading to the close(). That lowest level IIoEventListener must also
0041 // unregister itself, etc, up to the highest level IIoEventSource (usually EventDispatcherPrivate).
0042 
0043 class IIoEventSource
0044 {
0045 public:
0046     virtual ~IIoEventSource();
0047 
0048     // Contract: an IIoEventSource may have different IIoEventListeners listening to read and write
0049     // As a further restriction, a listener must always remove I/O interest from its old source
0050     // before enabling it on its new source.
0051     void addIoListener(IIoEventListener *iol);
0052     void removeIoListener(IIoEventListener *iol);
0053 
0054 protected:
0055     // add / remove only make sense for stateful APIs such as Linux epoll, so they don't have to be
0056     // implemented - the defaults just call updateIoInterestInternal().
0057     virtual void addIoListenerInternal(IIoEventListener *iol, uint32 ioRw);
0058     virtual void removeIoListenerInternal(IIoEventListener *iol);
0059     virtual void updateIoInterestInternal(IIoEventListener *iol, uint32 ioRw) = 0;
0060 
0061 private:
0062     friend class IIoEventListener;
0063     // called from IIoEventListener when its m_ioInterest changed
0064     void updateIoInterest(IIoEventListener *iol);
0065 };
0066 
0067 #endif // IIOEVENTSOURCE_H