Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flyweights with Boost and external data sources

Maybe there is a simple way around this that I'm not seeing, so hopefully somebody can explain it to me.

Let's say I have a class:

class A {
public:
  const double parameter;
  const std::string name;
  const std:: string fileName;

  A(const double parameter, const std::string name, const std::string fileName) : 
      parameter(parameter), name(name), fileName(fileName) {}; 
};

And the generator for that class is:

class AReader {
public:
  ifstream dataFile;
  AReader(const std::string filename);
  A* readObject(const std::string objectName);
};

I would like to use boost::flyweight to handle these A objects because there will be potentially millions of references to them and in reality they contain a lot of data. They will be hashed on name and fileName together.

What do I need to make this work? I need the boost::flyweight to call AReader.readObject and hash/store the resulting A class.

Does the AReader need to become a full factory and used as a custom factory? Or is it possible to use the default factory in the flyweight and somehow use AReader to generate the A instances (as opposed to implementing the entire storage pattern required by the factory), maybe by making an AReader instance an argument to something in the flyweight? Or is it possible to get const public variables (ie. once set, they don't change) from an external data source without resorting to a second class?

Edit

I'm also open to other suggestions not using Boost. I can certainly write my own implementation of a flyweight, or any other pattern if one is better suited. But if I can use something that already exists, that would be best. Whatever minimizes the amount of code I need to write because, as always, deadlines are short.

like image 549
tpg2114 Avatar asked Jan 13 '12 16:01

tpg2114


1 Answers

I haven't used Boost::flyweight but from the looks of it at the very least the key needs to be Assignable (in addition to being EqualityComparable and Hashable). With your const members you type is clearly not Assignable. From the looks of it, you don't have to make it Assignable if you have a key extractor. Using a key extractor only the key needs to be Assignable.

like image 109
Dietmar Kühl Avatar answered Oct 20 '22 22:10

Dietmar Kühl