1 #ifndef MSP_GLTK_LISTDATA_H_
2 #define MSP_GLTK_LISTDATA_H_
7 #include <sigc++/signal.h>
8 #include <msp/strings/lexicalcast.h>
16 sigc::signal<void, unsigned> signal_item_added;
17 sigc::signal<void, unsigned> signal_item_removed;
18 sigc::signal<void> signal_cleared;
19 sigc::signal<void, unsigned> signal_refresh_item;
24 virtual ~ListData() { }
26 virtual unsigned size() const = 0;
27 virtual std::string get_string(unsigned) const = 0;
28 void refresh(unsigned i) const
31 throw std::out_of_range("ListData::refresh");
32 signal_refresh_item.emit(i);
37 class ListDataStore: public ListData
45 void append(const T &v) { insert(items.size(), v); }
47 void insert(unsigned i, const T &v)
50 throw std::out_of_range("ListDataStore::insert");
52 items.insert(items.begin()+i, v);
53 signal_item_added.emit(i);
56 const T &get(unsigned i) const
59 throw std::out_of_range("ListDataStore::get");
64 int find(const T &v) const
66 for(unsigned i=0; i<items.size(); ++i)
72 using ListData::refresh;
73 void refresh(const T &v) const
77 signal_refresh_item.emit(i);
80 void remove(unsigned i)
83 throw std::out_of_range("ListDataStore::remove");
85 items.erase(items.begin()+i);
86 signal_item_removed.emit(i);
92 signal_cleared.emit();
95 virtual unsigned size() const { return items.size(); }
99 class BasicListData: public ListDataStore<T>
102 virtual std::string get_string(unsigned i) const
103 { return lexical_cast<std::string>(this->get(i)); }
107 class FunctionListData: public ListDataStore<T>
110 typedef std::string Func(const T &);
116 FunctionListData(Func f): func(f) { }
118 virtual std::string get_string(unsigned i) const
119 { return func(this->get(i)); }