我目前工作的一个项目,团队我们正在设计一个游戏,我们都在红宝石精通和一些(但不是全部)我们是精通C++。 最初我们用ruby做了后端,但是我们将它移植到C++以获得更快的速度。后端的C++端口与原始的ruby代码具有完全相同的特征和算法。 但是我们仍然有一堆的红宝石,做有用的东西代码,但我们宁可不要将它移植好,所以我们要使用Ruby代码来保持,并从C++类的数据。这是不现实的? 我们首先想到的是,我们可以节省一些数据结构的类似XML或Redis的和调用,但一些开发商不喜欢这个主意。 我们不需要任何特别复杂的数据结构的代码的不同部分,只是元组,字符串和整数之间传递。 有没有任何方法来集成ruby代码,以便它可以本地调用C++的东西? 我们需要嵌入代码吗?我们将不得不做一个红宝石扩展?如果有的话,你可以提出什么好的资源/教程?C++和Ruby共享公共类的最佳方法是什么?
例如说我们有这样的C++后端的一些代码:
class The_game{
private:
bool printinfo; //print the player diagnostic info at the beginning if true
int numplayers;
std::vector<Player*> players;
string current_action;
int action_is_on; // the index of the player in the players array that the action is now on
//more code here
public:
Table(std::vector<Player *> in_players, std::vector<Statistics *> player_stats ,const int in_numplayers);
~Table();
void play_game();
History actions_history;
};
class History{
private:
int action_sequence_number;
std::vector<Action*> recent_actions;
public:
void print_history();
void add_action(Action* the_action_to_be_added);
int get_action_sequence_number(){ return action_sequence_number;}
bool history_actions_are_equal();
int last_action_size(int street,int number_of_actions_ago);
History();
~History();
};
有没有办法通过在红宝石The_game对象本地调用在actions_history的东西吗? (原Ruby代码的对象都具有相同的名称和功能)
我的意思是:
class MyRubyClass
def method1(arg1)
puts arg1
self.f() # ... but still available
puts cpp_method.the_current_game.actions_history.get_action_sequence_number()
end
# Constructor:
def initialize(arg)
puts "In constructor with arg #{arg}"
#get the c++ object here and call it cpp_method
end
end
这可能吗?任何意见或建议表示赞赏。
感谢您的答复,但swig.org似乎是向下/ dissapeared。 – shuttle87 2010-05-25 07:41:30
您可以随时谷歌“痛饮+红宝石”:http://sourceforge.net/projects/swig/ – 2010-05-25 07:58:44