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