Changeset 10289 for trunk/FACT++


Ignore:
Timestamp:
04/05/11 12:08:24 (13 years ago)
Author:
tbretz
Message:
The Converter functions now throw exceptions. The previous way of reporting a conversion error was not convenient.
Location:
trunk/FACT++/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/Converter.cc

    r10287 r10289  
    3737Other conversion functions also exist.
    3838
    39 A result of a conversion is valid if valid() returns true and either the
    40 result of the conversion is empty and the format string was valid empty
    41 or both contain contents.
     39To check if the compilation of the format string was successfull
     40the valid() member functio is provided.
    4241
    4342The format parameter \b W(ord) is dedicated to this kind of conversion and
     
    494493//!    case of failure an empty vector is returned.
    495494//!
     495//! @throws
     496//!    std::runtime_error if the conversion was not successfull
     497//!
    496498template <class T>
    497499vector<T> Converter::Get(const std::string &str) const
    498500{
    499501    if (!valid())
    500         return vector<T>();
     502        throw runtime_error("Compiled format invalid!");
    501503
    502504    // If the format is empty we are already done
     
    546548            default:
    547549                // This should never happen!
    548                 wout << endl << kRed << "Format '" << i->first.first->name() << " not supported!" << endl;
    549                 break;
     550                throw runtime_error("Format '"+string(i->first.first->name())+" not supported!");
    550551            }
    551552
     
    564565    {
    565566        line.clear(); // This is necesasary to get a proper response from tellg()
    566         wout << kRed << "Error converting argument at " << arg << " [fmt=" << fFormat << "]!" << endl;
    567         wout << kRed << line.str() << endl;
    568         wout << kRed << setw(int(line.tellg())) << " " << "^" << endl;
    569         return vector<T>();
     567
     568        ostringstream err;
     569        err << "Error converting argument at " << arg << " [fmt=" << fFormat << "]!\n";
     570        err << line.str() << "\n";
     571        err << setw(int(line.tellg())) << " " << "^\n";
     572        throw runtime_error(err.str());
    570573    }
    571574
     
    574577    {
    575578        line.clear();
    576         wout << kRed << "Not enough arguments [fmt=" << fFormat << "]!" << endl;
    577         wout << kRed << line.str() << endl;
    578         wout << kRed << setw(int(line.tellg())+1) << " " << "^" << endl;
    579         return vector<T>();
     579
     580        ostringstream err;
     581        err << "Not enough arguments [fmt=" << fFormat << "]!\n";
     582        err << line.str() << "\n";
     583        err << setw(int(line.tellg())+1) << " " << "^\n";
     584        throw runtime_error(err.str());
    580585    }
    581586
     
    585590    if (line.good() && !line.eof())
    586591    {
    587         wout << kRed << "More arguments available than expected [" << fFormat << "]!" << endl;
    588         wout << kRed << line.str() << endl;
    589         wout << kRed << setw(int(line.tellg())+1) << " " << "^" << endl;
    590         return vector<T>();
     592        ostringstream err;
     593        err << "More arguments available than expected [fmt=" << fFormat << "]!\n";
     594        err << line.str() << "\n";
     595        err << setw(int(line.tellg())+1) << " " << "^\n";
     596        throw runtime_error(err.str());
    591597    }
    592598
     
    604610    return Get<char>(str);
    605611}
    606 
    607612
    608613// --------------------------------------------------------------------------
     
    619624//!    case of failure an empty vector is returned.
    620625//!
     626//! @throws
     627//!    std::runtime_error if the conversion was not successfull
     628//!
    621629template<class T>
    622630T Converter::Get(const void *dat, size_t size) const
    623631{
    624632    if (!valid())
    625         return T();
     633        throw runtime_error("Compiled format invalid!");
    626634
    627635    const char *ptr = reinterpret_cast<const char *>(dat);
     
    632640        if (ptr-size>=dat)
    633641        {
    634             wout << kRed << "Format description '" << fFormat << "' exceeds available data size (" << size << ")" << endl;
    635             return T();
     642            ostringstream err;
     643            err << "Format description [fmt=" << fFormat << "] exceeds available data size (" << size << ")";
     644            throw runtime_error(err.str());
    636645        }
    637646
     
    659668            case 'v':
    660669                // This should never happen!
    661                 wout << kRed << "Type 'void' not supported!" << endl;
    662                 return T();
     670                throw runtime_error("Type 'void' not supported!");
    663671            default:
    664                 // This should never happen!
    665                 wout << kRed << "TypeId '" << i->first.first->name() << "' not known!" << endl;
    666                 return T();
     672                throw runtime_error("TypeId '"+string(i->first.first->name())+"' not known!");
    667673            }
    668674        }
     
    671677    if (ptr-size!=dat)
    672678    {
    673         wout << kRed << "Data block size (" << size << ") doesn't fit format description '" << fFormat << "'" << endl;
    674         return T();
     679        ostringstream err;
     680        err << "Data block size (" << size << ") doesn't fit format description [fmt=" << fFormat << "]";
     681        throw runtime_error(err.str());
    675682    }
    676683
  • trunk/FACT++/src/ServiceList.cc

    r10267 r10289  
    554554
    555555    const Converter conv(lout, fmt);
    556 
    557     const vector<char> v = conv.GetVector(str.substr(p0));
    558     if (!conv.valid() || v.empty()!=conv.empty())
    559     {
    560         lout << kRed << "Couldn't properly parse the input... ignored." << endl;
     556    if (!conv)
     557    {
     558        lout << kRed << "Couldn't properly parse the format... ignored." << endl;
    561559        return false;
    562560    }
    563561
    564     const int rc = DimClient::sendCommand(cmd.c_str(), (void*)v.data(), v.size());
    565     if (rc)
    566         lout << kGreen << "Command " << cmd << " emitted successfully to DimClient." << endl;
    567     else
    568         lout << kRed << "ERROR - Sending command " << cmd << " failed." << endl;
     562    try
     563    {
     564        const vector<char> v = conv.GetVector(str.substr(p0));
     565
     566        const int rc = DimClient::sendCommand(cmd.c_str(), (void*)v.data(), v.size());
     567        if (rc)
     568            lout << kGreen << "Command " << cmd << " emitted successfully to DimClient." << endl;
     569        else
     570            lout << kRed << "ERROR - Sending command " << cmd << " failed." << endl;
     571    }
     572    catch (const std::runtime_error &e)
     573    {
     574        lout << kRed << e.what() << endl;
     575        return false;
     576    }
     577
    569578    return true;
    570579}
  • trunk/FACT++/src/StateMachineImp.cc

    r10273 r10289  
    234234
    235235    const Converter conv(lout, fmt, false);
    236 
    237     const vector<char> v = conv.GetVector(str.substr(p0));
    238     if (!conv.valid() || v.empty()!=conv.empty())
    239     {
    240         lout << kRed << "Couldn't properly parse the input... ignored." << endl;
     236    if (!conv)
     237    {
     238        lout << kRed << "Couldn't properly parse the format... ignored." << endl;
    241239        return false;
    242240    }
    243241
    244     return PostEvent(*evt, v.data(), v.size());
    245 /*
    246     const Converter c(lout, fmt, str.substr(p0));
    247 
    248     // Parsing was not successfull
    249     if (!c.GetRc())
    250     {
    251         lout << kRed << "Couldn't properly parse the input... ignored." << endl;
    252         return false;
    253     }
    254 
    255     return PostEvent(*evt, c.Ptr(), c.Size());
    256     */
     242    try
     243    {
     244        const vector<char> v = conv.GetVector(str.substr(p0));
     245        return PostEvent(*evt, v.data(), v.size());
     246    }
     247    catch (const std::runtime_error &e)
     248    {
     249        lout << kRed << e.what() << endl;
     250    }
     251
     252    return false;
    257253}
    258254
  • trunk/FACT++/src/dataLogger.cc

    r10272 r10289  
    637637
    638638                const Converter conv(Out(), I->getFormat());
    639 
    640                 std::string text = conv.GetString(I->getData(), I->getSize());
    641                 if (!conv.valid() || text.empty()!=conv.empty())
     639                if (!conv)
    642640                {
     641                    Error("Couldn't properly parse the format... ignored.");
     642                    return;
     643                }
     644
     645                std::string text;
     646                try
     647                {
     648                    text = conv.GetString(I->getData(), I->getSize());
     649                }
     650                catch (const std::runtime_error &e)
     651                {
     652                    Out() << kRed << e.what() << endl;
    643653                    Error("Couldn't properly parse the data... ignored.");
    644654                    return;
     
    706716               
    707717//      std::string text = ToString(evt.GetFormat().c_str(), evt.GetData(), evt.GetSize());
     718
    708719        const Converter conv(Out(), evt.GetFormat());
    709 
    710         std::string text = conv.GetString(evt.GetData(), evt.GetSize());
    711         if (!conv.valid() || text.empty()!=conv.empty())
     720        if (!conv)
    712721        {
     722            Error("Couldn't properly parse the format... ignored.");
     723            return GetCurrentState();
     724        }
     725
     726        std::string text;
     727        try
     728        {
     729            text = conv.GetString(evt.GetData(), evt.GetSize());
     730        }
     731        catch (const std::runtime_error &e)
     732        {
     733            Out() << kRed << e.what() << endl;
    713734            Error("Couldn't properly parse the data... ignored.");
    714735            return GetCurrentState();
    715736        }
     737
    716738
    717739        if (text.empty())
Note: See TracChangeset for help on using the changeset viewer.