forked from townforge/townforge
game: add missing file
This commit is contained in:
parent
6f4fc81b1d
commit
18c45236be
61
src/game/caching-source-builder.h
Normal file
61
src/game/caching-source-builder.h
Normal file
@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include <tb/tb_select_item.h>
|
||||
|
||||
static inline bool operator==(const tb::TBGenericStringItem &item0, const tb::TBGenericStringItem &item1)
|
||||
{
|
||||
return item0.str == item1.str && item0.id == item1.id
|
||||
&& item0.skin_image == item1.skin_image && item0.sub_source == item1.sub_source && item0.state == item1.state
|
||||
&& item0.tag.Equals(item1.tag);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
class CachingSourceBuilder
|
||||
{
|
||||
private:
|
||||
struct holder_t: public tb::TBGenericStringItem
|
||||
{
|
||||
T *item;
|
||||
|
||||
holder_t(T *item): TBGenericStringItem(""), item(item) {}
|
||||
};
|
||||
|
||||
public:
|
||||
CachingSourceBuilder(tb::TBSelectItemSourceList<T> &source): old_source(source) {}
|
||||
|
||||
~CachingSourceBuilder()
|
||||
{
|
||||
const int n_items = new_source.GetNumItems();
|
||||
if (!Changed())
|
||||
{
|
||||
for (int i = 0; i < n_items; ++i)
|
||||
delete new_source.GetItem(i)->item;
|
||||
return;
|
||||
}
|
||||
old_source.DeleteAllItems();
|
||||
for (int i = 0; i < n_items; ++i)
|
||||
{
|
||||
old_source.AddItem(new_source.GetItem(i)->item);
|
||||
}
|
||||
}
|
||||
|
||||
void AddItem(T *item) { new_source.AddItem(new holder_t(item)); }
|
||||
|
||||
protected:
|
||||
bool Changed() const
|
||||
{
|
||||
if (old_source.GetNumItems() != new_source.GetNumItems())
|
||||
return true;
|
||||
const int n_items = old_source.GetNumItems();
|
||||
for (int i = 0; i < n_items; ++i)
|
||||
{
|
||||
if (!(*old_source.GetItem(i) == *new_source.GetItem(i)->item))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
tb::TBSelectItemSourceList<T> &old_source;
|
||||
tb::TBSelectItemSourceList<holder_t> new_source;
|
||||
};
|
Loading…
Reference in New Issue
Block a user