PorygonLang/src/UserData/UserDataFunction.hpp

38 lines
1.5 KiB
C++
Raw Normal View History

2019-06-21 15:03:13 +00:00
#ifndef PORYGONLANG_USERDATAFUNCTION_HPP
#define PORYGONLANG_USERDATAFUNCTION_HPP
#include <utility>
#include "../Evaluator/EvalValues/ScriptFunctionEvalValue.hpp"
#include "UserDataFunctionType.hpp"
2019-06-21 15:03:13 +00:00
namespace Porygon::UserData{
class UserDataFunction : public Evaluation::GenericFunctionEvalValue {
Evaluation::EvalValue* (*_call)(void* obj, EvalValue* parameters[], int parameterCount);
void *_obj;
UserDataFunction(Evaluation::EvalValue* (*call)(void* obj, EvalValue* parameters[], int parameterCount), void* obj,
shared_ptr<GenericFunctionScriptType> type, size_t hash) : GenericFunctionEvalValue(std::move(type), hash){
_call = call;
_obj = obj;
}
public:
UserDataFunction(Evaluation::EvalValue* (*call)(void* obj, EvalValue* parameters[], int parameterCount), void* obj) :
GenericFunctionEvalValue(make_shared<UserDataFunctionType>(nullptr, vector<shared_ptr<ScriptType>>{}), rand()){
2019-06-21 15:03:13 +00:00
_call = call;
_obj = obj;
}
EvalValue* Call(EvalValue* parameters[], int parameterCount){
2019-06-29 14:18:59 +00:00
return _call(_obj, parameters, parameterCount);
2019-06-21 15:03:13 +00:00
}
const shared_ptr<EvalValue> Clone() const final {
// We don't run make_shared here as it can't call private constructors
return shared_ptr<UserDataFunction>(new UserDataFunction(_call, _obj, _type, _hash));
}
};
}
#endif //PORYGONLANG_USERDATAFUNCTION_HPP