File: code_templates.rb

package info (click to toggle)
genometools 1.6.6%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 50,576 kB
  • sloc: ansic: 271,876; ruby: 29,930; python: 5,106; sh: 3,083; makefile: 1,213; perl: 219; pascal: 159; haskell: 37; sed: 5
file content (391 lines) | stat: -rw-r--r-- 10,101 bytes parent folder | download | duplicates (5)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
$license = <<-END_LICENSE
/*
  Copyright (c) <%=year%> <%=name%> <<%=email%>>
  Copyright (c) <%=year%> Center for Bioinformatics, University of Hamburg

  Permission to use, copy, modify, and distribute this software for any
  purpose with or without fee is hereby granted, provided that the above
  copyright notice and this permission notice appear in all copies.

  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
END_LICENSE

$repfile = <<-END_REPFILE

#include <stdlib.h>

#include "<%=subdir%>/<%=fkt_pref%>.h"

typedef struct <%=classN%>Class <%=classN%>Class;
typedef struct <%=classN%>Members <%=classN%>Members;

/* TODO: add typedefs for functions here */
/* examples showcasing coding convention:
typedef unsigned int (*<%=classN%>FooFunc)(<%=classN%>*, GtUword*);
typedef int *(*<%=classN%>BarFunc)(<%=classN%>*, GtUword);
*/
typedef void (*<%=classN%>DeleteFunc)(<%=classN%>*);

struct <%=classN%> {
  const <%=classN%>Class *c_class;
  <%=classN%>Members *members;
};

struct <%=classN%>Class {
  size_t size;
  /* XXfunctionNamesXX do not change will be replaced by function names */
};

struct <%=classN%>Members {\
<% if options.refcount %>\n  GtUword refcount;<% end %>
};

const <%=classN%>Class* gt_<%=fkt_pref%>_class_new(size_t size,
<%= " "*(classN.length + fkt_pref.length + 26) %>\
/* XXfunctionPtrXX do not change */);

<%=classN%>*            gt_<%=fkt_pref%>_create(const <%=classN%>Class*);

void*<%= ' ' * (classN.length + 8) %>\
gt_<%=fkt_pref%>_cast(const <%=classN%>Class*,
<%= ' ' * (classN.length + fkt_pref.length + 22) %><%=classN%>*);
END_REPFILE

$hwrapper = <<-END_HWRAPPER
#ifndef <%=guard_macro%>
#define <%=guard_macro%>
<%=content%>
#endif
END_HWRAPPER

$gt_class_c = <<-END_GT_CLASS_C

#include "core/ma_api.h"

struct <%=klass%>
{
};

<%=creatordecl%>
{
  <%=klass%> *<%=basename%>;
  <%=basename%> = gt_malloc(sizeof (<%=klass%>));
  return <%=basename%>;
}

<%=destructordecl%>
{
  gt_free(<%=basename%>);
}
END_GT_CLASS_C

$gt_class_h = <<-END_GT_CLASS_H

typedef struct <%=klass%> <%=klass%>;

<%=creatordecl%>;
<%=destructordecl%>;
END_GT_CLASS_H

$interface_header = <<-INTERFACE_HEADER

/* The <%=classN%> interface
   TODO: add documentation */
typedef struct <%=classN%> <%=classN%>;
<% if options.refcount %>\

/* Increases the reference count of the <<%=classN%>>. */
<%=classN%>*<%=' '*(max_type_len - 1 - classN.length)%>\
gt_<%=fkt_pref%>_ref(<%=classN%> *<%=fkt_pref%>);\
<% end %>\
<%functions.each do |func, parameters| %>

/* TODO: document and add parameter names */
<%  type =parameters[0] + ' '*(max_type_len - parameters[0].length) %>\
<%  type = type.gsub(/ [*]/, '* ')
    funcname = type + 'gt_' + fkt_pref + '_' +
      func[0..-5].gsub(/(^|.)([A-Z])/) do
        rep = ''
        rep = $1+'_' if $1.length >0
        rep += $2.downcase
      end + '('
%>\
<%=funcname%>\
<%  parameters[1..-1].each_with_index do |parameter, idx|%>\
<%    if idx > 0 %>,
<%=     ' ' * funcname.length%>\
<%    end %>\
<%    if parameter[-1] == '*' %>\
<% puts parameter %>\
<%=     parameter.chop%> *\
<%    else %>\
<%=     parameter%>\
<%    end %>\
<%    if parameter.match /^\#{classN}\\*$/ %>\
<%=     fkt_pref %>\
<%    else %>\
 para<%=idx%> /*TODO: name*/\
<%    end %>\
<%  end %>);\
<%end %>
INTERFACE_HEADER

