|
|
|||
File indexing completed on 2026-09-26 09:04:01
0001 // Created on: 2007-06-28 0002 // Created by: OCC Team 0003 // Copyright (c) 2007-2014 OPEN CASCADE SAS 0004 // 0005 // This file is part of Open CASCADE Technology software library. 0006 // 0007 // This library is free software; you can redistribute it and/or modify it under 0008 // the terms of the GNU Lesser General Public License version 2.1 as published 0009 // by the Free Software Foundation, with special exception defined in the file 0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT 0011 // distribution for complete text of the license and disclaimer of any warranty. 0012 // 0013 // Alternatively, this file may be used under the terms of Open CASCADE 0014 // commercial license or contractual agreement. 0015 0016 #ifndef _Message_Messenger_HeaderFile 0017 #define _Message_Messenger_HeaderFile 0018 0019 #include <Message_Printer.hxx> 0020 #include <NCollection_Sequence.hxx> 0021 0022 #include <TCollection_HAsciiString.hxx> 0023 #include <TCollection_HExtendedString.hxx> 0024 0025 class Message_Printer; 0026 0027 // resolve name collisions with WinAPI headers 0028 #ifdef AddPrinter 0029 #undef AddPrinter 0030 #endif 0031 0032 //! Messenger is API class providing general-purpose interface for 0033 //! libraries that may issue text messages without knowledge 0034 //! of how these messages will be further processed. 0035 //! 0036 //! The messenger contains a sequence of "printers" which can be 0037 //! customized by the application, and dispatches every received 0038 //! message to all the printers. 0039 //! 0040 //! For convenience, a set of methods Send...() returning a string 0041 //! stream buffer is defined for use of stream-like syntax with operator << 0042 //! 0043 //! Example: 0044 //! ~~~~~ 0045 //! Messenger->SendFail() << " Unknown fail at line " << aLineNo << " in file " << aFile; 0046 //! ~~~~~ 0047 //! 0048 //! The message is sent to messenger on destruction of the stream buffer, 0049 //! call to Flush(), or passing manipulator std::ends, std::endl, or std::flush. 0050 //! Empty messages are not sent except if manipulator is used. 0051 class Message_Messenger : public Standard_Transient 0052 { 0053 DEFINE_STANDARD_RTTIEXT(Message_Messenger, Standard_Transient) 0054 public: 0055 //! Auxiliary class wrapping std::stringstream thus allowing constructing 0056 //! message via stream interface, and putting result into its creator 0057 //! Message_Messenger within destructor. 0058 //! 0059 //! It is intended to be used either as temporary object or as local 0060 //! variable, note that content will be lost if it is copied. 0061 class StreamBuffer 0062 { 0063 public: 0064 //! Destructor flushing constructed message. 0065 ~StreamBuffer() { Flush(); } 0066 0067 //! Flush collected string to messenger 0068 void Flush(bool doForce = false) 0069 { 0070 myStream.flush(); 0071 if (doForce || myStream.tellp() != std::streampos(0)) 0072 { 0073 if (myMessenger) 0074 { 0075 myMessenger->Send(myStream, myGravity); 0076 } 0077 myStream.str(std::string()); // empty the buffer for possible reuse 0078 } 0079 } 0080 0081 //! Formal copy constructor. 0082 //! 0083 //! Since buffer is intended for use as temporary object or local 0084 //! variable, copy (or move) is needed only formally to be able to 0085 //! return the new instance from relevant creation method. 0086 //! In practice it should never be called because modern compilers 0087 //! create such instances in place. 0088 //! However note that if this constructor is called, the buffer 0089 //! content (string) will not be copied (move is not supported for 0090 //! std::stringstream class on old compilers such as gcc 4.4, msvc 9). 0091 StreamBuffer(const StreamBuffer& theOther) 0092 : myMessenger(theOther.myMessenger), 0093 myGravity(theOther.myGravity) 0094 { 0095 } 0096 0097 //! Wrapper for operator << of the stream 0098 template <typename T> 0099 StreamBuffer& operator<<(const T& theArg) 0100 { 0101 myStream << theArg; 0102 return *this; 0103 } 0104 0105 //! Operator << for manipulators of ostream (ends, endl, flush), 0106 //! flushes the buffer (sends the message) 0107 StreamBuffer& operator<<(std::ostream& (*)(std::ostream&)) 0108 { 0109 Flush(true); 0110 return *this; 0111 } 0112 0113 //! Access to the stream object 0114 Standard_SStream& Stream() { return myStream; } 0115 0116 //! Cast to OStream& 0117 operator Standard_OStream&() { return myStream; } 0118 0119 //! Access to the messenger 0120 Message_Messenger* Messenger() { return myMessenger; } 0121 0122 private: 0123 friend class Message_Messenger; 0124 0125 //! Main constructor creating temporary buffer. 0126 //! Accessible only to Messenger class. 0127 StreamBuffer(Message_Messenger* theMessenger, Message_Gravity theGravity) 0128 : myMessenger(theMessenger), 0129 myGravity(theGravity) 0130 { 0131 } 0132 0133 private: 0134 // clang-format off 0135 Message_Messenger* myMessenger; // don't make a Handle since this object should be created on stack 0136 // clang-format on 0137 Message_Gravity myGravity; 0138 Standard_SStream myStream; 0139 }; 0140 0141 public: 0142 //! Empty constructor; initializes by single printer directed to std::cout. 0143 //! Note: the default messenger is not empty but directed to cout 0144 //! in order to protect against possibility to forget defining printers. 0145 //! If printing to cout is not needed, clear messenger by GetPrinters().Clear() 0146 Standard_EXPORT Message_Messenger(); 0147 0148 //! Create messenger with single printer 0149 Standard_EXPORT Message_Messenger(const occ::handle<Message_Printer>& thePrinter); 0150 0151 //! Add a printer to the messenger. 0152 //! The printer will be added only if it is not yet in the list. 0153 //! Returns True if printer has been added. 0154 Standard_EXPORT bool AddPrinter(const occ::handle<Message_Printer>& thePrinter); 0155 0156 //! Removes specified printer from the messenger. 0157 //! Returns True if this printer has been found in the list 0158 //! and removed. 0159 Standard_EXPORT bool RemovePrinter(const occ::handle<Message_Printer>& thePrinter); 0160 0161 //! Removes printers of specified type (including derived classes) 0162 //! from the messenger. 0163 //! Returns number of removed printers. 0164 Standard_EXPORT int RemovePrinters(const occ::handle<Standard_Type>& theType); 0165 0166 //! Returns current sequence of printers 0167 const NCollection_Sequence<occ::handle<Message_Printer>>& Printers() const { return myPrinters; } 0168 0169 //! Returns sequence of printers 0170 //! The sequence can be modified. 0171 NCollection_Sequence<occ::handle<Message_Printer>>& ChangePrinters() { return myPrinters; } 0172 0173 //! Dispatch a message to all the printers in the list. 0174 //! Three versions of string representations are accepted for 0175 //! convenience, by default all are converted to ExtendedString. 0176 Standard_EXPORT void Send(const char* const theString, 0177 const Message_Gravity theGravity = Message_Warning) const; 0178 0179 //! See above 0180 Standard_EXPORT void Send(const Standard_SStream& theStream, 0181 const Message_Gravity theGravity = Message_Warning) const; 0182 0183 //! See above 0184 Standard_EXPORT void Send(const TCollection_AsciiString& theString, 0185 const Message_Gravity theGravity = Message_Warning) const; 0186 0187 //! See above 0188 Standard_EXPORT void Send(const TCollection_ExtendedString& theString, 0189 const Message_Gravity theGravity = Message_Warning) const; 0190 0191 //! Create string buffer for message of specified type 0192 StreamBuffer Send(Message_Gravity theGravity) { return StreamBuffer(this, theGravity); } 0193 0194 //! See above 0195 Standard_EXPORT void Send(const occ::handle<Standard_Transient>& theObject, 0196 const Message_Gravity theGravity = Message_Warning) const; 0197 0198 //! Create string buffer for sending Fail message 0199 StreamBuffer SendFail() { return Send(Message_Fail); } 0200 0201 //! Create string buffer for sending Alarm message 0202 StreamBuffer SendAlarm() { return Send(Message_Alarm); } 0203 0204 //! Create string buffer for sending Warning message 0205 StreamBuffer SendWarning() { return Send(Message_Warning); } 0206 0207 //! Create string buffer for sending Info message 0208 StreamBuffer SendInfo() { return Send(Message_Info); } 0209 0210 //! Create string buffer for sending Trace message 0211 StreamBuffer SendTrace() { return Send(Message_Trace); } 0212 0213 //! Short-cut to Send (theMessage, Message_Fail) 0214 void SendFail(const TCollection_AsciiString& theMessage) { Send(theMessage, Message_Fail); } 0215 0216 //! Short-cut to Send (theMessage, Message_Alarm) 0217 void SendAlarm(const TCollection_AsciiString& theMessage) { Send(theMessage, Message_Alarm); } 0218 0219 //! Short-cut to Send (theMessage, Message_Warning) 0220 void SendWarning(const TCollection_AsciiString& theMessage) { Send(theMessage, Message_Warning); } 0221 0222 //! Short-cut to Send (theMessage, Message_Info) 0223 void SendInfo(const TCollection_AsciiString& theMessage) { Send(theMessage, Message_Info); } 0224 0225 //! Short-cut to Send (theMessage, Message_Trace) 0226 void SendTrace(const TCollection_AsciiString& theMessage) { Send(theMessage, Message_Trace); } 0227 0228 //! Dumps the content of me into the stream 0229 Standard_EXPORT void DumpJson(Standard_OStream& theOStream, int theDepth = -1) const; 0230 0231 private: 0232 NCollection_Sequence<occ::handle<Message_Printer>> myPrinters; 0233 }; 0234 0235 #endif // _Message_Messenger_HeaderFile
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|