/*!
 * PHP-Qt - The PHP language bindings for Qt
 *
 * Copyright (C) 2006 - 2007
 * Thomas Moenicke <tm at php-qt.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#include "context.h"
#include <QByteArray>

Context* Context::m_Context = 0;

class Context::Private
{
public:
    Private( Context* q )
        : q( q ),
        callType( Context::MethodCall )
    {
    }

    QStack< QByteArray* > m_methodNameStack;
    QStack< zend_class_entry* > classEntryStack;
    QStack< zval* > zvalStack;
    zend_class_entry* m_activeCe;
    zval* m_activeScope;
    bool m_parentCall;
    Context::CallType callType;

private:
    Context* const q;
};

Context::Context() :
	d( new Private( this ) )
{}

void Context::createContext()
{
    m_Context = new Context;
}

void Context::destroyContext()
{
    delete m_Context;
}

void
Context::setActiveCe(zend_class_entry* ce)
{
    m_Context->d->classEntryStack.push( ce );
}

zend_class_entry*
Context::activeCe()
{
	return m_Context->d->classEntryStack.top();
}

void
Context::removeActiveCe()
{
	m_Context->d->classEntryStack.pop();
}

zval*
Context::activeScope()
{
    return m_Context->d->m_activeScope;
}

void
Context::setActiveScope(zval* zval_ptr)
{
    m_Context->d->m_activeScope = zval_ptr;
}

void
Context::setParentCall(bool pc)
{
    m_Context->d->m_parentCall = pc;
}

bool
Context::parentCall()
{
    return m_Context->d->m_parentCall;
}

void
Context::setMethodName(const char* name)
{
    m_Context->d->m_methodNameStack.push( new QByteArray(name) );
}

void
Context::removeMethodName()
{
    m_Context->d->m_methodNameStack.pop();
}

QByteArray*
Context::methodName()
{
    return m_Context->d->m_methodNameStack.top();
}

const char*
Context::methodNameC()
{
    return m_Context->d->m_methodNameStack.top()->constData();
}

void Context::setCallType( const Context::CallType callType )
{
    m_Context->d->callType = callType;
}

const Context::CallType Context::callType()
{
    return m_Context->d->callType;
}


void Context::setStaticZVal( zval* z )
{
    m_Context->d->zvalStack.push( z );
}

void Context::removeStaticZVal()
{
    efree( m_Context->d->zvalStack.pop() );
}

