Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

notify.h

Go to the documentation of this file.
00001 // Filename: notify.h
00002 // Created by:  drose (28Feb00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #ifndef NOTIFY_H
00020 #define NOTIFY_H
00021 
00022 #include "dtoolbase.h"
00023 
00024 #include "notifyCategory.h"
00025 #include "notifySeverity.h"
00026 
00027 #include <string>
00028 #include <vector>
00029 #include <map>
00030 
00031 ////////////////////////////////////////////////////////////////////
00032 //       Class : Notify
00033 // Description : An object that handles general error reporting to the
00034 //               user.  It contains a pointer to an ostream, initially
00035 //               cerr, which can be reset at will to point to
00036 //               different output devices, according to the needs of
00037 //               the application.  All output generated within Panda
00038 //               should vector through the Notify ostream.
00039 //
00040 //               This also includes a collection of Categories and
00041 //               Severities, which may be independently enabled or
00042 //               disabled, so that error messages may be squelched or
00043 //               respected according to the wishes of the user.
00044 ////////////////////////////////////////////////////////////////////
00045 class EXPCL_DTOOLCONFIG Notify {
00046 PUBLISHED:
00047   Notify();
00048   ~Notify();
00049 
00050   void set_ostream_ptr(ostream *ostream_ptr, bool delete_later);
00051   ostream *get_ostream_ptr() const;
00052 
00053   typedef bool AssertHandler(const char *expression, int line,
00054                              const char *source_file);
00055 
00056   void set_assert_handler(AssertHandler *assert_handler);
00057   void clear_assert_handler();
00058   bool has_assert_handler() const;
00059   AssertHandler *get_assert_handler() const;
00060 
00061   bool has_assert_failed() const;
00062   const string &get_assert_error_message() const;
00063   void clear_assert_failed();
00064 
00065   NotifyCategory *get_top_category();
00066   NotifyCategory *get_category(const string &basename,
00067                                NotifyCategory *parent_category);
00068   NotifyCategory *get_category(const string &basename,
00069                                const string &parent_fullname);
00070   NotifyCategory *get_category(const string &fullname);
00071 
00072   static ostream &out();
00073   static ostream &null();
00074   static void write_string(const string &str);
00075   static Notify *ptr();
00076 
00077 public:
00078   static ios_fmtflags get_literal_flag();
00079 
00080   bool assert_failure(const char *expression, int line,
00081                       const char *source_file);
00082 
00083   static NotifySeverity string_severity(const string &string);
00084 
00085   void config_initialized();
00086 
00087 private:
00088   ostream *_ostream_ptr;
00089   bool _owns_ostream_ptr;
00090   ostream *_null_ostream_ptr;
00091 
00092   AssertHandler *_assert_handler;
00093   bool _assert_failed;
00094   string _assert_error_message;
00095 
00096   typedef map<string, NotifyCategory *> Categories;
00097   Categories _categories;
00098 
00099   static Notify *_global_ptr;
00100 };
00101 
00102 
00103 // This defines the symbol nout in the same way that cerr and cout are
00104 // defined, for compactness of C++ code that uses Notify in its
00105 // simplest form.  Maybe it's a good idea to define this symbol and
00106 // maybe it's not, but it does seem that "nout" isn't likely to
00107 // collide with any other name.
00108 
00109 #define nout (Notify::out())
00110 
00111 // Here are a couple of assert-type functions.  These are designed to
00112 // avoid simply dumping core, since that's quite troublesome when the
00113 // programmer is working in a higher-level environment that is calling
00114 // into the C++ layer.
00115 
00116 // nassertr() is intended to be used in functions that have return
00117 // values; it returns the indicated value if the assertion fails.
00118 
00119 // nassertv() is intended to be used in functions that do not have
00120 // return values; it simply returns if the assertion fails.
00121 
00122 // nassertd() does not return from the function, but instead executes
00123 // the following block of code (like an if statement) if the assertion
00124 // fails.
00125 
00126 // nassertr_always() and nassertv_always() are like nassertr() and
00127 // nassertv(), except that they will not get completely compiled out
00128 // if NDEBUG is set.  Instead, they will quietly return from the
00129 // function.  These macros are appropriate, for instance, for sanity
00130 // checking user input parameters, where optimal performance is not
00131 // paramount.
00132 
00133 #ifdef NDEBUG
00134 
00135 #define nassertr(condition, return_value)
00136 #define nassertv(condition)
00137 #define nassertd(condition) if (false)
00138 // We trust the compiler to optimize the above out.
00139 
00140 #define nassertr_always(condition, return_value) \
00141   { \
00142     if (!(condition)) { \
00143       return return_value; \
00144     } \
00145   }
00146 
00147 #define nassertv_always(condition) \
00148   { \
00149     if (!(condition)) { \
00150       return; \
00151     } \
00152   }
00153 
00154 #else   // NDEBUG
00155 
00156 #define nassertr(condition, return_value) \
00157   { \
00158     if (!(condition)) { \
00159       if (Notify::ptr()->assert_failure(#condition, __LINE__, __FILE__)) { \
00160         return return_value; \
00161       } \
00162     } \
00163   }
00164 
00165 #define nassertv(condition) \
00166   { \
00167     if (!(condition)) { \
00168       if (Notify::ptr()->assert_failure(#condition, __LINE__, __FILE__)) { \
00169         return; \
00170       } \
00171     } \
00172   }
00173 
00174 #define nassertd(condition) \
00175   if (!(condition) && \
00176       Notify::ptr()->assert_failure(#condition, __LINE__, __FILE__))
00177 
00178 #define nassertr_always(condition, return_value) nassertr(condition, return_value)
00179 #define nassertv_always(condition) nassertv(condition)
00180 
00181 
00182 #endif  // NDEBUG
00183 
00184 
00185 #include "notify.I"
00186 
00187 #endif

Generated on Fri Apr 18 01:33:56 2003 for DTool by doxygen1.3