$ref_func = <<-REF_FUNC
<%=classN%> *gt_<%=fkt_pref%>_ref(<%=classN%> *<%=fkt_pref%>)
{
  gt_assert(<%=fkt_pref%>);
  <%=fkt_pref%>->members->refcount++;
  return <%=fkt_pref%>;
}
REF_FUNC

$create_func = <<-CREATE_FUNC
<%=classN%> *gt_<%=fkt_pref%>_create(const <%=classN%>Class *<%=fkt_pref%>_c)
{
  <%=classN%> *<%=fkt_pref%>;
  gt_assert(<%=fkt_pref%>_c && <%=fkt_pref%>_c->size);
  <%=fkt_pref%> = gt_calloc((size_t) 1, <%=fkt_pref%>_c->size);
  <%=fkt_pref%>->c_class = <%=fkt_pref%>_c;
  <%=fkt_pref%>->members = gt_calloc((size_t) 1, sizeof (<%=classN%>Members));
  return <%=fkt_pref%>;
}
CREATE_FUNC

$cast_func = <<-CAST_FUNC
void *gt_<%=fkt_pref%>_cast(GT_UNUSED const <%=classN%>Class *<%=fkt_pref%>_c,
<%=' ' * (fkt_pref.length + 15)%><%=classN%> *<%=fkt_pref%>)
{
  gt_assert(<%=fkt_pref%>_c && <%=fkt_pref%> &&
            <%=fkt_pref%>->c_class == <%=fkt_pref%>_c);
  return <%=fkt_pref%>;
}
CAST_FUNC

$delete_func = <<-DELETE_FUNC
void gt_<%=fkt_pref%>_delete(<%=classN%> *<%=fkt_pref%>)
{
  if (<%=fkt_pref%> != NULL) {
<% if options.refcount %>\
    if (<%=fkt_pref%>->members->refcount) {
      <%=fkt_pref%>->members->refcount--;
      return;
    }
<%end %>\
    gt_assert(<%=fkt_pref%>->c_class);
    if (<%=fkt_pref%>->c_class->delete_func != NULL)
      <%=fkt_pref%>->c_class->delete_func(<%=fkt_pref%>);
    gt_free(<%=fkt_pref%>->members);
    gt_free(<%=fkt_pref%>);
  }
}
DELETE_FUNC

$interface_func = <<-INTERFACE_FUNC
<%funcname = type + 'gt_' + fkt_pref + '_' +
func[0..-5].gsub(/(^|.)([A-Z])/) do
  rep = ''
  rep = $1+'_' if $1.length >0
  rep += $2.downcase
end + '(' %>
<%=funcname %>\
<%paras.each_with_index do |para,idx| %>\
<%  if idx > 0 %>,
<%=   ' ' * (funcname.length) %>\
<%  end %>\
<%  if para[-1] == '*' %>\
<%=   para.chop %> *\
<%  else %>\
<%=   para %>\
<%  end %>\
<%  if para.match /^\#{classN}\\*$/ %>\
<%=   fkt_pref %>\
<%  else %>\
 para<%=idx%> /*TODO: name*/\
<%  end %>\
<%end %>)
{
  gt_assert(<%=fkt_pref%> != NULL);
  gt_assert(<%=fkt_pref%>->c_class != NULL);
  if (<%=fkt_pref%>->c_class->\
<%=func.gsub(/(^|.)([A-Z])/) do
  rep = ''
  rep = $1+'_' if $1.length >0
  rep += $2.downcase \
end %> != NULL)
    return <%=fkt_pref%>->c_class->\
<%=func.gsub(/(^|.)([A-Z])/) do
  rep = ''
  rep = $1+'_' if $1.length >0
  rep += $2.downcase \
end %>(\
<%paras.each_with_index do |para,idx| %>\
<%  if idx > 0 %>,
<%=   ' ' * (func.length + fkt_pref.length + 23) %>\
<%  end%>\
<%  if para.match /^\#{classN}\\*$/ %>\
<%=   fkt_pref %>\
<%  else %>\
 para<%=idx%> /*TODO: name*/\
<%  end %>\
<%end %>);
  return\
<%if type.match /[*]/ %>\
 NULL;\
<%elsif type.match /void/ %>\
;\
<%else %>\
 0;\
<%end %> /* TODO: check if default return value is sane */
}
INTERFACE_FUNC

