Fawkes API  Fawkes Development Version
aspect_provider.cpp
1 
2 /***************************************************************************
3  * aspect_provider.cpp - Fawkes Aspect Provider initializer/finalizer
4  *
5  * Created: Thu Nov 25 12:20:43 2010 (Thanksgiving, Pittsburgh)
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/aspect_provider.h>
25 #include <aspect/inifins/aspect_provider.h>
26 #include <aspect/manager.h>
27 
28 namespace fawkes {
29 
30 /** @class AspectProviderAspectIniFin <aspect/inifins/aspect_provider.h>
31  * Initializer/finalizer for the AspectProviderAspect.
32  * This initializer/finalizer will register the AspectIniFin instance with
33  * the main aspect manager on init, and unregister it on finalization. it
34  * will deny unloading if there are still threads using the provided aspect.
35  * @author Tim Niemueller
36  */
37 
38 /** Constructor.
39  * @param manager aspect manager to register new aspects to
40  */
42 : AspectIniFin("AspectProviderAspect")
43 {
44  aspect_manager_ = manager;
45 }
46 
47 void
49 {
50  AspectProviderAspect *provider_thread;
51  provider_thread = dynamic_cast<AspectProviderAspect *>(thread);
52 
53  if (provider_thread == NULL) {
54  throw CannotInitializeThreadException("Thread '%s' claims to have the "
55  "AspectProviderAspect, but RTTI says it "
56  "has not. ",
57  thread->name());
58  }
59 
60  const std::list<AspectIniFin *> & aspects = provider_thread->aspect_provider_aspects();
61  std::list<AspectIniFin *>::const_iterator a;
62  for (a = aspects.begin(); a != aspects.end(); ++a) {
63  aspect_manager_->register_inifin(*a);
64  }
65 }
66 
67 bool
69 {
70  AspectProviderAspect *p_thr;
71  p_thr = dynamic_cast<AspectProviderAspect *>(thread);
72 
73  if (p_thr == NULL)
74  return true;
75 
76  const std::list<AspectIniFin *> & aspects = p_thr->aspect_provider_aspects();
77  std::list<AspectIniFin *>::const_iterator a;
78  for (a = aspects.begin(); a != aspects.end(); ++a) {
79  if (aspect_manager_->has_threads_for_aspect((*a)->get_aspect_name())) {
80  return false;
81  }
82  }
83 
84  return true;
85 }
86 
87 void
89 {
90  AspectProviderAspect *provider_thread;
91  provider_thread = dynamic_cast<AspectProviderAspect *>(thread);
92 
93  if (provider_thread == NULL) {
94  throw CannotFinalizeThreadException("Thread '%s' claims to have the "
95  "AspectProviderAspect, but RTTI says it "
96  "has not. ",
97  thread->name());
98  }
99 
100  const std::list<AspectIniFin *> & aspects = provider_thread->aspect_provider_aspects();
101  std::list<AspectIniFin *>::const_iterator a;
102  for (a = aspects.begin(); a != aspects.end(); ++a) {
103  aspect_manager_->unregister_inifin(*a);
104  }
105 }
106 
107 } // end namespace fawkes
fawkes::AspectProviderAspect::aspect_provider_aspects
const std::list< AspectIniFin * > & aspect_provider_aspects() const
Get name of the provided aspect.
Definition: aspect_provider.cpp:86
fawkes::AspectProviderAspectIniFin::AspectProviderAspectIniFin
AspectProviderAspectIniFin(AspectManager *manager)
Constructor.
Definition: aspect_provider.cpp:47
fawkes::AspectProviderAspect
Definition: aspect_provider.h:41
fawkes::CannotInitializeThreadException
Definition: thread_initializer.h:39
fawkes::Thread::name
const char * name() const
Definition: thread.h:100
fawkes
fawkes::AspectProviderAspectIniFin::finalize
virtual void finalize(Thread *thread)
Definition: aspect_provider.cpp:94
fawkes::AspectProviderAspectIniFin::prepare_finalize
virtual bool prepare_finalize(Thread *thread)
Default finalize preparation.
Definition: aspect_provider.cpp:74
fawkes::Thread
Definition: thread.h:45
fawkes::AspectManager::register_inifin
void register_inifin(AspectIniFin *inifin)
Register initializer/finalizer.
Definition: manager.cpp:83
fawkes::AspectManager::has_threads_for_aspect
bool has_threads_for_aspect(const char *aspect_name)
Check if threads for a particular aspect still exist.
Definition: manager.cpp:115
fawkes::CannotFinalizeThreadException
Definition: thread_finalizer.h:39
fawkes::AspectManager::unregister_inifin
void unregister_inifin(AspectIniFin *inifin)
Unregister initializer/finalizer.
Definition: manager.cpp:95
fawkes::AspectProviderAspectIniFin::init
virtual void init(Thread *thread)
Definition: aspect_provider.cpp:54