JASSv2
instream.h
Go to the documentation of this file.
1 /*
2  INSTREAM.H
3  ----------
4  Copyright (c) 2016 Andrew Trotman
5  Released under the 2-clause BSD license (See:https://en.wikipedia.org/wiki/BSD_licenses)
6 */
14 #pragma once
15 
16 #include <vector>
17 
18 #include "document.h"
19 #include "allocator_memory.h"
20 
21 namespace JASS
22  {
23  /*
24  CLASS INSTREAM()
25  ----------------
26  */
44  class instream
45  {
46  protected:
49 
50  public:
51  /*
52  INSTREAM::INSTREAM()
53  --------------------
54  */
60  instream(allocator *memory = nullptr, instream *source = nullptr) :
61  memory(memory), // store the memory pointer (which this object does not free on deletion)
62  source(source) // store the instream (which this object does free on deletion)
63  {
64  }
65  /*
66  INSTREAM::~INSTREAM()
67  ---------------------
68  */
74  virtual ~instream()
75  {
76  delete source;
77  }
78 
79  /*
80  INSTREAM::READ()
81  ----------------
82  */
87  virtual void read(document &buffer) = 0;
88 
89  /*
90  INSTREAM::FETCH()
91  -----------------
92  */
99  size_t fetch(void *buffer, size_t bytes)
100  {
101  allocator_memory provider(buffer, bytes);
102  document into(provider);
103 
104  into.contents = slice(buffer, bytes);
105  read(into);
106  return into.contents.size();
107  }
108 
109  } ;
110  }
111 
virtual void read(document &buffer)=0
Read at most buffer.contents.size() bytes of data into buffer, resizing on eof.
Read data from an input stream.
Definition: instream.h:44
C++ slices (string-descriptors)
Definition: slice.h:27
Container class representing a document through the indexing pipeline.
Definition: document.h:31
instream(allocator *memory=nullptr, instream *source=nullptr)
Constructor.
Definition: instream.h:60
slice contents
The contents of the document (or likewise).
Definition: document.h:43
virtual ~instream()
Destructor.
Definition: instream.h:74
A document withing the indexing pipeline.
Virtual base class for C style allocators.
Definition: allocator.h:33
allocator * memory
Any and all memory allocation must happen using this object.
Definition: instream.h:47
instream * source
If this object is reading from another instream then this is that instream.
Definition: instream.h:48
Simple block-allocator that internally allocated a large chunk then allocates smaller blocks from thi...
Definition: allocator.h:21
size_t size(void) const
Return the length of this slice.
Definition: slice.h:186
Memory allocator out of a single non-growable fixed-sized buffer.
Definition: allocator_memory.h:25
size_t fetch(void *buffer, size_t bytes)
fetch() generates a document object, sets its contents to the passed buffer, calls read() and returns...
Definition: instream.h:99