pelib  2.0.0
src/AlgebraRawOutput.cpp
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 <iostream>
00022 #include <cstdlib>
00023 #include <fstream>
00024 #include <string>
00025 #include <boost/regex.hpp>
00026 #include <boost/algorithm/string.hpp>
00027 #include <iomanip>
00028 
00029 #include <pelib/AlgebraRawOutput.hpp>
00030 #include <pelib/RawScalar.hpp>
00031 #include <pelib/RawVector.hpp>
00032 #include <pelib/RawSet.hpp>
00033 #include <pelib/RawMatrix.hpp>
00034 
00035 #include <pelib/ParseException.hpp>
00036 #include <pelib/CastException.hpp>
00037 #include <pelib/PelibException.hpp>
00038 
00039 #ifdef debug
00040 #undef debug
00041 #endif
00042 
00043 #define debug(var) cout << "[" << __FILE__ << ":" << __FUNCTION__ << ":" << __LINE__ << "] " << #var << " = \"" << var << "\"" << endl;
00044 
00045 using namespace std;
00046 using namespace boost::algorithm;
00047 
00048 namespace pelib
00049 {
00050         AlgebraRawOutput::AlgebraRawOutput(std::vector<RawDataOutput*> handlers)
00051         {
00052                 this->outputs = handlers;
00053         }
00054 
00055         AlgebraRawOutput::AlgebraRawOutput(std::vector<RawDataOutput*> handlers, const std::vector<std::string> &list)
00056         {
00057                 this->outputs = handlers;
00058                 this->output_list = list;
00059         }
00060 
00061         AlgebraRawOutput::~AlgebraRawOutput()
00062         {
00063                 deleteOutputs();                        
00064         }
00065 
00066         void
00067         AlgebraRawOutput::deleteOutputs()
00068         {
00069                 for(std::vector<RawDataOutput*>::iterator i = outputs.begin(); i != outputs.end(); i = outputs.erase(i))
00070                 {
00071                         delete *i;
00072                 }
00073         }
00074 
00075         AlgebraRawOutput&
00076         AlgebraRawOutput::operator=(const AlgebraRawOutput &amplOutput)
00077         {
00078                 deleteOutputs();
00079 
00080                 for(std::vector<RawDataOutput*>::const_iterator i = amplOutput.outputs.begin(); i != amplOutput.outputs.end(); i++)
00081                 {
00082                         outputs.push_back((*i)->clone());
00083                 }
00084 
00085                 return *this;
00086         }
00087 
00088         void
00089         AlgebraRawOutput::dump(std::ostream& o, const Algebra &record) const
00090         {
00091                 std::map<std::string, const AlgebraData * const> records = record.getAllRecords();
00092                 if(output_list.size() > 0)
00093                 {
00094                         for(std::vector<std::string>::const_iterator i = output_list.begin(); i != output_list.end(); i++)
00095                         {
00096                                 std::map<std::string, const AlgebraData * const>::const_iterator rec = records.find(*i);
00097                                 if(rec != records.end())
00098                                 {
00099                                         dump(o, rec->second);
00100                                 }
00101                         }
00102                 }
00103                 else
00104                 {
00105                         for (std::map<std::string, const AlgebraData * const>::const_iterator rec = records.begin(); rec != records.end(); rec++)
00106                         {
00107                                 dump(o, rec->second);
00108                         }
00109                 }
00110         }
00111 
00112         void
00113         AlgebraRawOutput::dump(std::ostream& o, const AlgebraData *data) const
00114         {
00115                 std::vector<RawDataOutput*>::const_iterator out;
00116                 for (out = outputs.begin(); out != outputs.end(); out++)
00117                 {
00118                         const RawDataOutput *output = *out;
00119                         try
00120                         {
00121                                 output->dump(o, data);
00122                                 break;
00123                         } catch(CastException &e)
00124                         {
00125                                 // No suitable element to output
00126                                 // Couldn't cast the element to record: just let that go and try again with next element
00127                         }
00128                 }
00129 
00130                 if(out == outputs.end())
00131                 {
00132                         vector<RawDataOutput*> string_outputs = stringOutputs();
00133                         for (out = string_outputs.begin(); out != string_outputs.end(); out++)
00134                         {
00135                                 const RawDataOutput *output = *out;
00136                                 try
00137                                 {
00138                                         output->dump(o, data);
00139                                         break;
00140                                 } catch(CastException &e)
00141                                 {
00142                                         // No suitable element to output
00143                                         // Couldn't cast the element to record: just let that go and try again with next element
00144                                 }
00145                         }
00146 
00147                         if(out == string_outputs.end())
00148                         {
00149                                 throw PelibException("Could not find a suitable output format for data record of name \"" + string(data->getName()) + "\".");
00150                         }
00151                 }
00152         }
00153 
00154         void
00155         AlgebraRawOutput::dump(std::ostream& o, const AlgebraData &data) const
00156         {
00157                 dump(o, &data);
00158         }
00159 
00160         std::vector<RawDataOutput*> AlgebraRawOutput::floatOutputs()
00161         {
00162                 std::vector<RawDataOutput*> outputs;
00163 
00164                 outputs.push_back(new RawScalar<float>());
00165                 outputs.push_back(new RawVector<int, float>());
00166                 outputs.push_back(new RawSet<float>());
00167                 outputs.push_back(new RawMatrix<int, int, float>());
00168 
00169                 return outputs;
00170         }
00171 
00172         std::vector<RawDataOutput*> AlgebraRawOutput::stringOutputs()
00173         {
00174                 std::vector<RawDataOutput*> outputs;
00175 
00176                 outputs.push_back(new RawScalar<string>());
00177                 outputs.push_back(new RawVector<int, string>());
00178                 outputs.push_back(new RawSet<string>());
00179                 outputs.push_back(new RawMatrix<int, int, string>());
00180 
00181                 return outputs;
00182         }
00183 
00184         std::vector<RawDataOutput*> AlgebraRawOutput::intFloatOutputs()
00185         {
00186                 std::vector<RawDataOutput*> outputs;
00187 
00188                 outputs.push_back(new RawScalar<int>());
00189                 outputs.push_back(new RawScalar<float>());
00190                 outputs.push_back(new RawVector<int, int>());
00191                 outputs.push_back(new RawVector<int, float>());
00192                 outputs.push_back(new RawSet<int>());
00193                 outputs.push_back(new RawSet<float>());
00194                 outputs.push_back(new RawMatrix<int, int, int>());
00195                 outputs.push_back(new RawMatrix<int, int, float>());
00196 
00197                 return outputs;
00198         }
00199 }