static struct table *table_make_instance(const struct table_template *tbl_template)
{
struct mempool *pool = mp_new(4096);
- struct table *new_inst = mp_alloc_zero(pool, sizeof(struct table)); // FIXME: update allocation to the weird schema made by pchar and mj?
+ struct table *new_inst = mp_alloc_zero(pool, sizeof(struct table));
- new_inst->pool = mp_new(4096);
+ new_inst->pool = pool;
// initialize column definitions
int col_count = 0; // count the number of columns in the struct table
void table_cleanup(struct table *tbl)
{
mp_delete(tbl->pool);
- memset(tbl, 0, sizeof(struct table));
}
// TODO: test default column order
} TBL_COL_ITER_END
}
+void table_col_generic_format(struct table *tbl, int col, void *value, const struct xtype *expected_type)
+{
+ ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
+ ASSERT(tbl->columns[col].type_def == COL_TYPE_ANY || expected_type == tbl->columns[col].type_def);
+ tbl->last_printed_col = col;
+ tbl->row_printing_started = 1;
+ const char *cell_content = NULL;
+ TBL_COL_ITER_START(tbl, col, curr_col, curr_col_idx) {
+ enum xtype_fmt fmt = curr_col->output_type;
+ cell_content = expected_type->format(value, fmt, tbl->pool);
+ curr_col->cell_content = cell_content;
+ } TBL_COL_ITER_END
+}
+
void table_col_printf(struct table *tbl, int col, const char *fmt, ...)
{
ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
va_end(args);
}
-TABLE_COL_BODIES(int, int, COL_TYPE_INT)
-TABLE_COL_BODIES(uint, uint, COL_TYPE_UINT)
-TABLE_COL_BODIES(str, const char *, COL_TYPE_STR)
-TABLE_COL_BODIES(intmax, intmax_t, COL_TYPE_INTMAX)
-TABLE_COL_BODIES(uintmax, uintmax_t, COL_TYPE_UINTMAX)
-TABLE_COL_BODIES(s64, s64, COL_TYPE_S64)
-TABLE_COL_BODIES(u64, u64, COL_TYPE_U64)
-TABLE_COL_BODIES(double, double, COL_TYPE_DOUBLE)
+TABLE_COL_BODY(int, int)
+TABLE_COL_BODY(uint, uint)
+TABLE_COL_BODY(double, double)
+TABLE_COL_BODY(intmax, intmax_t)
+TABLE_COL_BODY(uintmax, uintmax_t)
+TABLE_COL_BODY(s64, s64)
+TABLE_COL_BODY(u64, u64)
+TABLE_COL_BODY(bool, bool)
-TABLE_COL(bool, bool, COL_TYPE_BOOL)
-TABLE_COL_STR(bool, bool, COL_TYPE_BOOL)
-TABLE_COL_FMT(bool, bool, COL_TYPE_BOOL)
+void table_col_str(struct table *tbl, int col, const char *val) {
+ table_col_generic_format(tbl, col, (void*)val, &xt_str);
+}
void table_reset_row(struct table *tbl)
{
table_col_int(test_tbl, test_col1_int, 10000);
table_col_uint(test_tbl, test_col2_uint, 10);
table_col_printf(test_tbl, test_col2_uint, "XXX-%u", 22222);
- table_col_bool(test_tbl, test_col3_bool, 1);
+ table_col_bool(test_tbl, test_col3_bool, true);
table_col_double(test_tbl, test_col4_double, 1.5);
table_col_printf(test_tbl, test_col4_double, "AAA");
table_end_row(test_tbl);
table_col_str(test_tbl, test_col0_str, "test");
table_col_int(test_tbl, test_col1_int, -100);
table_col_uint(test_tbl, test_col2_uint, 100);
- table_col_bool(test_tbl, test_col3_bool, 0);
+ table_col_bool(test_tbl, test_col3_bool, false);
table_col_printf(test_tbl, test_col4_double, "%.2lf", 1.5);
table_end_row(test_tbl);
}
* recently accessed cell.
***/
-#define TABLE_COL_PROTO(_name_, _type_) void table_col_##_name_(struct table *tbl, int col, _type_ val);\
- void table_col_##_name_##_name(struct table *tbl, const char *col_name, _type_ val);\
- void table_col_##_name_##_fmt(struct table *tbl, int col, enum xtype_fmt fmt, _type_ val);
+
+#define TABLE_COL_PROTO(_name_, _type_) void table_col_##_name_(struct table *tbl, int col, _type_ val);
// table_col_<type>_fmt has one disadvantage: it is not possible to
// check whether fmt contains format that contains formatting that is
// compatible with _type_
-TABLE_COL_PROTO(int, int);
-TABLE_COL_PROTO(uint, uint);
-TABLE_COL_PROTO(double, double);
-TABLE_COL_PROTO(str, const char *);
-TABLE_COL_PROTO(intmax, intmax_t);
-TABLE_COL_PROTO(uintmax, uintmax_t);
-TABLE_COL_PROTO(s64, s64);
-TABLE_COL_PROTO(u64, u64);
-TABLE_COL_PROTO(bool, bool);
+TABLE_COL_PROTO(int, int)
+TABLE_COL_PROTO(uint, uint)
+TABLE_COL_PROTO(double, double)
+TABLE_COL_PROTO(intmax, intmax_t)
+TABLE_COL_PROTO(uintmax, uintmax_t)
+TABLE_COL_PROTO(s64, s64)
+TABLE_COL_PROTO(u64, u64)
+TABLE_COL_PROTO(bool, bool)
-/** macros that enables easy definitions of bodies of table_col_<something> functions **/
-// FIXME: these macros really do not do what they are supposed to do :)
-// the problem is the default formatting
-
-#define TABLE_COL(_name_, _type_, _typeconst_) void table_col_##_name_(struct table *tbl, int col, _type_ val)\
- {\
- ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);\
- ASSERT(tbl->columns[col].type_def == COL_TYPE_ANY || _typeconst_ == tbl->columns[col].type_def);\
- tbl->last_printed_col = col;\
- tbl->row_printing_started = 1;\
- const char *cell_content = NULL;\
- TBL_COL_ITER_START(tbl, col, curr_col, curr_col_idx) {\
- if(tbl->columns[col].type_def != COL_TYPE_ANY) cell_content = tbl->columns[col].type_def->format(&val, curr_col->output_type, tbl->pool);\
- else cell_content = (_typeconst_)->format(&val, curr_col->output_type, tbl->pool);\
- curr_col->cell_content = cell_content;\
- } TBL_COL_ITER_END\
- }
+void table_col_str(struct table *tbl, int col, const char * val);
-#define TABLE_COL_STR(_name_, _type_, _typeconst_) void table_col_##_name_##_name(struct table *tbl, const char *col_name, _type_ val)\
- {\
- int col = table_get_col_idx(tbl, col_name);\
- table_col_##_name_(tbl, col, val);\
- }
+/** macros that enables easy definitions of bodies of table_col_<something> functions **/
-#define TABLE_COL_FMT(_name_, _type_, _typeconst_) void table_col_##_name_##_fmt(struct table *tbl, int col, enum xtype_fmt fmt, _type_ val) \
- {\
- ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);\
- ASSERT(tbl->columns[col].type_def == COL_TYPE_ANY || _typeconst_ == tbl->columns[col].type_def);\
- tbl->last_printed_col = col;\
- tbl->row_printing_started = 1;\
- const char *cell_content = NULL;\
- if(tbl->columns[col].type_def != COL_TYPE_ANY) cell_content = tbl->columns[col].type_def->format(&val, fmt, tbl->pool);\
- else cell_content = (_typeconst_)->format(&val, fmt, tbl->pool);\
- TBL_COL_ITER_START(tbl, col, curr_col, curr_col_idx) {\
- curr_col->cell_content = cell_content;\
- } TBL_COL_ITER_END\
+#define TABLE_COL_BODY(_name_, _type_) void table_col_##_name_(struct table *tbl, int col, _type_ val) {\
+ table_col_generic_format(tbl, col, (void*)&val, &xt_##_name_);\
}
-#define TABLE_COL_BODIES(_name_, _type_, _typeconst_) TABLE_COL(_name_, _type_, _typeconst_); \
- TABLE_COL_STR(_name_, _type_, _typeconst_);\
- TABLE_COL_FMT(_name_, _type_, _typeconst_);
-
-// FIXME: the next line was removed, the question is whether it should
-// be there. Currently it is impossible to undef the macro.
-// #undef TABLE_COL_PROTO
+void table_col_generic_format(struct table *tbl, int col, void *value, const struct xtype *expected_type);
/**
* Set a particular cell of the current row to a string formatted