Pregunta

¿Alguien puede escribir algún código de muestra para explicar este concepto? Sé para qué se usa una transmisión amortiguada, pero también me gustaría saber cómo implementar eso.

¡Gracias por adelantado!

¿Fue útil?

Solución

Puede buscar la implementación de su plataforma, el estándar C ++ o "C ++ iostreams y locales estándar" de Angelika Langer y Klaus Kreft.

Esté preparado para una curva bastante de aprendizaje. Las corrientes son viejas y una materia complicada. (Francis Glassborow: "Tengo muy pocas dudas de que las bibliotecas de E/S se encuentren entre los aspectos más difíciles de cualquier idioma".)

Otros consejos

Veeeery esquemáticamente, para una secuencia de "entrada":

class BufferedInputStream
{
public:
  BufferedInputStream(SomeExternalDevice d)
  : m_Device(d),
    m_Current(m_Buffer),
    m_End(m_Buffer)
  {}

  char get(){
    if (!IsDataAvailableInBuffer()){
      ReadAChunkFromDiskAndPutItInBuffer();
    }
    return PopByteFromBuffer();
  }

private:

  bool IsDataAvailableInBuffer()const{
    return m_Current != m_End;
  }

  void ReadAChunkFromDiskAndPutItInBuffer(){
    // Buffer must be empty
    assert(!IsDataAvailableInBuffer());

    // Read bytes from the device
    bytes_read = Read(m_Device, m_Buffer, BufferSize);

    // Reposition the "begin" and "end" pointers
    m_Current = m_Buffer;
    m_End = m_Buffer + bytes_read;
  }

  char PopByteFromBuffer(){
    assert(IsDataAvailableInBuffer());
    return *m_Current++;
  }

  // For example, an OS file handle
  SomeExternalDevice m_Device;

  // The buffer itself
  char m_Buffer[BufferSize];

  // Begin of buffer contents
  char* m_Current;

  // End of buffer contents
  char* m_End;
};

De esa manera, los datos se leen del disco en trozos del tamaño del búfer, y la mayoría de las llamadas a "Get ()" no tienen que terminar en llamadas al sistema operativo, ya que simplemente pueden devolver un byte del búfer.

Eche un vistazo a la implementación de STL paraststreamysstream.tcc (Enlaces a la implementación SGI STL).

La base stringstream la clase es basic_stringstream, que implementa el basic_iostream interfaz.

  // [27.7.4] Template class basic_stringstream
  /**
   *  @brief  Controlling input and output for std::string.
   *
   *  This class supports reading from and writing to objects of type
   *  std::basic_string, using the inherited functions from
   *  std::basic_iostream.  To control the associated sequence, an instance
   *  of std::basic_stringbuf is used, which this page refers to as @c sb.
  */

Hay la clase base basic_stringbuf que deriva de basic_streambuf. Esto sostiene el búfer.

  // [27.7.1] template class basic_stringbuf
  /**
   *  @brief  The actual work of input and output (for std::string).
   *
   *  This class associates either or both of its input and output sequences
   *  with a sequence of characters, which can be initialized from, or made
   *  available as, a @c std::basic_string.  (Paraphrased from [27.7.1]/1.)
   *
   *  For this class, open modes (of type @c ios_base::openmode) have
   *  @c in set if the input sequence can be read, and @c out set if the
   *  output sequence can be written.
  */
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top