• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
kunajk

asOBJ_REF and asOBJ_VALUE at the same time

7 posts in this topic

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. 

0

Share this post


Link to post
Share on other sites

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 );
Edited by Andreas Jonsson
2

Share this post


Link to post
Share on other sites

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);
0

Share this post


Link to post
Share on other sites

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.

2

Share this post


Link to post
Share on other sites

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");
}
0

Share this post


Link to post
Share on other sites

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. 

1

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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).

Edited by TheAtom
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0