2016-02-21 08:25:46 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#include "Routine.hpp"
|
2016-08-24 21:28:15 +02:00
|
|
|
#include "Base/DynArg.hpp"
|
2016-03-25 13:43:26 +01:00
|
|
|
#include "Library/Chrono.hpp"
|
2016-02-21 08:25:46 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
namespace SqMod {
|
|
|
|
|
2016-11-15 20:43:02 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SQMODE_DECL_TYPENAME(Typename, _SC("SqRoutine"))
|
|
|
|
|
2016-02-21 08:25:46 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Routine::Time Routine::s_Last = 0;
|
|
|
|
Routine::Time Routine::s_Prev = 0;
|
2016-05-22 05:20:38 +02:00
|
|
|
Routine::Routines Routine::s_Routines;
|
2016-03-12 07:47:50 +01:00
|
|
|
Routine::Objects Routine::s_Objects;
|
|
|
|
|
2016-02-23 04:23:56 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
void Routine::Process()
|
2016-02-23 04:23:56 +01:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
// Is this the first call?
|
|
|
|
if (s_Last == 0)
|
2016-03-12 07:47:50 +01:00
|
|
|
{
|
2016-06-04 18:17:42 +02:00
|
|
|
s_Last = Chrono::GetCurrentSysTime();
|
2016-05-22 05:20:38 +02:00
|
|
|
// We'll do it text time
|
2016-03-10 04:57:13 +01:00
|
|
|
return;
|
2016-03-12 07:47:50 +01:00
|
|
|
}
|
2016-05-22 05:20:38 +02:00
|
|
|
// Backup the last known time-stamp
|
|
|
|
s_Prev = s_Last;
|
|
|
|
// Get the current time-stamp
|
2016-06-04 18:17:42 +02:00
|
|
|
s_Last = Chrono::GetCurrentSysTime();
|
2016-05-22 05:20:38 +02:00
|
|
|
// Calculate the elapsed time
|
|
|
|
const Int32 delta = Int32((s_Last - s_Prev) / 1000L);
|
|
|
|
// Process all active routines
|
|
|
|
for (auto & r : s_Routines)
|
2016-03-12 07:47:50 +01:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
// Is this routine valid?
|
|
|
|
if (!r.second)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Decrease the elapsed time
|
|
|
|
r.first -= delta;
|
|
|
|
// Have we completed the routine interval?
|
|
|
|
if (r.first <= 0)
|
|
|
|
{
|
|
|
|
// Reset the elapsed time
|
|
|
|
r.first = r.second->m_Interval;
|
|
|
|
// Execute the routine
|
|
|
|
r.second->Execute();
|
|
|
|
}
|
2016-03-12 07:47:50 +01:00
|
|
|
}
|
2016-03-10 04:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
void Routine::Initialize()
|
2016-03-10 04:57:13 +01:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
// Clear all slots
|
|
|
|
s_Routines.fill(Element(0, nullptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Routine::Deinitialize()
|
|
|
|
{
|
|
|
|
// Process all routine instances
|
|
|
|
for (auto & r : s_Objects)
|
2016-03-12 07:47:50 +01:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
// Release all resources
|
|
|
|
r.first->Release();
|
|
|
|
// Mark it as terminated
|
|
|
|
r.first->m_Terminated = true;
|
2016-03-12 07:47:50 +01:00
|
|
|
}
|
2016-05-22 05:20:38 +02:00
|
|
|
// Release all references
|
|
|
|
s_Objects.clear();
|
|
|
|
// Clear all slots
|
|
|
|
s_Routines.fill(Element(0, nullptr));
|
|
|
|
// Clear the last time-stamp in case of a reload
|
|
|
|
s_Last = 0;
|
2016-03-12 07:47:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Object Routine::Associate(Routine * routine)
|
|
|
|
{
|
|
|
|
// Attempt to see if this instance already exists in the pool
|
|
|
|
Objects::iterator itr = s_Objects.find(routine);
|
|
|
|
// Is this routine remembered for the first time?
|
|
|
|
if (itr == s_Objects.end())
|
|
|
|
{
|
|
|
|
// Obtain the initial stack size
|
|
|
|
const StackGuard sg;
|
|
|
|
// Push this instance on the stack
|
|
|
|
ClassType< Routine >::PushInstance(DefaultVM::Get(), routine);
|
|
|
|
// Initialize this element into the pool
|
|
|
|
itr = s_Objects.emplace(routine, Var< Object >(DefaultVM::Get(), -1).value).first;
|
|
|
|
// If the iterator still points to a null element then we failed
|
|
|
|
if (itr == s_Objects.end())
|
|
|
|
{
|
2016-03-15 09:56:00 +01:00
|
|
|
// NOTE: Routine instance is destroyed by the script object if necessary!
|
2016-03-21 21:37:58 +01:00
|
|
|
STHROWF("Unable to remember routine instance");
|
2016-03-12 07:47:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Does this routine still keep a strong reference to it self?
|
|
|
|
else if (itr->second.IsNull())
|
|
|
|
{
|
|
|
|
// Obtain the initial stack size
|
|
|
|
const StackGuard sg;
|
|
|
|
// Push this instance on the stack
|
|
|
|
ClassType< Routine >::PushInstance(DefaultVM::Get(), routine);
|
|
|
|
// Obtain a strong reference to it
|
|
|
|
itr->second = Var< Object >(DefaultVM::Get(), -1).value;
|
|
|
|
}
|
|
|
|
// Return the object that we have
|
|
|
|
return itr->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Routine::Dissociate(Routine * routine)
|
|
|
|
{
|
|
|
|
// Attempt to see if this instance already exists in the pool
|
|
|
|
Objects::iterator itr = s_Objects.find(routine);
|
|
|
|
// Was this routine even stored in the pool?
|
|
|
|
if (itr != s_Objects.end() && !itr->second.IsNull())
|
|
|
|
{
|
|
|
|
itr->second.Release(); // Release the reference to self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
bool Routine::Associated(Routine * routine)
|
|
|
|
{
|
|
|
|
// Attempt to see if this instance already exists in the pool
|
|
|
|
Objects::iterator itr = s_Objects.find(routine);
|
|
|
|
// Return whether this routine is pooled and references itself
|
|
|
|
return (itr != s_Objects.end() && !itr->second.IsNull());
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Routine::Forget(Routine * routine)
|
|
|
|
{
|
|
|
|
s_Objects.erase(routine);
|
2016-03-10 04:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
void Routine::Insert(Routine * routine, bool associate)
|
2016-03-10 04:57:13 +01:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
// Do we even have what to insert?
|
|
|
|
if (!routine)
|
2016-03-12 07:47:50 +01:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
return;
|
2016-03-12 07:47:50 +01:00
|
|
|
}
|
2016-05-22 05:20:38 +02:00
|
|
|
// See if this routine is already inserted
|
|
|
|
Routines::iterator itr = std::find_if(s_Routines.begin(), s_Routines.end(),
|
|
|
|
[routine](Element & e) -> bool { return (e.second == routine); });
|
|
|
|
// Is this instance already inserted?
|
|
|
|
if (itr != s_Routines.end())
|
2016-03-10 04:57:13 +01:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
return; // We have what we came here for!
|
|
|
|
}
|
|
|
|
// Attempt to locate an unused routine slot
|
|
|
|
itr = std::find_if(s_Routines.begin(), s_Routines.end(),
|
|
|
|
[](Element & e) -> bool { return (e.second == nullptr); });
|
|
|
|
// Are there any slots available?
|
|
|
|
if (itr == s_Routines.end())
|
|
|
|
{
|
|
|
|
STHROWF("Maximum number of active routines was reached: %d", SQMOD_MAX_ROUTINES);
|
|
|
|
}
|
|
|
|
// Insert the routine at the obtained slot
|
|
|
|
itr->first = routine->m_Interval;
|
|
|
|
itr->second = routine;
|
|
|
|
// Associate this slot with the routine instance
|
|
|
|
routine->m_Slot = std::distance(s_Routines.begin(), itr);
|
|
|
|
// Keep a string reference to this routine instance, if requested
|
|
|
|
if (associate)
|
|
|
|
{
|
|
|
|
Associate(routine);
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
2016-03-10 04:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-07-12 21:45:14 +02:00
|
|
|
void Routine::Remove(Routine * routine, bool forget)
|
2016-03-10 04:57:13 +01:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
// Do we even have what to remove?
|
|
|
|
if (!routine)
|
2016-02-23 04:23:56 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-05-22 05:20:38 +02:00
|
|
|
Routines::iterator itr;
|
|
|
|
// Make sure the routine wasn't activated multiple time (paranoia)
|
|
|
|
do {
|
|
|
|
// Search for the slot with the specified routine instance
|
|
|
|
itr = std::find_if(s_Routines.begin(), s_Routines.end(),
|
2016-07-12 21:45:14 +02:00
|
|
|
[routine](const Element & e) -> bool { return (e.second == routine); });
|
2016-05-22 05:20:38 +02:00
|
|
|
// Was this routine instance activated?
|
|
|
|
if (itr != s_Routines.end())
|
2016-03-12 07:47:50 +01:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
itr->first = 0;
|
|
|
|
itr->second = nullptr;
|
2016-03-12 07:47:50 +01:00
|
|
|
}
|
2016-05-22 05:20:38 +02:00
|
|
|
} while (itr != s_Routines.end());
|
|
|
|
// Reset the routine instance slot
|
|
|
|
routine->m_Slot = 0xFFFF;
|
|
|
|
// Release any strong reference to this routine instance
|
2016-07-12 21:45:14 +02:00
|
|
|
if (forget)
|
|
|
|
{
|
|
|
|
Forget(routine);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Dissociate(routine);
|
|
|
|
}
|
2016-05-22 05:20:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Routine::Release()
|
|
|
|
{
|
|
|
|
// Release the callback
|
|
|
|
m_Callback.ReleaseGently();
|
|
|
|
// Release the arguments
|
|
|
|
m_Arg1.Release();
|
|
|
|
m_Arg2.Release();
|
|
|
|
m_Arg3.Release();
|
|
|
|
m_Arg4.Release();
|
|
|
|
m_Arg5.Release();
|
|
|
|
m_Arg6.Release();
|
|
|
|
m_Arg7.Release();
|
|
|
|
m_Arg8.Release();
|
|
|
|
m_Arg9.Release();
|
|
|
|
m_Arg10.Release();
|
|
|
|
m_Arg11.Release();
|
|
|
|
m_Arg12.Release();
|
|
|
|
m_Arg13.Release();
|
|
|
|
m_Arg14.Release();
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
void Routine::Execute()
|
2016-02-23 04:23:56 +01:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
// Is this routine suspended or has nothing to call?
|
|
|
|
if (m_Suspended || m_Callback.IsNull())
|
2016-02-23 04:23:56 +01:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
return; // We're done here!
|
|
|
|
}
|
|
|
|
// Attempt to forward the call
|
|
|
|
try
|
|
|
|
{
|
|
|
|
switch (m_Arguments)
|
2016-02-23 04:23:56 +01:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
// Attempt to identify how many arguments should be passed
|
|
|
|
case 0:
|
2016-05-24 06:24:21 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
m_Callback.Execute();
|
2016-05-24 06:24:21 +02:00
|
|
|
} break;
|
2016-05-22 05:20:38 +02:00
|
|
|
case 1:
|
2016-05-24 06:24:21 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
m_Callback.Execute(m_Arg1);
|
2016-05-24 06:24:21 +02:00
|
|
|
} break;
|
2016-05-22 05:20:38 +02:00
|
|
|
case 2:
|
2016-05-24 06:24:21 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
m_Callback.Execute(m_Arg1, m_Arg2);
|
2016-05-24 06:24:21 +02:00
|
|
|
} break;
|
2016-05-22 05:20:38 +02:00
|
|
|
case 3:
|
2016-05-24 06:24:21 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
m_Callback.Execute(m_Arg1, m_Arg2, m_Arg3);
|
2016-05-24 06:24:21 +02:00
|
|
|
} break;
|
2016-05-22 05:20:38 +02:00
|
|
|
case 4:
|
2016-05-24 06:24:21 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
m_Callback.Execute(m_Arg1, m_Arg2, m_Arg3, m_Arg4);
|
2016-05-24 06:24:21 +02:00
|
|
|
} break;
|
2016-05-22 05:20:38 +02:00
|
|
|
case 5:
|
2016-05-24 06:24:21 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
m_Callback.Execute(m_Arg1, m_Arg2, m_Arg3, m_Arg4, m_Arg5);
|
2016-05-24 06:24:21 +02:00
|
|
|
} break;
|
2016-05-22 05:20:38 +02:00
|
|
|
case 6:
|
2016-05-24 06:24:21 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
m_Callback.Execute(m_Arg1, m_Arg2, m_Arg3, m_Arg4, m_Arg5, m_Arg6);
|
2016-05-24 06:24:21 +02:00
|
|
|
} break;
|
2016-05-22 05:20:38 +02:00
|
|
|
case 7:
|
2016-05-24 06:24:21 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
m_Callback.Execute(m_Arg1, m_Arg2, m_Arg3, m_Arg4, m_Arg5, m_Arg6, m_Arg7);
|
2016-05-24 06:24:21 +02:00
|
|
|
} break;
|
2016-05-22 05:20:38 +02:00
|
|
|
case 8:
|
2016-05-24 06:24:21 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
m_Callback.Execute(m_Arg1, m_Arg2, m_Arg3, m_Arg4, m_Arg5, m_Arg6, m_Arg7,
|
|
|
|
m_Arg8);
|
2016-05-24 06:24:21 +02:00
|
|
|
} break;
|
2016-05-22 05:20:38 +02:00
|
|
|
case 9:
|
2016-05-24 06:24:21 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
m_Callback.Execute(m_Arg1, m_Arg2, m_Arg3, m_Arg4, m_Arg5, m_Arg6, m_Arg7,
|
|
|
|
m_Arg8, m_Arg9);
|
2016-05-24 06:24:21 +02:00
|
|
|
} break;
|
2016-05-22 05:20:38 +02:00
|
|
|
case 10:
|
2016-05-24 06:24:21 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
m_Callback.Execute(m_Arg1, m_Arg2, m_Arg3, m_Arg4, m_Arg5, m_Arg6, m_Arg7,
|
|
|
|
m_Arg8, m_Arg9, m_Arg10);
|
2016-05-24 06:24:21 +02:00
|
|
|
} break;
|
2016-05-22 05:20:38 +02:00
|
|
|
case 11:
|
2016-05-24 06:24:21 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
m_Callback.Execute(m_Arg1, m_Arg2, m_Arg3, m_Arg4, m_Arg5, m_Arg6, m_Arg7,
|
|
|
|
m_Arg8, m_Arg9, m_Arg10, m_Arg11);
|
2016-05-24 06:24:21 +02:00
|
|
|
} break;
|
2016-05-22 05:20:38 +02:00
|
|
|
case 12:
|
2016-05-24 06:24:21 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
m_Callback.Execute(m_Arg1, m_Arg2, m_Arg3, m_Arg4, m_Arg5, m_Arg6, m_Arg7,
|
|
|
|
m_Arg8, m_Arg9, m_Arg10, m_Arg11, m_Arg12);
|
2016-05-24 06:24:21 +02:00
|
|
|
} break;
|
2016-05-22 05:20:38 +02:00
|
|
|
case 13:
|
2016-05-24 06:24:21 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
m_Callback.Execute(m_Arg1, m_Arg2, m_Arg3, m_Arg4, m_Arg5, m_Arg6, m_Arg7,
|
|
|
|
m_Arg8, m_Arg9, m_Arg10, m_Arg11, m_Arg12, m_Arg13);
|
2016-05-24 06:24:21 +02:00
|
|
|
} break;
|
2016-05-22 05:20:38 +02:00
|
|
|
case 14:
|
2016-05-24 06:24:21 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
m_Callback.Execute(m_Arg1, m_Arg2, m_Arg3, m_Arg4, m_Arg5, m_Arg6, m_Arg7,
|
|
|
|
m_Arg8, m_Arg9, m_Arg10, m_Arg11, m_Arg12, m_Arg13, m_Arg14);
|
2016-05-24 06:24:21 +02:00
|
|
|
} break;
|
2016-07-12 21:45:14 +02:00
|
|
|
// Should not reach this point but just in case
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
m_Callback.Execute(m_Arg1, m_Arg2, m_Arg3, m_Arg4, m_Arg5, m_Arg6, m_Arg7,
|
|
|
|
m_Arg8, m_Arg9, m_Arg10, m_Arg11, m_Arg12, m_Arg13, m_Arg14);
|
|
|
|
} break;
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
|
|
|
}
|
2016-05-22 05:20:38 +02:00
|
|
|
catch (const Sqrat::Exception & e)
|
|
|
|
{
|
2016-07-09 13:18:09 +02:00
|
|
|
LogErr("Routine [%s] => Squirrel error [%s]", m_Tag.c_str(), e.what());
|
2016-05-22 05:20:38 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception & e)
|
|
|
|
{
|
|
|
|
LogErr("Routine [%s] => Program error [%s]", m_Tag.c_str(), e.what());
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
LogErr("Routine [%s] => Unknown error", m_Tag.c_str());
|
|
|
|
}
|
|
|
|
// Decrease the number of iterations if necessary
|
|
|
|
if (m_Iterations && (--m_Iterations) == 0)
|
|
|
|
{
|
|
|
|
Terminate(); // This routine reached the end of it's life
|
|
|
|
}
|
2016-02-23 04:23:56 +01:00
|
|
|
}
|
2016-02-21 08:25:46 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Routine::Routine(Object & env, Function & func, Interval interval)
|
|
|
|
: m_Iterations(0)
|
|
|
|
, m_Interval(interval)
|
2016-05-22 05:20:38 +02:00
|
|
|
, m_Slot(0xFFFF)
|
2016-02-21 08:25:46 +01:00
|
|
|
, m_Arguments(0)
|
|
|
|
, m_Suspended(false)
|
|
|
|
, m_Terminated(false)
|
2016-05-24 06:24:21 +02:00
|
|
|
, m_Callback()
|
2016-03-12 21:51:05 +01:00
|
|
|
, m_Tag(_SC(""))
|
2016-03-12 07:47:50 +01:00
|
|
|
, m_Data()
|
2016-02-21 08:25:46 +01:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
// Make sure the interval is good
|
|
|
|
if (m_Interval < 0)
|
|
|
|
{
|
|
|
|
STHROWF("Invalid routine interval");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-24 06:24:21 +02:00
|
|
|
// Does the callback need a custom environment?
|
|
|
|
if (env.IsNull())
|
|
|
|
{
|
|
|
|
m_Callback = func;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Callback = Function(env.GetVM(), env, func.GetFunc());
|
|
|
|
}
|
|
|
|
// Activate this routine
|
2016-05-22 05:20:38 +02:00
|
|
|
Insert(this, false);
|
|
|
|
}
|
2016-02-21 08:25:46 +01:00
|
|
|
}
|
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
Routine::Routine(Object & env, Function & func, Interval interval, Iterator iterations)
|
2016-02-21 08:25:46 +01:00
|
|
|
: m_Iterations(iterations)
|
|
|
|
, m_Interval(interval)
|
2016-05-22 05:20:38 +02:00
|
|
|
, m_Slot(0xFFFF)
|
2016-02-21 08:25:46 +01:00
|
|
|
, m_Arguments(0)
|
|
|
|
, m_Suspended(false)
|
|
|
|
, m_Terminated(false)
|
2016-05-24 06:24:21 +02:00
|
|
|
, m_Callback()
|
2016-03-12 21:51:05 +01:00
|
|
|
, m_Tag(_SC(""))
|
2016-03-12 07:47:50 +01:00
|
|
|
, m_Data()
|
2016-02-21 08:25:46 +01:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
// Make sure the interval is good
|
|
|
|
if (m_Interval < 0)
|
|
|
|
{
|
|
|
|
STHROWF("Invalid routine interval");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-24 06:24:21 +02:00
|
|
|
// Does the callback need a custom environment?
|
|
|
|
if (env.IsNull())
|
|
|
|
{
|
|
|
|
m_Callback = func;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Callback = Function(env.GetVM(), env, func.GetFunc());
|
|
|
|
}
|
|
|
|
// Activate this routine
|
2016-05-22 05:20:38 +02:00
|
|
|
Insert(this, false);
|
|
|
|
}
|
2016-02-21 08:25:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
Routine::Routine(Object & env, Function & func, Interval interval, Iterator iterations
|
2016-02-21 08:25:46 +01:00
|
|
|
, Object & a1)
|
|
|
|
: m_Iterations(iterations)
|
|
|
|
, m_Interval(interval)
|
2016-05-22 05:20:38 +02:00
|
|
|
, m_Slot(0xFFFF)
|
2016-02-21 08:25:46 +01:00
|
|
|
, m_Arguments(1)
|
|
|
|
, m_Suspended(false)
|
|
|
|
, m_Terminated(false)
|
2016-05-24 06:24:21 +02:00
|
|
|
, m_Callback()
|
2016-03-12 21:51:05 +01:00
|
|
|
, m_Tag(_SC(""))
|
2016-03-12 07:47:50 +01:00
|
|
|
, m_Data()
|
2016-02-21 08:25:46 +01:00
|
|
|
, m_Arg1(a1)
|
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
// Make sure the interval is good
|
|
|
|
if (m_Interval < 0)
|
|
|
|
{
|
|
|
|
STHROWF("Invalid routine interval");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-24 06:24:21 +02:00
|
|
|
// Does the callback need a custom environment?
|
|
|
|
if (env.IsNull())
|
|
|
|
{
|
|
|
|
m_Callback = func;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Callback = Function(env.GetVM(), env, func.GetFunc());
|
|
|
|
}
|
|
|
|
// Activate this routine
|
2016-05-22 05:20:38 +02:00
|
|
|
Insert(this, false);
|
|
|
|
}
|
2016-02-21 08:25:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
Routine::Routine(Object & env, Function & func, Interval interval, Iterator iterations
|
2016-02-21 08:25:46 +01:00
|
|
|
, Object & a1, Object & a2)
|
|
|
|
: m_Iterations(iterations)
|
|
|
|
, m_Interval(interval)
|
2016-05-22 05:20:38 +02:00
|
|
|
, m_Slot(0xFFFF)
|
2016-02-21 08:25:46 +01:00
|
|
|
, m_Arguments(2)
|
|
|
|
, m_Suspended(false)
|
|
|
|
, m_Terminated(false)
|
2016-05-24 06:24:21 +02:00
|
|
|
, m_Callback()
|
2016-03-12 21:51:05 +01:00
|
|
|
, m_Tag(_SC(""))
|
2016-03-12 07:47:50 +01:00
|
|
|
, m_Data()
|
2016-02-21 08:25:46 +01:00
|
|
|
, m_Arg1(a1), m_Arg2(a2)
|
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
// Make sure the interval is good
|
|
|
|
if (m_Interval < 0)
|
|
|
|
{
|
|
|
|
STHROWF("Invalid routine interval");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-24 06:24:21 +02:00
|
|
|
// Does the callback need a custom environment?
|
|
|
|
if (env.IsNull())
|
|
|
|
{
|
|
|
|
m_Callback = func;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Callback = Function(env.GetVM(), env, func.GetFunc());
|
|
|
|
}
|
|
|
|
// Activate this routine
|
2016-05-22 05:20:38 +02:00
|
|
|
Insert(this, false);
|
|
|
|
}
|
2016-02-21 08:25:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
Routine::Routine(Object & env, Function & func, Interval interval, Iterator iterations
|
2016-02-21 08:25:46 +01:00
|
|
|
, Object & a1, Object & a2, Object & a3)
|
|
|
|
: m_Iterations(iterations)
|
|
|
|
, m_Interval(interval)
|
2016-05-22 05:20:38 +02:00
|
|
|
, m_Slot(0xFFFF)
|
2016-02-21 08:25:46 +01:00
|
|
|
, m_Arguments(3)
|
|
|
|
, m_Suspended(false)
|
|
|
|
, m_Terminated(false)
|
2016-05-24 06:24:21 +02:00
|
|
|
, m_Callback()
|
2016-03-12 21:51:05 +01:00
|
|
|
, m_Tag(_SC(""))
|
2016-03-12 07:47:50 +01:00
|
|
|
, m_Data()
|
2016-02-21 08:25:46 +01:00
|
|
|
, m_Arg1(a1), m_Arg2(a2), m_Arg3(a3)
|
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
// Make sure the interval is good
|
|
|
|
if (m_Interval < 0)
|
|
|
|
{
|
|
|
|
STHROWF("Invalid routine interval");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-24 06:24:21 +02:00
|
|
|
// Does the callback need a custom environment?
|
|
|
|
if (env.IsNull())
|
|
|
|
{
|
|
|
|
m_Callback = func;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Callback = Function(env.GetVM(), env, func.GetFunc());
|
|
|
|
}
|
|
|
|
// Activate this routine
|
2016-05-22 05:20:38 +02:00
|
|
|
Insert(this, false);
|
|
|
|
}
|
2016-02-21 08:25:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
Routine::Routine(Object & env, Function & func, Interval interval, Iterator iterations
|
2016-02-21 08:25:46 +01:00
|
|
|
, Object & a1, Object & a2, Object & a3, Object & a4)
|
|
|
|
: m_Iterations(iterations)
|
|
|
|
, m_Interval(interval)
|
2016-05-22 05:20:38 +02:00
|
|
|
, m_Slot(0xFFFF)
|
2016-02-21 08:25:46 +01:00
|
|
|
, m_Arguments(4)
|
|
|
|
, m_Suspended(false)
|
|
|
|
, m_Terminated(false)
|
2016-05-24 06:24:21 +02:00
|
|
|
, m_Callback()
|
2016-03-12 21:51:05 +01:00
|
|
|
, m_Tag(_SC(""))
|
2016-03-12 07:47:50 +01:00
|
|
|
, m_Data()
|
2016-02-21 08:25:46 +01:00
|
|
|
, m_Arg1(a1), m_Arg2(a2), m_Arg3(a3), m_Arg4(a4)
|
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
// Make sure the interval is good
|
|
|
|
if (m_Interval < 0)
|
|
|
|
{
|
|
|
|
STHROWF("Invalid routine interval");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-24 06:24:21 +02:00
|
|
|
// Does the callback need a custom environment?
|
|
|
|
if (env.IsNull())
|
|
|
|
{
|
|
|
|
m_Callback = func;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Callback = Function(env.GetVM(), env, func.GetFunc());
|
|
|
|
}
|
|
|
|
// Activate this routine
|
2016-05-22 05:20:38 +02:00
|
|
|
Insert(this, false);
|
|
|
|
}
|
2016-02-21 08:25:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
Routine::Routine(Object & env, Function & func, Interval interval, Iterator iterations
|
2016-02-21 08:25:46 +01:00
|
|
|
, Object & a1, Object & a2, Object & a3, Object & a4, Object & a5)
|
|
|
|
: m_Iterations(iterations)
|
|
|
|
, m_Interval(interval)
|
2016-05-22 05:20:38 +02:00
|
|
|
, m_Slot(0xFFFF)
|
2016-02-21 08:25:46 +01:00
|
|
|
, m_Arguments(5)
|
|
|
|
, m_Suspended(false)
|
|
|
|
, m_Terminated(false)
|
2016-05-24 06:24:21 +02:00
|
|
|
, m_Callback()
|
2016-03-12 21:51:05 +01:00
|
|
|
, m_Tag(_SC(""))
|
2016-03-12 07:47:50 +01:00
|
|
|
, m_Data()
|
2016-02-21 08:25:46 +01:00
|
|
|
, m_Arg1(a1), m_Arg2(a2), m_Arg3(a3), m_Arg4(a4), m_Arg5(a5)
|
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
// Make sure the interval is good
|
|
|
|
if (m_Interval < 0)
|
|
|
|
{
|
|
|
|
STHROWF("Invalid routine interval");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-24 06:24:21 +02:00
|
|
|
// Does the callback need a custom environment?
|
|
|
|
if (env.IsNull())
|
|
|
|
{
|
|
|
|
m_Callback = func;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Callback = Function(env.GetVM(), env, func.GetFunc());
|
|
|
|
}
|
|
|
|
// Activate this routine
|
2016-05-22 05:20:38 +02:00
|
|
|
Insert(this, false);
|
|
|
|
}
|
2016-02-21 08:25:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Routine::~Routine()
|
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
// Release script resources
|
|
|
|
Release();
|
2016-07-12 21:45:14 +02:00
|
|
|
// Remove it from the pool of instances, just in case
|
|
|
|
Forget(this);
|
2016-02-21 08:25:46 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-21 08:25:46 +01:00
|
|
|
CSStr Routine::ToString() const
|
|
|
|
{
|
|
|
|
return ToStrF(_PRINT_INT_FMT, m_Interval);
|
|
|
|
}
|
|
|
|
|
2016-03-12 07:47:50 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
const String & Routine::GetTag() const
|
|
|
|
{
|
|
|
|
return m_Tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-11-16 12:12:49 +01:00
|
|
|
void Routine::SetTag(const StackStrF & tag)
|
2016-03-12 07:47:50 +01:00
|
|
|
{
|
2016-11-16 12:12:49 +01:00
|
|
|
if (tag.mLen)
|
|
|
|
{
|
|
|
|
m_Tag.assign(tag.mPtr, tag.mLen);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Tag.clear();
|
|
|
|
}
|
2016-03-12 07:47:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Object & Routine::GetData()
|
|
|
|
{
|
|
|
|
// Validate the routine lifetime
|
|
|
|
Validate();
|
|
|
|
// Return the requested information
|
|
|
|
return m_Data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Routine::SetData(Object & data)
|
|
|
|
{
|
|
|
|
// Validate the routine lifetime
|
|
|
|
Validate();
|
|
|
|
// Apply the specified value
|
|
|
|
m_Data = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-11-16 14:10:33 +01:00
|
|
|
Routine & Routine::ApplyTag(const StackStrF & tag)
|
2016-03-12 07:47:50 +01:00
|
|
|
{
|
2016-11-16 12:12:49 +01:00
|
|
|
if (tag.mLen)
|
|
|
|
{
|
|
|
|
m_Tag.assign(tag.mPtr, tag.mLen);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Tag.clear();
|
|
|
|
}
|
2016-03-12 07:47:50 +01:00
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Routine & Routine::ApplyData(Object & data)
|
|
|
|
{
|
|
|
|
// Validate the routine lifetime
|
|
|
|
Validate();
|
|
|
|
// Apply the specified value
|
|
|
|
m_Data = data;
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-02-21 08:25:46 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
Routine & Routine::Activate()
|
|
|
|
{
|
|
|
|
// Validate the routine lifetime
|
|
|
|
Validate();
|
|
|
|
// Make sure the interval is good
|
|
|
|
if (m_Interval < 0)
|
|
|
|
{
|
|
|
|
STHROWF("Invalid routine interval");
|
|
|
|
}
|
|
|
|
// Make sure no slot is associated
|
|
|
|
else if (m_Slot >= s_Routines.size())
|
|
|
|
{
|
2016-07-12 21:45:14 +02:00
|
|
|
// Keep a strong reference to the script object
|
|
|
|
Associate(this);
|
|
|
|
// Insert it into the pool of active routines
|
2016-05-22 05:20:38 +02:00
|
|
|
Insert(this);
|
|
|
|
}
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Routine & Routine::Deactivate()
|
|
|
|
{
|
|
|
|
// Validate the routine lifetime
|
|
|
|
Validate();
|
2016-07-12 21:45:14 +02:00
|
|
|
// Remove it from the active pool
|
2016-05-22 05:20:38 +02:00
|
|
|
Remove(this);
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Routine & Routine::Terminate()
|
2016-02-21 08:25:46 +01:00
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
// Was the routine already terminated?
|
2016-02-21 08:25:46 +01:00
|
|
|
if (m_Terminated)
|
2016-03-12 07:47:50 +01:00
|
|
|
{
|
2016-03-21 21:37:58 +01:00
|
|
|
STHROWF("Routine was already terminated");
|
2016-03-12 07:47:50 +01:00
|
|
|
}
|
2016-05-22 05:20:38 +02:00
|
|
|
// Release script resources
|
2016-03-10 04:57:13 +01:00
|
|
|
Release();
|
2016-07-12 21:45:14 +02:00
|
|
|
// Remove it from the active pool
|
|
|
|
Remove(this, true);
|
2016-05-22 05:20:38 +02:00
|
|
|
// Mark it as terminated
|
|
|
|
m_Terminated = true;
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Routine & Routine::Reanimate()
|
|
|
|
{
|
|
|
|
// Allow the instance to be used
|
|
|
|
m_Terminated = false;
|
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
2016-02-21 08:25:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
Routine & Routine::SetArg(Uint16 num, Object & val)
|
2016-02-21 08:25:46 +01:00
|
|
|
{
|
2016-03-12 07:47:50 +01:00
|
|
|
// Validate the routine lifetime
|
|
|
|
Validate();
|
2016-03-10 04:57:13 +01:00
|
|
|
// Identify which argument was requested
|
2016-02-21 08:25:46 +01:00
|
|
|
switch (num)
|
|
|
|
{
|
|
|
|
case 1: m_Arg1 = val; break;
|
|
|
|
case 2: m_Arg2 = val; break;
|
|
|
|
case 3: m_Arg3 = val; break;
|
|
|
|
case 4: m_Arg4 = val; break;
|
|
|
|
case 5: m_Arg5 = val; break;
|
|
|
|
case 6: m_Arg6 = val; break;
|
|
|
|
case 7: m_Arg7 = val; break;
|
|
|
|
case 8: m_Arg8 = val; break;
|
|
|
|
case 9: m_Arg9 = val; break;
|
|
|
|
case 10: m_Arg10 = val; break;
|
|
|
|
case 11: m_Arg11 = val; break;
|
|
|
|
case 12: m_Arg12 = val; break;
|
|
|
|
case 13: m_Arg13 = val; break;
|
|
|
|
case 14: m_Arg14 = val; break;
|
2016-03-21 21:37:58 +01:00
|
|
|
default: STHROWF("Argument is out of range: %d", num);
|
2016-02-21 08:25:46 +01:00
|
|
|
}
|
2016-03-12 07:47:50 +01:00
|
|
|
// Allow chaining
|
|
|
|
return *this;
|
2016-02-21 08:25:46 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 07:47:50 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
Object & Routine::GetArg(Uint16 num)
|
2016-02-21 08:25:46 +01:00
|
|
|
{
|
2016-03-12 07:47:50 +01:00
|
|
|
// Validate the routine lifetime
|
|
|
|
Validate();
|
2016-03-10 04:57:13 +01:00
|
|
|
// Identify which argument was requested
|
2016-02-21 08:25:46 +01:00
|
|
|
switch (num)
|
|
|
|
{
|
|
|
|
case 1: return m_Arg1;
|
|
|
|
case 2: return m_Arg2;
|
|
|
|
case 3: return m_Arg3;
|
|
|
|
case 4: return m_Arg4;
|
|
|
|
case 5: return m_Arg5;
|
|
|
|
case 6: return m_Arg6;
|
|
|
|
case 7: return m_Arg7;
|
|
|
|
case 8: return m_Arg8;
|
|
|
|
case 9: return m_Arg9;
|
|
|
|
case 10: return m_Arg10;
|
|
|
|
case 11: return m_Arg11;
|
|
|
|
case 12: return m_Arg12;
|
|
|
|
case 13: return m_Arg13;
|
|
|
|
case 14: return m_Arg14;
|
2016-03-21 21:37:58 +01:00
|
|
|
default: STHROWF("Argument is out of range: %d", num);
|
2016-02-21 08:25:46 +01:00
|
|
|
}
|
2016-03-10 04:57:13 +01:00
|
|
|
// Shouldn't really reach this point
|
2016-02-21 08:25:46 +01:00
|
|
|
return NullObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Routine::Interval Routine::GetInterval() const
|
|
|
|
{
|
|
|
|
return m_Interval;
|
|
|
|
}
|
|
|
|
|
2016-03-12 07:47:50 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-21 08:25:46 +01:00
|
|
|
void Routine::SetInterval(Interval interval)
|
|
|
|
{
|
2016-03-12 07:47:50 +01:00
|
|
|
// Validate the routine lifetime
|
|
|
|
Validate();
|
2016-03-10 04:57:13 +01:00
|
|
|
// Is the specified interval valid?
|
2016-05-22 05:20:38 +02:00
|
|
|
if (interval < 0)
|
2016-03-12 07:47:50 +01:00
|
|
|
{
|
2016-03-21 21:37:58 +01:00
|
|
|
STHROWF("Invalid routine interval");
|
2016-03-12 07:47:50 +01:00
|
|
|
}
|
2016-05-22 05:20:38 +02:00
|
|
|
// Do we have any slot to update?
|
|
|
|
if (m_Slot >= s_Routines.size())
|
|
|
|
{
|
|
|
|
// Update the interval
|
|
|
|
m_Interval = interval;
|
|
|
|
// We're done here!
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Make changes visible immediately
|
|
|
|
if (interval > m_Interval)
|
|
|
|
{
|
|
|
|
s_Routines[m_Slot].first += (interval - m_Interval);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s_Routines[m_Slot].first -= (m_Interval - interval);
|
|
|
|
}
|
2016-03-10 04:57:13 +01:00
|
|
|
// Update the interval
|
|
|
|
m_Interval = interval;
|
2016-02-21 08:25:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
Routine::Iterator Routine::GetIterations() const
|
2016-02-21 08:25:46 +01:00
|
|
|
{
|
|
|
|
return m_Iterations;
|
|
|
|
}
|
|
|
|
|
2016-03-12 07:47:50 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
void Routine::SetIterations(Iterator iterations)
|
2016-02-21 08:25:46 +01:00
|
|
|
{
|
2016-03-12 07:47:50 +01:00
|
|
|
// Validate the routine lifetime
|
|
|
|
Validate();
|
2016-03-10 04:57:13 +01:00
|
|
|
// Perform the requested operation
|
|
|
|
m_Iterations = iterations;
|
2016-02-21 08:25:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
Uint16 Routine::GetArguments() const
|
2016-02-21 08:25:46 +01:00
|
|
|
{
|
|
|
|
return m_Arguments;
|
|
|
|
}
|
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
void Routine::SetArguments(Uint16 num)
|
2016-02-21 08:25:46 +01:00
|
|
|
{
|
2016-03-12 07:47:50 +01:00
|
|
|
// Validate the routine lifetime
|
|
|
|
Validate();
|
2016-03-10 04:57:13 +01:00
|
|
|
// Is the specified argument count valid?
|
2016-03-12 07:47:50 +01:00
|
|
|
if (num > 14)
|
|
|
|
{
|
2016-03-21 21:37:58 +01:00
|
|
|
STHROWF("Argument is out of range: %d", num);
|
2016-03-12 07:47:50 +01:00
|
|
|
}
|
2016-03-10 04:57:13 +01:00
|
|
|
// Perform the requested operation
|
|
|
|
m_Arguments = num;
|
2016-02-21 08:25:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
bool Routine::GetSuspended() const
|
|
|
|
{
|
|
|
|
return m_Suspended;
|
|
|
|
}
|
|
|
|
|
2016-03-12 07:47:50 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-21 08:25:46 +01:00
|
|
|
void Routine::SetSuspended(bool toggle)
|
|
|
|
{
|
2016-03-12 07:47:50 +01:00
|
|
|
// Validate the routine lifetime
|
|
|
|
Validate();
|
2016-03-10 04:57:13 +01:00
|
|
|
// Perform the requested operation
|
|
|
|
m_Suspended = toggle;
|
2016-02-21 08:25:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
bool Routine::GetTerminated() const
|
|
|
|
{
|
|
|
|
return m_Terminated;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Function & Routine::GetCallback()
|
|
|
|
{
|
2016-03-12 07:47:50 +01:00
|
|
|
// Validate the routine lifetime
|
|
|
|
Validate();
|
2016-03-10 04:57:13 +01:00
|
|
|
// Return the requested information
|
|
|
|
return m_Callback;
|
2016-02-21 08:25:46 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 07:47:50 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-21 08:25:46 +01:00
|
|
|
void Routine::SetCallback(Object & env, Function & func)
|
|
|
|
{
|
2016-03-12 07:47:50 +01:00
|
|
|
// Validate the routine lifetime
|
|
|
|
Validate();
|
2016-03-10 04:57:13 +01:00
|
|
|
// Perform the requested operation
|
|
|
|
m_Callback = Function(env.GetVM(), env, func.GetFunc());
|
2016-02-21 08:25:46 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 07:47:50 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Object Routine::Create(Object & env, Function & func, Interval interval)
|
|
|
|
{
|
|
|
|
return Associate(new Routine(env, func, interval));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
Object Routine::Create(Object & env, Function & func, Interval interval, Iterator iterations)
|
2016-03-12 07:47:50 +01:00
|
|
|
{
|
|
|
|
return Associate(new Routine(env, func, interval, iterations));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
Object Routine::Create(Object & env, Function & func, Interval interval, Iterator iterations
|
2016-03-12 07:47:50 +01:00
|
|
|
, Object & a1)
|
|
|
|
{
|
|
|
|
return Associate(new Routine(env, func, interval, iterations, a1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
Object Routine::Create(Object & env, Function & func, Interval interval, Iterator iterations
|
2016-03-12 07:47:50 +01:00
|
|
|
, Object & a1, Object & a2)
|
|
|
|
{
|
|
|
|
return Associate(new Routine(env, func, interval, iterations, a1, a2));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
Object Routine::Create(Object & env, Function & func, Interval interval, Iterator iterations
|
2016-03-12 07:47:50 +01:00
|
|
|
, Object & a1, Object & a2, Object & a3)
|
|
|
|
{
|
|
|
|
return Associate(new Routine(env, func, interval, iterations, a1, a2, a3));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
Object Routine::Create(Object & env, Function & func, Interval interval, Iterator iterations
|
2016-03-12 07:47:50 +01:00
|
|
|
, Object & a1, Object & a2, Object & a3, Object & a4)
|
|
|
|
{
|
|
|
|
return Associate(new Routine(env, func, interval, iterations, a1, a2, a3, a4));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
Object Routine::Create(Object & env, Function & func, Interval interval, Iterator iterations
|
2016-03-12 07:47:50 +01:00
|
|
|
, Object & a1, Object & a2, Object & a3, Object & a4, Object & a5)
|
|
|
|
{
|
|
|
|
return Associate(new Routine(env, func, interval, iterations, a1, a2, a3, a4, a5));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Object Routine::FindByTag(CSStr tag)
|
|
|
|
{
|
|
|
|
// Perform a validity check on the specified tag
|
|
|
|
if (!tag || *tag == '\0')
|
|
|
|
{
|
2016-03-21 21:37:58 +01:00
|
|
|
STHROWF("The specified routine tag is invalid: null/empty");
|
2016-03-12 07:47:50 +01:00
|
|
|
}
|
|
|
|
// Process each routine in the pool
|
|
|
|
for (const auto & elem : s_Objects)
|
|
|
|
{
|
|
|
|
// Is this routine valid (should always be) and does the tag match the specified one?
|
|
|
|
if (elem.first != nullptr && elem.first->m_Tag.compare(tag) == 0)
|
|
|
|
{
|
|
|
|
// Do we need to obtain the script object again?
|
|
|
|
if (elem.second.IsNull())
|
|
|
|
{
|
|
|
|
// Obtain the initial stack size
|
|
|
|
const StackGuard sg;
|
|
|
|
// Push this instance on the stack
|
|
|
|
ClassType< Routine >::PushInstance(DefaultVM::Get(), elem.first);
|
|
|
|
// Obtain a strong reference to it and return it
|
|
|
|
return Var< Object >(DefaultVM::Get(), -1).value;
|
|
|
|
}
|
|
|
|
// Stop searching and return this routine
|
|
|
|
return elem.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Unable to locate a routine matching the specified tag
|
|
|
|
return NullObject();
|
|
|
|
}
|
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Forward the call to process routines.
|
|
|
|
*/
|
|
|
|
void ProcessRoutines()
|
|
|
|
{
|
|
|
|
Routine::Process();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Forward the call to initialize routines.
|
|
|
|
*/
|
|
|
|
void InitializeRoutines()
|
|
|
|
{
|
|
|
|
Routine::Initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Forward the call to terminate routines.
|
|
|
|
*/
|
|
|
|
void TerminateRoutines()
|
|
|
|
{
|
|
|
|
Routine::Deinitialize();
|
|
|
|
}
|
|
|
|
|
2016-02-21 08:25:46 +01:00
|
|
|
// ================================================================================================
|
|
|
|
void Register_Routine(HSQUIRRELVM vm)
|
|
|
|
{
|
2016-11-15 20:43:02 +01:00
|
|
|
RootTable(vm).Bind(Typename::Str,
|
|
|
|
Class< Routine, NoConstructor< Routine > >(vm, Typename::Str)
|
2016-06-03 20:26:19 +02:00
|
|
|
// Meta-methods
|
2016-08-24 21:28:15 +02:00
|
|
|
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Routine >, SQInteger, SQFloat, bool, std::nullptr_t, Routine >)
|
2016-11-15 20:43:02 +01:00
|
|
|
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
|
|
|
|
.Func(_SC("_tostring"), &Routine::ToString)
|
2016-05-22 05:20:38 +02:00
|
|
|
// Properties
|
2016-03-12 07:47:50 +01:00
|
|
|
.Prop(_SC("Tag"), &Routine::GetTag, &Routine::SetTag)
|
|
|
|
.Prop(_SC("Data"), &Routine::GetData, &Routine::SetData)
|
2016-02-21 08:25:46 +01:00
|
|
|
.Prop(_SC("Interval"), &Routine::GetInterval, &Routine::SetInterval)
|
|
|
|
.Prop(_SC("Iterations"), &Routine::GetIterations, &Routine::SetIterations)
|
|
|
|
.Prop(_SC("Arguments"), &Routine::GetArguments, &Routine::SetArguments)
|
|
|
|
.Prop(_SC("Suspended"), &Routine::GetSuspended, &Routine::SetSuspended)
|
|
|
|
.Prop(_SC("Terminated"), &Routine::GetTerminated)
|
|
|
|
.Prop(_SC("Callback"), &Routine::GetCallback)
|
2016-05-22 05:20:38 +02:00
|
|
|
// Functions
|
2016-11-16 12:12:49 +01:00
|
|
|
.FmtFunc(_SC("SetTag"), &Routine::ApplyTag)
|
2016-03-12 07:47:50 +01:00
|
|
|
.Func(_SC("SetData"), &Routine::ApplyData)
|
2016-02-21 08:25:46 +01:00
|
|
|
.Func(_SC("Terminate"), &Routine::Terminate)
|
|
|
|
.Func(_SC("Bind"), &Routine::SetCallback)
|
|
|
|
.Func(_SC("GetArg"), &Routine::GetArg)
|
|
|
|
.Func(_SC("SetArg"), &Routine::SetArg)
|
2016-03-12 07:47:50 +01:00
|
|
|
// Static Functions
|
|
|
|
.StaticFunc(_SC("FindByTag"), &Routine::FindByTag)
|
|
|
|
// Static Overloads
|
|
|
|
.StaticOverload< Object (*)(Object &, Function &, Routine::Interval) >
|
|
|
|
(_SC("Create"), &Routine::Create)
|
2016-05-22 05:20:38 +02:00
|
|
|
.StaticOverload< Object (*)(Object &, Function &, Routine::Interval, Routine::Iterator) >
|
2016-03-12 07:47:50 +01:00
|
|
|
(_SC("Create"), &Routine::Create)
|
2016-05-22 05:20:38 +02:00
|
|
|
.StaticOverload< Object (*)(Object &, Function &, Routine::Interval, Routine::Iterator,
|
2016-03-12 07:47:50 +01:00
|
|
|
Object &) >
|
|
|
|
(_SC("Create"), &Routine::Create)
|
2016-05-22 05:20:38 +02:00
|
|
|
.StaticOverload< Object (*)(Object &, Function &, Routine::Interval, Routine::Iterator,
|
2016-03-12 07:47:50 +01:00
|
|
|
Object &, Object &) >
|
|
|
|
(_SC("Create"), &Routine::Create)
|
2016-05-22 05:20:38 +02:00
|
|
|
.StaticOverload< Object (*)(Object &, Function &, Routine::Interval, Routine::Iterator,
|
2016-03-12 07:47:50 +01:00
|
|
|
Object &, Object &, Object &) >
|
|
|
|
(_SC("Create"), &Routine::Create)
|
2016-05-22 05:20:38 +02:00
|
|
|
.StaticOverload< Object (*)(Object &, Function &, Routine::Interval, Routine::Iterator,
|
2016-03-12 07:47:50 +01:00
|
|
|
Object &, Object &, Object &, Object &) >
|
|
|
|
(_SC("Create"), &Routine::Create)
|
2016-05-22 05:20:38 +02:00
|
|
|
.StaticOverload< Object (*)(Object &, Function &, Routine::Interval, Routine::Iterator,
|
2016-03-12 07:47:50 +01:00
|
|
|
Object &, Object &, Object &, Object &, Object &) >
|
|
|
|
(_SC("Create"), &Routine::Create)
|
2016-02-21 08:25:46 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // Namespace:: SqMod
|