LCOV - code coverage report
Current view: top level - src/bindings/cpp/examples - cpp_example_userexception.cpp (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 0 26 0.0 %
Date: 2022-05-21 16:19:22 Functions: 0 17 0.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief This example explains how to define user defined exception.
       5             :  *
       6             :  * That means that the user can implement their own exceptions,
       7             :  * e.g. potentially derived from their own base class with its
       8             :  * own what() information.
       9             :  * The user could even decide to use another mechanism
      10             :  * instead of what().
      11             :  *
      12             :  * The only requirements are: They must be named exactly like
      13             :  * the original classes and KDBException constructor must take a key
      14             :  * as argument (which has all information like error and warnings).
      15             :  *
      16             :  * It does not matter from which class the exceptions are derived
      17             :  * or which members they have (if they are binary compatible) as
      18             :  * long as they are used everywhere.
      19             :  *
      20             :  * Never use non-binary compatible user exceptions if you do not
      21             :  * use them everywhere where you include any of elektras header
      22             :  * files!
      23             :  *
      24             :  *
      25             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
      26             :  */
      27             : 
      28             : #include "kdbmacros.h"
      29             : #include <stdexcept>
      30             : 
      31           0 : class UserException : public std::exception
      32             : {
      33             : public:
      34           0 :         virtual const char * what () const throw () override
      35             :         {
      36           0 :                 return "User Exception";
      37             :         }
      38             : };
      39             : 
      40             : namespace kdb
      41             : {
      42             : 
      43           0 : class Exception : public UserException
      44             : {
      45             : public:
      46           0 :         virtual const char * what () const throw () override
      47             :         {
      48           0 :                 return "User Exception: Exception thrown by Elektra";
      49             :         }
      50             : };
      51             : 
      52           0 : class KeyException : public Exception
      53             : {
      54             : public:
      55           0 :         virtual const char * what () const throw () override
      56             :         {
      57           0 :                 return "User Exception: Exception thrown by a Key";
      58             :         }
      59             : };
      60             : 
      61             : class KeyNotFoundException : public Exception
      62             : {
      63             : public:
      64             :         explicit KeyNotFoundException (std::string const &)
      65             :         {
      66             :         }
      67             :         virtual const char * what () const throw ()
      68             :         {
      69             :                 return "User Exception: Key not found";
      70             :         }
      71             : 
      72             : private:
      73             : };
      74             : 
      75             : class KeyTypeMismatch : public KeyException
      76             : {
      77             : public:
      78             :         virtual const char * what () const throw () override
      79             :         {
      80             :                 return "User Exception: Binary or String key mismatch";
      81             :         }
      82             : };
      83             : 
      84           0 : class KeyInvalidName : public KeyException
      85             : {
      86             : public:
      87             :         KeyInvalidName (const std::string & name ELEKTRA_UNUSED, const std::string & more ELEKTRA_UNUSED)
      88           0 :         {
      89             :         }
      90             : 
      91           0 :         virtual const char * what () const throw () override
      92             :         {
      93           0 :                 return "User Exception: Invalid Keyname";
      94             :         }
      95             : };
      96             : 
      97             : class KeyTypeConversion : public KeyException
      98             : {
      99             : public:
     100             :         virtual const char * what () const throw () override
     101             :         {
     102             :                 return "User Exception: Exception thrown by get/set";
     103             :         }
     104             : };
     105             : } // namespace kdb
     106             : 
     107             : #define USER_DEFINED_EXCEPTIONS
     108             : #include <key.hpp>
     109             : 
     110             : namespace kdb
     111             : {
     112             : 
     113             : class KDBException : public Exception
     114             : {
     115             : public:
     116           0 :         explicit KDBException (Key key) : m_key (key)
     117             :         {
     118           0 :         }
     119             : 
     120           0 :         virtual ~KDBException () throw ()
     121           0 :         {
     122           0 :         }
     123             : 
     124           0 :         virtual const char * what () const throw () override
     125             :         {
     126           0 :                 return "User Exception: KDB";
     127             :         }
     128             : 
     129             : protected:
     130             :         Key m_key;
     131             : };
     132             : 
     133             : class ContractException : public KDBException
     134             : {
     135             : public:
     136             :         explicit ContractException (Key key) : KDBException (key)
     137             :         {
     138             :         }
     139             : 
     140             :         ~ContractException () noexcept override = default;
     141             : 
     142             :         const char * what () const noexcept override
     143             :         {
     144             :                 if (!m_key)
     145             :                 {
     146             :                         return "Malformed contract";
     147             :                 }
     148             :                 return KDBException::what ();
     149             :         }
     150             : };
     151             : 
     152             : } // namespace kdb
     153             : 
     154             : 
     155             : #include <kdb.hpp>
     156             : 
     157           0 : int main ()
     158             : {
     159           0 :         kdb::Key k ("abc", KEY_END);
     160           0 :         kdb::KDB kdb;
     161           0 :         kdb::KeySet ks;
     162           0 :         kdb.get (ks, k);
     163           0 : }

Generated by: LCOV version 1.13