/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
  QC_AutoGate.qpp
 
  Qore Programming Language
 
  Copyright (C) 2003 - 2023 David Nichols
 
  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/intern/QC_AutoGate.h"
#include "qore/intern/QC_Gate.h"

//! A helper class for the @ref Qore::Thread::Gate "Gate" class for exception-safe @ref Qore::Thread::Gate "Gate" handling
/** AutoGate objects, when used along with a @ref Qore::Thread::Gate "Gate" object, allow %Qore programmers to safely enter and exit a @ref Qore::Thread::Gate "Gate" lock, even if exceptions are thrown or return statements are executed in the block where the AutoGate object is created.

    AutoGate objects enter the gate lock for the lifetime of the AutoGate object. For this reason, it is only appropriate to assign an AutoGate object to a local variable, so when the local variable goes out of scope, the AutoGate object will be deleted and the gate automatically exited.

    For example:
    @code{.py}
our Gate gate();

sub check_error(error) {
    # note that the Gate is entered in the AutoGate constructor, and
    # the Gate will be exited as soon as the block is exited below.
    # (with either the throw statement or the return statement)
    AutoGate ag(gate);
    if (error)
        throw "ERROR", "sorry, an error happened";

    return "OK";
}
    @endcode

    @note This class is not available with the @ref PO_NO_THREAD_CLASSES parse option.
 */
qclass AutoGate [dom=THREAD_CLASS; arg=QoreAutoGate* ag; ns=Qore::Thread];

//! Creates the AutoGate object based on the Gate argument passed and immediately calls Gate::enter()
/** @param gate the @ref Qore::Thread::Gate "Gate" object to enter for the lifetime of the AutoGate object
    @par Example:
    @code{.py}
AutoGate ag(gate);
    @endcode
 */
AutoGate::constructor(Gate[QoreGate] gate) {
   QoreAutoGate *ag = new QoreAutoGate(gate, xsink);
   if (*xsink)
      ag->deref(xsink);
   else
      self->setPrivate(CID_AUTOGATE, ag);
}

//! Calls Gate::exit() and destroys the AutoGate object
/** 
    @par Example:
    @code{.py}
delete ag;
    @endcode
 */
AutoGate::destructor() {
   ag->destructor(xsink);
   ag->deref(xsink);
}

//! Throws an exception; objects of this class cannot be copied
/** 
    @throw AUTOGATE-COPY-ERROR objects of this class cannot be copied
 */
AutoGate::copy() {
   xsink->raiseException("AUTOGATE-COPY-ERROR", "objects of this class cannot be copied");
}
