Lists of Dynamically Allocated Variables

Adding Modules to help with scripting/coding plugins.
User avatar
Archive-Poster
Posts: 1090
Gender:
United States of America

Lists of Dynamically Allocated Variables

Post by Archive-Poster »

Lists of dynamically allocated variables

There is a very util file named sh_list.h that helps you to keep many dynamically allocated variables into lists.
I find this file or, more exactly, this class very util, and, I think it's hard enough to get learnt by yourself about it's purpose.

Here you can see some of it's features.

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "CString.h"
#include "sh_list.h"

class ExtraItem
{
public:
  String * pName;
  String * pInformation;

  size_t Index;
  size_t Cost;
};

List <ExtraItem *> g_ExtraItems;

size_t g_Jetpack = 0U;

static size_t RegisterExtraItem(const char * pName, const char * pInformation, const size_t Cost)
{
  static ExtraItem * pExtraItem = NULL;

  pExtraItem = new ExtraItem;

  assert(pExtraItem);

  pExtraItem->pName = new String;
  pExtraItem->pInformation = new String;

  pExtraItem->pName->assign(pName);
  pExtraItem->pInformation->assign(pInformation);

  pExtraItem->Index = g_ExtraItems.size() + 1U;
  pExtraItem->Cost = Cost;

  g_ExtraItems.push_back(pExtraItem);

  return pExtraItem->Index;
}

static void FreeAllExtraItems(void)
{
  static List <ExtraItem *> ::iterator ExtraItem = NULL;

  for (ExtraItem = g_ExtraItems.begin(); ExtraItem != g_ExtraItems.end(); ExtraItem++)
  {
    delete (*ExtraItem)->pName;
    delete (*ExtraItem)->pInformation;

    delete (*ExtraItem);
  }

  g_ExtraItems.clear();
}

static ExtraItem * GetExtraItemByIndex(size_t Index)
{
  static List <ExtraItem *> ::iterator ExtraItem = NULL;

  for (ExtraItem = g_ExtraItems.begin(); ExtraItem != g_ExtraItems.end(); ExtraItem++)
  {
    if ((*ExtraItem)->Index == Index)
    {
      return (*ExtraItem);
    }
  }

  return NULL;
}

int main(void)
{
  g_Jetpack = RegisterExtraItem("Jetpack", "Allows you to fly.", 32U);

  ExtraItem * pExtraItem = GetExtraItemByIndex(g_Jetpack);

  assert(pExtraItem);

  printf("Jetpack has an information that equals to '%s'.\n", pExtraItem->pInformation->c_str());

  FreeAllExtraItems();

  return 0U;
} 
Also, you may declare into classes, functions like listed below.

Code: Select all

class Task
{
public:
  size_t Handler(String * pInformation, size_t Index, float Radius);
  size_t Repetitions;
}; 
Attachments
CString + List.zip
(4.08 KiB) Downloaded 4 times

Who is online

Users browsing this forum: No registered users and 1 guest