Fawkes API  Fawkes Development Version
vision.cpp
1 
2 /***************************************************************************
3  * vision.cpp - Fawkes VisionAspect initializer/finalizer
4  *
5  * Created: Wed Nov 24 00:13:36 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.h>
25 #include <aspect/inifins/vision_master.h>
26 #include <aspect/vision.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 VisionAspectIniFin <aspect/inifins/vision.h>
35  * Initializer/finalizer for the VisionAspect.
36  * @author Tim Niemueller
37  */
38 
39 /** Constructor.
40  * @param master_inifin vision master aspect inifin to get master from
41  */
43  : AspectIniFin("VisionAspect")
44 {
45  __master_inifin = master_inifin;
46 }
47 
48 
49 void
51 {
52  VisionAspect *vision_thread;
53  vision_thread = dynamic_cast<VisionAspect *>(thread);
54  if (vision_thread == 0) {
55  throw CannotInitializeThreadException("Thread '%s' claims to have the "
56  "VisionAspect, but RTTI says it "
57  "has not. ", thread->name());
58  }
59 
60  try {
61  if ( (vision_thread->vision_thread_mode() == VisionAspect::CONTINUOUS) &&
62  (thread->opmode() != Thread::OPMODE_CONTINUOUS) ) {
63  throw CannotInitializeThreadException("Vision thread '%s' operates in "
64  "continuous mode but thread does not",
65  thread->name());
66  }
67  if ( (vision_thread->vision_thread_mode() == VisionAspect::CYCLIC) &&
68  (thread->opmode() != Thread::OPMODE_WAITFORWAKEUP) ) {
69  throw CannotInitializeThreadException("Vision thread '%s' operates in "
70  "cyclic mode but thread does not "
71  "operate in wait-for-wakeup mode.",
72  thread->name());
73  }
74 
75  __master_inifin->add_vision_thread(vision_thread);
76  vision_thread->init_VisionAspect(__master_inifin->vision_master());
77  } catch (DependencyViolationException &e) {
78  CannotInitializeThreadException ce("Dependency violation for "
79  "VisionAspect detected");
80  ce.append(e);
81  throw ce;
82  }
83 
84 }
85 
86 
87 bool
89 {
90  VisionAspect *vision_thread;
91  vision_thread = dynamic_cast<VisionAspect *>(thread);
92  if (vision_thread == 0) {
93  return true;
94  }
95 
96  if ( ! __master_inifin->can_remove_vision_thread(vision_thread) ) {
97  //__logger->log_warn("AspectIniFin", "Cannot remove vision master, there are "
98  // "still vision threads that depend on it");
99  return false;
100  }
101 
102  return true;
103 }
104 
105 void
107 {
108  VisionAspect *vision_thread;
109  vision_thread = dynamic_cast<VisionAspect *>(thread);
110  if (vision_thread == 0) {
111  throw CannotFinalizeThreadException("Thread '%s' claims to have the "
112  "VisionAspect, but RTTI says it "
113  "has not. ", thread->name());
114  }
115 
116  try {
117  __master_inifin->remove_vision_thread(vision_thread);
118  } catch (DependencyViolationException &e) {
119  CannotFinalizeThreadException ce("Dependency violation for "
120  "VisionAspect detected");
121  ce.append(e);
122  throw ce;
123  }
124 }
125 
126 } // end namespace fawkes
virtual bool prepare_finalize(Thread *thread)
Default finalize preparation.
Definition: vision.cpp:88
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.
OpMode opmode() const
Get operation mode.
Definition: thread.cpp:678
firevision::VisionMaster * vision_master()
Get vision master.
Thread cannot be initialized.
Dependency violation exception.
Definition: dependency.h:32
void init_VisionAspect(firevision::VisionMaster *vision_master)
Set vision master.
Definition: vision.cpp:75
Thread aspect to use in FireVision apps.
Definition: vision.h:35
VisionThreadMode vision_thread_mode()
Get the vision thread mode of this thread.
Definition: vision.cpp:85
void add_vision_thread(VisionAspect *thread)
Add a vision thread.
operate in continuous mode (default)
Definition: thread.h:53
const char * name() const
Get name of thread.
Definition: thread.h:95
virtual void init(Thread *thread)
Initialize thread.
Definition: vision.cpp:50
VisionAspectIniFin(VisionMasterAspectIniFin *master_inifin)
Constructor.
Definition: vision.cpp:42
Thread cannot be finalized.
operate in wait-for-wakeup mode
Definition: thread.h:54
void append(const char *format,...)
Append messages to the message list.
Definition: exception.cpp:341
virtual void finalize(Thread *thread)
Finalize thread.
Definition: vision.cpp:106
Initializer/finalizer for the VisionMasterAspect.
Definition: vision_master.h:41
Aspect initializer/finalizer base class.
Definition: inifin.h:36