Provided by: libbobcat-dev_3.19.01-1ubuntu1_amd64
NAME
FBB::SharedStream - I/O operations on shared memory
SYNOPSIS
#include <bobcat/sharedstream> Linking option: -lpthread -lbobcat
DESCRIPTION
This class combines the features of the std::istream and std::ostream classes, operating on shared memory. As with std::fstream objects, a seekp or seekg member call is required to switch from writing to reading or v.v.
NAMESPACE
FBB All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB.
INHERITS FROM
FBB::SharedStreambuf (private inheritance), std::istream, std::ostream, FBB::SharedEnum__ (cf. sharedmemory(3bobcat) for a description of this last base class).
CONSTRUCTORS
o SharedStream(): The default constructor defines a stub a SharedStream object that cannot immediately be used to access shared memory. To use it, its member open must first be called. o SharedStream(size_t maxSize, SizeUnit sizeUnit, std::ios::openmode openMode = std::ios::in | std::ios::out, size_t access = 0600): This constructor creates a stream inheriting the facilities of an std::istream and std::ostream that interfaces to a shared memory segment having a capacity of at least maxSize * sizeUnit bytes. By default, the shared memory segment is opened for reading and writing. Different from the open modes used for file streams, creating a shared memory stream with open modes ios::in | ios::out is OK. In this case the shared memory segment is created and once information has been written to the shared memory it can also be read again. The shared memory’s access rights are defined by the access parameter, using the well-known (chmod(1)) way to define the access rights for the owner, the group and others, using octal digits. If construction succeeds the shared memory is ready for use. If construction fails, an FBB::Exception is thrown. o SharedStream(int id, std::ios::openmode openMode = std::ios::in | std::ios::out): This constructor creates a stream inheriting the facilities of an std::istream and std::ostream that connects to a shared memory segment having ID id. If construction succeeds the shared memory is ready for use. If construction fails (e.g., no shared memory segment having ID id exists), an FBB::Exception is thrown. Specifying the ios::trunc flag immediately clears the contents of the shared memory. Copy and move constructors are not available.
OVERLOADED OPERATORS
The overloaded move and copy assignment operators are not available.
MEMBER FUNCTIONS
All members of std::istream and std::ostream and the enum values kB, MB, and GB, defined by FBB::SharedEnum__ are available. o void clear(): This member clears the error states of the std::istream and std::ostream base class objects. o int id() const: The ID of the shared memory segment is returned. o void kill(): Without locking the shared memory first, all shared memory is returned to the operating system. The FBB::SharedStream object is unusable after returning from kill. Other processes that were using the shared memory can continue to do so. o void remove(): The shared memory is first locked. Next, all shared memory is returned to the operating system. The FBB::SharedStream object is unusable after returning from remove. Other processes that were using the shared memory can continue to do so. o void memInfo(std::ostream &out, char const *end = "\n"): Information about the SharedMemory object is inserted into the provide ostream object. The IDs of the shared segments, their sizes, the maximum number of shared memory segments, the number of bytes that can be read from the shared memory, and its actual storage capacity, etc., are displayed. Following the information about the shaed memory, end is inserted into out. o void open(size_t maxSize, SizeUnit sizeUnit, std::ios::openmode openMode = std::ios::in | std::ios::out, size_t access = 0600): This member creates a shared memory segment having a capacity of at least maxSize * sizeUnit bytes, and connects the shared memory segment to the FBB::SharedStream. A matching close member does not exist and is not required. By default, the shared memory segment is opened for reading and writing. Different from the open modes used for file streams, creating a shared memory stream with open modes ios::in | ios::out is OK. In this case the shared memory segment is created and once information has been written to the shared memory it can also be read again. The shared memory’s access rights are defined by the access parameter, using the well-known (chmod(1)) way to define the access rights for the owner, the group and others, using octal digits. If opening succeeds the shared memory is ready for use. If opening fails, an FBB::Exception is thrown. o void open(int id, std::ios::openmode openMode = std::ios::in | std::ios::out): This member connects the FBB::SharedStream object to a shared memory segment having ID id. A matching close member does not exist and is not required. If opening succeeds the shared memory is ready for use. If opening fails (e.g., no shared memory segment having ID id exists), an FBB::Exception is thrown. Specifying the ios::trunc flag immediately clears the contents of the shared memory.
EXAMPLE
#include <iostream> #include <string> #include <ostream> #include <istream> #include <bobcat/exception> #include <bobcat/sharedstream> using namespace std; using namespace FBB; int main() { SharedStream shared; int id = -1; while (true) { cout << "\n" " K kill (no lock) existing shared segment\n" " R remove existing shared segment\n" " S show stats of current shared segment\n" " L <id> Load segment <id>\n" " c create new shared memory (sets id)\n" " l lock segment id until key pressed\n" " p <x> c put char c at offset x\n" " q quit\n" " r <x> <n> read n chars from offset x\n" " w <x> args write all args at offset x\n" "? "; char ch; cin >> ch; ios::off_type offset; cout << "Requested: " << ch << ’\n’; shared.clear(); switch (ch) { case ’c’: { shared.open(1, SharedStream::kB); id = shared.id(); cout << "id = " << id << ’\n’; shared.memInfo(cout); cout << ’\n’; } break; case ’K’: // delete segment case ’R’: // delete segment { if (id == -1) { cout << "No segment loaded\n"; continue; } cout << "Removing segment id = " << id << ’\n’; if (ch == ’R’) shared.remove(); else shared.kill(); id = -1; } break; case ’L’: cin >> id; cout << "Loading segment " << id << ’\n’; shared.open(id); shared.memInfo(cout); cout << ’\n’; break; case ’S’: if (id == -1) { cout << "No segment loaded\n"; continue; } shared.memInfo(cout); cout << ’\n’; break; case ’p’: // put a char behind the last written { if (id == -1) { cout << "No segment loaded\n"; continue; } cin >> offset >> ch; if (!cin) throw Exception() << "cmd specification error"; shared.seekp(offset); cout << "Segment id = " << id << " at write offset " << shared.tellp() << ’\n’; shared.put(ch); } break; case ’r’: // put a char behind the last written { if (id == -1) { cout << "No segment loaded\n"; continue; } int n; cin >> offset >> n; if (!cin) throw Exception() << "cmd specification error"; char buf[n]; shared.seekg(offset); cout << "Segment id = " << id << " at offset " << shared.tellg() << ", to read " << n << " bytes\n"; n = shared.read(buf, n).gcount(); if (n < 0) cout << "No data at " << offset << ’\n’; else { cout << "Retrieved " << n << " bytes, containing `"; cout.write(buf, n); cout << "’\n"; for (auto ch: buf) cout << static_cast<int>(ch) << ’ ’; cout << ’\n’; } } break; case ’w’: // write chars at offset { if (id == -1) { cout << "No segment loaded\n"; continue; } string line; cin >> offset; getline(cin, line); if (!cin) throw Exception() << "cmd specification error"; streampos pos = shared.seekp(offset).tellp(); cout << "Segment id = " << id << " at offset " << pos << ", to write " << line.length() << " bytes\n"; shared.write(line.data(), line.length()); if (!shared) cout << "No room left to write any bytes\n"; else cout << "Wrote " << (shared.tellp() - pos) << " bytes\n"; } break; case ’q’: return 0; default: cout << "request not implemented: " << ch << ’\n’; break; } } }
FILES
bobcat/sharedstream - defines the class interface
SEE ALSO
bobcat(7), chmod(1), isharedstream(3bobcat), osharedstream(3bobcat), sharedblock(3bobcat), sharedmemory(3bobcat) sharedmutex(3bobcat), sharedpos(3bobcat), sharedsegment(3bobcat), sharedstreambuf(3bobcat)
BUGS
Note that by default exceptions thrown from within a std::stream object are caught by the stream object, setting its ios::failbit flag. To allow exceptions to leave a stream object, its exceptions member can be called, e.g., using: myStream.exceptions(ios::failbit | ios::badbit | ios::eofbit);
DISTRIBUTION FILES
o bobcat_3.19.01-x.dsc: detached signature; o bobcat_3.19.01-x.tar.gz: source archive; o bobcat_3.19.01-x_i386.changes: change log; o libbobcat1_3.19.01-x_*.deb: debian package holding the libraries; o libbobcat1-dev_3.19.01-x_*.deb: debian package holding the libraries, headers and manual pages; o http://sourceforge.net/projects/bobcat: public archive location;
BOBCAT
Bobcat is an acronym of `Brokken’s Own Base Classes And Templates’.
COPYRIGHT
This is free software, distributed under the terms of the GNU General Public License (GPL).
AUTHOR
Frank B. Brokken (f.b.brokken@rug.nl).