Fawkes API  Fawkes Development Version
interface_list_maintainer.h
1 
2 /***************************************************************************
3  * interface_list_maintainer.h - BlackBoard interface list maintainer
4  *
5  * Created: Mon Mar 16 13:34:00 2015
6  * Copyright 2007-2014 Tim Niemueller [www.niemueller.de]
7  * 2015 Tobias Neumann
8  *
9  ****************************************************************************/
10 
11 /* This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version. A runtime exception applies to
15  * this software (see LICENSE.GPL_WRE file mentioned below for details).
16  *
17  * This program 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
20  * GNU Library General Public License for more details.
21  *
22  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
23  */
24 
25 #ifndef __BLACKBOARD_INTERFACE_LIST_MAINTAINER_H_
26 #define __BLACKBOARD_INTERFACE_LIST_MAINTAINER_H_
27 
28 #include <logging/logger.h>
29 #include <blackboard/blackboard.h>
30 
31 #include <core/utils/lock_list.h>
32 #include <interface/interface.h>
33 #include <blackboard/interface_listener.h>
34 #include <blackboard/interface_observer.h>
35 
36 #include <list>
37 #include <string>
38 
39 namespace fawkes {
40 
41 /** @class BlackBoardInterfaceListMaintainer "interface_list_maintainer.h"
42  * opens and maintains multiple interfaces defined by a pattern
43  * @author Tobias Neumann
44  */
46 :
49 {
50  public:
51  BlackBoardInterfaceListMaintainer(const char* n, BlackBoard* bb, Logger* l, const char *type, const char *pattern);
53 
54  template <class InterfaceType>
55  std::list<InterfaceType *> lock_and_get_list();
56 
57  void unlock_list();
58 
59  private:
60  // for BlackBoardInterfaceObserver
61  virtual void bb_interface_created(const char *type, const char *id) throw();
62 
63  // for BlackBoardInterfaceListener
64  virtual void bb_interface_writer_removed(fawkes::Interface *interface,
65  unsigned int instance_serial) throw();
66  virtual void bb_interface_reader_removed(fawkes::Interface *interface,
67  unsigned int instance_serial) throw();
68 
69  void conditional_close(fawkes::Interface *interface) throw();
70 
71  private:
72  BlackBoard *blackboard_;
73  Logger *logger_;
74  const char *name_;
76 };
77 
78 /** Locks the mutex in this class and returns a list of all interfaces defined by the pattern
79  *
80  * after the list is used unlock_list() needs to be called to unlock the mutex in this class
81  *
82  * @return list of interfaces defined by the pattern
83  */
84 template <class InterfaceType>
85 std::list<InterfaceType *>
87 {
88  ifs_.lock();
89  std::list<InterfaceType *> ifs_cpy;
90  for ( fawkes::LockList<fawkes::Interface *>::iterator pif = ifs_.begin();
91  pif != ifs_.end();
92  ++pif ) {
93  (*pif)->read();
94  ifs_cpy.push_back( dynamic_cast<InterfaceType*> (*pif) );
95  }
96  return ifs_cpy;
97 }
98 
99 } // end namespace fawkes
100 
101 #endif
std::list< InterfaceType * > lock_and_get_list()
Locks the mutex in this class and returns a list of all interfaces defined by the pattern...
virtual void lock() const
Lock list.
Definition: lock_list.h:128
Fawkes library namespace.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
BlackBoardInterfaceListMaintainer(const char *n, BlackBoard *bb, Logger *l, const char *type, const char *pattern)
Constructor.
List with a lock.
Definition: thread.h:40
BlackBoard interface observer.
opens and maintains multiple interfaces defined by a pattern
The BlackBoard abstract class.
Definition: blackboard.h:48
void unlock_list()
unlocks the mutex in this class
BlackBoard interface listener.
Interface for logging.
Definition: logger.h:34