$class_new_fkt = <<-CLASS_NEW_FKT
const <%=classN%>Class *gt_<%=fkt_pref%>_class_new(size_t size\
<%functions.each do |func, paras| %>,
<%=  ' ' * (27 + classN.length + fkt_pref.length)%>\
<%=classN%><%=func%> <%=func.gsub(/(^|.)([A-Z])/) do
  rep = ''
  rep = $1+'_' if $1.length > 0
  rep += $2.downcase
end%>\
<%end %>)
{
  <%=classN%>Class *<%=fkt_pref%>_c = gt_class_alloc(sizeof (*<%=fkt_pref%>_c));
  <%=fkt_pref%>_c->size = size;
<%functions.each do |func, paras| %>\
<% name = func.gsub(/(^|.)([A-Z])/) do
  rep = ''
  rep = $1+'_' if $1.length >0
  rep += $2.downcase
end %>\
  <%=fkt_pref%>_c-><%=name%> = <%=name%>;
<%end %>\
  return <%=fkt_pref%>_c;
}
CLASS_NEW_FKT

$interface_file = <<-INTERFACE_CODE
#include "<%=subdir%>/<%=fkt_pref%>_rep.h"

#include "core/assert_api.h"
#include "core/class_alloc_api.h"
#include "core/ma_api.h"
#include "core/unused_api.h"

<%=create_func%>
<% if options.refcount %>\
<%=ref_func%>
<% end %>\
<%=cast_func%>\
<%=interface_funcs%>
<%=class_new_fkt%>
<%=delete_func%>
INTERFACE_CODE

$implement_header = <<-IMPLEMENT_HEADER

#include "<%=classbase%>.h"

/* The <<%=iclassN%>> class implements the <<%=classN%>> interface.
   TODO: add documentation */
typedef struct <%=iclassN%> <%=iclassN%>;

/* Return a new <<%=classN%>> object, the implementation beeing of type
   <<%=iclassN%>>.
   TODO: add documentation */
<%=classN%>* <%=ifkt_pref%>_new(void /*TODO: add parameters*/);

/* TODO: add prototypes of implementation <<%=iclassN%>> only functions */
IMPLEMENT_HEADER

$implement_src = <<-IMPLEMENT_SRC

#include "<%=classbase%>_rep.h"
#include "<%=basename%>.h"
#include "core/unused_api.h"

struct <%=iclassN%> {
  <%=classN%> parent_instance;
  /* TODO: add implementation members */
};

#define <%=ifkt_pref%>_cast(cvar) \\
        <%=fkt_pref%>_cast(<%=ifkt_pref%>_class(), cvar)

<%functions.each do |func, parameters| %>\
<%  type = parameters[0] %>\
<%  funcnames << ifkt_pref + '_' +
      func[0..-5].gsub(/(^|.)([A-Z])/) do
        rep = ''
        rep = $1+'_' if $1.length >0
        rep += $2.downcase
      end
    funcname = type + funcnames[-1] + '('
%>\
static <%=funcname%>\
<%  parameters[1..-1].each_with_index do |parameter, idx|%>\
<%    if idx > 0 %>,
<%=     ' ' * (funcname.length + 7)%>\
<%    end %>GT_UNUSED \
<%    if parameter[-1] == '*' %>\
<%=     parameter.chop%> *\
<%    else %>\
<%=     parameter%>\
<%    end %>\
<%    if parameter.match /\#{classN}\\*$/ %>\
<%=     icvar %>\
<%    else %>\
 para<%=idx%> /*TODO: name*/\
<%    end %>\
<%  end %>)
{
<% if parameters[1].match classN %>\
<%=  iclassN%> <%=iclassN.gsub(/^Gt/,'').gsub(/[a-z_]/,'').downcase%>\
= <%=ifkt_pref%>_cast(<%=icvar%>);
<% end %>\
  /* TODO: add functionality */
  return\
<%  if type.match /[*]/ %>\
 NULL;\
<%  elsif type.match /void/ %>\
;\
<%  else %>\
 0;\
<%  end %> /* TODO: check if default return value is sane */
}

<%end %>\
/* TODO: implement <<%=iclassN%>> only functions */

/* map static local methods to interface */
const <%=classN%>Class* <%=ifkt_pref%>_class(void)
{
  static const <%=classN%>Class *this_c = NULL;
  if (this_c == NULL) {
    this_c = <%=fkt_pref%>_class_new(sizeof (<%=iclassN%>),
<%funcnames.each_with_index do |name,idx| %>\
<%  if idx > 0 %>,
<%  end %>\
<%= ' ' * (fkt_pref.length + 24) %><%=name%>\
<%end %>);
  }
  return this_c;
}

<%=classN%>* <%=ifkt_pref%>_new(void /* TODO: add parameters */)
{
  <%=classN%> *<%=cvar%>;
  <%=iclassN%> *<%=icvar%>;
  <%=cvar%> = <%=fkt_pref%>_create(<%=ifkt_pref%>_class());
  <%=icvar%> = <%=ifkt_pref%>_cast(<%=cvar%>);
  /* TODO: initialise */
  return <%=cvar%>;
}
IMPLEMENT_SRC