0

Set Buffer

Basically, it boils down to whether or not I can initialize a std::basic_fstream with a custom buffer. I wanted to use protected inheritance for the std::basic_streambuf so I had access to the get area pointers. Then simply pass my new object to the basic_fstream so it is used instead of the default buffers.

Use this:

#include <streambuf>
class strbuffer : protected std::streambuf {
public:
  strbuffer();
  char_type* operator+(unsigned int slots);
  char_type* cur;
  char_type* beg;
  char_type* eob;
};

strbuffer::strbuffer() : std::streambuf() {
  cur = strbuffer::gptr();
  beg = strbuffer::eback();
  eob = strbuffer::egptr();
}

// returns val of cur
std::basic_streambuf<char, std::char_traits<char>>::char_type* strbuffer::operator+(unsigned int slots) {
  int_type t;
  for (int i = 0; i < slots; i++)
    t = this->sbumpc();
  return new char_type(t);
}

With something like:

class fstream0 : public std::basic_fstream<char, std::char_traits<char>> {
public:
  fstream0();
  void fInit();
  void format();
  bool connectFile(std::string fname);
  bool connectFile(std::string path, std::string fname);
  void closeFile();
  std::ios_base::iostate get8(std::string& out); //32 bit size estimation?
  }

Where the constructor of fstream0 would take an argument that is a stream_buf (casting?) object:

    fstream0::fstream0(arg) : std::basic_fstream<char, std::char_traits<char>>() {
// create bare bones file stream
// buffer is provided, constructor overloaded with support for many file types
// char*, std::array, etc...
      strBuf = fstream0::rdbuf();
      fBuf = fstream0::rdbuf();
    }

Do I have to use pubsetbuf to write over already initialized buffers in order to use many different data types for my custom buffer? Including one that inherits from std::streambuf?

lonious
  • 676
  • 9
  • 25
  • 2
    `std::basic_streambuf` is meant to be derived and used by derived implemenations that override certain characteristics of the stream device – WhiZTiM Sep 22 '16 at 00:09
  • That constructor isn't public, so it isn't callable in this code. – Robert Prévost Sep 22 '16 at 00:09
  • `std::basic_streambuf` is the interface used to implement how characters are read or written. The default implementation of all functions just causes failures. You want to derive from it and implement `underflow()` (for input streams) and/or `overflow()` (for output streams). There are plenty of example how that is done. – Dietmar Kühl Sep 22 '16 at 00:13
  • I've inherited in my other proj. `class strbuffer : protected std::streambuf`, I need to compose this class with my other inherited class `class fstream0 : public std::basic_fstream>` but the base class constructor wants an std::streambuffer object and not my derived strbuffer object. I'm looking into replacing the default buffers after I create my file stream. – lonious Sep 22 '16 at 00:18
  • Sam Varshavchik, I've edited my question and I do not believe it is a duplicate as I READ the post you've submitted as its duplicate prior to my original post. Please review my edit. – lonious Sep 22 '16 at 00:44

0 Answers0