Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Writing my own stream class

Writing my own stream class

Scheduled Pinned Locked Moved ATL / WTL / STL
tutorialquestionc++wpfcom
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Patje
    wrote on last edited by
    #1

    For a module I want to write my own stream class. Although not identical the following example illustrates what I want to do: suppose you have a class representing a printer:

    class Printer { ...};
    

    I want the user of this class to send data to this class using the << operator, so that any data type/class, for which << is defined on streams, e.g.

    Printer myPrinter (printerName);
    PrinterStream &printStr = myPrinter.getStream();
    printStr << "Hello world" << 123 << std::endl;
    

    So, how can I write my own stream class so that I can intercept the data send to printStr? I probably need to inherit from some STL stream class, but I don't know where to start. Is this something that can be done easily, or is this almost impossible? Another alternative (although I don't really like it) is to let my getStream() method to return a stringstream. My class then simply needs a kind of flush method to get the data from the stringstream, send it to the output where I want it and then empty the stringstream. I don't like this alternative because it exposes the fact that's a stringstream to the caller. Also I cannot add my own output manipulators (e.g. to set the font or color) to the stream. Does anybody know a simple solution, or point me to an article that explains how to write your own stream class? Thanks in advance. Enjoy life, this is not a rehearsal !!! My Articles: - Implementing a Subject/Observer pattern with templates - Different ways of writing class factories - AutoRunner: a template class to automatically run start- and cleanup-code in code blocks

    C J 2 Replies Last reply
    0
    • P Patje

      For a module I want to write my own stream class. Although not identical the following example illustrates what I want to do: suppose you have a class representing a printer:

      class Printer { ...};
      

      I want the user of this class to send data to this class using the << operator, so that any data type/class, for which << is defined on streams, e.g.

      Printer myPrinter (printerName);
      PrinterStream &printStr = myPrinter.getStream();
      printStr << "Hello world" << 123 << std::endl;
      

      So, how can I write my own stream class so that I can intercept the data send to printStr? I probably need to inherit from some STL stream class, but I don't know where to start. Is this something that can be done easily, or is this almost impossible? Another alternative (although I don't really like it) is to let my getStream() method to return a stringstream. My class then simply needs a kind of flush method to get the data from the stringstream, send it to the output where I want it and then empty the stringstream. I don't like this alternative because it exposes the fact that's a stringstream to the caller. Also I cannot add my own output manipulators (e.g. to set the font or color) to the stream. Does anybody know a simple solution, or point me to an article that explains how to write your own stream class? Thanks in advance. Enjoy life, this is not a rehearsal !!! My Articles: - Implementing a Subject/Observer pattern with templates - Different ways of writing class factories - AutoRunner: a template class to automatically run start- and cleanup-code in code blocks

      C Offline
      C Offline
      cerez02
      wrote on last edited by
      #2

      use Subject/Observer pattern ;P

      1 Reply Last reply
      0
      • P Patje

        For a module I want to write my own stream class. Although not identical the following example illustrates what I want to do: suppose you have a class representing a printer:

        class Printer { ...};
        

        I want the user of this class to send data to this class using the << operator, so that any data type/class, for which << is defined on streams, e.g.

        Printer myPrinter (printerName);
        PrinterStream &printStr = myPrinter.getStream();
        printStr << "Hello world" << 123 << std::endl;
        

        So, how can I write my own stream class so that I can intercept the data send to printStr? I probably need to inherit from some STL stream class, but I don't know where to start. Is this something that can be done easily, or is this almost impossible? Another alternative (although I don't really like it) is to let my getStream() method to return a stringstream. My class then simply needs a kind of flush method to get the data from the stringstream, send it to the output where I want it and then empty the stringstream. I don't like this alternative because it exposes the fact that's a stringstream to the caller. Also I cannot add my own output manipulators (e.g. to set the font or color) to the stream. Does anybody know a simple solution, or point me to an article that explains how to write your own stream class? Thanks in advance. Enjoy life, this is not a rehearsal !!! My Articles: - Implementing a Subject/Observer pattern with templates - Different ways of writing class factories - AutoRunner: a template class to automatically run start- and cleanup-code in code blocks

        J Offline
        J Offline
        Joaquin M Lopez Munoz
        wrote on last edited by
        #3

        Writing your iostream class is not hard, but it's not trivial either. Basically you have to make CPrinter derive from std::ostream and provide a special std::streambuf class:

        class CPrinter: public std::ostream
        {
        typedef std::ostream super;
        CPrinterStreambuf sbuf; // more on this later
        public:
        CPrinter():super(&buf){}

        ...

        };

        Now, std::ostream handles all the IO via the provided CPrinterStreambuf object, which derives from the virtual base class std::streambuf. Depending on where you want the bytes to be sent, you might consider writing CPrinterStreambuf from scratch or deriving from say std::filebuf or std::stringbuf. Now you can intercept outgoing data by overriding overflow and sync. Here[^] is an example provided by MS that might be useful. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

        P 1 Reply Last reply
        0
        • J Joaquin M Lopez Munoz

          Writing your iostream class is not hard, but it's not trivial either. Basically you have to make CPrinter derive from std::ostream and provide a special std::streambuf class:

          class CPrinter: public std::ostream
          {
          typedef std::ostream super;
          CPrinterStreambuf sbuf; // more on this later
          public:
          CPrinter():super(&buf){}

          ...

          };

          Now, std::ostream handles all the IO via the provided CPrinterStreambuf object, which derives from the virtual base class std::streambuf. Depending on where you want the bytes to be sent, you might consider writing CPrinterStreambuf from scratch or deriving from say std::filebuf or std::stringbuf. Now you can intercept outgoing data by overriding overflow and sync. Here[^] is an example provided by MS that might be useful. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          P Offline
          P Offline
          Patje
          wrote on last edited by
          #4

          Thank you Joaquin, I finally found some time to experiment with this. I started from the Microsoft article you referenced but that did not work because there seems to be something like an old STL and a new STL. The article only worked for the old STL, and not for the new one. But after some digging into the documentation, I finally found the solution. Thanks for your help. Maybe I write an article on this. Enjoy life, this is not a rehearsal !!! My Articles: - Implementing a Subject/Observer pattern with templates - Different ways of writing class factories - AutoRunner: a template class to automatically run start- and cleanup-code in code blocks

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups