Giter VIP home page Giter VIP logo

cobject's Introduction

CObject

CObject is a library that adds support for object-orientation with inheritance, polymorphism, virtual destructors and reference counting to C.

Usage

In CObject, an object is a struct allocated on the stack or the heap that starts with a “guts” struct that contains a pointer to the class as well as the reference count. A class is a struct that contains the size of its instances, a reference to the superclass and a function pointer to the destructor that will be called when the reference count reaches zero.

To prepare an object for use with CObject, set its first member to COGuts, like this:

struct foo
{
	COGuts guts;

	// ... other variables go here ...
	int *sampleArray;
}

You will also need a class definition. Classes are COClass structs which can be defined like this:

COClass fooClass = {
	.destructor = &_fooDestroy,
	.superclass = NULL,
	.size       = sizeof (struct foo),
};

The destructor contains a pointer to a function that will be called when the instance will be deleted. This function should not return anything and should take one void * as argument, the pointer to the object being deallocated. The destructor should undo anything allocated/retained during initalization. It should not free the struct itself. For instance:

static void _fooDestroy(void *sFoo)
{
	struct foo *foo = aFoo;
	free(foo->sampleArray);
}

The superclass contains a pointer to the class definition of the superclass. When an object is deallocated, the destructor of its actual class will be called first, then the destructor of the superclass, and so on until no superclass can be found anymore.

The size contains the size of the struct that should be allocated. This is useful for functions that may need to allocate more memory for subclass instances.

Before an object can be used, it needs to be initialized first. This will initialize the reference count to 1, and it will also set the pointer to the given class. This is done using COInit, which takes a pointer to the struct (or to the guts, which is the same) and a pointer to the class definition. For example:

struct foo stackFoo;
COInit(&stackFoo, &fooClass);

If you are allocating an object on the heap, you must use COCreate instead of allocating the object manually and releasing it. For example:

struct foo *heapFoo = COCreate(&fooClass);

COInit (and COCreate which uses it) does not fully initialize your object. You will need to do that yourself. For instance:

stackFoo.sampleArray = calloc(100, sizeof (int));
heapFoo->sampleArray = calloc(100, sizeof (int));

At this point, the CORetain and CORelease functions can be used to retain and release objects. Objects start with a reference count of 1. When the reference count reaches zero, the destructor will be called and, in case the object is located on the heap, the object will be freed.

It is important even for stack-allocated objects that its reference count is zero when the object exits scope because its destructor will not be called otherwise. It is useful for stack-allocated to be marked using CO_OBJECT. When using a compiler with GCC extensions (such as GCC itself or Clang), a warning will be emitted when a stack-allocated object exits automatic scope with a nonzero reference count.

Example

See src/test/test.c for an example.

cobject's People

Contributors

denisdefreyne avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.