• Main Page
  • Related Pages
  • Classes
  • Files
  • File List

src/ScopedMap.hh

00001 /*
00002  * This file is part of peekabot.
00003  *
00004  * peekabot is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 3 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * peekabot is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #ifndef __PEEKABOT_SCOPED_MAP_HH
00019 #define __PEEKABOT_SCOPED_MAP_HH
00020 
00021 #include <stdexcept>
00022 #include <map>
00023 #include <stack>
00024 #include <boost/any.hpp>
00025 
00026 namespace peekabot
00027 {
00042     class ScopedMap
00043     {
00044     public:
00055         template<class T> T & get_variable(const std::string & variable_name)
00056             throw(boost::bad_any_cast, std::runtime_error)
00057         {
00058             // Verify that the variable exists
00059             if(m_any_map.find(variable_name) == m_any_map.end())
00060             {
00061                 std::string error = "ScopedMap: variable \"";
00062                 error += variable_name + "\" not found!";
00063                 throw(std::runtime_error(error));
00064             }
00065             
00066             // Fetch the variable's scope stack
00067             boost::any & any_stack = m_any_map[variable_name];
00068             // May throw a bad_any_cast exception:
00069             std::stack<T> & value_stack = boost::any_cast<std::stack<T>&>(any_stack);
00070             
00071             return value_stack.top();
00072         }
00073 
00082         template<class T> void push_variable(const std::string & variable_name,
00083                                              const T& value)
00084             throw(boost::bad_any_cast)
00085         {
00086             // If the variable exists, push it to the top of the stack
00087             if(m_any_map.find(variable_name) != m_any_map.end())
00088             {
00089                 boost::any & any_stack = m_any_map[variable_name];
00090                 std::stack<T> & scope_stack = boost::any_cast<std::stack<T>&>(any_stack);
00091                 
00092                 scope_stack.push(value);                
00093             }
00094             else
00095             {
00096                 std::stack<T> s;
00097                 s.push(value);
00098                 m_any_map[variable_name] = s;
00099 
00100             }
00101 
00102         }
00103 
00114         template<class T> void pop_variable(const std::string & variable_name)
00115             throw(boost::bad_any_cast, std::runtime_error)
00116         {
00117             // Verify that the variable exists
00118             if(m_any_map.find(variable_name) == m_any_map.end())
00119             {
00120                 std::string error = "ScopedMap: variable \"";
00121                 error += variable_name + "\" not found!";
00122                 throw(std::runtime_error(error));
00123             }
00124             
00125             // Fetch the variable's scope stack
00126             boost::any & any_stack = m_any_map[variable_name];
00127             // May throw a bad_any_cast exception:
00128             std::stack<T> & value_stack = boost::any_cast<std::stack<T>&>(any_stack);
00129             
00130             value_stack.pop();
00131         }
00132 
00133 
00137         template<class T> bool exists(const std::string & variable_name)
00138         {
00139             // Check if there exists a variable with the specified name...
00140             if(m_any_map.find(variable_name) == m_any_map.end())
00141                 return false;
00142             
00143             // The variable exists, but is it of the right type?
00144             try
00145             {
00146                 // Fetch the variable's scope stack
00147                 boost::any & any_stack = m_any_map[variable_name];
00148                 // May throw a bad_any_cast exception:
00149                 std::stack<T> & value_stack = boost::any_cast<std::stack<T>&>(any_stack);
00150                 
00151                 // If the stack is empty, there is no value for the variable, it has gone
00152                 // out of scope, i.e. it doesn't exist.
00153                 if(value_stack.empty())
00154                     return false;
00155                 else // If it's non-empty, the variable exists and has a value.
00156                     return true;
00157             }
00158             catch(boost::bad_any_cast & error)
00159             {
00160                 // If we caught this exception, it means that the variable existed, but
00161                 // it was of the wrong type.
00162                 return false;
00163             }
00164         }
00165         
00166         
00167     private:
00168         std::map<std::string, boost::any> m_any_map;
00169     };
00170     
00171 }
00172 
00173 #endif //__PEEKABOT_SCOPED_MAP_HH

Generated on Sun Jan 30 2011 for peekabot by  doxygen 1.7.1