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. C / C++ / MFC
  4. Writing my own stream class

Writing my own stream class

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestionc++wpfcom
2 Posts 2 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

    N 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

      N Offline
      N Offline
      Niklas L
      wrote on last edited by
      #2

      I would inherit from ostringstream, overloading output stream operators for your formatting classes. If the format written is binary (contains null characters) it needs special handling. (Remember that std::endl doesn't just append a newline character. It also makes the stream call flush(). Hence, you need to consider how the flush() method should behave when using this class.)

      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