Fawkes API  Fawkes Development Version
vision_master.cpp
1 
2 /***************************************************************************
3  * vision_master.cpp - Fawkes VisionMasterAspect 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/inifins/vision_master.h>
25 #include <aspect/vision_master.h>
26 #include <fvutils/base/vision_master.h>
27 #include <core/threading/thread_finalizer.h>
28 
29 namespace fawkes {
30 #if 0 /* just to make Emacs auto-indent happy */
31 }
32 #endif
33 
34 /** @class VisionMasterAspectIniFin <aspect/inifins/vision_master.h>
35  * Initializer/finalizer for the VisionMasterAspect.
36  * @author Tim Niemueller
37  */
38 
39 /** Constructor. */
41  : AspectIniFin("VisionMasterAspect")
42 {
43 }
44 
45 
46 void
48 {
49  VisionMasterAspect *vision_master_thread;
50  vision_master_thread = dynamic_cast<VisionMasterAspect *>(thread);
51  if (vision_master_thread == 0) {
52  throw CannotInitializeThreadException("Thread '%s' claims to have the "
53  "VisionMasterAspect, but RTTI says it "
54  "has not. ", thread->name());
55  }
56 
57  try {
58  __vision_dependency.add(vision_master_thread);
59  } catch (DependencyViolationException &e) {
60  CannotInitializeThreadException ce("Dependency violation for "
61  "VisionMasterAspect detected");
62  ce.append(e);
63  throw ce;
64  }
65 }
66 
67 
68 bool
70 {
71  VisionMasterAspect *vision_master_thread;
72  vision_master_thread = dynamic_cast<VisionMasterAspect *>(thread);
73  if (vision_master_thread == 0) {
74  return true;
75  }
76 
77  if ( ! __vision_dependency.can_remove(vision_master_thread) ) {
78  //__logger->log_warn("AspectIniFin", "Cannot remove vision master, there are "
79  // "still vision threads that depend on it");
80  return false;
81  }
82 
83  return true;
84 }
85 
86 void
88 {
89  VisionMasterAspect *vision_master_thread;
90  vision_master_thread = dynamic_cast<VisionMasterAspect *>(thread);
91  if (vision_master_thread == 0) {
92  throw CannotFinalizeThreadException("Thread '%s' claims to have the "
93  "VisionMasterAspect, but RTTI says it "
94  "has not. ", thread->name());
95  }
96 
97  try {
98  __vision_dependency.remove(vision_master_thread);
99  } catch (DependencyViolationException &e) {
100  CannotFinalizeThreadException ce("Dependency violation for "
101  "VisionMasterAspect detected");
102  ce.append(e);
103  throw ce;
104  }
105 }
106 
107 
108 /** Get vision master.
109  * @return vision master
110  */
113 {
114  return __vision_dependency.provider()->vision_master();
115 }
116 
117 
118 /** Add a vision thread.
119  * @param thread thread to add
120  */
121 void
123 {
124  __vision_dependency.add(thread);
125 }
126 
127 /** Remove a vision thread.
128  * @param thread thread to remove
129  */
130 void
132 {
133  __vision_dependency.remove(thread);
134 }
135 
136 /** Query if vision thread can be removed.
137  * @param thread thread to query for
138  * @return true if the thread can be removed, false otherwise
139  */
140 bool
142 {
143  return __vision_dependency.can_remove(thread);
144 }
145 
146 
147 } // end namespace fawkes
virtual void init(Thread *thread)
Initialize thread.
Fawkes library namespace.
void remove_vision_thread(VisionAspect *thread)
Remove a vision thread.
Thread class encapsulation of pthreads.
Definition: thread.h:42
bool can_remove_vision_thread(VisionAspect *thread)
Query if vision thread can be removed.
Vision Master Aspect.
Definition: vision_master.h:39
firevision::VisionMaster * vision_master()
Get vision master.
Thread cannot be initialized.
virtual void finalize(Thread *thread)
Finalize thread.
Dependency violation exception.
Definition: dependency.h:32
Thread aspect to use in FireVision apps.
Definition: vision.h:35
void add_vision_thread(VisionAspect *thread)
Add a vision thread.
const char * name() const
Get name of thread.
Definition: thread.h:95
Thread cannot be finalized.
virtual bool prepare_finalize(Thread *thread)
Default finalize preparation.
void append(const char *format,...)
Append messages to the message list.
Definition: exception.cpp:341
Aspect initializer/finalizer base class.
Definition: inifin.h:36