<commit_before>//
//  Attr.h
//  OpenLieroX
//
//  Created by <NAME> on 17.01.12.
//  code under LGPL
//

#ifndef OpenLieroX_Attr_h
#define OpenLieroX_Attr_h

#include <stdint.h>
#include <string>
#include <vector>
#include <utility>
#include <boost/function.hpp>
#include "util/WeakRef.h"
#include "CScriptableVars.h"
#include "gusanos/luaapi/classes.h"
#include "util/macros.h"
#include "util/BaseObject.h"

class CBytestream;
class CServerConnection;

struct AttrExt {
	bool updated;
	bool S2CupdateNeeded;
	AttrExt() : updated(false), S2CupdateNeeded(false) {}
};

struct AttrDesc {
	typedef uint16_t AttrId;

	ClassId objTypeId;
	ScriptVarType_t attrType;
	bool isStatic; // if true -> use memOffsets; otherwise, dyn funcs
	intptr_t attrMemOffset;
	intptr_t attrExtMemOffset;
	boost::function<ScriptVar_t (const BaseObject* base, const AttrDesc* attrDesc)> dynGetValue;
	boost::function<AttrExt& (BaseObject* base, const AttrDesc* attrDesc)> dynGetAttrExt;
	std::string attrName;
	AttrId attrId;
	ScriptVar_t defaultValue;

	bool serverside;
	bool serverCanUpdate;
	boost::function<void(BaseObject* base, const AttrDesc* attrDesc, ScriptVar_t oldValue)> onUpdate;
	
	AttrDesc()
	: objTypeId(0), attrType(SVT_INVALID), isStatic(true), attrMemOffset(0), attrExtMemOffset(0), attrId(0),
	  serverside(true), serverCanUpdate(true) {}
	std::string description() const;

	const void* getValuePtr(const BaseObject* base) const {
		assert(isStatic);
		return (void*)(uintptr_t(base) + attrMemOffset);
	}
	ScriptVarPtr_t getValueScriptPtr(BaseObject* base) const {
		return ScriptVarPtr_t(attrType, (void*)getValuePtr(base), defaultValue);
	}
	AttrExt& getAttrExt(BaseObject* base) const {
		if(isStatic)
			return *(AttrExt*)(uintptr_t(base) + attrMemOffset + attrExtMemOffset);
		else
			return dynGetAttrExt(base, this);
	}
	ScriptVar_t get(const BaseObject* base) const {
		if(isStatic)
			// the const-cast is safe because ScriptVarPtr_t.asScriptVar doesn't modify it
			return getValueScriptPtr((BaseObject*)base).asScriptVar();
		else
			return dynGetValue(base, this);
	}
	void set(BaseObject* base, const ScriptVar_t& v) const;

	bool authorizedToWrite(const BaseObject* base) const;
	bool shouldUpdate(const BaseObject* base) const;
};

struct AttribRef {
	ClassId objTypeId;
	AttrDesc::AttrId attrId;

	AttribRef() : objTypeId(-1), attrId(-1) {}
	AttribRef(const AttrDesc* attrDesc);
	void writeToBs(CBytestream* bs) const;
	void readFromBs(CBytestream* bs);
	std::string description() const;
	static AttribRef LowerLimit(ClassId c);
	static AttribRef UpperLimit(ClassId c);<commit_msg>AttrDesc::compare to efficiently compare attrib values<commit_after>//
//  Attr.h
//  OpenLieroX
//
//  Created by <NAME> on 17.01.12.
//  code under LGPL
//

#ifndef OpenLieroX_Attr_h
#define OpenLieroX_Attr_h

#include <stdint.h>
#include <string>
#include <vector>
#include <utility>
#include <boost/function.hpp>
#include "util/WeakRef.h"
#include "CScriptableVars.h"
#include "gusanos/luaapi/classes.h"
#include "util/macros.h"
#include "util/BaseObject.h"

class CBytestream;
class CServerConnection;

struct AttrExt {
	bool updated;
	bool S2CupdateNeeded;
	AttrExt() : updated(false), S2CupdateNeeded(false) {}
};

struct AttrDesc {
	typedef uint16_t AttrId;

	ClassId objTypeId;
	ScriptVarType_t attrType;
	bool isStatic; // if true -> use memOffsets; otherwise, dyn funcs
	intptr_t attrMemOffset;
	intptr_t attrExtMemOffset;
	boost::function<ScriptVar_t (const BaseObject* base, const AttrDesc* attrDesc)> dynGetValue;
	boost::function<AttrExt& (BaseObject* base, const AttrDesc* attrDesc)> dynGetAttrExt;
	std::string attrName;
	AttrId attrId;
	ScriptVar_t defaultValue;

	bool serverside;
	bool serverCanUpdate;
	boost::function<void(BaseObject* base, const AttrDesc* attrDesc, ScriptVar_t oldValue)> onUpdate;
	
	AttrDesc()
	: objTypeId(0), attrType(SVT_INVALID), isStatic(true), attrMemOffset(0), attrExtMemOffset(0), attrId(0),
	  serverside(true), serverCanUpdate(true) {}
	std::string description() const;

	const void* getValuePtr(const BaseObject* base) const {
		assert(isStatic);
		return (void*)(uintptr_t(base) + attrMemOffset);
	}
	ScriptVarPtr_t getValueScriptPtr(BaseObject* base) const {
		return ScriptVarPtr_t(attrType, (void*)getValuePtr(base), defaultValue);
	}
	AttrExt& getAttrExt(BaseObject* base) const {
		if(isStatic)
			return *(AttrExt*)(uintptr_t(base) + attrMemOffset + attrExtMemOffset);
		else
			return dynGetAttrExt(base, this);
	}
	ScriptVar_t get(const BaseObject* base) const {
		if(isStatic)
			// the const-cast is safe because ScriptVarPtr_t.asScriptVar doesn't modify it
			return getValueScriptPtr((BaseObject*)base).asScriptVar();
		else
			return dynGetValue(base, this);
	}
	int compare(const BaseObject* base, const ScriptVar_t& otherValue) const {
		if(isStatic)
			// the const-cast is safe because ScriptVarPtr_t.asScriptVar doesn't modify it
			return getValueScriptPtr((BaseObject*)base).valueCompare(otherValue);
		else
			return dynGetValue(base, this).compare(otherValue);
	}
	void set(BaseObject* base, const ScriptVar_t& v) const;

	bool authorizedToWrite(const BaseObject* base) const;
	bool shouldUpdate(const BaseObject* base) const;
};

struct AttribRef {
	ClassId objTypeId;
	AttrDesc::AttrId attrId;

	AttribRef() : objTypeId(-1), attrId(-1) {}
	AttribRef(const AttrDesc* attrDesc);
	void writeToBs(CBytestream* bs) const;
	void readFromBs(CBytestream* bs);
	std::string description() const;
	static AttribRef LowerLimit(ClassId c);
	static AttribRef UpperLimit(ClassId c);
{'fraction_non_alphanumeric': 0.11664860960635609, 'fraction_numerical': 0.00577825929938606, 'mean_word_length': 5.906483790523691, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1597312', 'n_tokens_mistral': 2002, 'n_tokens_neox': 1920, 'n_words': 456}