CCC (C with Condition Classes)


CCC (C with Condition Classes) is a small extension of C. It features user-level object structure definitions, conditional-style dispatching, multi-methods, and class scoped macros.

Let's consider an example.
L 10 20 11 25 R 5 10 11 ... ... ... ...
This is an array of integer that encodes figure data, letter L with following 4 number expresses a line data and letter R with following 4 number expresses a rectangle data. Such special data expression can not treat an object in traditional OOPLs, like C++. CCC can treat this as an object. This is an example.
@class object (int *array, int idx) {
  @class line if (array[idx] == 'L') {
    macro X1 {array[idx+1]}
    macro Y1 {array[idx+2]}
    macro X2 {array[idx+3]}
    macro Y2 {array[idx+4]}
  }
  @class rect if (array[idx] == 'R') {
    macro X {array[idx+1]}
    macro Y {array[idx+2]}
    macro W {array[idx+3]}
    macro H {array[idx+4]}
  }
}
array and idx are class arguments. line and rect are subclasses of class object . Each has a condition, like M(0) == 'L' . macro X1 {array[idx+1]} is a class local macro definition. This is only accessed in the class and its subclasses. These macros abstract an object representation, and can work like instance variables. After that, we can define methods in these classes, like OOPLs.
@+line {
  void draw(Window w) {
    drawLine(w, X1, Y1, X2, Y2);
  }
}
@+rect {
  void draw(Window w) {
    drawLine(w, X,   Y,   X+W,   Y);
    drawLine(w, X+W, Y,   X+W, Y+H);
    drawLine(w, X+W, Y+H,   X, Y+H);
    drawLine(w, X,   Y+H,   X,   Y);
  }
}
Then, a CCC compiler generates C code,
void line_draw(int *array, int idx, Window w) {
  drawLine(w, array[idx+1], array[idx+2], array[idx+3], array[idx+4]);
}
void rect_draw(int *array, int idx, Window w) {
  drawLine(w, array[idx+1], array[idx+2],
              array[idx+1]+array[idx+3], array[idx+2]+array[idx+4]);
  ...
}
void draw(int *array, int idx, Window w) {
  if (array[idx]=='L') {
    line_draw(array, idx, w);
    return;
  }
  if (array[idx]=='R') {
    rect_draw(array, idx, w);
    return;
  }
}


Reference
Harada, Yamazaki, Potter: CCC : user defined object structure in C , ECOOP 2001. presentation slide
Here is a trial version of CCC (version 0.01 with very cheep document).
ccc.tgz Feedback: hara@brl.ntt.co.jp
HARADA Yasunori's home page