errc.h

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 3 -*-
00002  *
00003  * Copyright (c) 2013, GREYC.
00004  * All rights reserved
00005  *
00006  * You may use this file under the terms of the BSD license as follows:
00007  *
00008  * "Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are
00010  * met:
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above copyright
00014  *     notice, this list of conditions and the following disclaimer in
00015  *     the documentation and/or other materials provided with the
00016  *     distribution.
00017  *   * Neither the name of the GREYC, nor the name of its
00018  *     contributors may be used to endorse or promote products
00019  *     derived from this software without specific prior written
00020  *     permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00025  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00026  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00028  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00029  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00030  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00031  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00032  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
00033  *
00034  *
00035  * For more information, refer to:
00036  * https://clouard.users.greyc.fr/Pandore
00037  */
00038 
00053 #ifndef __PERRCH__
00054 #define __PERRCH__
00055 
00056 #include <memory.h>
00057 
00058 #ifdef _WIN32
00059 #define HOME "USERPROFILE"
00060 #else
00061 #define HOME "HOME"
00062 #endif
00063 
00064 #define PANDORE_TMP "PANDORE_TMP"
00065 
00066 namespace pandore {
00068    typedef enum { FAILURE = 0, SUCCESS } FS_t;
00069    
00070    class Errc;
00071    
00083    void Exit( const Errc &e );
00084    
00096    void Exit( FS_t v );
00097    
00109    void Exit( int v );
00110    
00122    void Exit( Char v );
00123    
00135    void Exit( Uchar v );
00136    
00148    void Exit( Short v );
00149    
00161    void Exit( Ushort v );
00162    
00174    void Exit( Long v );
00175    
00187    void Exit( Ulong v );
00188    
00200    void Exit( Llong v );
00201    
00213    void Exit( Ullong v );
00214    
00226    void Exit( Float v );
00227    
00239    void Exit( Double v );
00240    
00241    
00264    class Errc {
00265       private:
00266          enum { FS_RET
00267                 , Char_RET
00268                 , Uchar_RET
00269                 , Short_RET
00270                 , Ushort_RET
00271                 , Long_RET
00272                 , Ulong_RET
00273                 , Llong_RET
00274                 , Ullong_RET
00275                 , Float_RET
00276                 , Double_RET
00277          } _ret; 
00278          
00279       public :
00280          /*
00281           * Creates a value from the specified predefined error code.
00282           * The type <code>FS_t</code> allows only two predefined
00283           * values : {SUCCESS, FAILURE}
00284           * @param value        the specified error code between {SUCCESS, FAILURE}.
00285           */
00286          Errc( const FS_t value = SUCCESS ) {
00287             _ret = FS_RET;
00288             _val.fs = value;
00289          }
00290          
00291          /*
00292           * Creates a value from the specified boolean value.
00293           * @param value        the specified boolean value.
00294           */
00295          Errc( bool value ) {
00296             _ret = FS_RET;
00297             _val.fs = (value) ? SUCCESS : FAILURE;
00298          }
00299          
00300          /*
00301           * Creates a value from the specified Char value.
00302           * @param value        the specified Char value.
00303           */
00304          Errc( Char value ) {
00305             _ret = Char_RET;
00306             _val.v_Char = value;
00307          }
00308          /*
00309           * Creates a value from the specified Uchar value.
00310           * @param value        the specified Uchar value.
00311           */
00312          Errc( Uchar value ) {
00313             _ret = Uchar_RET;
00314             _val.v_Uchar = value;
00315          }
00316          /*
00317           * Creates a value from the specified Short value.
00318           * @param value        the specified Short value.
00319           */
00320          Errc( Short value ) {
00321             _ret = Short_RET;
00322             _val.v_Short = value;
00323          }
00324          /*
00325           * Creates a value from the specified Ushort value.
00326           * @param value        the specified Ushort value.
00327           */
00328          Errc( Ushort value ) {
00329             _ret = Ushort_RET;
00330             _val.v_Ushort = value;
00331          }
00332          /*
00333           * Creates a value from the specified Long value.
00334           * @param value        the specified Long value.
00335           */
00336          Errc( Long value ) {
00337             _ret = Long_RET;
00338             _val.v_Long = value;
00339          }
00340          /*
00341           * Creates a value from the specified Ulong value.
00342           * @param value        the specified Ulong value.
00343           */
00344          Errc( Ulong value ) {
00345             _ret = Ulong_RET;
00346             _val.v_Ulong = value;
00347          }
00348          /*
00349           * Creates a value from the specified Llong value.
00350           * @param value        the specified Llong value.
00351           */
00352          Errc( Llong value ) {
00353             _ret = Llong_RET;
00354             _val.v_Llong = value;
00355          }
00356          /*
00357           * Creates a value from the specified Ullong value.
00358           * @param value        the specified Ullong value.
00359           */
00360          Errc( Ullong value ) {
00361             _ret = Ullong_RET;
00362             _val.v_Ullong = value;
00363          }
00364          /*
00365           * Creates a value from the specified Float value.
00366           * @param value        the specified Float value.
00367           */
00368          Errc( Float value ) {
00369             _ret = Float_RET;
00370             _val.v_Float = value;
00371          }
00372          /*
00373           * Creates a value from the specified Double value.
00374           * @param value        the specified Double value.
00375           */
00376          Errc( Double value ) {
00377             _ret = Double_RET;
00378             _val.v_Double = value;
00379          }
00380          
00381          /*
00382           * Creates a value from the specified Errc.
00383           * @param error        the specified Errc.
00384           */
00385          Errc( const Errc &error ) {
00386             _ret = error._ret;
00387             memcpy(&_val, &error._val, sizeof(_val));
00388          }
00389          
00394          Errc& operator =( const Errc &error ) {
00395             _ret = error._ret;
00396             memcpy(&_val, &error._val, sizeof(_val));
00397             return *this;
00398          }
00399          
00400          /*
00401           * Converts the value to a boolean value.
00402           * @return     true if the current value is SUCCESS or a numerical value !=0.
00403           */
00404          operator bool() { 
00405             switch(_ret) {
00406                case FS_RET: return _val.fs == SUCCESS;
00407                case Char_RET: return 1;
00408                case Uchar_RET: return 1;
00409                case Short_RET: return 1;
00410                case Ushort_RET: return 1;
00411                case Long_RET: return 1;
00412                case Ulong_RET: return 1;
00413                case Llong_RET: return 1;
00414                case Ullong_RET: return 1;
00415                case Float_RET: return 1;
00416                case Double_RET: return 1;
00417                default: return false;
00418             }
00419          }
00420          
00421          /*
00422           * Converts the current value to an error code value;
00423           * i.e., a value in the predefined set {SUCCESS, FAILURE}.
00424           * @return     FAILURE if the current value is any form of 0, SUCCESS otherwise.
00425           */
00426          operator FS_t() { return _val.fs; }
00427          
00428          /*
00429           * Converts the value to a Char value.
00430           * @return     the related value.
00431           */
00432          operator Char() { 
00433             switch(_ret) {
00434                case FS_RET: return (Char)(_val.fs == SUCCESS);
00435                case Char_RET: return (Char)_val.v_Char;
00436                case Uchar_RET: return (Char)_val.v_Uchar;
00437                case Short_RET: return (Char)_val.v_Short;
00438                case Ushort_RET: return (Char)_val.v_Ushort;
00439                case Long_RET: return (Char)_val.v_Long;
00440                case Ulong_RET: return (Char)_val.v_Ulong;
00441                case Float_RET: return (Char)_val.v_Float;
00442                case Double_RET : return (Char)_val.v_Double;
00443                case Llong_RET : return (Char)_val.v_Double;
00444                case Ullong_RET : return (Char)_val.v_Double;
00445                default: return 0;
00446             }
00447          }
00448          /*
00449           * Converts the value to a Uchar value.
00450           * @return     the related value.
00451           */
00452          operator Uchar() { 
00453             switch(_ret) {
00454                case FS_RET: return (Uchar)(_val.fs == SUCCESS);
00455                case Char_RET: return (Uchar)_val.v_Char;
00456                case Uchar_RET: return (Uchar)_val.v_Uchar;
00457                case Short_RET: return (Uchar)_val.v_Short;
00458                case Ushort_RET: return (Uchar)_val.v_Ushort;
00459                case Long_RET: return (Uchar)_val.v_Long;
00460                case Ulong_RET: return (Uchar)_val.v_Ulong;
00461                case Float_RET: return (Uchar)_val.v_Float;
00462                case Double_RET : return (Uchar)_val.v_Double;
00463                case Llong_RET : return (Uchar)_val.v_Double;
00464                case Ullong_RET : return (Uchar)_val.v_Double;
00465                default: return 0;
00466             }
00467          }
00468          /*
00469           * Converts the value to a Short value.
00470           * @return     the related value.
00471           */
00472          operator Short() { 
00473             switch(_ret) {
00474                case FS_RET: return (Short)(_val.fs == SUCCESS);
00475                case Char_RET: return (Short)_val.v_Char;
00476                case Uchar_RET: return (Short)_val.v_Uchar;
00477                case Short_RET: return (Short)_val.v_Short;
00478                case Ushort_RET: return (Short)_val.v_Ushort;
00479                case Long_RET: return (Short)_val.v_Long;
00480                case Ulong_RET: return (Short)_val.v_Ulong;
00481                case Float_RET: return (Short)_val.v_Float;
00482                case Double_RET : return (Short)_val.v_Double;
00483                case Llong_RET : return (Short)_val.v_Double;
00484                case Ullong_RET : return (Short)_val.v_Double;
00485                default: return 0;
00486             }
00487          }
00488          /*
00489           * Converts the value to a Ushort value.
00490           * @return     the related value.
00491           */
00492          operator Ushort() { 
00493             switch(_ret) {
00494                case FS_RET: return (Ushort)(_val.fs == SUCCESS);
00495                case Char_RET: return (Ushort)_val.v_Char;
00496                case Uchar_RET: return (Ushort)_val.v_Uchar;
00497                case Short_RET: return (Ushort)_val.v_Short;
00498                case Ushort_RET: return (Ushort)_val.v_Ushort;
00499                case Long_RET: return (Ushort)_val.v_Long;
00500                case Ulong_RET: return (Ushort)_val.v_Ulong;
00501                case Float_RET: return (Ushort)_val.v_Float;
00502                case Double_RET : return (Ushort)_val.v_Double;
00503                case Llong_RET : return (Ushort)_val.v_Double;
00504                case Ullong_RET : return (Ushort)_val.v_Double;
00505                default: return 0;
00506             }
00507          }
00508          /*
00509           * Converts the value to a Long value.
00510           * @return     the related value.
00511           */
00512          operator Long() { 
00513             switch(_ret) {
00514                case FS_RET: return (Long)(_val.fs == SUCCESS);
00515                case Char_RET: return (Long)_val.v_Char;
00516                case Uchar_RET: return (Long)_val.v_Uchar;
00517                case Short_RET: return (Long)_val.v_Short;
00518                case Ushort_RET: return (Long)_val.v_Ushort;
00519                case Long_RET: return (Long)_val.v_Long;
00520                case Ulong_RET: return (Long)_val.v_Ulong;
00521                case Float_RET: return (Long)_val.v_Float;
00522                case Double_RET : return (Long)_val.v_Double;
00523                case Llong_RET : return (Long)_val.v_Double;
00524                case Ullong_RET : return (Long)_val.v_Double;
00525                default: return 0;
00526             }
00527          }
00528          /*
00529           * Converts the value to a Ulong value.
00530           * @return     the related value.
00531           */
00532          operator Ulong() { 
00533             switch(_ret) {
00534                case FS_RET: return (Ulong)(_val.fs == SUCCESS);
00535                case Char_RET: return (Ulong)_val.v_Char;
00536                case Uchar_RET: return (Ulong)_val.v_Uchar;
00537                case Short_RET: return (Ulong)_val.v_Short;
00538                case Ushort_RET: return (Ulong)_val.v_Ushort;
00539                case Long_RET: return (Ulong)_val.v_Long;
00540                case Ulong_RET: return (Ulong)_val.v_Ulong;
00541                case Float_RET: return (Ulong)_val.v_Float;
00542                case Double_RET : return (Ulong)_val.v_Double;
00543                case Llong_RET : return (Ulong)_val.v_Double;
00544                case Ullong_RET : return (Ulong)_val.v_Double;
00545                default: return 0;
00546             }
00547          }
00548          /*
00549           * Converts the value to a Llong value.
00550           * @return     the related value.
00551           */
00552          operator Llong() { 
00553             switch(_ret) {
00554                case FS_RET: return (Llong)(_val.fs == SUCCESS);
00555                case Char_RET: return (Llong)_val.v_Char;
00556                case Uchar_RET: return (Llong)_val.v_Uchar;
00557                case Short_RET: return (Llong)_val.v_Short;
00558                case Ushort_RET: return (Llong)_val.v_Ushort;
00559                case Long_RET: return (Llong)_val.v_Long;
00560                case Ulong_RET: return (Llong)_val.v_Ulong;
00561                case Float_RET: return (Llong)_val.v_Float;
00562                case Double_RET : return (Llong)_val.v_Double;
00563                case Llong_RET : return (Llong)_val.v_Double;
00564                case Ullong_RET : return (Llong)_val.v_Double;
00565                default: return 0;
00566             }
00567          }
00568          /*
00569           * Converts the value to a Ullong value.
00570           * @return     the related value.
00571           */
00572          operator Ullong() { 
00573             switch(_ret) {
00574                case FS_RET: return (Ullong)(_val.fs == SUCCESS);
00575                case Char_RET: return (Ullong)_val.v_Char;
00576                case Uchar_RET: return (Ullong)_val.v_Uchar;
00577                case Short_RET: return (Ullong)_val.v_Short;
00578                case Ushort_RET: return (Ullong)_val.v_Ushort;
00579                case Long_RET: return (Ullong)_val.v_Long;
00580                case Ulong_RET: return (Ullong)_val.v_Ulong;
00581                case Float_RET: return (Ullong)_val.v_Float;
00582                case Double_RET : return (Ullong)_val.v_Double;
00583                case Llong_RET : return (Ullong)_val.v_Double;
00584                case Ullong_RET : return (Ullong)_val.v_Double;
00585                default: return 0;
00586             }
00587          }
00588          /*
00589           * Converts the value to a Float value.
00590           * @return     the related value.
00591           */
00592          operator Float() { 
00593             switch(_ret) {
00594                case FS_RET: return (Float)(_val.fs == SUCCESS);
00595                case Char_RET: return (Float)_val.v_Char;
00596                case Uchar_RET: return (Float)_val.v_Uchar;
00597                case Short_RET: return (Float)_val.v_Short;
00598                case Ushort_RET: return (Float)_val.v_Ushort;
00599                case Long_RET: return (Float)_val.v_Long;
00600                case Ulong_RET: return (Float)_val.v_Ulong;
00601                case Float_RET: return (Float)_val.v_Float;
00602                case Double_RET : return (Float)_val.v_Double;
00603                case Llong_RET : return (Float)_val.v_Double;
00604                case Ullong_RET : return (Float)_val.v_Double;
00605                default: return 0;
00606             }
00607          }
00608          /*
00609           * Converts the value to a Double value.
00610           * @return     the related value.
00611           */
00612          operator Double() { 
00613             switch(_ret) {
00614                case FS_RET: return (Double)(_val.fs == SUCCESS);
00615                case Char_RET: return (Double)_val.v_Char;
00616                case Uchar_RET: return (Double)_val.v_Uchar;
00617                case Short_RET: return (Double)_val.v_Short;
00618                case Ushort_RET: return (Double)_val.v_Ushort;
00619                case Long_RET: return (Double)_val.v_Long;
00620                case Ulong_RET: return (Double)_val.v_Ulong;
00621                case Float_RET: return (Double)_val.v_Float;
00622                case Double_RET : return (Double)_val.v_Double;
00623                case Llong_RET : return (Double)_val.v_Double;
00624                case Ullong_RET : return (Double)_val.v_Double;
00625                default: return 0;
00626             }
00627          }
00628          
00632          bool operator !() {
00633             bool b = *this;
00634             return !b;
00635          }
00636          
00644          void Exit() const;
00645          
00646          friend bool operator !=( Errc &e1, FS_t value ) ;
00647          friend bool operator !=( FS_t value, Errc &e1 ) ;
00648          friend bool operator ==( Errc &e1, FS_t value ) ;
00649          friend bool operator ==( FS_t value, Errc &e1 ) ;
00650          friend bool operator &&( Errc &e1, Errc &e2 ) ;
00651          friend bool operator &&( Errc &e, bool b ) ;
00652          friend bool operator &&( bool b, Errc &e );
00653          
00654          friend bool operator ||( Errc &e1, Errc &e2 ) ;
00655          friend bool operator ||( Errc &e, bool b ) ;
00656          friend bool operator ||( bool b, Errc &e );
00657          
00658       private:
00660          union {
00661                FS_t fs;
00662                Char v_Char;
00663                Uchar v_Uchar;
00664                Short v_Short;
00665                Ushort v_Ushort;
00666                Long v_Long;
00667                Ulong v_Ulong;
00668                Llong v_Llong;
00669                Ullong v_Ullong;
00670                Float v_Float;
00671                Double v_Double;
00672          } _val;
00673    };
00674    
00675    inline bool operator ==( Errc &e, FS_t value ) {
00676       if (e._ret == Errc::FS_RET) {
00677          return e._val.fs == value;
00678       } else {
00679          return false;
00680       }
00681    }
00682    
00683    inline bool operator ==( FS_t value, Errc &e ) {
00684       if (e._ret == Errc::FS_RET) {
00685          return e._val.fs == value;
00686       } else {
00687          return false;
00688       }
00689    }
00690    
00691    inline bool operator !=( Errc &e, FS_t value ) {
00692       if (e._ret == Errc::FS_RET) {
00693          return e._val.fs != value;
00694       } else {
00695          return true;
00696       }
00697    }
00698    
00699    inline bool operator !=( FS_t value, Errc &e ) {
00700       if (e._ret == Errc::FS_RET) {
00701          return e._val.fs != value;
00702       } else {
00703          return true;
00704       }
00705    }
00706    
00712    inline bool operator &&( Errc &e1, Errc &e2 ) {
00713       bool b1 = e1;
00714       bool b2 = e2;
00715       return b1 && b2;
00716    }
00717    
00723    inline bool operator &&( Errc &e, bool b ) {
00724       bool b1 = e;
00725       return b1 && b;
00726    }
00727    
00733    inline bool operator &&( bool b, Errc &e ) {
00734       bool b1 = e;
00735       return b && b1;
00736    }
00737    
00743    inline bool operator ||( Errc &e1, Errc &e2 ) {
00744       bool b1 = e1;
00745       bool b2 = e2;
00746       return b1 || b2;
00747    }
00748    
00754    inline bool operator ||( Errc &e, bool b ) {
00755       bool b1 = e;
00756       return b1 || b;
00757    }
00758 
00764    inline bool operator ||( bool b, Errc &e ) {
00765       bool b1 = e;
00766       return b || b1;
00767    }
00768 
00769 } //End of pandore:: namespace
00770 
00771 #endif // __PERRCH__

The Pantheon project
Image Team GREYC Laboratory
UMR CNRS 6072 - ENSICAEN - University of Caen, France
This page was last modified on