Jump to content

  • Log In with Google      Sign In   
  • Create Account

- - - - -

Template methods


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Jake Albano   Members   -  Reputation: 631

Like
0Likes
Like

Posted 08 August 2012 - 06:59 PM

I have a class which is responsible for creating instances of my component classes, registered as a singleton. Currently it looks like this:

[source lang = "cpp"]// IComponent is an abstract C++ class that all of my components extend in the applicationIComponent@ animation = componentFactory.get("SpriteAnimation");[/source]

Obviously this isn't ideal; for one thing, errors won't be caught until the line is executed. I'm wondering if there's any way for a method to take a template parameter, like this:

[source lang = "cpp"]IComponent@ animation = componentFactory.get<SpriteAnimation>(); // returns an instance of SpriteAnimation[/source]

All I can find in the docs is a way to register template types, which won't help me with this. Is there something I'm missing, or could this be added in a future release?

Thanks

Edited by Jake Albano, 08 August 2012 - 07:08 PM.


Sponsor:

#2 Andreas Jonsson   Moderators   -  Reputation: 3366

Like
0Likes
Like

Posted 08 August 2012 - 07:34 PM

You could create a special template type, that will simply provide type info rather than being a container itself.

It might for example look like this:

IComponent@ animation = componentFactory.get(type<SpriteAnimation>());

The 'type' object would then be instanciated similarly to how the CScriptArray is instanciated, and would allow the C++ application to determine what type is desired.

You could even use the template callback behaviour to prevent the script from instanciating types your application doesn't support.

This is actually a pretty cool idea. I might implement this as an add-on. The type object could even provide further information on the type, for example allow a script to enumerate properties and class methods, etc.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#3 Jake Albano   Members   -  Reputation: 631

Like
0Likes
Like

Posted 09 August 2012 - 11:14 AM

That's an interesting approach, and one I probably wouldn't have thought of. The one issue I can see is that I would have to create a specific type for each situation, or it still wouldn't be caught at compile time, but that's certainly better than the way I've been doing it so far. Thanks for the help!

#4 InvalidPointer   Members   -  Reputation: 1433

Like
0Likes
Like

Posted 09 August 2012 - 09:21 PM

I've actually exposed asIObjectType this way to scripts and it works fairly well. The only thing I dislike is that you're (Jake) not writing idiomatic code, but with that said it's insanely flexible and can be implemented wholly with pre-implemented features.

EDIT(2): My implementation. It's not going to be a perfect cut-and-paste job, but this should get the ball rolling.
/*==================================================================*\
  AngelscriptReflection.cpp
  ------------------------------------------------------------------
  Purpose:

  ------------------------------------------------------------------
  ©2010-2012 Eldritch Entertainment, LLC.
\*==================================================================*/

