UTStatus.h

Go to the documentation of this file.
00001 //==================================================================================================
00002 // Copyright (C) 2010  Brian Tietz    sdbtietz at yahoo dot com
00003 //
00004 // This program is free software; you can redistribute it and/or modify it under the terms of the
00005 // GNU General Public License as published by the Free Software Foundation, version 2.0 of the
00006 // License.
00007 //
00008 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
00009 // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00010 // General Public License for more details.
00011 //
00012 // You should have received a copy of the GNU General Public License along with this program; if
00013 // not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00014 // 02110-1301, USA.
00015 //
00016 // For commercial software, the copyright holder (Brian Tietz, email sdbtietz at yahoo dot com)
00017 // reserves the right and is willing to waive the proprietary source code disclosure aspects of that
00018 // license as applied to the UT library in exchange for either substantial contributions to the
00019 // development of the UT library or other forms of compensation.  Any such waiver must be
00020 // established in writing between the copyright holder and the commercial entity obtaining such a
00021 // waiver.
00022 //==================================================================================================
00023 
00024 
00025 #ifndef _UT_H_
00026 #error "DO NOT INCLUDE THIS FILE - INCLUDE UT.h INSTEAD"
00027 #endif
00028 
00029 
00030 //==================================================================================================
00031 //=== Error status value space integration
00032 //==================================================================================================
00040 typedef int32 error_code_t;
00041 
00042 #if __cplusplus
00047 #define ERROR_CODE(error_code)      ( static_cast<error_code_t>(error_code) )
00048 #else
00049 #define ERROR_CODE(error_code)      ( (error_code_t)(error_code) )
00050 #endif
00051 
00052 #if WINDOWS
00053 
00055 #define eERR_windows_error_base     ERROR_CODE(0xC0000000)
00056 
00057 #endif
00058 
00061 #define eERR_unknown_error          ERROR_CODE(0xE0000000)
00062 
00064 #define eERR_user_base              ERROR_CODE(0xE0010000)
00065 
00067 #define eERR_posix_error_base       ERROR_CODE(0xEF000000)
00068 
00069 
00070 
00071 
00072 //==================================================================================================
00073 //=== Forward name declarations
00074 //==================================================================================================
00075 #if __cplusplus
00076 
00077 class String_t;
00078 class RefCountedString_t;
00079 
00080 
00081 //==================================================================================================
00082 class UT_EXPORT Status_t
00083 //==================================================================================================
00096 {
00097     //----------------------------------------------------------------------------------------------
00098     public:
00099     //----------------------------------------------------------------------------------------------
00100     inline                      Status_t();
00103 
00104     inline                      Status_t(error_code_t error_code);
00113 
00114     inline                      Status_t( error_code_t error_code, const utf8* explanation );
00129 
00130     inline                      Status_t( error_code_t error_code, stringliteral* explanation );
00148 
00149                                 Status_t( error_code_t error_code, const String_t& explanation );
00164 
00165     inline                      Status_t(const Status_t& copy);
00169 
00170                                 Status_t( const Status_t& copy, const utf8* explanation );
00179 
00180                                 Status_t( const Status_t& copy, stringliteral* explanation );
00192 
00193                                 Status_t( const Status_t& copy, const String_t& explanation );
00202 
00203     inline                      ~Status_t();
00209 
00210     inline const Status_t&      operator =(const Status_t& set);
00219 
00220     inline const Status_t&      operator =(error_code_t set);
00227 
00228     inline error_code_t         StatusCode() const;
00232 
00233     inline bool                 Success() const;
00237 
00238     inline bool                 SuccessAndTrue() const;
00242 
00243     inline bool                 SuccessAndFalse() const;
00247 
00248     const String_t*             Explanation() const;
00250 
00251     bool                        operator ==(const Status_t& other) const;
00256 
00257     inline bool                 operator ==(error_code_t error_code) const;
00261 
00262     inline bool                 operator !=(const Status_t& other);
00268 
00269     inline bool                 operator !=(error_code_t error_code);
00273 
00274     inline bool                 operator <(int32 value);
00277 
00278     inline bool                 operator <=(int32 value);
00282 
00283     inline bool                 operator >(int32 value);
00287 
00288     inline bool                 operator >=(int32 value);
00292 
00293     #if WINDOWS
00294     static Status_t             GetLastWinError();
00297 
00298     static Status_t             GetLastWinError(const utf8* extra_explanation);
00306 
00307     static Status_t             GetLastWinError(const String_t& extra_explanation);
00315 
00316     static Status_t             GetLastWinError(stringliteral* extra_explanation);
00327 
00328     inline static Status_t      WinError(int32 windows_error_code);
00331 
00332     inline static Status_t      WinError(   int32 windows_error_code,
00333                                             const utf8* extra_explanation );
00342 
00343     inline static Status_t      WinError(   int32 windows_error_code,
00344                                             stringliteral* extra_explanation );
00356     #endif
00357 
00358     inline static Status_t      POSIXError(int posix_error_code);
00361 
00362     inline static Status_t      POSIXError( int posix_error_code,
00363                                             const utf8* extra_explanation );
00372 
00373     inline static Status_t      POSIXError( int posix_error_code,
00374                                             stringliteral* extra_explanation );
00386 
00387     //----------------------------------------------------------------------------------------------
00388     private:
00389     //----------------------------------------------------------------------------------------------
00390     void                        CreateExplanation(const utf8* explanation);
00391                                     // Creates explanatory text where m_explanation will be a
00392                                     // RefCountedString_t with a reference count of 1 and will have
00393                                     // made an autonomous copy of the explanatory text.
00394 
00395     void                        CreateExplanation(stringliteral* explanation);
00396                                     // Creates explanatory text where m_explanation will be a
00397                                     // RefCountedString_t with a reference count of 1.  The
00398                                     // explanatory String_t persistently references the string
00399                                     // literal; the pointer is borrowed.  Therefore the Status_t
00400                                     // object and its explanatory text, including copies of it and
00401                                     // copies returned by value are dependent on the pointer with
00402                                     // which it was constructed.  The contents of that pointer must
00403                                     // remain valid for the life of the Status_t object and all
00404                                     // copies.
00405 
00406     void                        AddExplanation(RefCountedString_t* explanation);
00407                                     // Attaches the specified explanation to this, and adds 
00408                                     // reference to it, preventing the explanation
00409                                     // RefCountedString_t object from being deleted until this 
00410                                     // either been deleted or assigned a new value.
00411 
00412     void                        RemoveExplanation();
00413                                     // Removes a reference to the attached explanatory text when
00414                                     // deleting this or assigning it a new value to facilitate its
00415                                     // deletion when the last reference is removed.
00416 
00417     //----------------------------------------------------------------------------------------------
00418     private:
00419     //----------------------------------------------------------------------------------------------
00420     error_code_t                m_error_code;
00421     RefCountedString_t*         m_explanation;
00422 };
00423 
00424 
00425 #endif // __cplusplus
00426 
00427 
00428 //==================================================================================================
00429 //=== User-defined error support for Status_t
00430 //==================================================================================================
00431 
00445 #define DEFINE_USER_ERROR( error, string )
00446 #include "UTErrorCodes.h"
00447 
00448 
00449 #if __cplusplus
00450 
00451 
00452 //==================================================================================================
00453 //==================================================================================================
00454 //===
00455 //=== Inline function implementations
00456 //===
00457 //==================================================================================================
00458 //==================================================================================================
00459 inline
00460 Status_t::Status_t()
00461 {
00462     m_error_code = 0;
00463     m_explanation = NULL;
00464 }
00465 
00466 
00467 inline
00468 Status_t::Status_t(error_code_t error_code)
00469 {
00470     m_error_code = error_code;
00471     m_explanation = NULL;
00472 }
00473 
00474 
00475 inline
00476 Status_t::Status_t( error_code_t error_code, const utf8* explanation )
00477 {
00478     m_error_code = error_code;
00479     assert(explanation);
00480     CreateExplanation(explanation);
00481 }
00482 
00483 
00484 inline
00485 Status_t::Status_t( error_code_t error_code, stringliteral* explanation )
00486 {
00487     m_error_code = error_code;
00488     assert(explanation);
00489     CreateExplanation(explanation);
00490 }
00491 
00492 
00493 inline
00494 Status_t::Status_t(const Status_t& copy)
00495 {
00496     m_error_code = copy.m_error_code;
00497     m_explanation = NULL;
00498     if(copy.m_explanation)
00499         AddExplanation(copy.m_explanation);
00500 }
00501 
00502 
00503 inline
00504 Status_t::~Status_t()
00505 {
00506     if(m_explanation)
00507         RemoveExplanation();
00508 }
00509 
00510 
00511 inline const Status_t&
00512 Status_t::operator =(const Status_t& set)
00513 {
00514     m_error_code = set.m_error_code;
00515     if(m_explanation)
00516         RemoveExplanation();
00517     if( set.Explanation() )
00518         AddExplanation(set.m_explanation);
00519     return *this;
00520 }
00521 
00522 
00523 inline const Status_t&
00524 Status_t::operator =(error_code_t set)
00525 {
00526     m_error_code = set;
00527     if(m_explanation)
00528         RemoveExplanation();
00529     return *this;
00530 }
00531 
00532 
00533 inline bool
00534 Status_t::Success() const
00535 {
00536     return (m_error_code >= 0);
00537 }
00538 
00539 
00540 inline bool
00541 Status_t::SuccessAndTrue() const
00542 {
00543     return (m_error_code > 0);
00544 }
00545 
00546 
00547 inline bool
00548 Status_t::SuccessAndFalse() const
00549 {
00550     return (m_error_code == 0);
00551 }
00552 
00553 
00554 inline error_code_t
00555 Status_t::StatusCode() const
00556 {
00557     return m_error_code;
00558 }
00559 
00560 
00561 inline bool
00562 Status_t::operator ==(error_code_t error_code) const
00563 {
00564     return (m_error_code == error_code);
00565 }
00566 
00567 
00568 inline bool
00569 Status_t::operator !=(const Status_t& other)
00570 {
00571     return !(*this == other);
00572 }
00573 
00574 
00575 inline bool
00576 Status_t::operator !=(error_code_t error_code)
00577 {
00578     return (m_error_code != error_code);
00579 }
00580 
00581 
00582 inline bool
00583 Status_t::operator <(int32 value)
00584 {
00585     return (m_error_code < value);
00586 }
00587 
00588 
00589 inline bool
00590 Status_t::operator <=(int32 value)
00591 {
00592     return (m_error_code <= value);
00593 }
00594 
00595 
00596 inline bool
00597 Status_t::operator >(int32 value)
00598 {
00599     return (m_error_code > value);
00600 }
00601 
00602 
00603 inline bool
00604 Status_t::operator >=(int32 value)
00605 {
00606     return (m_error_code >= value);
00607 }
00608 
00609 
00610 #if WINDOWS
00611 
00612 
00613 inline Status_t
00614 Status_t::WinError(int32 windows_error_code)
00615 {
00616     // GetLastError, winerror.h
00617     //   3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
00618     //   1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
00619     //  +---+-+-+-----------------------+-------------------------------+
00620     //  |Sev|C|R|     Facility          |               Code            |
00621     //  +---+-+-+-----------------------+-------------------------------+
00622     //      Sev - is the severity code
00623     //          00 - Success
00624     //          01 - Informational
00625     //          10 - Warning
00626     //          11 - Error
00627     //      C - is the Customer code flag
00628     //      R - is a reserved bit
00629     //      Facility - is the facility code
00630     //      Code - is the facility's status code
00631     return (windows_error_code | eERR_windows_error_base);
00632 }
00633 
00634 
00635 inline Status_t
00636 Status_t::WinError( int32 win_error_code, const utf8* explanation )
00637 {
00638     return Status_t( win_error_code | eERR_windows_error_base, explanation );
00639 }
00640 
00641 
00642 inline Status_t
00643 Status_t::WinError( int32 win_error_code, stringliteral* explanation )
00644 {
00645     return Status_t( win_error_code | eERR_windows_error_base, explanation );
00646 }
00647 
00648 
00649 #endif
00650 
00651 
00652 inline Status_t
00653 Status_t::POSIXError(int posix_error_code)
00654 {
00655     // errno, errno.h
00656     return ( (posix_error_code & 0x0000FFFF) | eERR_posix_error_base );
00657 }
00658 
00659 
00660 inline Status_t
00661 Status_t::POSIXError( int posix_error_code, const utf8* explanation )
00662 {
00663     return Status_t( (posix_error_code & 0x0000FFFF) | eERR_posix_error_base, explanation );
00664 }
00665 
00666 
00667 inline Status_t
00668 Status_t::POSIXError( int posix_error_code, stringliteral* explanation )
00669 {
00670     return Status_t( (posix_error_code & 0x0000FFFF) | eERR_posix_error_base, explanation );
00671 }
00672 
00673 
00674 #endif // __cplusplus

Generated on Tue Dec 14 22:35:05 2010 for UT library by  doxygen 1.6.1