Fawkes API
Fawkes Development Version
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
All
Classes
Namespaces
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Groups
Pages
instance_factory.cpp
1
2
/***************************************************************************
3
* instance_factory.cpp - BlackBoard interface instance factory
4
*
5
* Created: Mon Mar 03 18:01:53 2008
6
* Copyright 2006-2011 Tim Niemueller [www.niemueller.de]
7
*
8
****************************************************************************/
9
10
/* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation; either version 2 of the License, or
13
* (at your option) any later version. A runtime exception applies to
14
* this software (see LICENSE.GPL_WRE file mentioned below for details).
15
*
16
* This program is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU Library General Public License for more details.
20
*
21
* Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22
*/
23
24
#include <blackboard/internal/instance_factory.h>
25
#include <blackboard/exceptions.h>
26
27
#include <interface/interface.h>
28
29
#include <utils/system/dynamic_module/module_manager.h>
30
#include <utils/system/dynamic_module/module.h>
31
32
#include <cstdlib>
33
#include <cstring>
34
35
namespace
fawkes {
36
37
/** @class BlackBoardInstanceFactory <blackboard/internal/instance_factory.h>
38
* BlackBoard instance factory.
39
* This class is used to interact with the interface shared object to create
40
* and delete interface instances.
41
*
42
* @author Tim Niemueller
43
*/
44
45
46
/** Constructor.*/
47
BlackBoardInstanceFactory::BlackBoardInstanceFactory
()
48
{
49
__mm =
new
ModuleManager
(IFACEDIR);
50
}
51
52
53
/** Destructor */
54
BlackBoardInstanceFactory::~BlackBoardInstanceFactory
()
55
{
56
delete
__mm;
57
}
58
59
60
/** Creates a new interface instance.
61
* This method will look in the for the appropriate library in LIBDIR/interfaces
62
* and then use the factory function for the interface of the given type. If
63
* this was found a new instance of the interface is returned.
64
* @param type type of the interface
65
* @param identifier identifier of the interface
66
* @return a new instance of the requested interface type
67
* @exception BlackBoardInterfaceNotFoundException thrown if the factory function
68
* for the given interface type could not be found
69
*/
70
Interface
*
71
BlackBoardInstanceFactory::new_interface_instance
(
const
char
*type,
const
char
*identifier)
72
{
73
Module
*mod = NULL;
74
std::string filename = std::string(
"lib"
) + type +
"."
+ __mm->
get_module_file_extension
();
75
try
{
76
mod = __mm->
open_module
(filename.c_str());
77
}
catch
(
Exception
&e) {
78
throw
BlackBoardInterfaceNotFoundException
(type,
" Module file not found."
);
79
}
80
81
if
( ! mod->
has_symbol
(
"interface_factory"
) ) {
82
throw
BlackBoardInterfaceNotFoundException
(type,
" Generator function not found."
);
83
}
84
85
InterfaceFactoryFunc
iff = (
InterfaceFactoryFunc
)mod->
get_symbol
(
"interface_factory"
);
86
87
Interface
*iface = iff();
88
iface->set_type_id(type, identifier);
89
90
return
iface;
91
}
92
93
94
/** Destroy an interface instance.
95
* The destroyer function for the given interface is called to destroy the given
96
* interface instance.
97
* @param interface to destroy
98
* @exception BlackBoardInterfaceNotFoundException thrown if the destroyer function
99
* for the given interface could not be found. The interface will not be freed.
100
*/
101
void
102
BlackBoardInstanceFactory::delete_interface_instance
(
Interface
*interface)
103
{
104
std::string filename = std::string(
"lib"
) + interface->__type +
"."
+ __mm->
get_module_file_extension
();
105
Module
*mod = __mm->
get_module
(filename.c_str());
106
107
if
( ! mod) {
108
throw
BlackBoardInterfaceNotFoundException
(interface->__type,
" Interface module not opened."
);
109
}
110
111
if
( ! mod->
has_symbol
(
"interface_destroy"
) ) {
112
throw
BlackBoardInterfaceNotFoundException
(interface->__type,
" Destroyer function not found."
);
113
}
114
115
InterfaceDestroyFunc
idf = (
InterfaceDestroyFunc
)mod->
get_symbol
(
"interface_destroy"
);
116
idf(interface);
117
118
mod->
unref
();
119
__mm->
close_module
(mod);
120
}
121
122
}
// end namespace fawkes
src
libs
blackboard
internal
instance_factory.cpp
Generated by
1.8.1.2