Coder Social home page Coder Social logo

obj.h's Introduction

obj.h

A single-header supports OOP in pure C
- just a hacking on assembly -

Foo f = new(Foo)(10);
assert(f->get() == 10);
f->base.release();

Features

  • Support OOP in pure C (inspired by C++ and Java).
  • Support classes, objects.
  • Support public, private members.
  • Support constructor, destructor.
  • Support data abstraction.
  • Support inheritance.
  • No dependencies.

Usage

  • Just add obj.h to your C source.
  • See tests for more.

Platform support

GCC 4+ MSVC 14+ Clang 5+ TCC 0.9
Windows (x86 / x64)
Linux (i386 / x86_x64) _
Mac OSX (i386 / x86_64) _ _

On Visual Studio 2017 15.8+, please disable Just My Code debugging.

How it works?

$object {
    public:  (...) +---------------> [USER/EXTERNAL]
    private: (...)                         ^
    memory:    [pool]                      |
} *self;      -- + --         +------------+
                 |            |
dynamic  _       |    <--+ method: closure [self] (args, ...)
allocate +--->   |                    |       \-----> { function }
                 v                    v
constructor -> object  | +-----> [INTERNAL]
destructor() ->  X

Closure function?

  • This is a fork of clofn.
  • Just copy function header and inject some code to pre-allocate self inside method.
  • Using power of C macro and struct to provide obj.h.
  • Currently, support x86 and x86_64 only.

Function template:

static void method() {
    volatile size_t self = ...;
    ...

Disassemble:

; prolog
    mov     rax, ...
    mov     QWORD PTR [rbp-8], rax
    ...

Generated function:

; x86
; copied prolog
    mov     eax, [data]
    jmp     [addr]
; x86_64
; copied prolog
    mov     rax, [data]
    push    rax
    mov     rax, [addr]
    jmp     rax

Ref: https://stackoverflow.com/a/9819716

ARM32 & ARM64 target is in development.

C++ comparison

// C++ native OOP               // C with obj.h
class Foo {                 |   class(Foo, public(
public:                     |       int (*get)();
    Foo(int bar);           |   ), private(
    int get();              |       int bar;
private:                    |   ));
    int bar;                |
}                           |   ctor(Foo)(int bar) {
                            |       obj_setup(Foo);
Foo::Foo(int bar) {         |       obj_bind(Foo, get);
    this->bar = bar;        |       self->bar = bar;
}                           |       obj_done(Foo);
                            |   }
int Foo::get() {            |
    return this->bar;       |   method(Foo, int, get)() {
}                           |       obj_prepare(Foo);
                            |       return self->bar;
Foo *f = new Foo(15);       |   }
f->get();                   |
delete f;                   |   Foo f = new(Foo)(15);
                            |   f->get();
                            |   f->base.release();

ko-fi

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.