C++ Stream API
Stream core
-
class dyad_stream_core
Core DYAD synchronization state and operations for C++ stream interception.
Wraps a
dyad_ctxand exposes the producer/consumer synchronization operations needed by the C++ stream interception layer. Handles context initialization, file locking, path prefix matching, and open/close synchronization hooks.A single instance is typically embedded in a stream wrapper object. The context may be initialized either from environment variables via
init(bool)or from explicit parameters viainit(const dyad_params&).Note
dyad_stream_coredoes not initialize the DYAD context automatically in its constructor, nor does it tear it down in its destructor. Initialization and teardown are costly operations, and C++ objects may trigger many implicit copy constructions and destructions. The caller is responsible for explicitly callingdyad_stream_core::init()once and managing the context lifetime. stream wrapper classes only performs shallow copy of dyad_stream_core object as deep copying is not only costly but also can be desruptive by running routines that are supposed to run only once during the run.Public Functions
-
dyad_stream_core()
Constructs an uninitialized stream core.
-
~dyad_stream_core()
Finalizes the stream core, releasing the DYAD context.
-
void init(const bool reinit = false)
Initializes the stream core from environment variables.
Retrieves an existing DYAD context via
dyad_ctx_get()or initializes a new one if none exists, ifreinitistrue, or if theDYAD_REINITenvironment variable is set. In case of (re)initialization, it relies on the environment variable-based method,dyad_init_env()which reads configuration from environment variables. The existence of environment variablesDYAD_PATH_PRODUCERandDYAD_PATH_CONSUMERlimits the potential producer and consumer roles role of the enclosing stream wrapper object. Has no effect if already initialized and neitherreinitnor the environment variableDYAD_REINITis set.- Parameters:
reinit – [in] If
true, forces re-initialization even if already initialized.
-
void init(const dyad_params &p)
Initializes the stream core from explicit parameters.
Calls
dyad_init()with the settings inpand retrieves the resulting context. Producer and consumer roles are limited based on whether the managed paths inpare empty. Also setsctx->use_fs_locksbased on whetherDYAD_HAS_STD_FSTREAM_FDis set at cmake configure time.- Parameters:
p – [in] Parameters to initialize DYAD with.
-
void log_info(const std::string &msg_head) const
Logs the current DYAD configuration at INFO level.
- Parameters:
msg_head – [in] Header string printed before the configuration.
-
void finalize()
Releases the DYAD context and resets initialization state.
-
bool is_dyad_producer() const
Returns
trueif this instance is configured as a producer.
-
bool is_dyad_consumer() const
Returns
trueif this instance is configured as a consumer.
-
bool open_sync(const char *path)
Ensures a file is ready to read before a stream open.
Calls
dyad_consume()if the instance is a consumer and is initialized. Returnstrueif the file is ready or if no action is needed (not a consumer, or not initialized).- Parameters:
path – [in] Path to the file being opened.
- Returns:
trueon success or no-op,falseifdyad_consume()failed.
-
bool close_sync(const char *path)
Publishes a file after a stream close.
Calls
dyad_produce()if the instance is a producer and is initialized. Returnstrueif the file was published or if no action is needed (not a producer, or not initialized).- Parameters:
path – [in] Path to the file being closed.
- Returns:
trueon success or no-op,falseifdyad_produce()failed.
-
bool chk_initialized() const
Returns
trueif the stream core has been initialized.
-
bool chk_fsync_write() const
Returns
trueiffsync()on write is enabled in the context.
-
bool cmp_canonical_path_prefix(bool is_prod, const char *const path)
Checks whether
pathfalls under a DYAD-managed directory.Delegates to
cmp_canonical_path_prefix()and stores the extracted relative path inupath, retrievable viaget_upath().- Parameters:
is_prod – [in] If
true, match against the producer-managed path; otherwise match against the consumer-managed path.path – [in] Path to check.
- Returns:
trueifpathis under the managed directory.
-
std::string get_upath() const
Returns the relative path extracted by the last
cmp_canonical_path_prefix()call.
-
int file_lock_exclusive(int fd) const
Acquires an exclusive lock on an open file descriptor.
- Parameters:
fd – [in] File descriptor to lock.
- Returns:
dyad_rc_treturn code fromdyad_excl_flock().
Acquires a shared lock on an open file descriptor.
- Parameters:
fd – [in] File descriptor to lock.
- Returns:
dyad_rc_treturn code fromdyad_excl_flock().
-
int file_unlock(int fd) const
Releases a lock on an open file descriptor.
- Parameters:
fd – [in] File descriptor to unlock.
- Returns:
dyad_rc_treturn code fromdyad_release_flock().
-
dyad_stream_core()
Input stream
-
template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
class basic_ifstream_dyad A drop-in replacement for
std::basic_ifstreamthat integrates DYAD consumer-side synchronization.Wraps
std::basic_ifstreamand interceptsopen()andclose()to calldyad_consume()via the embeddeddyad_stream_core, ensuring the file is ready to read before the stream is opened. All other stream operations are delegated to the underlyingstd::basic_ifstream.The interface mirrors
std::basic_ifstreamas closely as possible, including C++11 move semantics,std::stringoverloads, and (in C++17)std::filesystem::pathoverloads, conditionally compiled based on the standard version in use.- Template Parameters:
_CharT – Character type of the stream.
_Traits – Character traits type. Defaults to
std::char_traits<_CharT>.
Public Functions
-
basic_ifstream_dyad(const dyad_stream_core &core)
Constructs a stream with an existing
dyad_stream_core.Copies
coreinto the embeddedm_coreand allocates the underlying stream without opening it. Does not callm_core.init()since the provided core is assumed to be already initialized.- Parameters:
core – [in] Initialized stream core to copy.
-
basic_ifstream_dyad()
Constructs an unopened stream and initializes the core from environment variables via
m_core.init().
-
explicit basic_ifstream_dyad(const char *filename, ios_base::openmode mode = ios_base::in)
Constructs and opens the stream, triggering consumer synchronization.
Initializes the core from environment variables, calls
open_sync()to ensure the file is ready viadyad_consume(), then opens the underlying stream.m_filenameis set only if the stream opened successfully.- Parameters:
filename – [in] Path to the file to open.
mode – [in] Open mode. Defaults to
ios_base::in.
-
~basic_ifstream_dyad()
Destroys the stream, releasing the underlying
basic_ifstream.In C++03, explicitly deletes the raw pointer. In C++11 and later, resets the
unique_ptr. Has no effect if the stream pointer is alreadynullptr.
-
void open(const char *filename, ios_base::openmode mode = ios_base::in)
Opens the file, triggering consumer synchronization.
Acquires a shared lock via
DYAD_SHARED_LOCK_CPP_IFSTREAM, callsopen_sync()to ensure the file is ready viadyad_consume(), then opens the underlying stream. If the stream pointer isnullptr, returns without action.m_filenameis set only if the stream opened successfully.- Parameters:
filename – [in] Path to the file to open.
mode – [in] Open mode. Defaults to
ios_base::in.
-
bool is_open()
Returns
trueif the underlying stream is open. Returnsfalseif the stream pointer isnullptr.
-
void close()
Closes the stream and releases the file lock.
Releases the shared lock before closing the underlying stream. Has no effect if the stream pointer is
nullptr.
-
filebuf *rdbuf() const
Returns the underlying stream buffer, or
nullptrif the stream pointer isnullptr.
-
basic_ifstream &get_stream()
Returns a reference to the underlying
std::basic_ifstream.Warning
If the stream pointer is
nullptr, behavior is undefined (TODO: throw).
-
void init(const dyad_stream_core &core)
Reinitializes the stream core from an existing
dyad_stream_core.Copies
coreintom_core, marks it as initialized viaset_initialized(), and logs the new state vialog_info().- Parameters:
core – [in] Stream core to copy into this instance.
-
inline const dyad_stream_core &core() const
Allow read-only access to the embedded
dyad_stream_core.
-
using dyad::ifstream_dyad = basic_ifstream_dyad<char>
basic_ifstream_dyadspecialization forchar.
-
using dyad::wifstream_dyad = basic_ifstream_dyad<wchar_t>
basic_ifstream_dyadspecialization forwchar_t.
Output stream
-
template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
class basic_ofstream_dyad A drop-in replacement for
std::basic_ofstreamthat integrates DYAD producer-side synchronization.Wraps
std::basic_ofstreamand interceptsopen()andclose()to acquire an exclusive file lock and calldyad_produce()via the embeddeddyad_stream_core, notifying consumers that the file is ready. IfDYAD_HAS_STD_FSTREAM_FDis set, locking is only applied when the file falls under the producer-managed path. Optionally callsfsync()before closing ifctx->fsync_writeis enabled.The interface mirrors
std::basic_ofstreamas closely as possible, including C++11 move semantics,std::stringoverloads, and (in C++17)std::filesystem::pathoverloads, conditionally compiled based on the standard version in use.- Template Parameters:
_CharT – Character type of the stream.
_Traits – Character traits type. Defaults to
std::char_traits<_CharT>.
Public Functions
-
basic_ofstream_dyad(const dyad_stream_core &core)
Constructs a stream with an existing
dyad_stream_core.Copies
coreinto the embeddedm_coreand allocates the underlying stream without opening it. Does not callm_core.init()since the provided core is assumed to be already initialized.- Parameters:
core – [in] Initialized stream core to copy.
-
basic_ofstream_dyad()
Constructs an unopened stream and initializes the core from environment variables via
m_core.init().
-
explicit basic_ofstream_dyad(const char *filename, ios_base::openmode mode = ios_base::out)
Constructs and opens the stream, acquiring an exclusive lock if the file is under the producer-managed path.
Initializes the core from environment variables and opens the underlying stream. If
DYAD_HAS_STD_FSTREAM_FDis set and the file falls under the producer-managed path, acquires an exclusive lock viaDYAD_EXCLUSIVE_LOCK_CPP_OFSTREAMand setsm_filename.- Parameters:
filename – [in] Path to the file to open.
mode – [in] Open mode. Defaults to
ios_base::out.
-
~basic_ofstream_dyad()
Destroys the stream, syncing and publishing if still open.
If the stream is still open, optionally calls
fsync_ofstream()ifctx->fsync_writeis enabled, releases the exclusive lock, resets the stream, then callsclose_sync()to publish the file viadyad_produce(). If the stream is already closed, simply resets it.
-
void open(const char *filename, ios_base::openmode mode = ios_base::out)
Opens the file and acquires an exclusive lock if under the producer-managed path.
Opens the underlying stream, then if
DYAD_HAS_STD_FSTREAM_FDis set and the file is under the producer-managed path, acquires an exclusive lock viaDYAD_EXCLUSIVE_LOCK_CPP_OFSTREAMand setsm_filename. Has no effect if the stream pointer isnullptr.- Parameters:
filename – [in] Path to the file to open.
mode – [in] Open mode. Defaults to
ios_base::out.
-
bool is_open()
Returns
trueif the underlying stream is open. Returnsfalseif the stream pointer isnullptr.
-
void close()
Closes the stream, syncing and publishing the file.
Optionally calls
fsync_ofstream()ifctx->fsync_writeis enabled, releases the exclusive lock viaDYAD_UNLOCK_CPP_OFSTREAM, closes the underlying stream, then callsclose_sync()to publish the file viadyad_produce(). Has no effect if the stream pointer isnullptr.
-
filebuf *rdbuf() const
Returns the underlying stream buffer, or
nullptrif the stream pointer isnullptr.
-
basic_ofstream &get_stream()
Returns a reference to the underlying
std::basic_ofstream.
-
void init(const dyad_stream_core &core)
Reinitializes the stream core from an existing
dyad_stream_core.Copies
coreintom_core, marks it as initialized viaset_initialized(), and logs the new state vialog_info().- Parameters:
core – [in] Stream core to copy into this instance.
-
inline const dyad_stream_core &core() const
Allow read-only access to the embedded
dyad_stream_core.
-
using dyad::ofstream_dyad = basic_ofstream_dyad<char>
basic_ofstream_dyadspecialization forchar.
-
using dyad::wofstream_dyad = basic_ofstream_dyad<wchar_t>
basic_ofstream_dyadspecialization forwchar_t.
Bidirectional stream
-
template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
class basic_fstream_dyad A drop-in replacement for
std::basic_fstreamthat integrates DYAD producer and consumer-side synchronization.Wraps
std::basic_fstreamand interceptsopen()andclose()to perform both consumer and producer synchronization via the embeddeddyad_stream_core. On open, callsopen_sync()to ensure the file is ready viadyad_consume(). If the instance is a producer, also acquires an exclusive lock (conditioned onDYAD_HAS_STD_FSTREAM_FDand path prefix matching). On close, if the instance is a producer, optionally callsfsync_fstream()and releases the lock before callingclose_sync()to publish the file viadyad_produce().The interface mirrors
std::basic_fstreamas closely as possible, including C++11 move semantics,std::stringoverloads, and (in C++17)std::filesystem::pathoverloads, conditionally compiled based on the standard version in use.- Template Parameters:
_CharT – Character type of the stream.
_Traits – Character traits type. Defaults to
std::char_traits<_CharT>.
Public Functions
-
basic_fstream_dyad(const dyad_stream_core &core)
Constructs a stream with an existing
dyad_stream_core.Copies
coreinto the embeddedm_coreand allocates the underlying stream without opening it. Does not callm_core.init()since the provided core is assumed to be already initialized.- Parameters:
core – [in] Initialized stream core to copy.
-
basic_fstream_dyad()
Constructs an unopened stream and initializes the core from environment variables via
m_core.init().
-
explicit basic_fstream_dyad(const char *filename, ios_base::openmode mode = ios_base::in | ios_base::out)
Constructs and opens the stream with both consumer and producer synchronization.
Initializes the core from environment variables, calls
open_sync()to ensure the file is ready viadyad_consume(), then opens the underlying stream. If the instance is a producer and the stream opened successfully, acquires an exclusive lock (conditioned onDYAD_HAS_STD_FSTREAM_FDand path prefix matching in C++03) and setsm_filename.- Parameters:
filename – [in] Path to the file to open.
mode – [in] Open mode. Defaults to
ios_base::in|out.
-
~basic_fstream_dyad()
Destroys the stream, syncing and publishing if still open.
If the stream is still open and the instance is a producer, optionally calls
fsync_fstream()ifctx->fsync_writeis enabled, releases the exclusive lock, resets the stream, then callsclose_sync()to publish the file viadyad_produce(). If the stream is already closed, simply resets it.
-
void open(const char *filename, ios_base::openmode mode = ios_base::in | ios_base::out)
Opens the file with both consumer and producer synchronization.
Calls
open_sync()to ensure the file is ready viadyad_consume(), then opens the underlying stream. If the instance is a producer and the stream opened successfully, acquires an exclusive lock (conditioned onDYAD_HAS_STD_FSTREAM_FDand path prefix matching) and setsm_filename. Has no effect if the stream pointer isnullptr.- Parameters:
filename – [in] Path to the file to open.
mode – [in] Open mode. Defaults to
ios_base::in|out.
-
bool is_open()
Returns
trueif the underlying stream is open. Returnsfalseif the stream pointer isnullptr.
-
void close()
Closes the stream, syncing and publishing the file if a producer.
If the instance is a producer, optionally calls
fsync_fstream()ifctx->fsync_writeis enabled, releases the exclusive lock viaDYAD_UNLOCK_CPP_OFSTREAM, closes the underlying stream, then callsclose_sync()to publish the file viadyad_produce(). Has no effect if the stream pointer isnullptr.
-
filebuf *rdbuf() const
Returns the underlying stream buffer, or
nullptrif the stream pointer isnullptr.
-
basic_fstream &get_stream()
Returns a reference to the underlying
std::basic_fstream.
-
void init(const dyad_stream_core &core)
Reinitializes the stream core from an existing
dyad_stream_core.Copies
coreintom_core, marks it as initialized viaset_initialized(), and logs the new state vialog_info().- Parameters:
core – [in] Stream core to copy into this instance.
-
inline const dyad_stream_core &core() const
Allow read-only access to the embedded
dyad_stream_core.
-
using dyad::fstream_dyad = basic_fstream_dyad<char>
basic_fstream_dyadspecialization forchar.
-
using dyad::wfstream_dyad = basic_fstream_dyad<wchar_t>
basic_fstream_dyadspecialization forwchar_t.