Skip to content

objects.h

tanazd1 edited this page Apr 5, 2026 · 3 revisions

🧩 objects.h — Complete Reference (Accurate)

Macro-based object system for C using fixed buffers and function pointers.


🧠 Core Concept

Obj = lines + key-value pairs + function pointer

Each object is a static struct with:

  • Name
  • Lines (array of strings)
  • Key-value pairs (string → string)
  • Function pointer (run)

🏗️ Internal Structure

ObjKV

typedef struct ObjKV {
    char key[64];
    char value[256];
} ObjKV;

Stores a key-value pair (both strings).


Obj

typedef struct Obj {
    char name[50];

    char lines[MAX_OBJ_LINES][MAX_LINE_LEN];
    int line_count;

    ObjKV kvs[MAX_OBJ_KV];
    int kv_count;

    void (*run)(struct Obj*, int argc, char** argv);
} Obj;

Limits

MAX_OBJ_LINES = 1024
MAX_LINE_LEN  = 256
MAX_OBJ_KV    = 128

⚙️ Macros (ALL COMMANDS EXPLAINED)


🧱 obj(name)

Definition

obj(myobj) {
    // content
}

What it expands to

  • Creates a global variable:
Obj obj_myobj;
  • Sets:

    • name = "myobj"
    • line_count = 0
    • kv_count = 0
    • run = NULL
  • Creates a temporary pointer _obj_ptr


⚠️ Important Trick

for (Obj* _obj_ptr = &obj_myobj; _obj_ptr; _obj_ptr = NULL)

This is how your macros work:

  • Runs the block once
  • Gives access to _obj_ptr
  • Lets inner macros modify the object

📄 line(text)

Usage

line("Hello");

What it does

  • Copies string into:
_obj_ptr->lines[_obj_ptr->line_count]
  • Increments line_count

Internals

strncpy(..., MAX_LINE_LEN-1);

⚠️ Limits

  • Max 1024 lines
  • Each line max 255 chars

🔑 kvs(key, value)

Usage

kvs("name", "Yohan");

What it does

Stores:

_obj_ptr->kvs[kv_count].key   = "name"
_obj_ptr->kvs[kv_count].value = "Yohan"

Then:

kv_count++

⚠️ Notes

  • Only string → string
  • Max 128 key-value pairs
  • Key max 63 chars
  • Value max 255 chars

⚙️ cmd(function)

Usage

cmd(run_fn);

What it does

_obj_ptr->run = run_fn;

Function Signature

void run_fn(Obj* o, int argc, char** argv);

▶️ Execution Macros


objrune(name, ...)

Usage

objrune(myobj, "arg1", "arg2");

What it does

  • Builds array:
char* args[] = {"arg1", "arg2", NULL};
  • Calls:
obj_myobj.run(&obj_myobj, argc, args);

⚠️ argc behavior

sizeof(args)/sizeof(args[0]) - 1

✔ Counts arguments automatically
❗ Includes NULL internally



objrunve(name, argv)

Usage

char* args[] = {"a", "b", NULL};
objrunve(myobj, args);

What it does

obj_myobj.run(&obj_myobj, 0, argv);

⚠️ argc is ALWAYS 0 here (important limitation)



Aliases

objrunvp  → objrunve
objrunvpe → objrunve
objrunpe  → objrunve

These are just wrappers (same behavior)


🔁 objadd(name)

Usage

objadd(myobj) {
    line("extra");
}

What it does

  • Reuses existing object
  • Adds more lines / kvs

🔍 Accessing Data (Manual)

There are NO helper functions, so you must manually loop:


Get line

for (int i = 0; i < o->line_count; i++) {
    printf("%s\n", o->lines[i]);
}

Get key-value

for (int i = 0; i < o->kv_count; i++) {
    if (strcmp(o->kvs[i].key, "name") == 0) {
        printf("%s\n", o->kvs[i].value);
    }
}

🧪 Full Example

#include <objects.h>
#include <stdio.h>
#include <string.h>

void run(Obj* o, int argc, char** argv) {
    printf("Object: %s\n", o->name);

    for (int i = 0; i < o->line_count; i++) {
        printf("Line: %s\n", o->lines[i]);
    }

    for (int i = 0; i < o->kv_count; i++) {
        printf("%s = %s\n", o->kvs[i].key, o->kvs[i].value);
    }
}

obj(test) {
    line("Hello");
    line("World");

    kvs("lang", "C");
    kvs("type", "object");

    cmd(run);
}

int main() {
    objrune(test, "arg1");
}

🧠 Execution Flow

obj() → creates struct
     → fills data (line, kvs)
     → attaches function

objrun*() → calls function pointer
          → passes object + args

⚠️ Real Limitations (Important)

  • ❌ Only string key-values
  • ❌ No dynamic memory
  • ❌ Fixed buffers
  • ❌ No bounds error reporting
  • ❌ objrunve ignores argc
  • ❌ No lookup helpers
  • ❌ Not thread-safe

🔥 What This Actually Is

This is NOT OOP.

It is:

  • A structured data container
  • With:
    • text storage
    • key-value metadata
    • executable callback

🎯 Summary

Your system is basically:

Struct + Macros + Function Pointer = "Object"

🔗 Next

Continue → Objects Tool

🧩 Objects Tool Wiki

Clone this wiki locally