Previous: , Up: dumpable   [Contents][Index]


5.3 Example of dumpable trait implementation

Let’s say we have this struct definition:

typedef struct spiffy_t        spiffy_t;

struct spiffy_t {
  int   alpha;
  int   beta;
};

we can implement a dumpable trait as follows:

static void
ccname_trait_method(ccstructs_dumpable_T, spiffy_t, dump)
  (cce_destination_t L, ccstructs_dumpable_T I)
{
  CCSTRUCTS_PC(spiffy_t, S, I.self);
  int   rv;

  errno = 0;
  rv = fprintf(stderr, "spiffy_t: alpha=%d, beta=%d\n", S->alpha, S->beta);
  if (0 > rv) {
    cce_raise(L, cce_condition_new_errno_clear());
  }
}

static ccname_trait_table_type(ccstructs_dumpable_T) const
    ccname_trait_table(ccstructs_dumpable_T, spiffy_t) = {
  .dump = ccname_trait_method(ccstructs_dumpable_T, spiffy_t, dump)
};

ccstructs_dumpable_T
ccname_trait_new(ccstructs_dumpable_T, spiffy_t) (spiffy_t * S)
{
  return ccname_trait_new(ccstructs_dumpable_T)(ccstructs_core(S),
    &ccname_trait_table(ccstructs_dumpable_T, spiffy_t));
}

then we can use the trait as follows:

cce_location_t        L[1];

if (cce_location(L)) {
  cce_run_catch_handlers_final(L);
} else {
  spiffy_t   S = {
    .alpha    = 1,
    .beta     = 2
  };
  ccstructs_dumpable_T  I = ccname_trait_new(ccstructs_dumpable_T, spiffy_t)(&S);

  ccstructs_dumpable_dump(L, I);
  cce_run_body_handlers(L);
}

Previous: , Up: dumpable   [Contents][Index]

This document describes version 0.3.0-devel.3 of CCStructs.