简体   繁体   中英

lua function to C++ function

I'm working on converting a lua program into a C++ program but I've hit a road block, I can't figure out how to convert this into 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

I know many basics of both languages and i know it works in lua but i need it in C++. Can anyone help me?

Lua has something called Tables which allows you to add key-value pairs without a predefined struct as in C/C++. So the Lua code you posted is adding key-value pairs to pool (read comments in code):

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'

In C++ you have several options. 1) you can create a struct and declare what you need (I'm guessing on some datatypes but if you have the full Lua program you can figure them out):

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;
}

If you have a class then you can use the struct Pool shown below (add the struct Pool definition from above to the .h file below above class Kingdom ):

// 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;
}

In your .cpp file:

#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;
}

Option 2) would be if all of your key-value pairs were the same type; ie all keys were std::string and all values were int . Remember, the Lua code is using Tables so you can create the equivalent in C++ with std::map<> . Then you could use std::map<std::string, int> as follows:

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

Then in your .cpp file change the constructor to:

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
}

With std::map you can create key-value pairs on the fly just like the Lua code you provided. In short, I'd go with the struct Pool approach because with std::map<> you'll have to remember strings which isn't good practice and your IDE should have intellisense which will always show you the contents of struct Pool whenever you hit the . or -> operators.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM