LCOV - code coverage report
Current view: top level - snapwebsites - snap_string_list.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 5 19 26.3 %
Date: 2019-12-15 17:13:15 Functions: 3 14 21.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Snap Websites Server -- wrapper of popen()/pclose() with iostream like functions
       2             : // Copyright (c) 2013-2019  Made to Order Software Corp.  All Rights Reserved
       3             : //
       4             : // This program is free software; you can redistribute it and/or modify
       5             : // it under the terms of the GNU General Public License as published by
       6             : // the Free Software Foundation; either version 2 of the License, or
       7             : // (at your option) any later version.
       8             : //
       9             : // This program is distributed in the hope that it will be useful,
      10             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             : // GNU General Public License for more details.
      13             : //
      14             : // You should have received a copy of the GNU General Public License
      15             : // along with this program; if not, write to the Free Software
      16             : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
      17             : #pragma once
      18             : 
      19             : 
      20             : #include "snapwebsites/snap_exception.h"
      21             : 
      22             : 
      23             : // Qt lib
      24             : //
      25             : #include <QStringList>
      26             : 
      27             : 
      28             : // C++ lib
      29             : //
      30             : #include <vector>
      31             : 
      32             : 
      33             : 
      34             : namespace snap
      35             : {
      36             : 
      37           0 : class snap_string_list_exception : public snap_exception
      38             : {
      39             : public:
      40           0 :     explicit snap_string_list_exception(const char *        whatmsg) : snap_exception("snap_string_list", whatmsg) {}
      41             :     explicit snap_string_list_exception(const std::string & whatmsg) : snap_exception("snap_string_list", whatmsg) {}
      42             :     explicit snap_string_list_exception(const QString &     whatmsg) : snap_exception("snap_string_list", whatmsg) {}
      43             : };
      44             : 
      45             : 
      46           0 : class snap_string_list_exception_out_of_range : public snap_string_list_exception
      47             : {
      48             : public:
      49           0 :     explicit snap_string_list_exception_out_of_range(const char *        whatmsg) : snap_string_list_exception(whatmsg) {}
      50             :     explicit snap_string_list_exception_out_of_range(const std::string & whatmsg) : snap_string_list_exception(whatmsg) {}
      51             :     explicit snap_string_list_exception_out_of_range(const QString &     whatmsg) : snap_string_list_exception(whatmsg) {}
      52             : };
      53             : 
      54             : 
      55           1 : class snap_string_list
      56             :     : public QStringList
      57             : {
      58             : public:
      59           0 :     inline snap_string_list() noexcept : QStringList() { }
      60             :     inline explicit snap_string_list(const QString &i) : QStringList(i) { }
      61           1 :     inline snap_string_list(const QStringList &l) : QStringList(l) { }
      62             :     inline snap_string_list(const QList<QString> &l) : QStringList(l) { }
      63             : #ifdef Q_COMPILER_INITIALIZER_LISTS
      64             :     inline snap_string_list(std::initializer_list<QString> args) : QStringList(args) { }
      65             : #endif
      66             :     inline snap_string_list(const std::vector<QString> &l)
      67             :     {
      68             :         for(auto & s : l)
      69             :         {
      70             :             append(s);
      71             :         }
      72             :     }
      73             :     inline snap_string_list(const std::vector<std::string> &l)
      74             :     {
      75             :         for(auto & s : l)
      76             :         {
      77             :             append(QString::fromUtf8(s.c_str()));
      78             :         }
      79             :     }
      80             : 
      81           0 :     const QString & at(int i) const
      82             :     {
      83           0 :         if(i < 0 || i >= size())
      84             :         {
      85           0 :             throw snap_string_list_exception_out_of_range("index is out of range for the at() function");
      86             :         }
      87           0 :         return QStringList::at(i);
      88             :     }
      89             : 
      90           0 :     const QString & operator [] (int i) const
      91             :     {
      92           0 :         if(i < 0 || i >= size())
      93             :         {
      94           0 :             throw snap_string_list_exception_out_of_range("index is out of range for the at() function");
      95             :         }
      96           0 :         return QStringList::operator[](i);
      97             :     }
      98             : 
      99           4 :     QString & operator [] (int i)
     100             :     {
     101           4 :         if(i < 0 || i >= size())
     102             :         {
     103           0 :             throw snap_string_list_exception_out_of_range("index is out of range for the at() function");
     104             :         }
     105           4 :         return QStringList::operator[](i);
     106             :     }
     107             : 
     108             :     operator std::vector<QString> () const
     109             :     {
     110             :         std::vector<QString> result;
     111             : 
     112             :         for(auto const & s : *this)
     113             :         {
     114             :             result.push_back(s);
     115             :         }
     116             : 
     117             :         return result;
     118             :     }
     119             : 
     120             :     operator std::vector<std::string> () const
     121             :     {
     122             :         std::vector<std::string> result;
     123             : 
     124             :         for(auto const & s : *this)
     125             :         {
     126             :             result.push_back(s.toUtf8().data());
     127             :         }
     128             : 
     129             :         return result;
     130             :     }
     131             : 
     132             :     // we should add all the functions that deal with a bare index...
     133             : };
     134             : 
     135             : 
     136             : } // namespace snap
     137             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13