Fawkes API  Fawkes Development Version
blackboard.cpp
1 
2 /***************************************************************************
3  * blackboard.cpp - Fawkes BlackBoard Aspect initializer/finalizer
4  *
5  * Created: Tue Nov 23 23:06:13 2010
6  * Copyright 2006-2010 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 <aspect/blackboard.h>
25 #include <aspect/inifins/blackboard.h>
26 #include <blackboard/blackboard.h>
27 #include <blackboard/ownership.h>
28 
29 namespace fawkes {
30 
31 /** @class BlackBoardAspectIniFin <aspect/inifins/blackboard.h>
32  * Initializer/finalizer for the BlackBoardAspect.
33  * @author Tim Niemueller
34  */
35 
36 /** Constructor.
37  * @param blackboard blackboard instance to pass to threads
38  */
40 : AspectIniFin("BlackBoardAspect")
41 {
42  blackboard_ = blackboard;
43 }
44 
45 void
47 {
48  BlackBoardAspect *blackboard_thread;
49  blackboard_thread = dynamic_cast<BlackBoardAspect *>(thread);
50  if (blackboard_thread == NULL) {
51  throw CannotInitializeThreadException("Thread '%s' claims to have the "
52  "BlackBoardAspect, but RTTI says it "
53  "has not. ",
54  thread->name());
55  }
56 
57  if (!blackboard_) {
58  throw CannotInitializeThreadException("Thread '%s' needs BlackBoardAspect, "
59  "but not blackboard available");
60  }
61 
62  BlackBoard *bb;
63  if (blackboard_thread->blackboard_owner_name_) {
64  bb = new BlackBoardWithOwnership(blackboard_, blackboard_thread->blackboard_owner_name_);
65  } else {
66  bb = new BlackBoardWithOwnership(blackboard_, thread->name());
67  }
68 
69  blackboard_thread->init_BlackBoardAspect(bb);
70 }
71 
72 void
74 {
75  BlackBoardAspect *blackboard_thread;
76  blackboard_thread = dynamic_cast<BlackBoardAspect *>(thread);
77  if (blackboard_thread == NULL) {
78  throw CannotFinalizeThreadException("Thread '%s' claims to have the "
79  "BlackBoardAspect, but RTTI says it "
80  "has not. ",
81  thread->name());
82  }
83 
84  delete blackboard_thread->blackboard;
85  blackboard_thread->blackboard = NULL;
86 }
87 
88 } // end namespace fawkes
fawkes::BlackBoardAspectIniFin::finalize
virtual void finalize(Thread *thread)
Finalize thread.
Definition: blackboard.cpp:73
fawkes::BlackBoard
The BlackBoard abstract class.
Definition: blackboard.h:46
fawkes::CannotInitializeThreadException
Thread cannot be initialized.
Definition: thread_initializer.h:34
fawkes::Thread::name
const char * name() const
Get name of thread.
Definition: thread.h:100
fawkes::BlackBoardAspectIniFin::init
virtual void init(Thread *thread)
Initialize thread.
Definition: blackboard.cpp:46
fawkes::AspectIniFin
Aspect initializer/finalizer base class.
Definition: inifin.h:34
fawkes::BlackBoardAspect
Thread aspect to access to BlackBoard.
Definition: blackboard.h:34
fawkes
Fawkes library namespace.
fawkes::BlackBoardAspect::init_BlackBoardAspect
void init_BlackBoardAspect(BlackBoard *bb)
Init BlackBoard aspect.
Definition: blackboard.cpp:66
fawkes::BlackBoardAspectIniFin::BlackBoardAspectIniFin
BlackBoardAspectIniFin(BlackBoard *blackboard)
Constructor.
Definition: blackboard.cpp:39
fawkes::BlackBoardWithOwnership
BlackBoard that traces interface ownership.
Definition: ownership.h:31
fawkes::Thread
Thread class encapsulation of pthreads.
Definition: thread.h:46
fawkes::BlackBoardAspect::blackboard
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
Definition: blackboard.h:44
fawkes::CannotFinalizeThreadException
Thread cannot be finalized.
Definition: thread_finalizer.h:34