xorp

xrl_error.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2001-2011 XORP, Inc and Others
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License, Version
00008 // 2.1, June 1999 as published by the Free Software Foundation.
00009 // Redistribution and/or modification of this program under the terms of
00010 // any other version of the GNU Lesser General Public License is not
00011 // permitted.
00012 // 
00013 // This program is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00016 // see the GNU Lesser General Public License, Version 2.1, a copy of
00017 // which can be found in the XORP LICENSE.lgpl file.
00018 // 
00019 // XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00020 // http://xorp.net
00021 
00022 // $XORP: xorp/libxipc/xrl_error.hh,v 1.21 2008/10/02 21:57:24 bms Exp $
00023 
00024 #ifndef __LIBXIPC_XRL_ERROR_HH__
00025 #define __LIBXIPC_XRL_ERROR_HH__
00026 
00027 #include "libxorp/xorp.h"
00028 #include "libxorp/c_format.hh"
00029 
00030 
00031 class XrlErrlet;
00032 
00033 enum XrlErrorCode {
00034     OKAY          = 100,
00035     BAD_ARGS          = 101,
00036     COMMAND_FAILED    = 102,
00037 
00038     NO_FINDER         = 200,
00039     RESOLVE_FAILED    = 201,
00040     NO_SUCH_METHOD    = 202,
00041 
00042     SEND_FAILED       = 210,
00043     REPLY_TIMED_OUT   = 211,
00044     SEND_FAILED_TRANSIENT = 212,
00045 
00046     INTERNAL_ERROR    = 220
00047 };
00048 
00056 class XrlError {
00057 public:
00062     static const XrlError& OKAY();
00063 
00068     static const XrlError& BAD_ARGS();
00069 
00074     static const XrlError& COMMAND_FAILED();
00075 
00080     static const XrlError& NO_FINDER();
00081 
00086     static const XrlError& RESOLVE_FAILED();
00087 
00092     static const XrlError& NO_SUCH_METHOD();
00093 
00097     static const XrlError& SEND_FAILED();
00098 
00103     static const XrlError& REPLY_TIMED_OUT();
00104 
00108     static const XrlError& SEND_FAILED_TRANSIENT();
00109 
00117     static const XrlError& INTERNAL_ERROR();
00118 
00122     XrlErrorCode error_code() const;
00123 
00124     bool isOK() const { return error_code() == ::OKAY; }
00125 
00129     const char* error_msg() const;
00130 
00135     const string& note() const { return _note; }
00136 
00140     string str() const {
00141     string r = c_format("%d ", error_code()) + error_msg();
00142     return note().size() ? (r + " " + note()) : r;
00143     }
00144 
00148     static bool known_code(uint32_t code);
00149 
00150     XrlError();
00151     XrlError(XrlErrorCode error_code, const string& note = "");
00152     XrlError(const XrlError& xe) : _errlet(xe._errlet), _note(xe._note) {}
00153 
00154     /* Strictly for classes that have access to XrlErrlet to construct
00155        XrlError's */
00156     XrlError(const XrlErrlet& x, const string& note = "") :
00157     _errlet(&x), _note(note) {}
00158 
00159     XrlError(const XrlErrlet*);
00160 
00161 protected:
00162     const XrlErrlet* _errlet;
00163     string       _note;
00164 };
00165 
00166 
00174 struct XrlCmdError {
00175 public:
00182     static const XrlCmdError& OKAY() { return _xce_ok; }
00183 
00187     static const XrlCmdError BAD_ARGS(const string& reason = "") {
00188     return XrlError(XrlError::BAD_ARGS().error_code(), reason);
00189     }
00190 
00194     static const XrlCmdError COMMAND_FAILED(const string& reason = "") {
00195     return XrlError(XrlError::COMMAND_FAILED().error_code(), reason);
00196     }
00197 
00201     operator XrlError() const { return _xrl_error; }
00202 
00206     string str() const { return string("XrlCmdError ") + _xrl_error.str(); }
00207 
00211     XrlErrorCode error_code() const { return _xrl_error.error_code(); }
00212 
00213     bool isOK() const { return _xrl_error.isOK(); }
00214 
00218     const string& note() const { return _xrl_error.note(); }
00219 
00220 private:
00221     XrlCmdError(const XrlError& xe) : _xrl_error(xe) {}
00222     XrlError _xrl_error;
00223     static XrlCmdError _xce_ok;
00224 };
00225 
00226 
00231 inline bool operator==(const XrlError& e1, const XrlError& e2)
00232 {
00233     return e1.error_code() == e2.error_code();
00234 }
00235 
00240 inline bool operator!=(const XrlError& e1, const XrlError& e2)
00241 {
00242     return e1.error_code() != e2.error_code();
00243 }
00244 
00245 #endif // __LIBXIPC_XRL_ERROR_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations