Flowers with Boost and External Data Sources

Maybe there is an easy way around this that I don't see, so hopefully someone can explain this 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 this class:

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

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

What do I need to do for this job? I need boost::flyweight call AReader.readObject and hash / save the resulting class A

Does AReader need to become a full factory and be used as a custom factory? Or can you use the factory default in flies and somehow use AReader to generate A instances (as opposed to implementing the entire storage template required by the factory), perhaps by creating AReader an example argument in something in flies? Or can you get the public variables const (i.e. when they are set, they don't change) from an external data source without resorting to the second class?

Edit

I am also open to other offers not using Boost. I can, of course, write my own fly implementation or any other template if it works better. But if I can use what already exists, that would be better. Whatever reduces the amount of code I need to write, because, as always, there are short deadlines.

+7
source share
3 answers

I did not use Boost :: flyweight, but from his views, at least the key should be Assignable (besides EqualityComparable and Hashable ). With your const members that you enter is clearly not Assignable . In appearance you do not need to do this Assignable if you have an extractor key. When using a key extractor, the key must be Assignable .

+1
source

The main way to use flyweight in your case is for readObject to return flies. Inside, readObject creates a completely new object, and when you create the corresponding fly-like object, it then checks to see if the object is already in the fly store. If so, it will lose your new object and return flies that reference the object in the store. If not, it adds a new object to its pool.

Now this should be trivial to implement, but depending on your use case may be ineffective. To improve performance, you can use the key_value functionality, which allows you to refer to objects through your key and create them only if they are not already present in the store.

0
source

Despite the fact that key_value Flyweight seems to fit the bill, it would seem that there is a slight hitch. You should be able to build key_value flyweight using only one key type parameter ( key_value flyweights ). To make it work with the desired key (file_name + name), you have to pack these 2 fields into one ( tuple ? Not even sure if this will work.)

Assuming you're interested in making the most of it with the least amount of work, why not just Flyweight strings in your class, as shown in Fly Basics ?

This means that the objects A not hashed the way you want, but the lines are easily wrapped with weight loss, and they seem to be your memory problems. (if that does not simplify)

0
source

All Articles