PorygonLang/src/UserData/UserDataValue.hpp

67 lines
2.0 KiB
C++
Raw Normal View History

2019-06-14 12:59:38 +00:00
#ifndef PORYGONLANG_USERDATAVALUE_HPP
#define PORYGONLANG_USERDATAVALUE_HPP
#include <utility>
2019-06-14 12:59:38 +00:00
#include "../Evaluator/EvalValues/EvalValue.hpp"
#include "UserData.hpp"
#include "UserDataStorage.hpp"
namespace Porygon::UserData {
class UserDataValue : public Evaluation::EvalValue {
const shared_ptr<UserData> _userData;
void *_obj;
public:
UserDataValue(shared_ptr<UserData> userData, void *obj)
: _userData(std::move(userData)) {
_obj = obj;
}
UserDataValue(uint32_t userDataId, void *obj)
: _userData(UserDataStorage::GetUserDataType(userDataId)) {
_obj = obj;
}
const TypeClass GetTypeClass() const final {
return TypeClass::UserData;
}
const bool operator==(EvalValue *b) const final {
if (b->GetTypeClass() != TypeClass::UserData)
return false;
return _obj == ((UserDataValue *) b)->_obj;
}
const shared_ptr<EvalValue> Clone() const final {
return make_shared<UserDataValue>(_userData, _obj);
}
const std::size_t GetHashCode() const final {
return reinterpret_cast<intptr_t>(_obj);
}
const shared_ptr<EvalValue> IndexValue(EvalValue *val) const final {
auto fieldId = val->GetHashCode();
auto field = _userData->GetField(fieldId);
return shared_ptr<EvalValue>(field->Get(_obj));
}
const shared_ptr<EvalValue> IndexValue(uint32_t hash) const final {
auto field = _userData->GetField(hash);
return shared_ptr<EvalValue>(field->Get(_obj));
}
void SetIndexValue(EvalValue *key, const shared_ptr<EvalValue> &value) const final {
auto fieldId = key->GetHashCode();
auto field = _userData->GetField(fieldId);
field->Set(_obj, value.get());
}
void* GetObjectPointer(){
return _obj;
}
};
}
2019-06-14 12:59:38 +00:00
#endif //PORYGONLANG_USERDATAVALUE_HPP