Skip to content

Instantly share code, notes, and snippets.

@zlargon
Last active March 22, 2017 00:21
Show Gist options
  • Save zlargon/823f1bb011b0fd095f37523c4385fae5 to your computer and use it in GitHub Desktop.
Save zlargon/823f1bb011b0fd095f37523c4385fae5 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
// Number
typedef struct {
int value;
} Number;
Number new (int value);
void add (Number * ctx, int value);
void sub (Number * ctx, int value);
// Number API
struct {
Number (* new) (int value);
void (* add) (Number * ctx, int value);
void (* sub) (Number * ctx, int value);
} Number_API = {
.new = new,
.add = add,
.sub = sub,
};
// #MAT
// MAT(matA).mul(matB)
// using string cat (##)
// #NUM
// Number_API.add(A, B)
// NUM(A).add(B)
// TODO: 請問這裡該如何實作?
#define NUM(n) Number_API
// Main
int main () {
Number n = Number_API.new(100);
printf("%d\n", n.value); // 100
Number_API.add(&n, 10);
printf("%d\n", n.value); // 110
NUM(n).add(10); // TODO
// n.add(10); // TODO
printf("%d\n", n.value); // 120
return 0;
}
// Number_API Implement
Number new (int value) {
return (Number) {
.value = value,
};
}
void add (Number * ctx, int value) {
ctx->value += value;
};
void sub (Number * ctx, int value) {
ctx->value -= value;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment