/* libsswf_tag_scriptlimits.c++ -- written by Alexis WILKE for Made to Order Software Corp. (c) 2002-2008 */

/*

Copyright (c) 2002-2008 Made to Order Software Corp.

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.

*/

/** \file
 *
 * \brief The implementation of the sswf::TagScriptLimits class
 *
 * This file declares the body of the functions which are not
 * inline. It is part of the SSWF library.
 */

#include	"sswf/libsswf.h"

using namespace sswf;


/////////////////////////////////////////// TagScriptLimits

/** \class sswf::TagScriptLimits
 *
 * \brief Defines the limits to the script interpreter
 *
 * This tag is used to limit the script execution in regard to
 * recursivity, length of time it takes to execute a script,
 * and some other such parameters.
 *
 * \sa sswf::TagDoAction
 * \sa <a href="../SWFalexref.html#tag_scriptlimits">SWF Alexis' Reference&mdash;Script Limits</a>
 * \sa <a href="../SWFalexref.html#swf_tag">SWF Alexis' Reference&mdash;swf_tag</a>
 */


/** \brief Initialize a script limit tag
 *
 * This object can be added to a TagHeader and represents the
 * limits to use to execute all the action scripts.
 *
 * By default, there is no limit.
 *
 * \param[in] parent The parent of the script limit tag
 */
TagScriptLimits::TagScriptLimits(TagBase *parent)
	: TagBase("scriptlimits", parent)
{
	f_depth = 0;
	f_timeout = 0;
}


/** \brief Get the type of a script limit tag
 *
 * This function determines what the type of a script limit tag is.
 *
 * \return SWF_TYPE_DEFINE
 */
TagBase::swf_type_t TagScriptLimits::TypeFlags(void) const
{
	return SWF_TYPE_DEFINE;
}


/** \brief Defines the maximum recursion depth
 *
 * This function defines the depth that the Call action can go to.
 * This is called the recursion limit since in general you will hit
 * the limit when a function calls itself this many times. It is
 * very unlikely that this isn't simply used as the number of calls
 * within calls can be done (A calls B, B calls C, C calls D is a
 * depth of 3.)
 *
 * In general, the default value is enough. You may want to increase
 * this value if you have more level of recursion.
 *
 * The value is limited between 1 and 65535.
 *
 * \param[in] depth The depth level of recursion
 *
 * \sa sswf::TagScriptLimits::GetMaxRecursionDepth()
 */
void TagScriptLimits::SetMaxRecursionDepth(int depth)
{
	f_depth = depth;
}


/** \brief Defines the amount of time for completion
 *
 * This function defines the number of seconds a script can
 * take before to be timed out.
 *
 * Obviously, for a realtime movie, anything taking more than a
 * few milliseconds is slow. So even 1 second will be considered
 * very long.
 *
 * Yet, if you use an ActionScript to do some complex mathematical
 * computations, you may want to increase this timeout.
 *
 * The value is limited between 1 and 65535.
 *
 * \sa sswf::TagScriptLimits::GetTimeoutSeconds()
 */
void TagScriptLimits::SetTimeoutSeconds(int timeout)
{
	f_timeout = timeout;
}


/** \brief Reads the current recursion depth
 *
 * The recursion depth set by SetMaxRecursionDepth() can be retreived
 * with this function.
 *
 * When set to zero, the player default is used.
 *
 * \return The current recursion depth
 *
 * \sa sswf::TagScriptLimits::SetMaxRecursionDepth()
 */
int TagScriptLimits::GetMaxRecursionDepth(void) const
{
	return f_depth;
}


/** \brief Reads the current time out period
 *
 * The time out period set by SetTimeOutSeconds() function can be
 * retreived with this function.
 *
 * When set to zero, the player default is used.
 *
 * \return The current time out in seconds
 *
 * \sa sswf::TagScriptLimits::SetTimeoutSeconds()
 */
int TagScriptLimits::GetTimeoutSeconds(void) const
{
	return f_timeout;
}


/** \brief Ensures the minimum version
 *
 * This function requests that the movie be at least of version 7.
 *
 * \return ErrorManager::ERROR_CODE_NONE when no error occured, an error otherwise
 */
ErrorManager::error_code_t TagScriptLimits::PreSave(void)
{
	MinimumVersion(7);

	return ErrorManager::ERROR_CODE_NONE;
}


/** \brief Saves the script limits
 *
 * This function saves the script limits to the output Data buffer.
 *
 * If the depth is still zero, 256 is used.
 *
 * If the timeout value is still zero, 20 is used.
 *
 * \param[in,out] data The Data buffer where the data is saved
 *
 * \return An error if one occurs, or ErrorManager::ERROR_CODE_NONE
 */
ErrorManager::error_code_t TagScriptLimits::Save(Data& data)
{
	SaveTag(data, SWF_TAG_SCRIPT_LIMITS, 4);

	// the defaults are as defined in the version 7 documentation of SWF
	data.PutShort((unsigned short) (f_depth   == 0 ? 256 : f_depth));
	data.PutShort((unsigned short) (f_timeout == 0 ?  20 : f_timeout));

	return ErrorManager::ERROR_CODE_NONE;
}





/* The following options fold the documentation; use 'zi' to turn on and off
 *
 * vim: foldexpr=getline(v\:lnum)!~'^/\\*\\*'&&getline(v\:lnum)!~'^\ \\*'?0\:1 foldcolumn=2 foldmethod=expr
 */
