summaryrefslogtreecommitdiff
path: root/tools/m4/list.m4
blob: 954beb1939c240d6f8d8491ddee4174d54db346a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
_PUSH()

dnl
dnl  These variables affect the generation of the list
dnl
define(GP_LIST_HELPER_NAMESPACE,`define(`__LIST_NAMESPACE__',$1)')
define(GP_LIST_ELEM,`define(`__LISTELEM__',`$*')')
define(GP_LIST_ITER,`define(`__LISTITER__',`$*')')
define(GP_LIST_NOINSERT,`define(`__LISTEO__')')

dnl
dnl GP_LIST(ListName, ParentCppType, ParentCType, ChildCppType, FieldNameC)
dnl
dnl In the .ccg file, you'll need to implement:
dnl   iterator insert(iterator position, element_type& e);
dnl
dnl  Fieldname assumed to be children if not specified
define(GP_LIST,`
_PUSH()

define(`__LISTNAME__',$1)
define(`__LISTPARENT__',$2)
define(`__LISTPARENT_G__',$3)
define(`__LISTTYPE__',$4)
define(`__LISTELEM__',const Element)
define(`__LISTITER__',Glib::List_Iterator< __LISTTYPE__ >)
define(`__LIST_NAMESPACE__',$2_Helpers)
#define(`__LISTFIELD__',ifelse($5,,children,$5))
define(`__LISTFIELD__',$5)

_SECTION(SECTION_USR)
')

dnl
dnl  GP_LIST_END()
dnl
dnl   Closes a list
define(GP_LIST_END,`dnl
_POP()

class __LISTNAME__ : public Glib::HelperList< __LISTTYPE__, __LISTELEM__, __LISTITER__ >
{
public:
  __LISTNAME__`'();
  explicit __LISTNAME__`'(__LISTPARENT_G__* gparent);
  __LISTNAME__`'(const __LISTNAME__& src);
  virtual ~__LISTNAME__`'() {}

  __LISTNAME__& operator=(const __LISTNAME__& src);

  typedef Glib::HelperList< __LISTTYPE__, __LISTELEM__,  __LISTITER__ > type_base;

  __LISTPARENT_G__* gparent();
  const __LISTPARENT_G__* gparent() const;

  virtual GList*& glist() const;      // front of list

  virtual void erase(iterator start, iterator stop);
  virtual iterator erase(iterator);  //Implented as custom or by LIST_CONTAINER_REMOVE
  virtual void remove(const_reference); //Implented as custom or by LIST_CONTAINER_REMOVE

  /// This is order n. (use at own risk)
  reference operator[](size_type l) const;

ifdef(`__LISTEO__',`dnl
protected:
  //Hide these because it's read-only:
  iterator insert(iterator position, element_type& e);

  inline void pop_front();
  inline void pop_back();
,`dnl
public:
  iterator insert(iterator position, element_type& e); //custom-implemented.

  template <class InputIterator>
  inline void insert(iterator position, InputIterator first, InputIterator last)
  {
    for(;first != last; ++first)
      position = insert(position, *first);
  }

 inline void push_front(element_type& e)
    { insert(begin(), e); }
  inline void push_back(element_type& e)
    { insert(end(), e); }
')dnl

_IMPORT(SECTION_USR)
};

_PUSH(SECTION_CC)

namespace __LIST_NAMESPACE__
{

__LISTNAME__::__LISTNAME__`'()
{}

__LISTNAME__::__LISTNAME__`'(__LISTPARENT_G__* gparent)
: type_base((GObject*)gparent)
{}

__LISTNAME__::__LISTNAME__`'(const __LISTNAME__& src)
:
  type_base(src)
{}

__LISTNAME__& __LISTNAME__::operator=(const __LISTNAME__& src)
{
  type_base::operator=(src);
  return *this;
}

ifelse(__LISTFIELD__,CUSTOM,`dnl
',`dnl else
GList*& __LISTNAME__::glist() const
{
  return ((__LISTPARENT_G__*)gparent_)->__LISTFIELD__;
}
')dnl endif

void __LISTNAME__::erase(iterator start, iterator stop)
{
  type_base::erase(start, stop);
}

__LISTPARENT_G__* __LISTNAME__::gparent()
{
  return (__LISTPARENT_G__*)type_base::gparent();
}

const __LISTPARENT_G__* __LISTNAME__::gparent() const
{
  return (__LISTPARENT_G__*)type_base::gparent();
}

__LISTNAME__::reference __LISTNAME__::operator[](size_type l) const
{
  return type_base::operator[](l);
}

} /* namespace __LIST_NAMESPACE__ */

undefine(`__LISTNAME__')dnl
undefine(`__LISTTYPE__')dnl
undefine(`__LISTPARENT__')dnl
undefine(`__LISTELEM__')dnl
undefine(`__LISTFIELD__')dnl
_POP()
')

dnl
dnl  GP_LIST_FIND(access_method)
dnl
dnl    Defines  find(containertype) and find(Widget&)
dnl    access_method is the name of method returning a Widget*
define(GP_LIST_FIND,`
  iterator find(const_reference c);
  iterator find(Widget&);
_PUSH(SECTION_CC)

namespace __LIST_NAMESPACE__
{

__LISTNAME__::iterator __LISTNAME__::find(const_reference w)
{
  iterator i = begin();
  while (i != end() && (i->ifelse($1,,,$1()->)gobj() != w.ifelse($1,,,$1()->)gobj()))
    ++i;
  return i;
}

__LISTNAME__::iterator __LISTNAME__::find(Widget& w)
{
  iterator i = begin();
  while (i != end() && ((GtkWidget*)i->ifelse($1,,,$1()->)gobj() != w.gobj()))
    ++i;
  return i;
}

} /* namespace __LIST_NAMESPACE__ */

_POP()
')

dnl
dnl  GP_LIST_CONTAINER_REMOVE(access_method)
dnl
dnl    Implements remove(const_reference), erase(iterator)
dnl    and defines remove(Widget&)
dnl    (assumes that the widget uses gtk+ container methods).
dnl    access_method is the name of the method returning a Widget*
define(GP_LIST_CONTAINER_REMOVE,`
virtual void remove(Widget& w); //Implented as custom or by LIST_CONTAINER_REMOVE
_PUSH(SECTION_CC)

namespace __LIST_NAMESPACE__
{

void __LISTNAME__::remove(const_reference child)
{
  gtk_container_remove(GTK_CONTAINER(gparent_),
                       (GtkWidget*)(child.ifelse($1,,,$1()->)gobj()));
}

void __LISTNAME__::remove(Widget& widget)
{
  gtk_container_remove(GTK_CONTAINER(gparent_), (GtkWidget*)(widget.gobj()));
}

__LISTNAME__::iterator __LISTNAME__::erase(iterator position)
{
  //Check that it is a valid iterator, to a real item:
  if ( !position.node_|| (position == end()) )
    return end();

  //Get an iterator the the next item, to return:
  iterator next = position;
  next++;

  //Use GTK+ C function to remove it, by providing the GtkWidget*:
  gtk_container_remove( GTK_CONTAINER(gparent_), (GtkWidget*)(position->ifelse($1,,,$1()->)gobj()) );
  return next;
}

} /* namespace __LIST_NAMESPACE__ */

_POP()
')

_POP()dnl