asOBJ_REF and asOBJ_VALUE at the same time

Started by
6 comments, last by TheAtom 11 years, 1 month ago

Hello,

I want register function


void StringUtils::removeSections( std::string* result, const std::string& html, const std::string& startStr, const std::string& endStr, bool withTags )

as script global function:


r = engine->RegisterGlobalFunction("void removeSections(string@, const string& in, const string& in, const string& in, bool)", asFUNCTION(StringUtils::removeSections), asCALL_CDECL); assert( r >= 0 );

and use it in script in that way:


	string strToRet;
	removeSections(@strToRet, str, "<--", "-->", true);
	print(strToRet + "\n");

But as i undestrund, to use "string@" the string must be the ref type. So I am confused, how make string Ref and Value type at the same time.

Advertisement

A type cannot be both value type and reference type at the same time. However, you don't need that to register the removeSections function, instead register the output parameter as 'string &out'.


r = engine->RegisterGlobalFunction("void removeSections(string &out, const string &in, const string &in, const string &in, bool)", asFUNCTION(StringUtils::removeSections), asCALL_CDECL); assert( r >= 0 );

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Thanks for answer. Its working, but I prefer '@' befour result param, to show, that this param to show that this param will be changed(programing style only). I understand, that I cant use only @ in ref type objects?


removeSections(@strToRet, str, "<--", "-->", true);

Correct. @ can only be used for reference types. It is used to show that you want to do something with the handle of the object rather than the object itself.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Thanks for reply. Im new in angelscript but I like it. I write a ref string class and it works for my test scripts. Coudl you take a look if all is correct?


#pragma once
#include <string>

class asIScriptEngine;

class RefString
{
	friend RefString& operator+(const RefString& str1, const RefString& str2);
	friend bool operator==(const RefString& lhs, const RefString& rhs);
	friend RefString* StringFactory(RefString& refString);
	friend void RegisterRefString(asIScriptEngine *engine);

	mutable int* refCount;
	std::string* pString;
	static asIScriptEngine* mEngine;
public:
	RefString();
	RefString(unsigned int byteLength, const char *s);
	RefString(const std::string& str);
	~RefString();

	void setString(const std::string& str) { if(pString) *pString = str; else pString = new std::string(str); }
	std::string* getString() { return pString; }
	
	RefString& operator=(const RefString& str);

	void addRef() const;
	void release() const;

	static void RegisterRefString(asIScriptEngine *engine);
	static void RegisterStdStringUtils(asIScriptEngine *engine);
};

RefString& operator+(const RefString& str1, const RefString& str2);
bool operator==(const RefString& str1, const RefString& str2);


#include "RefString.h"
#include <assert.h>
#include <angelscript.h>
#include "..\StringUtils.h"

using namespace std;

asIScriptEngine* RefString::mEngine = NULL;

RefString::RefString()
{
	// Let the constructor initialize the reference counter to 1
	refCount = new int(1);
	pString = new std::string;
}

RefString::RefString(unsigned int byteLength, const char *s)
{
	// Let the constructor initialize the reference counter to 1
	refCount = new int(1);
	pString = new std::string(s, byteLength);
}

RefString::RefString(const std::string& str)
{
	refCount = new int(1);
	pString = new std::string(str);
}

RefString::~RefString()
{
	delete refCount;
	delete pString;
}

RefString* StringFactory()
{
	// The class constructor is initializing the reference counter to 1
	RefString* stringRefCounter = new RefString(); 
	return stringRefCounter;//->getString();
}

RefString* StringFactory(RefString& refString)
{
	(*refString.refCount)++;
	return &refString;
}

// Our string factory implementation
RefString* StringFactory(unsigned int byteLength, const char *s)
{
	RefString* stringRefCounter = new RefString(byteLength, s); 
	return stringRefCounter;//->getString();
}

void RefString::addRef() const
{
	// Increase the reference counter
	(*refCount)++;
}
void RefString::release() const 
{
	// Decrease ref count and delete if it reaches 0
		if( --(*refCount) == 0 )
		delete this;
}

bool operator==(const RefString& str1, const RefString& str2)
{
	return *(str1.pString) == *(str2.pString);
}

RefString& RefString::operator=(const RefString& str)
{
	if(*this == str) return *this;

	if(this->pString) delete this->pString;

	str.addRef();
	this->pString = str.pString;
	this->refCount = str.refCount;
	return *this;
}

RefString& operator+(const RefString& str1, const RefString& str2)
{
	return *(new RefString(*(str1.pString) + *(str2.pString))); 
}

void RefString::RegisterRefString(asIScriptEngine *engine)
{
	RefString::mEngine = engine;

	int r;
	// Register the string type
	r = engine->RegisterObjectType("string", sizeof(RefString), asOBJ_REF); assert( r >= 0 );
	// Registering the factory behaviour
	r = engine->RegisterObjectBehaviour("string", asBEHAVE_FACTORY, "string@ f()", asFUNCTIONPR(StringFactory, (), RefString*), asCALL_CDECL); assert( r >= 0 );
	r = engine->RegisterObjectBehaviour("string", asBEHAVE_FACTORY, "string@ f(string@)", asFUNCTIONPR(StringFactory, (RefString&), RefString*), asCALL_CDECL); assert( r >= 0 );
	// Registering the addref/release behaviours
	r = engine->RegisterObjectBehaviour("string", asBEHAVE_ADDREF,  "void f()", asMETHOD(RefString,addRef), asCALL_THISCALL); assert( r >= 0 );
	r = engine->RegisterObjectBehaviour("string", asBEHAVE_RELEASE, "void f()", asMETHOD(RefString,release), asCALL_THISCALL); assert( r >= 0 );

	// Registering the string factory
	r = engine->RegisterStringFactory("string@", asFUNCTIONPR(StringFactory, (unsigned int, const char*), RefString*), asCALL_CDECL); assert( r >= 0 );

	r = engine->RegisterObjectMethod("string", "string &opAssign(const string &in)", asMETHODPR(RefString, operator =, (const RefString&), RefString&), asCALL_THISCALL); assert( r >= 0 );
	r = engine->RegisterObjectMethod("string", "string@ opAdd(const string &in) const", asFUNCTIONPR(operator+, (const RefString&, const RefString&), RefString&), asCALL_CDECL_OBJFIRST); assert( r >= 0 );
	r = engine->RegisterObjectMethod("string", "bool opEquals(const string &in) const", asFUNCTIONPR(operator==, (const RefString &, const RefString &), bool), asCALL_CDECL_OBJFIRST); assert( r >= 0 );
}

