Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:16

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_SequenceOfPrinters.hxx>
0020 
0021 #include <TCollection_HAsciiString.hxx>
0022 #include <TCollection_HExtendedString.hxx>
0023 
0024 class Message_Printer;
0025 
0026 // resolve name collisions with WinAPI headers
0027 #ifdef AddPrinter
0028   #undef AddPrinter
0029 #endif
0030 
0031 class Message_Messenger;
0032 DEFINE_STANDARD_HANDLE(Message_Messenger, Standard_Transient)
0033 
0034 //! Messenger is API class providing general-purpose interface for
0035 //! libraries that may issue text messages without knowledge
0036 //! of how these messages will be further processed.
0037 //!
0038 //! The messenger contains a sequence of "printers" which can be
0039 //! customized by the application, and dispatches every received
0040 //! message to all the printers.
0041 //!
0042 //! For convenience, a set of methods Send...() returning a string
0043 //! stream buffer is defined for use of stream-like syntax with operator << 
0044 //!
0045 //! Example:
0046 //! ~~~~~
0047 //! Messenger->SendFail() << " Unknown fail at line " << aLineNo << " in file " << aFile;
0048 //! ~~~~~
0049 //!
0050 //! The message is sent to messenger on destruction of the stream buffer,
0051 //! call to Flush(), or passing manipulator std::ends, std::endl, or std::flush.
0052 //! Empty messages are not sent except if manipulator is used.
0053 class Message_Messenger : public Standard_Transient
0054 {
0055   DEFINE_STANDARD_RTTIEXT(Message_Messenger, Standard_Transient)
0056 public:
0057   //! Auxiliary class wrapping std::stringstream thus allowing constructing
0058   //! message via stream interface, and putting result into its creator
0059   //! Message_Messenger within destructor.
0060   //!
0061   //! It is intended to be used either as temporary object or as local
0062   //! variable, note that content will be lost if it is copied.
0063   class StreamBuffer
0064   {
0065   public:
0066 
0067     //! Destructor flushing constructed message.
0068     ~StreamBuffer() { Flush(); }
0069 
0070     //! Flush collected string to messenger
0071     void Flush(Standard_Boolean doForce = Standard_False)
0072     {
0073       myStream.flush();
0074       if (doForce || myStream.tellp() != std::streampos(0))
0075       {
0076         if (myMessenger)
0077         {
0078           myMessenger->Send(myStream, myGravity);
0079         }
0080         myStream.str(std::string()); // empty the buffer for possible reuse
0081       }
0082     }
0083 
0084     //! Formal copy constructor.
0085     //!
0086     //! Since buffer is intended for use as temporary object or local
0087     //! variable, copy (or move) is needed only formally to be able to
0088     //! return the new instance from relevant creation method.
0089     //! In practice it should never be called because modern compilers
0090     //! create such instances in place.
0091     //! However note that if this constructor is called, the buffer
0092     //! content (string) will not be copied  (move is not supported for
0093     //! std::stringstream class on old compilers such as gcc 4.4, msvc 9).
0094     StreamBuffer (const StreamBuffer& theOther)
0095     : myMessenger(theOther.myMessenger), myGravity(theOther.myGravity)
0096     {
0097     }
0098 
0099     //! Wrapper for operator << of the stream
0100     template <typename T>
0101     StreamBuffer& operator << (const T& theArg)
0102     {
0103       myStream << theArg;
0104       return *this;
0105     }
0106 
0107     //! Operator << for manipulators of ostream (ends, endl, flush),
0108     //! flushes the buffer (sends the message)
0109     StreamBuffer& operator << (std::ostream& (*)(std::ostream&))
0110     {
0111       Flush(Standard_True);
0112       return *this;
0113     }
0114 
0115     //! Access to the stream object
0116     Standard_SStream& Stream () { return myStream; }
0117 
0118     //! Cast to OStream&
0119     operator Standard_OStream& () { return myStream; }
0120 
0121     //! Access to the messenger
0122     Message_Messenger* Messenger () { return myMessenger; }
0123 
0124   private:
0125     friend class Message_Messenger;
0126 
0127     //! Main constructor creating temporary buffer.
0128     //! Accessible only to Messenger class.
0129     StreamBuffer (Message_Messenger* theMessenger, Message_Gravity theGravity)
0130     : myMessenger (theMessenger),
0131       myGravity (theGravity)
0132     {}
0133 
0134   private:
0135     Message_Messenger* myMessenger; // don't make a Handle since this object should be created on stack
0136     Message_Gravity    myGravity;
0137     Standard_SStream   myStream;
0138   };
0139 
0140 public:
0141 
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 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 Standard_Boolean AddPrinter (const 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 Standard_Boolean RemovePrinter (const 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 Standard_Integer RemovePrinters (const Handle(Standard_Type)& theType);
0165   
0166   //! Returns current sequence of printers
0167   const Message_SequenceOfPrinters& Printers() const { return myPrinters; }
0168 
0169   //! Returns sequence of printers
0170   //! The sequence can be modified.
0171   Message_SequenceOfPrinters& 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 Standard_CString 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 Handle(Standard_Transient)& theObject, const Message_Gravity theGravity = Message_Warning) const;
0196 
0197   //! Create string buffer for sending Fail message
0198   StreamBuffer SendFail () { return Send (Message_Fail); }
0199 
0200   //! Create string buffer for sending Alarm message
0201   StreamBuffer SendAlarm () { return Send (Message_Alarm); }
0202 
0203   //! Create string buffer for sending Warning message
0204   StreamBuffer SendWarning () { return Send (Message_Warning); }
0205 
0206   //! Create string buffer for sending Info message
0207   StreamBuffer SendInfo () { return Send (Message_Info); }
0208 
0209   //! Create string buffer for sending Trace message
0210   StreamBuffer SendTrace () { return Send (Message_Trace); }
0211 
0212   //! Short-cut to Send (theMessage, Message_Fail)
0213   void SendFail (const TCollection_AsciiString& theMessage) { Send (theMessage, Message_Fail); }
0214 
0215   //! Short-cut to Send (theMessage, Message_Alarm)
0216   void SendAlarm (const TCollection_AsciiString& theMessage) { Send (theMessage, Message_Alarm); }
0217 
0218   //! Short-cut to Send (theMessage, Message_Warning)
0219   void SendWarning (const TCollection_AsciiString& theMessage) { Send (theMessage, Message_Warning); }
0220 
0221   //! Short-cut to Send (theMessage, Message_Info)
0222   void SendInfo (const TCollection_AsciiString& theMessage) { Send (theMessage, Message_Info); }
0223 
0224   //! Short-cut to Send (theMessage, Message_Trace)
0225   void SendTrace (const TCollection_AsciiString& theMessage) { Send (theMessage, Message_Trace); }
0226 
0227   //! Dumps the content of me into the stream
0228   Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
0229 
0230 private:
0231 
0232   Message_SequenceOfPrinters myPrinters;
0233 
0234 };
0235 
0236 #endif // _Message_Messenger_HeaderFile