English 中文(简体)
带波斯特和外部数据来源的体重
原标题:Flyweights with Boost and external data sources

或许可以简单地看一看我不见,因此,希望有些人能够向我解释。

我要说的是:

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) {}; 
};

这一类的生成者是:

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

我愿使用<代码>boost:flyload处理这些<代码>A物体,因为可能有数百万次提及这些物体,而且实际上含有许多数据。 将在<代码> 姓名和上加印。

我需要做些什么? 我需要<代码>boost:flyetter/code>, 电话:AReader.readObject, 并储存由此产生的<代码>A

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>/strong>

我也愿意接受不使用波斯特的其他建议。 我当然可以写一下我自己执行高超或任何其他模式的情况。 但是,如果我能够利用已经存在的东西,那将是最好的。 不管怎样将守则数量降至最低,我需要写字,因为与以往一样,期限是短的。

问题回答

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.

The basic way to use flyweight in your case is for readObject to return a flyweight. Internally, readObject creates a brand new object, and when you create the corresponding flyweight object, it then checks whether the object is already within the flyweight store. If so, it will drop your new object, and return a flyweight referencing the object in the store. If not, it adds the new object to its pool.

现在,这应该是三边的,但视你的使用情况,可能效率低下。 为了提高绩效,您可使用key_alu功能,该功能允许你通过钥匙参考物体,并且只有在这些物体不在仓库内的情况下才能制造。

Although a key_value Flyweight seems to fit the bill, it would seem there s a minor hitch. You should be able to construct a key_value Flyweight by using just one parameter of the key type (key_value flyweights). So to make it work with the key you want (filename+name) you d have to pack those 2 fields in one (tuple? not even sure that d work.)

Assuming you re interested in getting the most with the least amount of work, why not just Flyweight the strings in your class as demonstrated in Flyweight Basics?

This means A objects aren t hashed the way you want, but strings are easily flyweighted, and these seem to be your memory-problematic fields. (unless this is an oversimplification)





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签