void RefString::RegisterStdStringUtils(asIScriptEngine * engine)
{
	int r;
	r = engine->RegisterGlobalFunction("void removeHtmlTags(string@, const string& in)", asFUNCTION(StringUtils::removeHtmlTags), asCALL_CDECL); assert( r >= 0 );
	r = engine->RegisterGlobalFunction("void removeSections(string @, const string& in, const string& in, const string& in, bool)", asFUNCTION(StringUtils::removeSections), asCALL_CDECL); assert( r >= 0 );	
	r = engine->RegisterGlobalFunction("void removeWhiteCharsAtBeging(string@, const string& in)", asFUNCTION(StringUtils::removeWhiteCharsAtBeging), asCALL_CDECL); assert( r >= 0 );
	r = engine->RegisterGlobalFunction("void removeWhiteCharsAtEnding(string@, const string& in)", asFUNCTION(StringUtils::removeWhiteCharsAtEnding), asCALL_CDECL); assert( r >= 0 );
	r = engine->RegisterGlobalFunction("void removeMultipleEnters(string@, const string& in)", asFUNCTION(StringUtils::removeMultipleEnters), asCALL_CDECL); assert( r >= 0 );
}





void main()
{
	print("Hello world\n");
	print("AB" + "CD" + "\n");

	string sr1 = "raz";
	string sr2 = "dwa";
	string str3("dupa\n");
	print(sr1);
	print(sr2);
	print(str3);
	if(sr1 == sr2) print("rowne");
	else print("rozne");
}

It appears to be correct.

It interesting to see that you went with a shared internal buffer and reference counter. I have long had in mind to attempt to do a performance comparison with a design like this compared to the ordinary std::string. Perhaps you have some numbers to show?

Remember to make the reference counter threadsafe if you plan to use multithreading.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Hi, thanks for help :)

I don't make any comparison, becouse this is my only implementation. I am beginner with angelscript, so I dont do a rocket science :) I used shared refPointer becouse i dont want to copy string when I use operator=. Honestly I am afraid, that my implementation will be slower than ordinary std::string.


void RefString::release() const 
{
	// Decrease ref count and delete if it reaches 0
	if( --(*refCount) == 0 )
	{
		for(size_t i=0; i<clones->size(); i++)
		{
			if((*clones)[i] == this) continue;
			delete (*clones)[i];
		}

		delete this->refCount;
		delete this->pString;
		delete this->clones;

		delete this;
	}
}


RefString& RefString::operator=(const RefString& str)
{
	if(this->pString == str.pString) return *this;

	std::vector<RefString*> thisClonesCopy = *(this->clones);
	std::vector<RefString*> strClonesCopy = *(str.clones);

	sort(strClonesCopy.begin(), strClonesCopy.end());
	sort(thisClonesCopy.begin(), thisClonesCopy.end());
	this->clones->resize(strClonesCopy.size() + thisClonesCopy.size());

	std::vector<RefString*>::iterator it = set_union(strClonesCopy.begin(), strClonesCopy.end(), thisClonesCopy.begin(), thisClonesCopy.end(), this->clones->begin());
	this->clones->resize(it - this->clones->begin());

	*(str.refCount) += *(this->refCount);

	if(this->pString) delete this->pString;
	if(this->refCount) delete this->refCount;
	if(str.clones) delete str.clones;

	for(size_t i=0; i<clones->size(); i++)
	{
		(*clones)[i]->pString = str.pString;
		(*clones)[i]->clones = this->clones;
		(*clones)[i]->refCount = str.refCount;
	}

	return *this;
}

Best regards

It is unnecessary to hold RefString's clones in each RefString instance. Simply replace pString with a smart pointer to a reference-counted version of std::string. This way there are two reference counting mechanisms involved:

1. Each reference-counted std::string holds the number of RefString's that refer to it.

2. RefString's own reference count holds the number of AngelScript (or C++, if necessary) objects that refer to it, such as string handles.

So RefString's assignment can simply acquire the source RefString's smart pointer to the reference-counted std::string. By doing so it will release the reference-counted std::string it held previously (if any), decreasing its reference counter, which will result in its deletion if and only if no other RefString holds a reference to it.

That aside, unless you are handling reference of your string object inside ScriptUtils object, you may want to register your functions with signatures like


"void removeHtmlTags(string@+, const string& in)"

`+` indicating an autohandle.

About COW strings in general, they often prove to be more trouble than they are worth. If lots of raw assignments are expected, they might be a good thing. See http://stackoverflow.com/questions/707014/why-vc-strings-are-not-reference-counted for some reasons why many stl implementations just skip it (at least vc++ and stlport do; gcc libs apparently do not).

This topic is closed to new replies.

Advertisement