pelib  2.0.0
include/pelib/AmplOutputScalar.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/AmplOutputData.hpp>
00022 #include <pelib/DataParser.hpp>
00023 #include <pelib/Scalar.hpp>
00024 #include <pelib/CastException.hpp>
00025 #include <pelib/ParseException.hpp>
00026 #include <pelib/NoDecimalFloatException.hpp>
00027 
00028 #ifndef PELIB_AMPLOUTPUTSCALAR
00029 #define PELIB_AMPLOUTPUTSCALAR
00030 
00031 namespace pelib
00032 {
00034         template <class Value>
00035         class
00036         AmplOutputScalar: public AmplOutputData
00037         {
00038                 public:
00042                         AmplOutputScalar(bool strict = true)
00043                         {
00044                                 this->strict = strict;
00045                         }
00046                         
00048                         virtual
00049                         AmplOutputScalar*
00050                         clone() const
00051                         {
00052                                 return new AmplOutputScalar();
00053                         }
00054 
00056                         virtual
00057                         std::string
00058                         getDetailedPattern()
00059                         {
00060                                 return "(\\w[\\w\\d_]*)\\s*=\\s*([-+\\w\\d][-\\w\\d_\\.+]*)";
00061                         }
00062 
00064                         virtual
00065                         std::string
00066                         getGlobalPattern()
00067                         {
00068                                 return "\\w[\\w\\d_]*\\s*=\\s*[-+\\w\\d][-\\w\\d_\\.+]*";
00069                         }
00070 
00072                         virtual
00073                         AlgebraData*
00074                         parse(std::istream &in)
00075                         {
00076                                 std::string str((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
00077 
00078                                 boost::cmatch match;
00079                                 if(!boost::regex_match(str.c_str(), match, boost::regex(std::string("(?:.*?)").append(getDetailedPattern()))))
00080                                 {
00081                                         throw ParseException(std::string("String \"").append(str).append("\" doesn't match regex \"").append(getDetailedPattern()).append("\". "));
00082                                 }
00083 
00084                                 Scalar<Value> *scalar = new Scalar<Value>(match[1], AlgebraDataParser::convert<Value>(match[2], strict));
00085                                 return scalar;
00086                         }
00087 
00089                         virtual
00090                         void
00091                         dump(std::ostream &stream, const AlgebraData *data) const
00092                         {
00093                                 const Scalar<Value> *scalar = dynamic_cast<const Scalar<Value>* >(data);
00094                                 if(scalar == NULL) throw CastException("parameter \"data\" was not of type \"Scalar<Value>\".");
00095 
00096                                 Value val = scalar->getValue();
00097                                 if(scalar->getPrecision() == AlgebraData::higher)
00098                                 {
00099                                         val = AlgebraData::fixPrecision(val, stream.precision());
00100                                 }
00101 
00102                                 stream << scalar->getName() << " = " << val << std::endl;
00103                         }
00104         
00105                 protected:
00106                         bool strict;
00107                 private:                
00108         };
00109 }
00110 
00111 #endif