lua函数到C ++函数

时间:2016-03-11 05:48:16

标签: c++ lua

我正在努力将lua程序转换为C ++程序,但我遇到了障碍,我无法弄清楚如何将其转换为C ++

function newPool()
    local pool = {}
    pool.species = {} --imports data from local species = {}
    pool.generation = 0
    pool.innovation = Outputs
    pool.currentSpecies = 1
    pool.currentGenome = 1
    pool.currentFrame = 0
    pool.maxFitness = 0

    return pool
end

我知道两种语言的许多基础知识,我知道它在lua中有效,但我需要它在C ++中。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

Lua有一个名为Tables的东西,允许你添加键值对,而不像C / C ++那样添加预定义的struct。所以您发布的Lua代码是将键值对添加到pool(在代码中读取注释):

local pool = {}           -- Declare a new Table
pool.species = {}         -- Add another Table to pool called 'species'
pool.generation = 0       -- Add the key 'generation' with value '0'
pool.innovation = Outputs -- Add the key 'innovation' with value 'Outputs'
pool.currentSpecies = 1   -- Add the key 'currentSpecies' with value '1'
pool.currentGenome = 1    -- Add the key 'currentGenome' with value '1'
pool.currentFrame = 0     -- Add the key 'currentFrame' with value '0'
pool.maxFitness = 0       -- Add the key 'maxFitness' with value '0'

在C ++中,您有几种选择。 1)你可以创建一个struct并声明你需要的东西(我猜一些数据类型,但如果你有完整的Lua程序,你可以搞清楚):

struct Pool
{
   Species species;     // You'll have to define Species in another struct
   int generation;
   SomeEnum innovation; // You'll have to define SomeEnum in an enum
   int currentSpecies;
   int currentGenome;
   int currentFrame;
   int maxFitness;
}

如果您有课程,则可以使用下面显示的struct Pool(将上面的struct Pool定义添加到class Kingdom上方的.h文件中):

// I'm doing this as a class since you are programming in C++ and I
// assume you will want to add more functions to operate on similar
// objects.
class Kingdom
{
public:
   Kingdom();
   Pool* NewPool();
private:
   Pool _pool;
}

在你的.cpp文件中:

#include "Kingdom.h"

Kingdom::Kingdom()
{
  // _pool.species = whatever you define struct Species as
  _pool.generation = 0;
  _pool.innovation = SomeEnum::Outputs; // You'll have to define SomeEnum
  _pool.currentSpecies = 1;
  _pool.currentGenome = 1;
  _pool.currentFrame = 0;
  _pool.maxFitness = 0; 
}

Pool* Kingdom::NewPool()
{
  Pool* newPool = new Pool;
  memcpy(newPool, &_pool, sizeof(Pool)); // Make a copy
  return newPool;                        // Return the new copy

  // The newPool value is dynamic memory so when the calling function is done
  // with newPool it should delete it, example:
  // Kingdom myKingdom;
  // Pool* myNewPoolStruct = myKingdom.NewPool();
  // ... do some coding here
  // delete myNewPoolStruct;
}

选项2)如果所有键值对都是相同的类型;即所有密钥均为std::string,所有值均为int。请记住,Lua代码正在使用Tables,因此您可以使用std::map<>在C ++中创建等效代码。然后您可以使用std::map<std::string, int>,如下所示:

// In your .h file change
Pool* NewPool();
Pool _pool;
// to
std::map<std::string, int> NewPool();
std::map<std::string, int> _pool;

然后在.cpp文件中将构造函数更改为:

Kingdom::Kingdom()
{
  _pool["species"] = 0;    // Some int representation of species
  _pool["generation"] = 0;
  _pool["innovation"] = 1; // Some int representation of Outputs
  _pool["currentSpecies"] = 1;
  _pool["currentGenome"] = 1;
  _pool["currentFrame"] = 0;
  _pool["maxFitness"] = 0;
}

std::map<std::string, int> NewPool()
{
  std::map<std::string, int> newPool;
  newPool = _pool;  // Copy - double check this against std::map
  return newPool;   // Double check this is a true copy and not a pointer
}

使用std::map,您可以动态创建键值对,就像您提供的Lua代码一样。简而言之,我会使用struct Pool方法,因为使用std::map<>你必须记住字符串,这不是一个好的做法,你的IDE应该有intellisense,它将始终显示{的内容只要您点击struct Pool.运营商,就会{1}}。