summaryrefslogtreecommitdiffstats
path: root/scripts/new-object.sh
blob: ef2aea822e547939511ff101b0035a57c28c83b8 (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
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
#!/bin/bash

NAMESPACE="Ply"
AUTHOR="YOUR NAME <youremail@here.com>"
#-----------------------------------------------------------------------------
uppercase ()
{
  echo "${1}" | tr 'a-z' 'A-Z'
}

lowercase ()
{
  echo "${1}" | tr 'A-Z' 'a-z'
}

change_character ()
{
  echo "${1}" | tr "${2}" "${3}"
}

delete_character ()
{
  echo "${1}" | tr -d "${2}"
}

#-----------------------------------------------------------------------------
MACRO_NAMESPACE="$(change_character $(uppercase ${NAMESPACE}) '-' '_')"
METHOD_NAMESPACE="$(change_character $(lowercase ${NAMESPACE}) '-' '_')"

OBJECT_NAME="$(delete_character ${1} '-')"
MACRO_OBJECT_NAME="$(change_character $(uppercase ${1}) '-' '_')"
METHOD_OBJECT_NAME="$(change_character $(lowercase ${1}) '-' '_')"
FULL_OBJECT_NAME="$(delete_character ${NAMESPACE} '-')${OBJECT_NAME}"
SHORT_OBJECT_NAME="$(lowercase $(echo ${1} | awk -F- '{print $NF}'))"

HEADER_FILENAME="$(lowercase ${NAMESPACE}-${1}).h"
HEADER_GUARD="$(change_character $(change_character $(uppercase ${HEADER_FILENAME}) '-' '_') '.' '_')" 

SOURCE_FILENAME="$(lowercase ${NAMESPACE}-${1}).c"

MACRO_PREFIX="${MACRO_NAMESPACE}_${MACRO_OBJECT_NAME}"
METHOD_PREFIX="${METHOD_NAMESPACE}_${METHOD_OBJECT_NAME}"

ERROR_QUARK="${METHOD_NAMESPACE}-$(lowercase ${1})"
HUMAN_READABLE_NAME=$(change_character $(lowercase ${1}) '-' ' ')

#-----------------------------------------------------------------------------
cat <<  > ${HEADER_FILENAME}
/* vim: ts=4 sw=2 expandtab autoindent cindent
 * ${HEADER_FILENAME} - ${2}
 *
 * Copyright (C) $(date +%Y) ${AUTHOR}
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.  
 *
 * Written By: ${AUTHOR}
 */
#ifndef ${HEADER_GUARD}
#define ${HEADER_GUARD}

#include <glib.h>
#include <glib-object.h>

G_BEGIN_DECLS

#define ${MACRO_NAMESPACE}_TYPE_${MACRO_OBJECT_NAME} (${METHOD_PREFIX}_get_type ())
#define ${MACRO_PREFIX}(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), ${MACRO_NAMESPACE}_TYPE_${MACRO_OBJECT_NAME}, ${FULL_OBJECT_NAME}))
#define ${MACRO_PREFIX}_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), ${MACRO_NAMESPACE}_TYPE_${MACRO_OBJECT_NAME}, ${FULL_OBJECT_NAME}Class))
#define ${MACRO_NAMESPACE}_IS_${MACRO_OBJECT_NAME}(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), ${MACRO_NAMESPACE}_TYPE_${MACRO_OBJECT_NAME}))
#define ${MACRO_NAMESPACE}_IS_${MACRO_OBJECT_NAME}_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), ${MACRO_NAMESPACE}_TYPE_${MACRO_OBJECT_NAME}))
#define ${MACRO_PREFIX}_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS((obj), ${MACRO_NAMESPACE}_TYPE_${MACRO_OBJECT_NAME}, ${FULL_OBJECT_NAME}Class))
#define ${MACRO_PREFIX}_ERROR (${METHOD_PREFIX}_error_quark ())

typedef struct _${FULL_OBJECT_NAME} ${FULL_OBJECT_NAME};
typedef struct _${FULL_OBJECT_NAME}Class ${FULL_OBJECT_NAME}Class;
typedef struct _${FULL_OBJECT_NAME}Private ${FULL_OBJECT_NAME}Private;
typedef enum _${FULL_OBJECT_NAME}Error ${FULL_OBJECT_NAME}Error;

struct _${FULL_OBJECT_NAME}
{
  GObject parent;

  /*< private >*/
  ${FULL_OBJECT_NAME}Private *priv;
};

struct _${FULL_OBJECT_NAME}Class
{
  GObjectClass parent_class;

  /* signals */
#if 0
  void (* foo) (${FULL_OBJECT_NAME} *${SHORT_OBJECT_NAME});
#endif
};

enum _${FULL_OBJECT_NAME}Error
{
  ${MACRO_PREFIX}_ERROR_GENERIC = 0,
};

#ifndef ${MACRO_NAMESPACE}_HIDE_FUNCTION_DECLARATIONS
GType ${METHOD_PREFIX}_get_type (void);
GQuark ${METHOD_PREFIX}_error_quark (void);

${FULL_OBJECT_NAME} *${METHOD_PREFIX}_new (void) G_GNUC_MALLOC;
#endif

G_END_DECLS
#endif /* ${HEADER_GUARD} */

#-----------------------------------------------------------------------------
cat <<  > ${SOURCE_FILENAME}
/* vim: ts=4 sw=2 expandtab autoindent cindent
 * ${SOURCE_FILENAME} - ${2}
 *
 * Copyright (C) $(date +%Y) ${AUTHOR}
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.  
 *
 * Written by: ${AUTHOR}
 */
#include "config.h"
#include "${HEADER_FILENAME}"

#include <errno.h>
#include <string.h>

#include <glib.h>
#include <glib-object.h>
#include <glib/gi18n.h>

struct _${FULL_OBJECT_NAME}Private
{
  int bar;
};

static void ${METHOD_PREFIX}_finalize (GObject *object);
#if 0
static void ${METHOD_PREFIX}_class_install_signals (${FULL_OBJECT_NAME}Class *${SHORT_OBJECT_NAME}_class);
static void ${METHOD_PREFIX}_class_install_properties (${FULL_OBJECT_NAME}Class *${SHORT_OBJECT_NAME}_class);

static void ${METHOD_PREFIX}_set_property (GObject      *object,
                                           guint         prop_id,
                                           const GValue *value,
                                           GParamSpec   *pspec);
static void ${METHOD_PREFIX}_get_property (GObject    *object,
                                           guint       prop_id,
                                           GValue     *value,
                                           GParamSpec *pspec);

enum
{
  PROP_0 = 0,
  PROP_BAR
};

#define ${MACRO_PREFIX}_DEFAULT_BAR 1

enum
{
  FOO = 0,
  NUMBER_OF_SIGNALS
};

static guint ${METHOD_PREFIX}_signals[NUMBER_OF_SIGNALS];
#endif

G_DEFINE_TYPE (${FULL_OBJECT_NAME}, ${METHOD_PREFIX}, G_TYPE_OBJECT);

static void
${METHOD_PREFIX}_class_init (${FULL_OBJECT_NAME}Class *${SHORT_OBJECT_NAME}_class)
{
  GObjectClass *object_class;

  object_class = G_OBJECT_CLASS (${SHORT_OBJECT_NAME}_class);

  object_class->finalize = ${METHOD_PREFIX}_finalize;
  
#if 0
  ${METHOD_PREFIX}_class_install_properties (${SHORT_OBJECT_NAME}_class);
  ${METHOD_PREFIX}_class_install_signals (${SHORT_OBJECT_NAME}_class);
#endif

  g_type_class_add_private (${SHORT_OBJECT_NAME}_class, sizeof (${FULL_OBJECT_NAME}Private));
}

#if 0
static void
${METHOD_PREFIX}_class_install_signals (${FULL_OBJECT_NAME}Class *${SHORT_OBJECT_NAME}_class)
{
  GObjectClass *object_class;

  object_class = G_OBJECT_CLASS (${SHORT_OBJECT_NAME}_class);

  ${METHOD_PREFIX}_signals[FOO] =
    g_signal_new ("foo", G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (${FULL_OBJECT_NAME}Class, foo),
                  NULL, NULL, g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
  ${SHORT_OBJECT_NAME}_class->foo = NULL;
}

static void
${METHOD_PREFIX}_class_install_properties (${FULL_OBJECT_NAME}Class *${SHORT_OBJECT_NAME}_class)
{
  GObjectClass *object_class;
  GParamSpec *param_spec;

  object_class = G_OBJECT_CLASS (${SHORT_OBJECT_NAME}_class);
  object_class->set_property = ${METHOD_PREFIX}_set_property;
  object_class->get_property = ${METHOD_PREFIX}_get_property;

  param_spec = g_param_spec_int ("bar", _("Bar"),
                               _("The amount of bar"),
                               0, G_MAXINT,
                               ${MACRO_PREFIX}_DEFAULT_BAR,
                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
  g_object_class_install_property (object_class, PROP_BAR, param_spec);
}
#endif

static void
${METHOD_PREFIX}_init (${FULL_OBJECT_NAME} *${SHORT_OBJECT_NAME})
{
  ${SHORT_OBJECT_NAME}->priv = G_TYPE_INSTANCE_GET_PRIVATE (${SHORT_OBJECT_NAME}, 
                                                            ${MACRO_NAMESPACE}_TYPE_${MACRO_OBJECT_NAME},
                                                            ${FULL_OBJECT_NAME}Private);

}

static void
${METHOD_PREFIX}_finalize (GObject *object)
{
  ${FULL_OBJECT_NAME} *${SHORT_OBJECT_NAME};
  GObjectClass *parent_class;
  
  ${SHORT_OBJECT_NAME} = ${MACRO_PREFIX} (object);

  parent_class = G_OBJECT_CLASS (${METHOD_PREFIX}_parent_class);



  if (parent_class->finalize != NULL)
    parent_class->finalize (object);
}

#if 0
static void 
${METHOD_PREFIX}_set_property (GObject      *object,
                               guint         prop_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
  ${FULL_OBJECT_NAME} *${SHORT_OBJECT_NAME} = ${MACRO_PREFIX} (object);

  switch (prop_id)
    {
      case PROP_BAR:
        ${METHOD_PREFIX}_set_bar (${SHORT_OBJECT_NAME}, 
                                  g_value_get_int (value));
        break;
      
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void 
${METHOD_PREFIX}_get_property (GObject    *object,
                               guint       prop_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
  ${FULL_OBJECT_NAME} *${SHORT_OBJECT_NAME} = ${MACRO_PREFIX} (object);

  switch (prop_id)
    {
      case PROP_BAR:
            g_value_set_int (value, 
                             ${METHOD_PREFIX}_get_bar (${SHORT_OBJECT_NAME}));
        break;
      
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}
#endif

GQuark
${METHOD_PREFIX}_error_quark (void)
{
  static GQuark error_quark = 0;

  if (error_quark == 0)
    error_quark = g_quark_from_static_string ("${ERROR_QUARK}");

  return error_quark;
}

${FULL_OBJECT_NAME} *
${METHOD_PREFIX}_new (void)
{
  ${FULL_OBJECT_NAME} *${SHORT_OBJECT_NAME};

  ${SHORT_OBJECT_NAME} = g_object_new (${MACRO_NAMESPACE}_TYPE_${MACRO_OBJECT_NAME}, NULL);

  return ${SHORT_OBJECT_NAME};
}

#if 0
void
${METHOD_PREFIX}_set_bar (${FULL_OBJECT_NAME} *${SHORT_OBJECT_NAME},
                          int                  bar)
{
  if (${SHORT_OBJECT_NAME}->priv->bar != bar)
    {
      ${SHORT_OBJECT_NAME}->priv->bar = bar;
      g_object_notify (G_OBJECT (${SHORT_OBJECT_NAME}), "bar");
    }
}

int
${METHOD_PREFIX}_get_bar (${FULL_OBJECT_NAME} *${SHORT_OBJECT_NAME})
{
  return ${SHORT_OBJECT_NAME}->priv->bar;
}
#endif

#ifdef ${MACRO_PREFIX}_ENABLE_TEST

#include <stdio.h>
#include <glib.h>

int
main (int    argc,
      char **argv)
{
  ${FULL_OBJECT_NAME} *${SHORT_OBJECT_NAME};
  int exit_code;

  g_log_set_always_fatal (G_LOG_LEVEL_ERROR 
                          | G_LOG_LEVEL_CRITICAL 
			  | G_LOG_LEVEL_WARNING);

  g_type_init ();

  ${SHORT_OBJECT_NAME} = ${METHOD_PREFIX}_new ();

  g_object_unref (${SHORT_OBJECT_NAME});

  exit_code = 0;

  return exit_code;
}
#endif /* ${MACRO_PREFIX}_ENABLE_TEST */


uncrustify -c $(dirname $0)/default.cfg ${HEADER_FILENAME}
mv ${HEADER_FILENAME}.uncrustify ${HEADER_FILENAME}
uncrustify -c $(dirname $0)/default.cfg ${SOURCE_FILENAME}
mv ${SOURCE_FILENAME}.uncrustify ${SOURCE_FILENAME}