Add custom GObject template
This commit is contained in:
parent
143ac48982
commit
251be4803b
2
c/.gitignore
vendored
Normal file
2
c/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
*.user
|
||||
build-*
|
3
c/README.MD
Normal file
3
c/README.MD
Normal file
@ -0,0 +1,3 @@
|
||||
# C Templates
|
||||
## gobject_template
|
||||
The basic framework of a custom GObject. Including CMakeList and everything.
|
17
c/gobject-template/CMakeLists.txt
Normal file
17
c/gobject-template/CMakeLists.txt
Normal file
@ -0,0 +1,17 @@
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
|
||||
project(gobject-template)
|
||||
#set(CMAKE_BUILD_TYPE Release)
|
||||
set(CMAKE_C_FLAGS "-Wall")
|
||||
|
||||
find_package(PkgConfig REQUIRED)
|
||||
pkg_check_modules(GLIB2 REQUIRED glib-2.0)
|
||||
pkg_check_modules(GOBJECT2 REQUIRED gobject-2.0)
|
||||
|
||||
INCLUDE_DIRECTORIES (${GLIB2_INCLUDE_DIRS} ${GOBJECT2_INCLUDE_DIRS})
|
||||
LINK_DIRECTORIES (${GLIB2_LIBRARY_DIRS} ${GOBJECT2_LIBRARY_DIRS})
|
||||
add_definitions(${GLIB2_CFLAGS_OTHER} ${GOBJECT2_CFLAGS_OTHER})
|
||||
|
||||
aux_source_directory(./src SRC_LIST)
|
||||
add_executable(${PROJECT_NAME} ${SRC_LIST})
|
||||
target_link_libraries(${PROJECT_NAME} ${GLIB2_LIBRARIES} ${GOBJECT2_LIBRARIES})
|
20
c/gobject-template/src/main.c
Normal file
20
c/gobject-template/src/main.c
Normal file
@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
#include "my_gobj.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
MyGobj *obj, *obk;
|
||||
|
||||
fprintf(stderr, "DECLARED.\n");
|
||||
|
||||
obj = my_gobj_new();
|
||||
obk = my_gobj_new();
|
||||
|
||||
fprintf(stderr, "CREATED.\n");
|
||||
|
||||
g_object_unref(obj);
|
||||
g_object_unref(obk);
|
||||
|
||||
fprintf(stderr, "DESTROYED.\n");
|
||||
return 0;
|
||||
}
|
55
c/gobject-template/src/my_gobj.c
Normal file
55
c/gobject-template/src/my_gobj.c
Normal file
@ -0,0 +1,55 @@
|
||||
#include "my_gobj.h"
|
||||
|
||||
struct _MyGobj {
|
||||
GObject parent_instance;
|
||||
|
||||
// Private data goes here
|
||||
};
|
||||
|
||||
G_DEFINE_TYPE (MyGobj, my_gobj, G_TYPE_OBJECT)
|
||||
|
||||
static void my_gobj_dispose (GObject *gobject)
|
||||
{
|
||||
MyGobj *obj = my_gobj_get_instance_private(MY_GOBJ(gobject));
|
||||
|
||||
// Called multiple times after last unref
|
||||
|
||||
G_OBJECT_CLASS(my_gobj_parent_class)->dispose(gobject);
|
||||
}
|
||||
|
||||
static void my_gobj_finalize (GObject *gobject)
|
||||
{
|
||||
MyGobj *obj = my_gobj_get_instance_private(MY_GOBJ(gobject));
|
||||
|
||||
// Called once after last unref
|
||||
|
||||
G_OBJECT_CLASS(my_gobj_parent_class)->finalize(gobject);
|
||||
}
|
||||
|
||||
static void my_gobj_constructed (GObject *gobject)
|
||||
{
|
||||
// Called once per g_object_new
|
||||
G_OBJECT_CLASS(my_gobj_parent_class)->constructed(gobject);
|
||||
}
|
||||
|
||||
static void my_gobj_class_init(MyGobjClass *klass)
|
||||
{
|
||||
GObjectClass *object_class = G_OBJECT_CLASS(klass);
|
||||
|
||||
object_class->dispose = my_gobj_dispose;
|
||||
object_class->finalize = my_gobj_finalize;
|
||||
object_class->constructed = my_gobj_constructed;
|
||||
|
||||
// Called once upon first g_object_new of this type
|
||||
}
|
||||
|
||||
static void my_gobj_init(MyGobj *self)
|
||||
{
|
||||
// Called once per g_object_new
|
||||
}
|
||||
|
||||
MyGobj *my_gobj_new(void)
|
||||
{
|
||||
return g_object_new (MY_TYPE_GOBJ,
|
||||
0);
|
||||
}
|
15
c/gobject-template/src/my_gobj.h
Normal file
15
c/gobject-template/src/my_gobj.h
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef MY_GOBJ_H
|
||||
#define MY_GOBJ_H
|
||||
|
||||
#include <glib-object.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#define MY_TYPE_GOBJ my_gobj_get_type()
|
||||
G_DECLARE_FINAL_TYPE (MyGobj, my_gobj, MY, GOBJ, GObject)
|
||||
|
||||
MyGobj *my_gobj_new(void);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif // MY_GOBJ_H
|
Loading…
Reference in New Issue
Block a user