开发者

Comparing streams

I'm looking into generalizing my data sources in my C++ application by using streams. However, my code also uses a resource manager that functions in a manner similar to a factory, except its primary purpose is to ensure that the same resource doesn't get loaded twice into memory.

myown::ifstream data("image.jpg");
std::ifstream data2("image2.jpeg");

ResourcePtr<Image> img1 = manager.acquire(data); 
ResourcePtr<Image> img2 = manager.acquire(data);

cout << img1 == img2; // True

ResourcePtr<Image> img3 = manager.acquire(data2);
cout << img1 == img3; // False

For it to do this, it obviously has to do some checks. Is there a reasonable way (readable and efficient) to implement this, if the r开发者_运维知识库esource manager has data streams as input?


You cannot "compare" data streams. Streams are not containers; they are flows of data.


BTW, cout << a == b is (cout << a) == b; I think you meant cout << (a==b).


The level of abstraction where the identity of the data is well above your streams. Think about what your stream would do with that information if it knew it. It could not act upon it, it is just a bunch of data. In terms of the interface, a stream doesn't necessarily even have an end. You would be violating least surprise for me if you tried to tie identity to it at that level.

That sounds like a reasonable abstraction for your ResourcePtr, though. You could hash the data when you load it into ResourcePtr, but a key on the file path is probably just as good.


Like Tomalak said, you can't compare streams. You'll have to wrap them in some class which associates an ID to them, possibly based on the absolute path if they are all associated to files on the file system

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