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