tolua++のクラス登録解読メモ

なんかいろいろやっているっぽい。

実験コード
sample.pkg

$#include "sample.h"

module sample {

struct Object
{
    int counter_;
};

class Factory
{
    Factory();
    ~Factory();
    Object* create();
};

}

sample.cpp

#include "sample.h"
#include <iostream>

Object::Object()
    : counter_(0)
{
    std::cout << __func__ << std::endl;
}

Object::~Object()
{
    std::cout << __func__ << "(" << counter_ << ")" << std::endl;
}

Object* Factory::create()
{
    instances_.push_back(Object());
    return &instances_.back();
}

Factory::Factory()
{
    std::cout << __func__ << std::endl;
}

Factory::~Factory()
{
    std::cout << __func__ << std::endl;
}

呼び出し

local f=sample.Factory()

for i=1, 5 do
    print('['..i..']')
    local o=f:create()
    o.counter_=i
end

print('--------------------')

出力

Factory
[1]
Object
~Object(0)
[2]
Object
~Object(1)
~Object(0)
[3]
Object
~Object(1)
~Object(2)
~Object(0)
[4]
Object
~Object(0)
[5]
Object
~Object(1)
~Object(2)
~Object(3)
~Object(4)
~Object(0)
--------------------
~Factory
~Object(1)
~Object(2)
~Object(3)
~Object(4)
~Object(5)

迂闊だ。vectorをreserveしてなかったw