This class combines the features of the std::istream and std::ostream classes, operating on memory segments (cf. shmget(2)).
As with std::fstream objects, FBB::MemoryStream objects do not keep separate offsets for reading and writing: the seek-members always refer to the (single) offset maintained by the FBB::MemoryBridge object to which the MemoryStream object (indirectly) interfaces.
The access rights of the data stored in MemoryStream objects are defined by the access parameter, interpreted as an octal value, using the specifications used by (chmod(1)).
The erase parameter is used to specify what happens to the memory segments once the MemoryStream object goes out of scope. When omitted or specified as false the allocated memory segments are not erased from memory when the object goes out of scope (and can be reaccessed until the computer is rebooted) using the memory object's ID (see the next constructor). When specified as true the memory segments are erased from memory when the object goes out of scope.
If construction fails, an FBB::Exception is thrown.
The erase parameter is used as it is used by the previous constructor.
Specifying the ios::trunc flag (or a comparable flag, like merely ios::out) immediately clears the current content of the memory segments.
An FBB::Exception is thrown if construction fails (e.g., no memory segment having ID id exists),
The move/copy constructors and assignment operators are not available.
The bufSize parameter required by the second MemoryStream constructor and the open member (see below) specifies the default number nummber of shared memory memory blocks and their sizes. The size of the memory blocks is specified as k, M or G, indicating block sizes in kilo-, Mega- and GigaBytes. Before those letters the default number of blocks is specified. E.g., "100M". Internally the number of kiloBytes is converted to `pages', using the system's page size, which is commonly equal to 4 kB (so when specifying "5k" then the stream prepares for two shared data segments, each having a capacity of 4 kB. The number of MegaBytes is used as specified, and when specifying GB the data segments are .5 GB.
The number of shared data segments is aotomatically enlarged when the current capacity is exceeded, and the potentially available data segments are initially not allocated: they're allocated once information is written into their areas.
All members of std::iostream are available.
If opening fails, an FBB::Exception is thrown.
If opening fails, an FBB::Exception is thrown.
#include <iostream>
#include <string>
#include <ostream>
#include <istream>
#include <bobcat/exception>
#include <bobcat/memorystream>
using namespace std;
using namespace FBB;
int main(int argc, char **argv)
{
MemoryStream ms;
char ch = 'h';
string cmd;
while (true)
{
ios::off_type offset;
switch (ch)
{
case 'h':
cout <<
"\n"
" b open a MemoryStream for 10k\n"
" h this info\n"
" i show info about the memory stream\n"
" o show the current absolute offset\n"
" q quit\n"
" s <x> seek (abs) offset x\n"
" x extract the next line from the current offset\n"
" < insert lines (until empty) at the current "
"offset\n"
" > extract lines (until EOF) from the current "
"offset\n";
break;
case 'b':
ms.open("10k", true);
[[fallthrough]];
case 'i':
ms.info(cout);
break;
case 'o':
cout << ms.seekg(0, ios::cur).tellg() << '\n';
break;
case 'q':
return 0;
case 's':
{
size_t offset;
cin >> offset;
cout << "tellg: " << ms.seekg(offset).tellg() << '\n';
}
break;
case 'x':
{
string line;
if (getline(ms, line))
cout << line << '\n';
else
{
cout << "No lines available\n";
ms.clear();
}
}
break;
case '<':
{
cin.ignore(100, '\n');
while (true)
{
string line;
getline(cin, line);
if (line.empty())
break;
ms << line << endl;
}
}
break;
case '>':
{
string line;
while (getline(ms, line))
cout << line << '\n';
ms.clear();
ms.seekg(0, ios::end);
}
break;
default:
cout << "request `" << ch << "' not implemented\n";
break;
}
cout << "? ";
cin >> ch;
}
}
Debian Bobcat project files: