pelib  2.0.0
include/pelib/CppVector.hpp
Go to the documentation of this file.
00001 /*
00002  Copyright 2015 Nicolas Melot
00003 
00004  This file is part of Pelib.
00005 
00006  Pelib is free software: you can redistribute it and/or modify
00007  it under the terms of the GNU General Public License as published by
00008  the Free Software Foundation, either version 3 of the License, or
00009  (at your option) any later version.
00010 
00011  Pelib is distributed in the hope that it will be useful,
00012  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  GNU General Public License for more details.
00015 
00016  You should have received a copy of the GNU General Public License
00017  along with Pelib. If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 
00021 #include <pelib/CppData.hpp>
00022 #include <pelib/Vector.hpp>
00023 #include <pelib/Scalar.hpp>
00024 #include <pelib/CastException.hpp>
00025 
00026 #ifndef PELIB_CPPVECTOR
00027 #define PELIB_CPPVECTOR
00028 
00029 namespace pelib
00030 {
00032         template <class Key, class Value>
00033         class CppVector: public CppData
00034         {
00035                 typedef std::map<Key, Value> VectorType;
00036                 
00037                 public:
00039                         CppVector() : CppData()
00040                         {
00041                                 // Do nothing
00042                         }
00043 
00045                         virtual
00046                         CppVector*
00047                         clone() const
00048                         {
00049                                 return new CppVector();
00050                         }
00051 
00056                         virtual
00057                         void
00058                         dump(std::ostream &o, const AlgebraData *data) const
00059                         {                               
00060                                 const Vector<Key, Value> *vector = dynamic_cast<const Vector<Key, Value>*>(data);
00061                                 if(vector == NULL) throw CastException("parameter \"data\" was not of type \"Vector<Key, Value>\".");
00062                                 
00063                                 // Dummy key and value and string for their type
00064                                 Key key;
00065                                 Value value;
00066                                 std::string key_str = type_name(typeid(key));
00067                                 std::string value_str = type_name(typeid(value));
00068 
00069                                 // Declare a map to contain values
00070                                 o << "std::map<" << key_str << ", " << value_str << "> map_" << vector->getName() << ";" << std::endl;
00071 
00072                                 // Fill the map with values
00073                                 std::map<Key, Value> map = vector->getValues();
00074                                 for(typename std::map<Key, Value>::iterator iter = map.begin(); iter != map.end(); iter++)
00075                                 {
00076                                         key = iter->first;
00077                                         value = iter->second;
00078                                         //key_str = type_name(typeid(key)).c_str();
00079                                         //value_str = type_name(typeid(value)).c_str();
00080                                         
00081                                         o << "map_" << vector->getName() << ".insert(std::pair<" << key_str.c_str() << ", " << value_str.c_str() << ">(" << key << ", " << value << "));" << std::endl;
00082                                 }
00083                                 
00084                                 o << "pelib::Vector<" << key_str << ", " << value_str << "> " << vector->getName() << "(\"" << vector->getName() << "\", map_" << vector->getName() << ");" << std::endl;       
00085                         }
00086 
00087                 protected:
00088                 private:        
00089         };
00090 }
00091 
00092 #endif