selectable.h
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * As a special exception, you may use this file as part of a free
8  * software library without restriction. Specifically, if other files
9  * instantiate templates or use macros or inline functions from this
10  * file, or you compile this file and link it with other files to
11  * produce an executable, this file does not by itself cause the
12  * resulting executable to be covered by the GNU General Public
13  * License. This exception does not however invalidate any other
14  * reasons why the executable file might be covered by the GNU Library
15  * General Public License.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25  */
26 #ifndef CXXTOOLS_SELECTABLE_H
27 #define CXXTOOLS_SELECTABLE_H
28 
29 #include <cxxtools/signal.h>
30 #include <cxxtools/selector.h>
31 #include <cxxtools/timespan.h>
32 
33 namespace cxxtools
34 {
35 
36 class SelectableImpl;
37 
39 {
40  Selectable(const Selectable&) { }
41  Selectable& operator=(const Selectable&) { return *this; }
42 
43  public:
44  static const std::size_t WaitInfinite = Selector::WaitInfinite;
45 
46  enum State
47  {
48  Disabled = 0,
49  Idle = 1,
50  Busy = 2,
51  Avail = 3
52  };
53 
54  public:
56  virtual ~Selectable();
57 
58  void setSelector(SelectorBase* parent);
59 
60  SelectorBase* selector() const;
61 
63 
66  void close();
67 
68  bool wait(Milliseconds msecs = WaitInfinite);
69 
71 
77  bool enabled() const;
78 
79  bool idle() const;
80 
81  bool busy() const;
82 
83  bool avail() const;
84 
85  virtual SelectableImpl& simpl() = 0;
86 
87  protected:
89  Selectable();
90 
92  void setEnabled(bool isEnabled);
93 
94  //TODO: tell Selector more specifically what changed
95  // add onReinit() to Selector:
96  // pass old state as second arg with onChanged()
97  void setState(State state);
98 
100  virtual void onClose() = 0;
101 
102  virtual bool onWait(Timespan timeout) = 0;
103 
104  virtual void onAttach(SelectorBase&) = 0;
105 
106  virtual void onDetach(SelectorBase&) = 0;
107 
108  private:
109  SelectorBase* _parent;
110  State _state;
111 };
112 
113 } // namespace cxxtools
114 
115 #endif