Skip to content

Instantly share code, notes, and snippets.

@AbePralle
Last active March 2, 2016 06:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbePralle/4475422c18b650f50c0d to your computer and use it in GitHub Desktop.
Save AbePralle/4475422c18b650f50c0d to your computer and use it in GitHub Desktop.
Simple example of wrapping a C++ vector in Rogue
#==================================================================
# VectorWrapper.rogue
#
# v2.1
#
# March 1, 2016 by Abe Pralle
#
# Compile with RogueC v1.0.6+ from
# https://github.com/AbePralle/Rogue
#
# roguec VectorWrapper.rogue --execute
#==================================================================
println "C++ vector wrapping example"
local v = Vector<<Int32>>()
v.add( 3 ).add( 4 ).add( 0 )
v[2] = v[0] + v[1]
trace v.count
trace v
println
local v2 = Vector<<String>>()
v2.add( "Alpha" ).add( "Beta" ).add( "Gamma" )
trace v2.count
trace v2
println
nativeHeader
#include <vector>
using namespace std;
endNativeHeader
class Vector<<$DataType>>
PROPERTIES
native "vector<$($DataType)>* vector;"
METHODS
method init
native "$this->vector = new vector<$($DataType)>();"
method clean_up
println "Deleting " + type_name
while (count) remove_last
native "delete $this->vector;"
method add( n:$DataType )->this
native "$this->vector->push_back( $(n.retain) );"
return this
method count->Int32
return native( "$this->vector->size()" )->Int32
method get( index:Int32 )->$DataType
return native( "$this->vector->operator[]( $index )" )->$DataType
method last->$DataType
return this[ count-1 ]
method remove_last->$DataType
local result = last
native "$this->vector->pop_back();"
return native( "$(result.release)" )->$DataType
method set( index:Int32, value:$DataType )
native "$this->vector->operator[]( $index ) = $value;"
method to->String
local buffer = StringBuilder()
buffer.print( '[' )
local first = true
forEach (n in this)
if (first) first = false
else buffer.print( ',' )
buffer.print( n )
endForEach
buffer.print( ']' )
return buffer->String
endClass
C++ vector wrapping example
v.count:3
v:[3,4,7]
v2.count:3
v2:[Alpha,Beta,Gamma]
Deleting Vector<<Int32>>
Deleting Vector<<String>>
RogueClassInt32Vector* RogueInt32Vector__init( RogueClassInt32Vector* THIS )
{
THIS->vector = new vector<RogueInt32>();
return (RogueClassInt32Vector*)(THIS);
}
RogueClassInt32Vector* RogueInt32Vector__add__Int32( RogueClassInt32Vector* THIS, RogueInt32 n_0 )
{
THIS->vector->push_back( n_0 );
return (RogueClassInt32Vector*)(THIS);
}
RogueInt32 RogueInt32Vector__remove_last( RogueClassInt32Vector* THIS )
{
RogueInt32 result_0 = (((RogueInt32Vector__last( THIS ))));
THIS->vector->pop_back();
return (RogueInt32)(result_0);
}
RogueClassStringVector* RogueStringVector__init( RogueClassStringVector* THIS )
{
THIS->vector = new vector<RogueString*>();
return (RogueClassStringVector*)(THIS);
}
RogueClassStringVector* RogueStringVector__add__String( RogueClassStringVector* THIS, RogueString* n_0 )
{
THIS->vector->push_back( ((RogueString*)RogueObject_retain((RogueObject*)n_0)) );
return (RogueClassStringVector*)(THIS);
}
RogueString* RogueStringVector__remove_last( RogueClassStringVector* THIS )
{
RogueString* result_0 = (((RogueStringVector__last( THIS ))));
THIS->vector->pop_back();
return (RogueString*)(((RogueString*)RogueObject_release((RogueObject*)result_0)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment