Search code examples
liststlsizealchemy

Alchemy cannot get the size of list


    //--------------------------------------------test.cpp

// g++ test.cpp -O3 -Wall -swc -o test.swc


#include <iostream>
#include <list>
#include <vector>
#include "AS3.h"
using namespace std;

//vector<float> vf;
list<float> vf;

static AS3_Val getSize(void* self, AS3_Val args)
{
    int num = vf.size();
    return AS3_Int(num);
}

int main()
{
    AS3_Val getSizeMethod = AS3_Function( NULL, getSize);

    AS3_Val result = AS3_Object( "getSize:AS3ValType", getSizeMethod); 

    AS3_Release( getSizeMethod );

    AS3_LibInit( result );

    return 0;
}




    //-------------------------------------------------test.as

// C:\alchemy\flex4\bin\mxmlc -library-path+=./test.swc -static-link-runtime-shared-libraries=true test.as 


package{
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFieldType;

    import cmodule.test.CLibInit

    public class test extends Sprite{
        public function test() {
            var info:TextField = new TextField();
            this.addChild(info);


            var loader:CLibInit = new CLibInit();
            var lib:Object = loader.init();
            info.appendText("size:" + lib.getSize() + "\n");
        }
    }
}

-------------------------------------question------------------

1.test.swf cannot run, but if I use vector, it's ok!
2.If I push back some elements into the list, it can run, but the size which I get is wrong!

Can anyone help me! thanks!!!


Solution

  • Static initializers are broken in Alchemy. To work around this, you need to construct your list in main(). For example,

    list<float> vf;
    

    becomes

    list<float> *vf;
    

    and in your main you need to create it:

    vf= new list<float>();
    

    and your getSize method would return

    int num = vf->size();