custom stream manipulator for class

前端 未结 4 1351
情话喂你
情话喂你 2020-12-20 10:44

I am trying to write a simple audit class that takes input via operator << and writes the audit after receiving a custom manipulator like this:

class C         


        
相关标签:
4条回答
  • 2020-12-20 11:07

    To make it work you have to add overload of operator << for functions, than call the function from it:

     class CAudit
     {
      //...other details here as in original question
    
      CAudit& operator << (CAudit & (*func)(CAudit &))
      {
            return func(*this);
      }
     };
    
     CAudit audit;
     audit << "some text" << CAudit::write;
    
    0 讨论(0)
  • 2020-12-20 11:23

    I do something very similar for tracing, but use a stringstream. This ensures that all 3rd party operator << () and manipulators work. I also use the desctructor instead of the customer write manipulator.

    class DebugStream
    {
    public:
        DebugStream(short level, const char * file, int line) {
            sstream << "L" << level << "\t" << file << "\t" << line << "\t";
        }
        ~DebugStream() { write(sstream.str()); }
    
        std::ostream & stream() { return sstream; }
    private:
        std::stringstream sstream;
    
        DebugStream(const DebugStream &);
        DebugStream & operator=(const DebugStream &);
    };
    

    This is then made available with some macros:

    #define DBG_ERROR if (1<=dbg_level()) DebugStream(1, __FILE__, __LINE__).stream()
    #define DBG_INFO  if (2<=dbg_level()) DebugStream(2, __FILE__, __LINE__).stream()
    

    And the code just uses the macros

    DBG_INFO << "print some debug information";
    

    You don't need a specific write manipulator to flush the data to the log file. When the anonymous DebugStream object goes out of scope (once control leaves the line) the the contents are automatically written.

    Although I usually avoid macros in this case the use of the if statement means you don't have the overhead of building the trace line unless you actually require it.

    Returning the ostream via the stream() method enables this to work for global member functions, as anonymous objects cannot be passed as non-const reference parameters.

    0 讨论(0)
  • 2020-12-20 11:27

    Binary shift operator and stream operator is the same operator. It is completely legal to overload operator+ for your class to write "Hello world" on std::cout (although it is a very bad idea). The same way C++ standard authors decided to overload operator<< for streams as writing to the stream.
    You didn't write clearly what is your problem. My guess is compilation error. The best thing in this case is to quote the error message. If I am right, the problem is, that you defined only operator<< for LPCSTR, and then you want it to work function object on the right side.
    You use word "manipulator", but you misunderstand something. Manipulator for a stream (stream from STL) is a function that performs some actions on the stream it is written to. And it works only because of this overload:

    ostream& operator<< (ostream& ( *pf )(ostream&));
    

    which takes a function and applies it to a stream.
    Similarly you need:

    CAudit& operator<< (CAudit& ( *pf )(CAudit& audit))
    {
      return (*pf)(audit);
    }
    
    0 讨论(0)
  • 2020-12-20 11:28

    Wouldn't this

    class CAudit
    {
    public:
        template< typename T >
        CAudit& operator<<( const T& data )
        {
            audittext << data;
            return *this;
        }
    
        class write {};
    
        void operator<<( const write& data )
        {
            /* whatever */
        }
    
    private:
        std::stringstream audittext;
    };
    

    do what you want?

    0 讨论(0)
提交回复
热议问题