Storing my recipes

E

Eric Lilja

Hello, I am currently designing a small program that's supposed to become a
helper program
for a computer game. The program is to store a number of recipes and this
collection of recipes
is expected to grow to quite big over time. Each recipe can be categorized
into one of several
categories, for example: wood-working, tailoring, brewing, blacksmithing
etc.
I am planning on having a base class Recipe and inherit a new class for each
category of recipe.
I also plan on having an Item class for each item that is used in a
recipe...and now to my problem,
several recipes use the same items...my program would consume a lot of
memory indeed if each
recipe that uses, for example, the item Water had its own copy of the Item
class instance describing water.
How should I solve that? Maybe assign a unique ID to each item (and maybe
each recipe) and have a global
item_map which maps item_IDs to items. So instead of storing objects of type
Item in the recipes
I store keys to this item map. Other alternatives? Hmm...ohh, and I need to
be able to serialize everything.
So what I am looking for is, ideally, a solution that can be implemented
using standard c++ that doesn't
consume too much memory and that allows for easy serialization.

Thanks for any input
 
I

Ioannis Vranos

Eric said:
Hello, I am currently designing a small program that's supposed to become a
helper program
for a computer game. The program is to store a number of recipes and this
collection of recipes
is expected to grow to quite big over time. Each recipe can be categorized
into one of several
categories, for example: wood-working, tailoring, brewing, blacksmithing
etc.
I am planning on having a base class Recipe and inherit a new class for each
category of recipe.
I also plan on having an Item class for each item that is used in a
recipe...and now to my problem,
several recipes use the same items...my program would consume a lot of
memory indeed if each
recipe that uses, for example, the item Water had its own copy of the Item
class instance describing water.
How should I solve that? Maybe assign a unique ID to each item (and maybe
each recipe) and have a global
item_map which maps item_IDs to items. So instead of storing objects of type
Item in the recipes
I store keys to this item map. Other alternatives? Hmm...ohh, and I need to
be able to serialize everything.
So what I am looking for is, ideally, a solution that can be implemented
using standard c++ that doesn't
consume too much memory and that allows for easy serialization.


Some quick approaches:


1)

enum Item {WATER, RED_WINE};

class Spaggheti: public Recipe
{
std::vector<Item> items;

// ...
void some_func() const
{
if(items[0]==WATER)
// ...
}

// ...
};



2)

vector<string>Items;

// ...


class Spaggheti: public Recipe
{
vector<string>::const_iterator item;

// ...

public:
Spaggheti() { item=Items.begin()+3; }

//...


void some_func() const
{
if(*item=="WATER")
// ...
}

// ...
};




There are a lot of such "hacks" that can minimise memory use.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top