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.
A
into the flyweight (key extractor rather than assignable), but is the only way to use a helper-class that generatesA
to make it a full-blown factory to be used by the flyweight (implementing the storage and other methods required)? – Renfrow