Remove duplicated code of native handles

This commit is contained in:
Arkshine
2015-07-11 18:01:04 +02:00
parent 883c852897
commit dbc9c7e0da
13 changed files with 330 additions and 583 deletions

View File

@ -7,8 +7,16 @@
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://alliedmods.net/amxmodx-license
#ifndef _NATIVES_HANDLES_H_
#define _NATIVES_HANDLES_H_
#include <am-vector.h>
// Note: All handles start at 1. 0 and below are invalid handles.
// This way, a plugin that doesn't initialize a vector or
// string will not be able to modify another plugin's data
// on accident.
template <typename T>
class Handle
{
@ -49,19 +57,37 @@ class Handle
return m_handles[handle];
}
int create()
template <typename... Targs>
int create(Targs... Fargs)
{
for (size_t i = 0; i < m_handles.length(); ++i)
{
if (!m_handles[i])
{
m_handles[i] = new T;
m_handles[i] = new T(Fargs...);
return static_cast<int>(i) + 1;
}
}
m_handles.append(new T);
m_handles.append(new T(Fargs...));
return m_handles.length();
}
int clone(T *data)
{
for (size_t i = 0; i < m_handles.length(); ++i)
{
if (!m_handles[i])
{
m_handles[i] = data;
return static_cast<int>(i) + 1;
}
}
m_handles.append(data);
return m_handles.length();
}
@ -85,4 +111,6 @@ class Handle
return true;
}
};
};
#endif // _NATIVE_HANDLES_H_