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/noncopyable.h>
31 #include <cxxtools/api.h>
32 #include <cxxtools/selector.h>
33 
34 namespace cxxtools
35 {
36 
37 class SelectableImpl;
38 
40 {
41  public:
42  static const std::size_t WaitInfinite = Selector::WaitInfinite;
43 
44  enum State
45  {
46  Disabled = 0,
47  Idle = 1,
48  Busy = 2,
49  Avail = 3
50  };
51 
52  public:
54  virtual ~Selectable();
55 
56  void setSelector(SelectorBase* parent);
57 
58  SelectorBase* selector();
59 
60  const SelectorBase* selector() const;
61 
63 
66  void close();
67 
68  bool wait(std::size_t 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(std::size_t msecs) = 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