【问题标题】:lua function to C++ functionlua 函数到 C++ 函数
【发布时间】:2016-03-11 05:48:15
【问题描述】:

我正在努力将 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++ 中使用它。谁能帮帮我?

【问题讨论】:

  • 这里不是池化结构吗?只需声明一个结构并创建某种构造函数并初始化此结构并返回其指针。一定要在构造器中分配结构体,否则它只会在函数范围内是本地的,并且它的地址是一个悬空指针,你会返回。

标签: c++ lua


【解决方案1】:

Lua 有一个叫做Tables 的东西,它允许你在没有预定义struct 的情况下添加键值对,就像在C/C++ 中一样。因此,您发布的 Lua 代码正在将键值对添加到 pool(在代码中读取 cmets):

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 代码使用表,因此您可以使用 std::map<> 在 C++ 中创建等效代码。然后你可以使用std::map&lt;std::string, int&gt;如下:

// 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&lt;&gt;,您必须记住不是好的做法的字符串,并且您的IDE 应该具有智能感知,无论何时您都会向您显示struct Pool 的内容点击.-&gt; 运算符。

【讨论】:

  • 可以假设 newSpecies() 和 newGenome() 的设置相同吗?这两个和其他几个都使用相同的 lua 设置,但具有不同的值。
  • 你可以假设相同。为 Species 和 Genome 创建一个 struct,并在 struct Pool 内部添加它们,如我在上面的代码中所示。
猜你喜欢
  • 2015-07-14
  • 1970-01-01
  • 2021-04-15
  • 1970-01-01
  • 1970-01-01
  • 2011-08-15
  • 2017-06-05
  • 2011-02-25
  • 2014-09-01
相关资源
最近更新 更多