source: colin/trunk/src/EvaluationManagerFactory.cpp @ 5786

Revision 5786, 2.5 KB checked in by wehart, 5 years ago (diff)

Update of source to include Acro copyright statement

Line 
1/*  _________________________________________________________________________
2 *
3 *  Acro: A Common Repository for Optimizers
4 *  Copyright (c) 2008 Sandia Corporation.
5 *  This software is distributed under the BSD License.
6 *  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7 *  the U.S. Government retains certain rights in this software.
8 *  For more information, see the README.txt file in the top Acro directory.
9 *  _________________________________________________________________________
10 */
11
12#include <colin/EvaluationManagerFactory.h>
13#include <colin/EvaluationManager.h>
14
15using std::string;
16using std::pair;
17using std::map;
18
19namespace colin {
20namespace {
21
22static EvaluationManagerFactory* factory = NULL;
23
24} // namespace colin::(local)
25
26
27/** We use a static pointer here (and not a static object) to guarantee
28 *  that the EvalManagerFactory() singleton is NEVER destroyed before
29 *  any other singleton.
30 *
31 *  This is an intentional memory "leak".
32 */
33EvaluationManagerFactory& EvalManagerFactory()
34{
35   if ( factory == NULL )
36      factory = new EvaluationManagerFactory();
37   return *factory;
38}
39
40
41
42EvaluationManagerFactory::EvaluationManagerFactory()
43   : m_last_mgr_id(0),
44     m_default_manager_name("Serial"),
45     m_default_manager(NULL)
46{}
47
48
49EvaluationManagerFactory::~EvaluationManagerFactory()
50{
51   std::map<size_t, EvaluationManager_Base*>::iterator it = m_managers.begin();
52   while (it != m_managers.end())
53   {
54      delete it->second;
55      ++it;
56   }
57   m_managers.clear();
58}
59
60
61bool
62EvaluationManagerFactory::register_manager(std::string name, create_fcn_t fcn)
63{
64   if (! m_registry.insert(pair<string, create_fcn_t>(name, fcn)).second)
65   {
66      EXCEPTION_MNGR(std::runtime_error, "EvaluationManagerFactory::"
67                     "register_manager(): duplicate manager name, '"
68                     << name << "')");
69      return false;
70   }
71   return true;
72}
73
74
75EvaluationManager_Base*
76EvaluationManagerFactory::default_manager()
77{
78   if (m_default_manager == NULL)
79   { m_default_manager = create(m_default_manager_name); }
80
81   return m_default_manager;
82}
83
84
85EvaluationManager_Base*
86EvaluationManagerFactory::create(std::string name)
87{
88   map<string, create_fcn_t>::iterator it = m_registry.find(name);
89   if (it == m_registry.end())
90   {
91      EXCEPTION_MNGR(std::runtime_error, "EvaluationManagerFactory::create(): "
92                     "EvaluationManager name " << name << " DNE.");
93   }
94
95   ++m_last_mgr_id;
96   EvaluationManager_Base* ans = it->second(m_last_mgr_id);
97   m_managers[m_last_mgr_id] = ans;
98   return ans;
99}
100
101} // namespace colin
Note: See TracBrowser for help on using the repository browser.