VirtualMachineManager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
3 
4  This file is part of the TerraLib - a Framework for building GIS enabled applications.
5 
6  TerraLib is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published by
8  the Free Software Foundation, either version 3 of the License,
9  or (at your option) any later version.
10 
11  TerraLib is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with TerraLib. See COPYING. If not, write to
18  TerraLib Team at <terralib-team@terralib.org>.
19  */
20 
21 /*!
22  \file terralib/vm/core/VirtualMachineManager.cpp
23 
24  \brief A singleton for managing TerraLib Virtual Machines.
25 
26  \author Frederico Augusto BedĂȘ
27  \author Gilberto Ribeiro de Queiroz
28 */
29 
30 // TerraLib
31 #include "VirtualMachineManager.h"
32 
33 #include "Exception.h"
34 #include "VirtualMachine.h"
35 
36 #include "../../core/translator/Translator.h"
37 
38 // STL
39 #include <map>
40 
42 {
43  std::map<std::string, std::shared_ptr<VirtualMachine> > m_vms; //!< A map from (VM id) to (VM instance).
44 };
45 
47 te::vm::core::VirtualMachineManager::get(const std::string& id) const
48 {
49  std::map<std::string, std::shared_ptr<VirtualMachine> >::iterator it = m_pimpl->m_vms.find(id);
50 
51  if(it == m_pimpl->m_vms.end())
52  throw te::OutOfRangeException() << te::ErrorDescription(TE_TR("There is no Virtual Machine registered for the required id."));
53 
54  return it->second.get();
55 }
56 
57 void
58 te::vm::core::VirtualMachineManager::insert(const std::string& id, std::unique_ptr<VirtualMachine> vm)
59 {
60  std::map<std::string, std::shared_ptr<VirtualMachine> >::iterator it = m_pimpl->m_vms.find(id);
61 
62  if(it != m_pimpl->m_vms.end())
63  throw te::InvalidArgumentException() << te::ErrorDescription(TE_TR("There is a Virtual Machine already registered with the required id."));
64 
65  m_pimpl->m_vms[id] = std::shared_ptr<VirtualMachine>(vm.release());
66 }
67 
68 void
70 {
71  m_pimpl->m_vms.clear();
72 }
73 
74 void
76 {
77  std::map<std::string, std::shared_ptr<VirtualMachine> >::iterator it = m_pimpl->m_vms.find(id);
78 
79  if(it == m_pimpl->m_vms.end())
80  throw te::OutOfRangeException() << te::ErrorDescription(TE_TR("There is NO Virtual Machine registered for the required id."));
81 
82  m_pimpl->m_vms.erase(id);
83 }
84 
87 {
88  static VirtualMachineManager m_singleton; // The singleton instance.
89 
90  return m_singleton;
91 }
92 
94 {
95  m_pimpl = new Impl();
96 }
97 
99 {
100  delete m_pimpl;
101 }
void insert(const std::string &id, std::unique_ptr< VirtualMachine > vm)
It adds a new VM to be managed.
An abstract class that defines a Virtual Machine for executing code in any TerraLib supported languag...
A singleton for managing TerraLib Virtual Machines.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
void erase(const std::string &id)
It removes a given virtual machine.
boost::error_info< struct tag_error_description, std::string > ErrorDescription
The base type for error report messages.
~VirtualMachineManager()
Singleton destructor.
static VirtualMachineManager & instance()
Return a reference to the singleton.
VirtualMachineManager()
Singleton constructor.
An exception indicating that a given item was not found in a collection (or range).
Exception classes for the TerraLib Virtual Machine Library.
VirtualMachine * get(const std::string &id) const
It returns the VM identified by id.
An exception indicating that a given argument is not valid, for instance if a given item already exis...
std::map< std::string, std::shared_ptr< VirtualMachine > > m_vms
A map from (VM id) to (VM instance).
A singleton for managing TerraLib Virtual Machines.