Fawkes API Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * blackboard.h - BlackBoard Interface 00004 * 00005 * Created: Sat Sep 16 17:09:15 2006 (on train to Cologne) 00006 * Copyright 2006-2008 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. A runtime exception applies to 00014 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Library General Public License for more details. 00020 * 00021 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00022 */ 00023 00024 #ifndef __BLACKBOARD_BLACKBOARD_H_ 00025 #define __BLACKBOARD_BLACKBOARD_H_ 00026 00027 #include <core/exceptions/software.h> 00028 #include <interface/interface.h> 00029 00030 #include <list> 00031 #include <string> 00032 #include <typeinfo> 00033 00034 namespace fawkes { 00035 00036 class BlackBoardInterfaceManager; 00037 class BlackBoardMemoryManager; 00038 class BlackBoardMessageManager; 00039 class BlackBoardNetworkHandler; 00040 class BlackBoardNotifier; 00041 class InterfaceInfoList; 00042 class BlackBoardInterfaceListener; 00043 class BlackBoardInterfaceObserver; 00044 class FawkesNetworkHub; 00045 00046 class BlackBoard 00047 { 00048 public: 00049 virtual ~BlackBoard(); 00050 00051 virtual Interface * open_for_reading(const char *interface_type, const char *identifier) = 0; 00052 virtual Interface * open_for_writing(const char *interface_type, const char *identifier) = 0; 00053 virtual void close(Interface *interface) = 0; 00054 00055 virtual InterfaceInfoList * list_all() = 0; 00056 virtual bool is_alive() const throw() = 0; 00057 virtual bool try_aliveness_restore() throw() = 0; 00058 00059 virtual std::list<Interface *> open_multiple_for_reading(const char *interface_type, 00060 const char *id_pattern = "*") = 0; 00061 00062 template <class InterfaceType> 00063 std::list<InterfaceType *> open_multiple_for_reading(const char *id_pattern = "*"); 00064 00065 template <class InterfaceType> 00066 InterfaceType * open_for_reading(const char *identifier); 00067 00068 template <class InterfaceType> 00069 InterfaceType * open_for_writing(const char *identifier); 00070 00071 static const unsigned int BBIL_FLAG_DATA; 00072 static const unsigned int BBIL_FLAG_MESSAGES; 00073 static const unsigned int BBIL_FLAG_READER; 00074 static const unsigned int BBIL_FLAG_WRITER; 00075 static const unsigned int BBIL_FLAG_ALL; 00076 00077 static const unsigned int BBIO_FLAG_CREATED; 00078 static const unsigned int BBIO_FLAG_DESTROYED; 00079 static const unsigned int BBIO_FLAG_ALL; 00080 00081 virtual void register_listener(BlackBoardInterfaceListener *listener, 00082 unsigned int flags) = 0; 00083 virtual void unregister_listener(BlackBoardInterfaceListener *listener) = 0; 00084 00085 virtual void register_observer(BlackBoardInterfaceObserver *observer, 00086 unsigned int flags) = 0; 00087 virtual void unregister_observer(BlackBoardInterfaceObserver *observer) = 0; 00088 00089 std::string demangle_fawkes_interface_name(const char *type); 00090 }; 00091 00092 00093 /** Get interface of given type. 00094 * This will open a new interface for reading just like the non-template version of 00095 * open_for_reading(). But with the template method you will get a correctly typed object 00096 * that you can use. An TypeMismatchException is thrown if the string representation 00097 * of the type and the actual class type of the interface do not match. 00098 * @param identifier identifier of the interface 00099 * @return new fully initialized interface instance of requested type 00100 * @exception OutOfMemoryException thrown if there is not enough free space for 00101 * the requested interface. 00102 * @exception TypeMismatchException thrown if type in interface_type and the actual class 00103 * type do not fit. 00104 */ 00105 template <class InterfaceType> 00106 InterfaceType * 00107 BlackBoard::open_for_reading(const char *identifier) 00108 { 00109 std::string type_name = demangle_fawkes_interface_name(typeid(InterfaceType).name()); 00110 Interface *interface = open_for_reading(type_name.c_str(), identifier); 00111 return static_cast<InterfaceType *>(interface); 00112 } 00113 00114 00115 /** Open all interfaces of given type for reading. 00116 * This will create interface instances for all currently registered interfaces of 00117 * the given type. The result can be casted to the appropriate type. 00118 * @param id_pattern pattern of interface IDs to open, supports wildcards similar 00119 * to filenames (*, ?, []), see "man fnmatch" for all supported. 00120 * @return list of new fully initialized interface instances of requested type. The 00121 * is allocated using new and you have to free it using delete after you are done 00122 * with it! 00123 */ 00124 template <class InterfaceType> 00125 std::list<InterfaceType *> 00126 BlackBoard::open_multiple_for_reading(const char *id_pattern) 00127 { 00128 std::string type_name = demangle_fawkes_interface_name(typeid(InterfaceType).name()); 00129 std::list<Interface *> il = open_multiple_for_reading(type_name.c_str(), id_pattern); 00130 std::list<InterfaceType *> rv; 00131 for (std::list<Interface *>::iterator i = il.begin(); i != il.end(); ++i) { 00132 rv.push_back(static_cast<InterfaceType *>(*i)); 00133 } 00134 00135 return rv; 00136 } 00137 00138 00139 /** Get writer interface of given type. 00140 * This will open a new interface for writing just like the non-template version of 00141 * open_for_writing(). But with the template method you will get a correctly typed object 00142 * that you can use. An TypeMismatchException is thrown if the string representation 00143 * of the type and the actual class type of the interface do not match. 00144 * @param identifier identifier of the interface 00145 * @return new fully initialized interface instance of requested type 00146 * @exception OutOfMemoryException thrown if there is not enough free space for 00147 * the requested interface. 00148 * @exception BlackBoardWriterActiveException thrown if there is already a writing 00149 * instance with the same type/id 00150 * @exception TypeMismatchException thrown if type in interface_type and the actual class 00151 * type do not fit. 00152 */ 00153 template <class InterfaceType> 00154 InterfaceType * 00155 BlackBoard::open_for_writing(const char *identifier) 00156 { 00157 std::string type_name = demangle_fawkes_interface_name(typeid(InterfaceType).name()); 00158 Interface *interface = open_for_writing(type_name.c_str(), identifier); 00159 return static_cast<InterfaceType *>(interface);; 00160 } 00161 00162 } // end namespace fawkes 00163 00164 #endif