]> git.tdb.fi Git - libs/gltk.git/blob - source/listdata.h
Add a function to find the index of an item in ListDataStore
[libs/gltk.git] / source / listdata.h
1 #ifndef MSP_GLTK_LISTDATA_H_
2 #define MSP_GLTK_LISTDATA_H_
3
4 #include <string>
5 #include <vector>
6 #include <sigc++/signal.h>
7 #include <msp/strings/lexicalcast.h>
8
9 namespace Msp {
10 namespace GLtk {
11
12 class ListData
13 {
14 public:
15         sigc::signal<void, unsigned> signal_item_added;
16         sigc::signal<void, unsigned> signal_item_removed;
17         sigc::signal<void> signal_cleared;
18         sigc::signal<void> signal_refresh_strings;
19
20 protected:
21         ListData() { }
22 public:
23         virtual ~ListData() { }
24
25         virtual unsigned size() const = 0;
26         virtual std::string get_string(unsigned) const = 0;
27         void refresh_strings() const { signal_refresh_strings.emit(); }
28 };
29
30 template<typename T>
31 class ListDataStore: public ListData
32 {
33 protected:
34         std::vector<T> items;
35
36         ListDataStore() { }
37
38 public:
39         void append(const T &v) { insert(items.size(), v); }
40
41         void insert(unsigned i, const T & v)
42         {
43                 if(i>items.size())
44                         throw std::out_of_range("ListDataStore::insert");
45
46                 items.insert(items.begin()+i, v);
47                 signal_item_added.emit(i);
48         }
49
50         const T &get(unsigned i) const
51         {
52                 if(i>=items.size())
53                         throw std::out_of_range("ListDataStore::get");
54
55                 return items[i];
56         }
57
58         int find(const T &v) const
59         {
60                 for(unsigned i=0; i<items.size(); ++i)
61                         if(items[i]==v)
62                                 return i;
63                 return -1;
64         }
65
66         void remove(unsigned i)
67         {
68                 if(i>=items.size())
69                         throw std::out_of_range("ListDataStore::remove");
70
71                 items.erase(items.begin()+i);
72                 signal_item_removed.emit(i);
73         }
74
75         void clear()
76         {
77                 items.clear();
78                 signal_cleared.emit();
79         }
80
81         virtual unsigned size() const { return items.size(); }
82 };
83
84 template<typename T>
85 class BasicListData: public ListDataStore<T>
86 {
87 public:
88         virtual std::string get_string(unsigned i) const
89         { return lexical_cast<std::string>(this->get(i)); }
90 };
91
92 template<typename T>
93 class FunctionListData: public ListDataStore<T>
94 {
95 public:
96         typedef std::string Func(const T &);
97
98 private:
99         Func *func;
100
101 public:
102         FunctionListData(Func f): func(f) { }
103
104         virtual std::string get_string(unsigned i) const
105         { return func(this->get(i)); }
106 };
107
108 } // namespace GLtk
109 } // namespace Msp
110
111 #endif