Skip to main content
GameDev.net gamedev.net
🔒 Locked

implementing partial templates

Started by Chisser98 Dec 17, 2017 at 6:45 PM 6 replies 3k views
Original Post
Chisser98
Chisser98

Hi,

I'm curious if anyone else would think that the use of a 'partial template' system in Angelscript would be useful. Specifically, if I want to bind a std::vector, I need to give it a unique name for each type (i.e. vectorInt, vectorFloat, etc).

What if Angelscript could allow the use of the angle bracket syntax to name a type (i.e. Bind std::vector to vector).

A user would not be able to instantiate a vector unless this type was registered.

does anyone think this might be useful?

I haven't looked at the code yet, so I'm not sure how complicated this might be.

Miss
Miss

Not entirely sure what you mean, but generic types are already possible (they're called sub-types in Angelscript). Good example is the scriptarray addon, which registers an array.

Solokiller
Solokiller

I think what's confusing to people new to the language is that C++ templates and Angelscript templates aren't the same thing. In C++ it's a specialization that relies on compile time code generation, in Angelscript it relies on storing an object's type along with the object.

Unfortunately you can't just register a type vector and use std::vector for it, unless you have a class that can store an Angelscript object. Even if you did, it'd be more efficient to use a specially designed container that only stores type T's type info instead, since that takes up less memory.

Chisser98
Chisser98

hmm..what I was trying to get at is to allow the use of the angle brackets when defining a type in Angelscript.

so I could register the type vector Which would be implemented with a std::vector . so in angel script I could write something like:


vector<int> myInts;

myInts.push_back(1);


and it would work, but if I used:


vector<float> myFloats;

myFloats.push_back(1.0f);

it would fail to compile since the type vector is not a defined type.

does that make sense?

Solokiller
Solokiller

So you want to register template specializations? That sounds good, it should complement the existing validation system, but then the callback wouldn't be invoked for specializations, right?

WitchLord
WitchLord

You can already do this without changing the angelscript library.

You can register the template type with dummy behaviours and a callback routine that prevents any template instance that you don't explicitly want. Then you can register the template specializations for the types that you do want.

This way the non-specialized template type can never be used in the script, and the compiler will print a useful error message. (If desired the callback routine can even print an additional informative message).


AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel
Chisser98
Chisser98

Oh awesome, I didn't know that, thanks WitchLord!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.