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. */
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
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
85 {
86  ctrl_mgr_ = ctrl_mgr;
87 }
88 
89 } // end namespace fawkes
Fawkes library namespace.
~ASPAspectIniFin(void)
Destructor.
Definition: asp_inifin.cpp:49
Thread class encapsulation of pthreads.
Definition: thread.h:45
void set_control_manager(const LockPtr< ClingoControlManager > &ctrl_mgr)
Sets the control manager.
Definition: asp_inifin.cpp:84
LockPtr<> is a reference-counting shared lockable smartpointer.
Definition: lockptr.h:54
const char * name() const
Get name of thread.
Definition: thread.h:100
Thread aspect to get access to an ASP solver.
Definition: asp.h:36
Thread cannot be finalized.
ASPAspectIniFin(void)
Constructor.
Definition: asp_inifin.cpp:44
void finalize(Thread *thread) override
Finalize thread.
Definition: asp_inifin.cpp:68
void init(Thread *thread) override
Initialize thread.
Definition: asp_inifin.cpp:54
Aspect initializer/finalizer base class.
Definition: inifin.h:33