amxmodx/amxmodx/natives_handles.h

120 lines
2.0 KiB
C
Raw Normal View History

2015-06-24 17:45:12 +02:00
// vim: set ts=4 sw=4 tw=99 noet:
//
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
// Copyright (C) The AMX Mod X Development Team.
//
// This software is licensed under the GNU General Public License, version 3 or higher.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://alliedmods.net/amxmodx-license
#ifndef _NATIVES_NATIVES_HANDLES_H_
#define _NATIVES_NATIVES_HANDLES_H_
2015-09-30 19:08:39 +02:00
#include <amtl/am-vector.h>
#include <amtl/am-autoptr.h>
2015-06-24 17:45:12 +02:00
// 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.
2015-06-24 17:45:12 +02:00
template <typename T>
class NativeHandle
2015-06-24 17:45:12 +02:00
{
private:
ke::Vector<ke::AutoPtr<T>> m_handles;
2015-06-24 17:45:12 +02:00
public:
NativeHandle() {}
~NativeHandle()
2015-06-24 17:45:12 +02:00
{
this->clear();
}
void clear()
{
m_handles.clear();
}
size_t size()
{
return m_handles.length();
}
T *lookup(size_t handle)
2015-06-24 17:45:12 +02:00
{
--handle;
if (handle >= m_handles.length())
2015-06-24 17:45:12 +02:00
{
return nullptr;
}
return m_handles[handle].get();
2015-06-24 17:45:12 +02:00
}
template <typename... Targs>
size_t create(Targs... Fargs)
2015-06-24 17:45:12 +02:00
{
for (size_t i = 0; i < m_handles.length(); ++i)
{
if (!m_handles[i])
{
m_handles[i] = ke::AutoPtr<T>(new T(Fargs...));
2015-06-24 17:45:12 +02:00
return i + 1;
2015-06-24 17:45:12 +02:00
}
}
m_handles.append(ke::AutoPtr<T>(new T(Fargs...)));
return m_handles.length();
}
size_t clone(T *data)
{
for (size_t i = 0; i < m_handles.length(); ++i)
{
if (!m_handles[i])
{
m_handles[i] = ke::AutoPtr<T>(data);
return i + 1;
}
}
m_handles.append(ke::AutoPtr<T>(data));
2015-06-24 17:45:12 +02:00
return m_handles.length();
}
bool destroy(size_t handle)
2015-06-24 17:45:12 +02:00
{
--handle;
2015-06-24 17:45:12 +02:00
if (handle >= m_handles.length())
2015-06-24 17:45:12 +02:00
{
return false;
}
if (!m_handles[handle])
{
return false;
}
m_handles[handle] = nullptr;
return true;
}
};
2015-12-24 18:25:52 +01:00
enum ForwardState
{
FSTATE_ACTIVE,
FSTATE_STOP
};
#endif // _NATIVES_NATIVES_HANDLES_H_