/* -*- mode: c++; indent-tabs-mode: nil -*- */
/** @file QC_StreamBase.qpp StreamBase class (interface) definition */
/*
  Qore Programming Language

  Copyright (C) 2016 - 2023 Qore Technologies, s.r.o.

  Permission is hereby granted, free of charge, to any person obtaining a
  copy of this software and associated documentation files (the "Software"),
  to deal in the Software without restriction, including without limitation
  the rights to use, copy, modify, merge, publish, distribute, sublicense,
  and/or sell copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  DEALINGS IN THE SOFTWARE.

  Note that the Qore library is released under a choice of three open-source
  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
  information.
*/

#include <qore/Qore.h>
#include "qore/StreamBase.h"

//! This class defines the API for single thread streams
/**
    Base class for stream classes which are not designed to be accessed from multiple threads; they have been
    implemented without locking for fast and efficient use when used from a single thread.  For methods that
    would be unsafe to use in another thread, any use of such methods in threads other than the thread where
    the constructor was called will cause a \c STREAM-THREAD-ERROR to be thrown, unless the stream is handed
    off to another thread using the @ref Qore::StreamBase::unassignThread() "StreamBase::unassignThread()"
    method in the thread that currently owns the stream, and the
    @ref Qore::StreamBase::reassignThread() "StreamBase::reassignThread()" method in the new thread.

    @since %Qore 0.9
 */
qclass StreamBase [arg=StreamBase* sb; ns=Qore];

//! Throws an exception if called directly; this class can only be instantiated by builtin subclasses
/**
    @throw STREAMBASE-CONSTRUCTOR-ERROR this exception is thrown if this class is constructed directly (also if directly inherited by user classes)
 */
StreamBase::constructor() {
   xsink->raiseException("STREAMBASE-CONSTRUCTOR-ERROR", "this class is an abstract class and cannot be constructed directly or inherited directly by a user-defined class");
}

//! Reassigns current thread as thread used for stream manipulation
/** This method must be called from the new thread after calling the
    @ref Qore::StreamBase::unassignThread() "StreamBase::unassignThread()" method in the thread that currently owns the stream

    @throw STREAM-THREAD-ERROR thrown if called when the stream is assigned to another thread

    @see @ref Qore::StreamBase::unassignThread() "StreamBase::unassignThread()"
 */
nothing StreamBase::reassignThread() {
	sb->reassignThread(xsink);
}

//! Unassigns current thread as thread used for stream manipulation
/** This method must be called in the current thread that owns the stream before calling the
    @ref Qore::StreamBase::reassignThread() "StreamBase::reassignThread()" method in the new thread

    @throw STREAM-THREAD-ERROR thrown if called when the stream is assigned to another thread

    @see @ref Qore::StreamBase::reassignThread() "StreamBase::reassignThread()"
 */
nothing StreamBase::unassignThread() {
	sb->unassignThread(xsink);
}

//! Get the currently assigned thread id or -1 if not assigned to any thread
/**
*/
int StreamBase::getThreadId() {
   return sb->getThreadId();
}
