Basically Storage is continuous array of elements of same type. String and array of ints are also kinds of storage. The difference is that Storage can be much bigger than computer memory. Storage can be also mapped to real physical devices such as File, Network connection etc. In fact it is very similar concept to Stream, but it has additional requirement that all elements of Storage are of the same type. Storage have narrower set of operations than arrays. Supported operations are:
- checking if element with given index is in storage or outside of stream of elements - eos
- consuming first or few first elements from storage - get
- peeking into any element in storage (only forward) - peek
- it is also possible to operate directly on 'working window' - frame
You can move across storage in one direction and operate on 'working window'. First element of storage have always index of '0'. Below simple visualization of storage:
sink -----------|O--working window--O|----------source
O - optional buffers for input and output
It might look complicated in a first look but, it's not in fact. Using this module it is possible to write all operations on arrays in a such a way that it will be possible to use any other Storage than string and have it read data from e.g. file.
In storage module there are definitions of above operations for strings, so you can use:
char text; text.put("blabla"); char c= text.get() if (text.eos(10)) throw new Exception("Error");
When using in you program you can template it in such a way that it will work nevertheless user will put as Storage string or e.g. FileStorage?, which will read data from file. When using for strings there should be no performance penalty as all functions should be inlined Unfortunately DMD does not optimize functions which takes arguments by ref. (See: bugzilla issue: 2008). There is one concrete Storage implemented for files in package doost.storage.
This module is already quite important in my library, but I still think it needs some more conceptual work.