//==================================================================//
// INCLUDES
//==================================================================//
#include <Scripting/Angelscript/AngelscriptReflection.hpp>
#include <Scripting/Angelscript/AngelscriptContextExtraData.hpp>
#include <Scripting/Angelscript/AngelscriptCommon.hpp>
#include <Util/Containers/UTF8String.hpp>
#include <Util/Assert.hpp>
#include <angelscript.h>
//------------------------------------------------------------------//
//==================================================================//
// LIBRARIES
//==================================================================//
ET_LINK_LIBRARY( "angelscript.lib" )
//------------------------------------------------------------------//
using namespace ::Eldritch2;
using namespace ::Eldritch2::Scripting;
using namespace ::Eldritch2::Util;
#define TYPE  "Type"
#define FUNCTION "Function"
#define MEMBER  "Member"
#define ATTRIBUTE "Attribute"
namespace
{
static const char typeName[]  = TYPE;
static const char functionName[] = FUNCTION;
static const char memberName[] = MEMBER;
static const char attributeName[] = ATTRIBUTE;
// ---------------------------------------------------
static UTF8String TypeGetNameProperty( asIObjectType* const thisPtr )
{
  AngelscriptContextExtraData* const contextExtraData = static_cast<AngelscriptContextExtraData*>( asGetActiveContext()->GetUserData() );
  return UTF8String( thisPtr->GetName(), contextExtraData->allocator );
}
// ---------------------------------------------------
static UTF8String TypeGetNamespaceProperty( asIObjectType* const thisPtr )
{
  AngelscriptContextExtraData* const contextExtraData = static_cast<AngelscriptContextExtraData*>( asGetActiveContext()->GetUserData() );
  return UTF8String( thisPtr->GetNamespace(), contextExtraData->allocator );
}
// ---------------------------------------------------
static ETNoAliasHint asIObjectType* TypeGetBaseTypeProperty( asIObjectType* const thisPtr )
{
  asIObjectType* const basePtr = thisPtr->GetBaseType();
  if( ETBranchLikelyHint( nullptr != basePtr ) )
  {
   basePtr->AddRef();
  }
  return basePtr;
}
// ---------------------------------------------------
static ETNoAliasHint asIObjectType* TypeGetSubtypeProperty( asIObjectType* const thisPtr )
{
  asIObjectType* const basePtr = thisPtr->GetSubType();
  if( ETBranchLikelyHint( nullptr != basePtr ) )
  {
   basePtr->AddRef();
  }
  return basePtr;
}
// ---------------------------------------------------
static ETNoAliasHint bool ETCDecl TypeIsInterface( asIObjectType* const thisPtr )
{
  return ( ( 0u == thisPtr->GetSize() ) & !!( asOBJ_SCRIPT_OBJECT & thisPtr->GetFlags() ) );
}
// ---------------------------------------------------
static ETNoAliasHint bool ETCDecl TypeIsFinal( asIObjectType* const thisPtr )
{
  return !!( asOBJ_NOINHERIT & thisPtr->GetFlags() );
}
// ---------------------------------------------------
static ETNoAliasHint asIScriptFunction* ETCDecl TypeGetMethodProperty( asIObjectType* const thisPtr, uint32 methodIndex )
{
  asIScriptFunction* const function = thisPtr->GetMethodByIndex( methodIndex );
  if( ETBranchLikelyHint( nullptr != function ) )
  {
   function->AddRef();
  }
  return function;
}
// ---------------------------------------------------
static ETNoAliasHint asIObjectType* ETCDecl TypeOf( void* const object, int typeID )
{
  ETUnreferencedParameter( object );
  if( asIObjectType* const result = asGetActiveContext()->GetEngine()->GetObjectTypeById( typeID ) )
  {
   result->AddRef();
   return result;
  }
  return nullptr;
}
// ---------------------------------------------------
static asIObjectType* ETCDecl GetTypeByName( const UTF8String& name )
{
  asIScriptEngine* const scriptEngine = asGetActiveContext()->GetEngine();
  AngelscriptNamespace typeNamespace;
  AngelscriptClassName typeName;
  if( ExtractClassPath( typeNamespace, typeName, name ) )
  {
   if( asIScriptModule* const scriptModule = scriptEngine->GetModule( typeNamespace ) )
   {
	asIObjectType* const objectType = scriptEngine->GetObjectTypeById( scriptModule->GetTypeIdByDecl( typeName ) );
	if( objectType )
	{
	 objectType->AddRef();
	 return objectType;
	}
   }
  }
  return nullptr;
}
// ---------------------------------------------------
static asIObjectType* ETCDecl GetTypeByName( const UTF8String& name, const UTF8String& package )
{
  asIScriptEngine* const scriptEngine = asGetActiveContext()->GetEngine();
  AngelscriptNamespace typeNamespace;
  AngelscriptClassName typeName;
  StrCpy( typeName, name );
  StrCpy( typeNamespace, package );
  if( asIScriptModule* const scriptModule = scriptEngine->GetModule( typeNamespace ) )
  {
   if( asIObjectType* const objectType = scriptEngine->GetObjectTypeById( scriptModule->GetTypeIdByDecl( typeName ) ) )
   {
	objectType->AddRef();
	return objectType;
   }
  }
  return nullptr;
}
} // anonymous namespace
namespace Eldritch2
{
namespace Scripting
{
  void RegisterReflection( asIScriptEngine* const engine )
  {
   ETRuntimeVerification( 0 <= engine->RegisterObjectType( typeName, 0u, asOBJ_REF ) );
   ETRuntimeVerification( 0 <= engine->RegisterObjectType( functionName, 0u, asOBJ_REF ) );
   // ETRuntimeVerification( 0 <= engine->RegisterObjectType( memberName, 0u, asOBJ_REF ) );
   ETRuntimeVerification( 0 <= engine->RegisterInterface( attributeName ) );
   ETRuntimeVerification( 0 <= engine->RegisterObjectBehaviour( typeName, asBEHAVE_ADDREF, "void f()",
				   asMETHOD( asIObjectType, AddRef ), asCALL_THISCALL ) );
   ETRuntimeVerification( 0 <= engine->RegisterObjectBehaviour( typeName, asBEHAVE_RELEASE, "void f()",
				   asMETHOD( asIObjectType, Release ), asCALL_THISCALL ) );
   ETRuntimeVerification( 0 <= engine->RegisterObjectBehaviour( functionName, asBEHAVE_ADDREF, "void f()",
				   asMETHOD( asIScriptFunction, AddRef ), asCALL_THISCALL ) );
   ETRuntimeVerification( 0 <= engine->RegisterObjectBehaviour( functionName, asBEHAVE_RELEASE, "void f()",
				   asMETHOD( asIScriptFunction, Release ), asCALL_THISCALL ) );
   ETRuntimeVerification( 0 <= engine->RegisterObjectMethod( typeName, TYPE "@ get_BaseType() const",
				   asFUNCTIONPR( TypeGetBaseTypeProperty, ( asIObjectType* const ), asIObjectType* ), asCALL_CDECL_OBJFIRST ) );
   ETRuntimeVerification( 0 <= engine->RegisterObjectMethod( typeName, TYPE "@ get_Subtype() const",
				   asFUNCTIONPR( TypeGetSubtypeProperty, ( asIObjectType* const ), asIObjectType* ), asCALL_CDECL_OBJFIRST ) );
   ETRuntimeVerification( 0 <= engine->RegisterObjectMethod( typeName, "string get_Name() const",
				   asFUNCTIONPR( TypeGetNameProperty, ( asIObjectType* const ), UTF8String ), asCALL_CDECL_OBJFIRST ) );
   ETRuntimeVerification( 0 <= engine->RegisterObjectMethod( typeName, "string get_Namespace() const",
				   asFUNCTIONPR( TypeGetNamespaceProperty, ( asIObjectType* const ), UTF8String ), asCALL_CDECL_OBJFIRST ) );
   ETRuntimeVerification( 0 <= engine->RegisterObjectMethod( typeName, "bool get_IsInterface() const",
				   asFUNCTIONPR( TypeIsInterface, ( asIObjectType* const ), bool ), asCALL_CDECL_OBJFIRST ) );
   ETRuntimeVerification( 0 <= engine->RegisterObjectMethod( typeName, "bool get_IsFinal() const",
				   asFUNCTIONPR( TypeIsFinal, ( asIObjectType* const ), bool ), asCALL_CDECL_OBJFIRST ) );
   ETRuntimeVerification( 0 <= engine->RegisterObjectMethod( typeName, "bool DerivesFrom( const " TYPE "@ ) const",
				   asMETHODPR( asIObjectType, DerivesFrom, ( const asIObjectType* ) const, bool ), asCALL_THISCALL ) );
   ETRuntimeVerification( 0 <= engine->RegisterObjectMethod( typeName, "bool Implements( const " TYPE "@ ) const",
				   asMETHODPR( asIObjectType, Implements, ( const asIObjectType* ) const, bool ), asCALL_THISCALL ) );
   ETRuntimeVerification( 0 <= engine->RegisterObjectMethod( typeName, "uint32 get_MethodCount() const",
				   asMETHODPR( asIObjectType, GetMethodCount, () const, asUINT ), asCALL_THISCALL ) );
   ETRuntimeVerification( 0 <= engine->RegisterObjectMethod( typeName, FUNCTION "@ get_Method( uint32 ) const",
				   asFUNCTIONPR( TypeGetMethodProperty, ( asIObjectType* const, uint32 ), asIScriptFunction* ), asCALL_CDECL_OBJFIRST ) );
   ETRuntimeVerification( 0 <= engine->RegisterGlobalFunction( TYPE "@ TypeOf( ?&in )",
				   asFUNCTIONPR( TypeOf, ( void* const, int ), asIObjectType* ), asCALL_CDECL ) );
   ETRuntimeVerification( 0 <= engine->RegisterGlobalFunction( TYPE "@ GetTypeByName( const string&in )",
				   asFUNCTIONPR( GetTypeByName, ( const UTF8String& ), asIObjectType* ), asCALL_CDECL ) );
   ETRuntimeVerification( 0 <= engine->RegisterGlobalFunction( TYPE "@ GetTypeByName( const string&in, const string&in )",
				   asFUNCTIONPR( GetTypeByName, ( const UTF8String&, const UTF8String& ), asIObjectType* ), asCALL_CDECL ) );
  }
} // namespace Scripting
} // namespace Eldritch2

EDIT 3: One thing I really, really would like to look into is attributes Like What C# Has™, on a totally unrelated note. I can think of a way to hack it together again using stock functionality, but on the subject of reflection it's really a powerful concept.

Edited by InvalidPointer, 09 August 2012 - 09:31 PM.

clb: At the end of 2012, the positions of jupiter, saturn, mercury, and deimos are aligned so as to cause a denormalized flush-to-zero bug when computing earth's gravitational force, slinging it to the sun.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS