errc.h

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 3 -*-
00002  *
00003  * Copyright (c), 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 
00045 #ifndef __PERRCH__
00046 #define __PERRCH__
00047 
00048 #include <memory.h>
00049 
00050 #ifdef _WIN32
00051 #define HOME "USERPROFILE"
00052 #else
00053 #define HOME "HOME"
00054 #endif
00055 
00056 #define PANDORE_TMP "PANDORE_TMP"
00057 
00058 namespace pandore {
00060    typedef enum { FAILURE = 0, SUCCESS } FS_t;
00061    
00062    class Errc;
00063    
00075    void Exit( const Errc &e );
00076    
00088    void Exit( FS_t v );
00089    
00101    void Exit( int v );
00102    
00114    void Exit( Char v );
00115    
00127    void Exit( Uchar v );
00128    
00140    void Exit( Short v );
00141    
00153    void Exit( Ushort v );
00154    
00166    void Exit( Long v );
00167    
00179    void Exit( Ulong v );
00180    
00192    void Exit( Llong v );
00193    
00205    void Exit( Ullong v );
00206    
00218    void Exit( Float v );
00219    
00231    void Exit( Double v );
00232    
00233    
00256    class Errc {
00257       private:
00258          enum { FS_RET
00259                 , Char_RET
00260                 , Uchar_RET
00261                 , Short_RET
00262                 , Ushort_RET
00263                 , Long_RET
00264                 , Ulong_RET
00265                 , Llong_RET
00266                 , Ullong_RET
00267                 , Float_RET
00268                 , Double_RET
00269          } _ret; 
00270          
00271       public :
00272          /*
00273           * Creates a value from the specified predefined error code.
00274           * The type <code>FS_t</code> allows only two predefined
00275           * values : {SUCCESS, FAILURE}
00276           * @param value        the specified error code between {SUCCESS, FAILURE}.
00277           */
00278          Errc( const FS_t value = SUCCESS ) {
00279             _ret = FS_RET;
00280             _val.fs = value;
00281          }
00282          
00283          /*
00284           * Creates a value from the specified boolean value.
00285           * @param value        the specified boolean value.
00286           */
00287          Errc( bool value ) {
00288             _ret = FS_RET;
00289             _val.fs = (value) ? SUCCESS : FAILURE;
00290          }
00291          
00292          /*
00293           * Creates a value from the specified Char value.
00294           * @param value        the specified Char value.
00295           */
00296          Errc( Char value ) {
00297             _ret = Char_RET;
00298             _val.v_Char = value;
00299          }
00300          /*
00301           * Creates a value from the specified Uchar value.
00302           * @param value        the specified Uchar value.
00303           */
00304          Errc( Uchar value ) {
00305             _ret = Uchar_RET;
00306             _val.v_Uchar = value;
00307          }
00308          /*
00309           * Creates a value from the specified Short value.
00310           * @param value        the specified Short value.
00311           */
00312          Errc( Short value ) {
00313             _ret = Short_RET;
00314             _val.v_Short = value;
00315          }
00316          /*
00317           * Creates a value from the specified Ushort value.
00318           * @param value        the specified Ushort value.
00319           */
00320          Errc( Ushort value ) {
00321             _ret = Ushort_RET;
00322             _val.v_Ushort = value;
00323          }
00324          /*
00325           * Creates a value from the specified Long value.
00326           * @param value        the specified Long value.
00327           */
00328          Errc( Long value ) {
00329             _ret = Long_RET;
00330             _val.v_Long = value;
00331          }
00332          /*
00333           * Creates a value from the specified Ulong value.
00334           * @param value        the specified Ulong value.
00335           */
00336          Errc( Ulong value ) {
00337             _ret = Ulong_RET;
00338             _val.v_Ulong = value;
00339          }
00340          /*
00341           * Creates a value from the specified Llong value.
00342           * @param value        the specified Llong value.
00343           */
00344          Errc( Llong value ) {
00345             _ret = Llong_RET;
00346             _val.v_Llong = value;
00347          }
00348          /*
00349           * Creates a value from the specified Ullong value.
00350           * @param value        the specified Ullong value.
00351           */
00352          Errc( Ullong value ) {
00353             _ret = Ullong_RET;
00354             _val.v_Ullong = value;
00355          }
00356          /*
00357           * Creates a value from the specified Float value.
00358           * @param value        the specified Float value.
00359           */
00360          Errc( Float value ) {
00361             _ret = Float_RET;
00362             _val.v_Float = value;
00363          }
00364          /*
00365           * Creates a value from the specified Double value.
00366           * @param value        the specified Double value.
00367           */
00368          Errc( Double value ) {
00369             _ret = Double_RET;
00370             _val.v_Double = value;
00371          }
00372          
00373          /*
00374           * Creates a value from the specified Errc.
00375           * @param error        the specified Errc.
00376           */
00377          Errc( const Errc &error ) {
00378             _ret = error._ret;
00379             memcpy(&_val, &error._val, sizeof(_val));
00380          }
00381          
00386          Errc& operator =( const Errc &error ) {
00387             _ret = error._ret;
00388             memcpy(&_val, &error._val, sizeof(_val));
00389             return *this;
00390          }
00391          
00392          /*
00393           * Converts the value to a boolean value.
00394           * @return     true if the current value is SUCCESS or a numerical value !=0.
00395           */
00396          operator bool() { 
00397             switch(_ret) {
00398                case FS_RET: return _val.fs == SUCCESS;
00399                case Char_RET: return 1;
00400                case Uchar_RET: return 1;
00401                case Short_RET: return 1;
00402                case Ushort_RET: return 1;
00403                case Long_RET: return 1;
00404                case Ulong_RET: return 1;
00405                case Llong_RET: return 1;
00406                case Ullong_RET: return 1;
00407                case Float_RET: return 1;
00408                case Double_RET: return 1;
00409                default: return false;
00410             }
00411          }
00412          
00413          /*
00414           * Converts the current value to an error code value;
00415           * i.e., a value in the predefined set {SUCCESS, FAILURE}.
00416           * @return     FAILURE if the current value is any form of 0, SUCCESS otherwise.
00417           */
00418          operator FS_t() { return _val.fs; }
00419          
00420          /*
00421           * Converts the value to a Char value.
00422           * @return     the related value.
00423           */
00424          operator Char() { 
00425             switch(_ret) {
00426                case FS_RET: return (Char)(_val.fs == SUCCESS);
00427                case Char_RET: return (Char)_val.v_Char;
00428                case Uchar_RET: return (Char)_val.v_Uchar;
00429                case Short_RET: return (Char)_val.v_Short;
00430                case Ushort_RET: return (Char)_val.v_Ushort;
00431                case Long_RET: return (Char)_val.v_Long;
00432                case Ulong_RET: return (Char)_val.v_Ulong;
00433                case Float_RET: return (Char)_val.v_Float;
00434                case Double_RET : return (Char)_val.v_Double;
00435                case Llong_RET : return (Char)_val.v_Double;
00436                case Ullong_RET : return (Char)_val.v_Double;
00437                default: return 0;
00438             }
00439          }
00440          /*
00441           * Converts the value to a Uchar value.
00442           * @return     the related value.
00443           */
00444          operator Uchar() { 
00445             switch(_ret) {
00446                case FS_RET: return (Uchar)(_val.fs == SUCCESS);
00447                case Char_RET: return (Uchar)_val.v_Char;
00448                case Uchar_RET: return (Uchar)_val.v_Uchar;
00449                case Short_RET: return (Uchar)_val.v_Short;
00450                case Ushort_RET: return (Uchar)_val.v_Ushort;
00451                case Long_RET: return (Uchar)_val.v_Long;
00452                case Ulong_RET: return (Uchar)_val.v_Ulong;
00453                case Float_RET: return (Uchar)_val.v_Float;
00454                case Double_RET : return (Uchar)_val.v_Double;
00455                case Llong_RET : return (Uchar)_val.v_Double;
00456                case Ullong_RET : return (Uchar)_val.v_Double;
00457                default: return 0;
00458             }
00459          }
00460          /*
00461           * Converts the value to a Short value.
00462           * @return     the related value.
00463           */
00464          operator Short() { 
00465             switch(_ret) {
00466                case FS_RET: return (Short)(_val.fs == SUCCESS);
00467                case Char_RET: return (Short)_val.v_Char;
00468                case Uchar_RET: return (Short)_val.v_Uchar;
00469                case Short_RET: return (Short)_val.v_Short;
00470                case Ushort_RET: return (Short)_val.v_Ushort;
00471                case Long_RET: return (Short)_val.v_Long;
00472                case Ulong_RET: return (Short)_val.v_Ulong;
00473                case Float_RET: return (Short)_val.v_Float;
00474                case Double_RET : return (Short)_val.v_Double;
00475                case Llong_RET : return (Short)_val.v_Double;
00476                case Ullong_RET : return (Short)_val.v_Double;
00477                default: return 0;
00478             }
00479          }
00480          /*
00481           * Converts the value to a Ushort value.
00482           * @return     the related value.
00483           */
00484          operator Ushort() { 
00485             switch(_ret) {
00486                case FS_RET: return (Ushort)(_val.fs == SUCCESS);
00487                case Char_RET: return (Ushort)_val.v_Char;
00488                case Uchar_RET: return (Ushort)_val.v_Uchar;
00489                case Short_RET: return (Ushort)_val.v_Short;
00490                case Ushort_RET: return (Ushort)_val.v_Ushort;
00491                case Long_RET: return (Ushort)_val.v_Long;
00492                case Ulong_RET: return (Ushort)_val.v_Ulong;
00493                case Float_RET: return (Ushort)_val.v_Float;
00494                case Double_RET : return (Ushort)_val.v_Double;
00495                case Llong_RET : return (Ushort)_val.v_Double;
00496                case Ullong_RET : return (Ushort)_val.v_Double;
00497                default: return 0;
00498             }
00499          }
00500          /*
00501           * Converts the value to a Long value.
00502           * @return     the related value.
00503           */
00504          operator Long() { 
00505             switch(_ret) {
00506                case FS_RET: return (Long)(_val.fs == SUCCESS);
00507                case Char_RET: return (Long)_val.v_Char;
00508                case Uchar_RET: return (Long)_val.v_Uchar;
00509                case Short_RET: return (Long)_val.v_Short;
00510                case Ushort_RET: return (Long)_val.v_Ushort;
00511                case Long_RET: return (Long)_val.v_Long;
00512                case Ulong_RET: return (Long)_val.v_Ulong;
00513                case Float_RET: return (Long)_val.v_Float;
00514                case Double_RET : return (Long)_val.v_Double;
00515                case Llong_RET : return (Long)_val.v_Double;
00516                case Ullong_RET : return (Long)_val.v_Double;
00517                default: return 0;
00518             }
00519          }
00520          /*
00521           * Converts the value to a Ulong value.
00522           * @return     the related value.
00523           */
00524          operator Ulong() { 
00525             switch(_ret) {
00526                case FS_RET: return (Ulong)(_val.fs == SUCCESS);
00527                case Char_RET: return (Ulong)_val.v_Char;
00528                case Uchar_RET: return (Ulong)_val.v_Uchar;
00529                case Short_RET: return (Ulong)_val.v_Short;
00530                case Ushort_RET: return (Ulong)_val.v_Ushort;
00531                case Long_RET: return (Ulong)_val.v_Long;
00532                case Ulong_RET: return (Ulong)_val.v_Ulong;
00533                case Float_RET: return (Ulong)_val.v_Float;
00534                case Double_RET : return (Ulong)_val.v_Double;
00535                case Llong_RET : return (Ulong)_val.v_Double;
00536                case Ullong_RET : return (Ulong)_val.v_Double;
00537                default: return 0;
00538             }
00539          }
00540          /*
00541           * Converts the value to a Llong value.
00542           * @return     the related value.
00543           */
00544          operator Llong() { 
00545             switch(_ret) {
00546                case FS_RET: return (Llong)(_val.fs == SUCCESS);
00547                case Char_RET: return (Llong)_val.v_Char;
00548                case Uchar_RET: return (Llong)_val.v_Uchar;
00549                case Short_RET: return (Llong)_val.v_Short;
00550                case Ushort_RET: return (Llong)_val.v_Ushort;
00551                case Long_RET: return (Llong)_val.v_Long;
00552                case Ulong_RET: return (Llong)_val.v_Ulong;
00553                case Float_RET: return (Llong)_val.v_Float;
00554                case Double_RET : return (Llong)_val.v_Double;
00555                case Llong_RET : return (Llong)_val.v_Double;
00556                case Ullong_RET : return (Llong)_val.v_Double;
00557                default: return 0;
00558             }
00559          }
00560          /*
00561           * Converts the value to a Ullong value.
00562           * @return     the related value.
00563           */
00564          operator Ullong() { 
00565             switch(_ret) {
00566                case FS_RET: return (Ullong)(_val.fs == SUCCESS);
00567                case Char_RET: return (Ullong)_val.v_Char;
00568                case Uchar_RET: return (Ullong)_val.v_Uchar;
00569                case Short_RET: return (Ullong)_val.v_Short;
00570                case Ushort_RET: return (Ullong)_val.v_Ushort;
00571                case Long_RET: return (Ullong)_val.v_Long;
00572                case Ulong_RET: return (Ullong)_val.v_Ulong;
00573                case Float_RET: return (Ullong)_val.v_Float;
00574                case Double_RET : return (Ullong)_val.v_Double;
00575                case Llong_RET : return (Ullong)_val.v_Double;
00576                case Ullong_RET : return (Ullong)_val.v_Double;
00577                default: return 0;
00578             }
00579          }
00580          /*
00581           * Converts the value to a Float value.
00582           * @return     the related value.
00583           */
00584          operator Float() { 
00585             switch(_ret) {
00586                case FS_RET: return (Float)(_val.fs == SUCCESS);
00587                case Char_RET: return (Float)_val.v_Char;
00588                case Uchar_RET: return (Float)_val.v_Uchar;
00589                case Short_RET: return (Float)_val.v_Short;
00590                case Ushort_RET: return (Float)_val.v_Ushort;
00591                case Long_RET: return (Float)_val.v_Long;
00592                case Ulong_RET: return (Float)_val.v_Ulong;
00593                case Float_RET: return (Float)_val.v_Float;
00594                case Double_RET : return (Float)_val.v_Double;
00595                case Llong_RET : return (Float)_val.v_Double;
00596                case Ullong_RET : return (Float)_val.v_Double;
00597                default: return 0;
00598             }
00599          }
00600          /*
00601           * Converts the value to a Double value.
00602           * @return     the related value.
00603           */
00604          operator Double() { 
00605             switch(_ret) {
00606                case FS_RET: return (Double)(_val.fs == SUCCESS);
00607                case Char_RET: return (Double)_val.v_Char;
00608                case Uchar_RET: return (Double)_val.v_Uchar;
00609                case Short_RET: return (Double)_val.v_Short;
00610                case Ushort_RET: return (Double)_val.v_Ushort;
00611                case Long_RET: return (Double)_val.v_Long;
00612                case Ulong_RET: return (Double)_val.v_Ulong;
00613                case Float_RET: return (Double)_val.v_Float;
00614                case Double_RET : return (Double)_val.v_Double;
00615                case Llong_RET : return (Double)_val.v_Double;
00616                case Ullong_RET : return (Double)_val.v_Double;
00617                default: return 0;
00618             }
00619          }
00620          
00624          bool operator !() {
00625             bool b = *this;
00626             return !b;
00627          }
00628          
00636          void Exit() const;
00637          
00638          friend bool operator !=( Errc &e1, FS_t value ) ;
00639          friend bool operator !=( FS_t value, Errc &e1 ) ;
00640          friend bool operator ==( Errc &e1, FS_t value ) ;
00641          friend bool operator ==( FS_t value, Errc &e1 ) ;
00642          friend bool operator &&( Errc &e1, Errc &e2 ) ;
00643          friend bool operator &&( Errc &e, bool b ) ;
00644          friend bool operator &&( bool b, Errc &e );
00645          
00646          friend bool operator ||( Errc &e1, Errc &e2 ) ;
00647          friend bool operator ||( Errc &e, bool b ) ;
00648          friend bool operator ||( bool b, Errc &e );
00649          
00650       private:
00652          union {
00653                FS_t fs;
00654                Char v_Char;
00655                Uchar v_Uchar;
00656                Short v_Short;
00657                Ushort v_Ushort;
00658                Long v_Long;
00659                Ulong v_Ulong;
00660                Llong v_Llong;
00661                Ullong v_Ullong;
00662                Float v_Float;
00663                Double v_Double;
00664          } _val;
00665    };
00666    
00667    inline bool operator ==( Errc &e, FS_t value ) {
00668       if (e._ret == Errc::FS_RET) {
00669          return e._val.fs == value;
00670       } else {
00671          return false;
00672       }
00673    }
00674    
00675    inline bool operator ==( FS_t value, Errc &e ) {
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 !=( Errc &e, FS_t value ) {
00684       if (e._ret == Errc::FS_RET) {
00685          return e._val.fs != value;
00686       } else {
00687          return true;
00688       }
00689    }
00690    
00691    inline bool operator !=( FS_t value, Errc &e ) {
00692       if (e._ret == Errc::FS_RET) {
00693          return e._val.fs != value;
00694       } else {
00695          return true;
00696       }
00697    }
00698    
00704    inline bool operator &&( Errc &e1, Errc &e2 ) {
00705       bool b1 = e1;
00706       bool b2 = e2;
00707       return b1 && b2;
00708    }
00709    
00715    inline bool operator &&( Errc &e, bool b ) {
00716       bool b1 = e;
00717       return b1 && b;
00718    }
00719    
00725    inline bool operator &&( bool b, Errc &e ) {
00726       bool b1 = e;
00727       return b && b1;
00728    }
00729    
00735    inline bool operator ||( Errc &e1, Errc &e2 ) {
00736       bool b1 = e1;
00737       bool b2 = e2;
00738       return b1 || b2;
00739    }
00740    
00746    inline bool operator ||( Errc &e, bool b ) {
00747       bool b1 = e;
00748       return b1 || b;
00749    }
00750 
00756    inline bool operator ||( bool b, Errc &e ) {
00757       bool b1 = e;
00758       return b || b1;
00759    }
00760 
00761 } //End of pandore:: namespace
00762 
00763 #endif // __PERRCH__

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