pelib
2.0.0
|
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 <iomanip> 00027 00028 #include <pelib/Cpp.hpp> 00029 #include <pelib/CppScalar.hpp> 00030 #include <pelib/CppVector.hpp> 00031 #include <pelib/CppSet.hpp> 00032 #include <pelib/CppMatrix.hpp> 00033 00034 #include <pelib/ParseException.hpp> 00035 #include <pelib/CastException.hpp> 00036 00037 namespace pelib 00038 { 00039 Cpp::Cpp() 00040 { 00041 this->setName(std::string("make_algebra")); 00042 00043 // Add outputs 00044 addOutputs(); 00045 } 00046 00047 Cpp::Cpp(std::vector<CppData*> outputs) 00048 { 00049 this->setName(std::string("make_algebra")); 00050 this->outputs = outputs; 00051 } 00052 00053 Cpp::Cpp(const std::string &name) 00054 { 00055 this->setName(name); 00056 00057 // Add outputs 00058 addOutputs(); 00059 } 00060 00061 Cpp::Cpp(const Cpp &src) 00062 { 00063 *this = src; 00064 } 00065 00066 Cpp::~Cpp() 00067 { 00068 deleteOutputs(); 00069 } 00070 00071 std::string 00072 Cpp::getName() const 00073 { 00074 return this->name; 00075 } 00076 00077 void 00078 Cpp::setName(const std::string &name) 00079 { 00080 this->name = name; 00081 } 00082 00083 void 00084 Cpp::deleteOutputs() 00085 { 00086 for(std::vector<CppData*>::iterator i = outputs.begin(); i != outputs.end(); i = outputs.erase(i)) 00087 { 00088 delete *i; 00089 } 00090 } 00091 00092 Cpp& 00093 Cpp::operator=(const Cpp &src) 00094 { 00095 deleteOutputs(); 00096 this->name = src.getName(); 00097 00098 for(std::vector<CppData*>::const_iterator i = src.outputs.begin(); i != src.outputs.end(); i++) 00099 { 00100 outputs.push_back((*i)->clone()); 00101 } 00102 00103 return *this; 00104 } 00105 00106 void 00107 Cpp::dump(std::ostream& o, const Algebra &record) const 00108 { 00109 o << "pelib::Algebra" << std::endl << this->getName() << "()" << std::endl << "{" << std::endl; 00110 o << "pelib::Algebra new_algebra;" << std::endl; 00111 00112 std::map<std::string, const AlgebraData * const> records = record.getAllRecords(); 00113 for (std::map<std::string, const AlgebraData * const>::const_iterator rec = records.begin(); rec != records.end(); rec++) 00114 { 00115 try 00116 { 00117 dump(o, rec->second); 00118 o << "new_algebra.insert(&" << rec->first << ");" << std::endl; 00119 } 00120 catch (CastException &e) 00121 { 00122 // Do nothing 00123 } 00124 00125 } 00126 00127 o << "return new_algebra;" << std::endl; 00128 o << "}" << std::endl; 00129 } 00130 00131 void 00132 Cpp::dump(std::ostream& o, const AlgebraData *data) const 00133 { 00134 bool was_output = false; 00135 00136 for (std::vector<CppData*>::const_iterator out = outputs.begin(); out != outputs.end(); out++) 00137 { 00138 const CppDataOutput *output = *out; 00139 00140 try 00141 { 00142 output->dump(o, data); 00143 was_output = true; 00144 00145 break; 00146 } catch(CastException &e) 00147 { 00148 // No suitable element to output 00149 // Couldn't cast the element to record: just let that go and try again with next element 00150 } 00151 } 00152 00153 if(!was_output) 00154 { 00155 throw CastException("Could not find output for structure \"" + data->getName() + "\"."); 00156 } 00157 } 00158 00159 void 00160 Cpp::addOutputs() 00161 { 00162 outputs.push_back(new CppScalar<int>()); 00163 outputs.push_back(new CppScalar<float>()); 00164 outputs.push_back(new CppVector<int, int>()); 00165 outputs.push_back(new CppVector<int, float>()); 00166 outputs.push_back(new CppSet<int>()); 00167 outputs.push_back(new CppSet<float>()); 00168 outputs.push_back(new CppMatrix<int, int, int>()); 00169 outputs.push_back(new CppMatrix<int, int, float>()); 00170 } 00171 }