Fawkes API  Fawkes Development Version
asp_inifin.cpp
1 
2 /***************************************************************************
3  * asp_inifin.cpp - Fawkes ASPAspect initializer/finalizer
4  *
5  * Created: Thu Oct 20 15:49:31 2016
6  * Copyright 2016 Björn Schäpers
7  * 2018 Tim Niemueller [www.niemueller.org]
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 <core/threading/thread_finalizer.h>
25 #include <logging/logger.h>
26 #include <plugins/asp/aspect/asp.h>
27 #include <plugins/asp/aspect/asp_inifin.h>
28 #include <plugins/asp/aspect/clingo_access.h>
29 #include <plugins/asp/aspect/clingo_control_manager.h>
30 
31 namespace fawkes {
32 
33 /**
34  * @class ASPAspectIniFin <plugins/asp/aspect/asp_inifin.h>
35  * ASPAspect initializer/finalizer.
36  * This initializer/finalizer will provide the ASP node handle to threads with the ASPAspect.
37  * @author Björn Schäpers
38  *
39  * @property ASPAspectIniFin::ctrl_mgr_
40  * @brief The control manager.
41  */
42 
43 /** Constructor. */
44 ASPAspectIniFin::ASPAspectIniFin() : AspectIniFin("ASPAspect")
45 {
46 }
47 
48 /** Destructor. */
50 {
51 }
52 
53 void
55 {
56  ASPAspect *asp_thread = dynamic_cast<ASPAspect *>(thread);
57  if (asp_thread == nullptr) {
58  throw CannotInitializeThreadException("Thread '%s' claims to have the ASPAspect, "
59  "but RTTI says it has not.",
60  thread->name());
61  }
62 
63  asp_thread->init_ASPAspect(
64  ctrl_mgr_->create_control(asp_thread->control_name_, asp_thread->log_comp_));
65 }
66 
67 void
68 ASPAspectIniFin::finalize(Thread *thread)
69 {
70  ASPAspect *asp_thread = dynamic_cast<ASPAspect *>(thread);
71  if (asp_thread == nullptr) {
72  throw CannotFinalizeThreadException("Thread '%s' claims to have the ASPAspect, "
73  "but RTTI says it has not.",
74  thread->name());
75  }
76 
77  asp_thread->finalize_ASPAspect();
78 }
79 
80 /** Sets the control manager.
81  * @param[in] ctrl_mgr The new control manager
82  */
83 void
84 ASPAspectIniFin::set_control_manager(const LockPtr<ClingoControlManager> &ctrl_mgr)
85 {
86  ctrl_mgr_ = ctrl_mgr;
87 }
88 
89 } // end namespace fawkes
fawkes::CannotInitializeThreadException
Definition: thread_initializer.h:37
fawkes::Thread::name
const char * name() const
Definition: thread.h:99
fawkes::ASPAspectIniFin::init
void init(Thread *thread) override
Definition: asp_inifin.cpp:58
fawkes::ASPAspect
Definition: asp.h:40
fawkes::ASPAspectIniFin::~ASPAspectIniFin
~ASPAspectIniFin(void)
Destructor.
Definition: asp_inifin.cpp:53
fawkes
fawkes::Thread
Definition: thread.h:44
fawkes::ASPAspectIniFin::finalize
void finalize(Thread *thread) override
Definition: asp_inifin.cpp:72
fawkes::CannotFinalizeThreadException
Definition: thread_finalizer.h:37
fawkes::ASPAspectIniFin::ASPAspectIniFin
ASPAspectIniFin(void)
Constructor.
Definition: asp_inifin.cpp:48
fawkes::ASPAspectIniFin::set_control_manager
void set_control_manager(const LockPtr< ClingoControlManager > &ctrl_mgr)
Sets the control manager.
Definition: asp_inifin.cpp:88