|
Boost : |
From: Goran Mitrovic (gmit_at_[hidden])
Date: 2004-08-24 17:19:29
Bronek Kozicki <brok <at> rubikon.pl> writes:
> Goran Mitrovic wrote:
> > However, I've just tried with latest Comeau compiler (maybe I shouldn't
> > believe them it's 100% standard compliant), with strict compiling mode
turned
> > on - and mine implementation works there!
> maybe you have just found defect in C++ standard or problem in Comeau
> compiler :) Care to send it for others to look at ?
I doubt, but, hey, why not after all...
You can declare my compile-time variable as:
declare_var_init(var1, 0)
get its value by:
get_var(var1)
update it by:
update_var(var1, 100 * get_var(var1) + 5)
and make invalid by:
invalid_var(var1)
You may repeat last three steps in any combinations (but only at the global
scope), have unimited number of variables and commands may be contained within
macros...
Note that by get_var() you will get a constant number, not an integer literal,
so, it's unusable for preprocessor library, for example, but it can happily be
used as an template argument.
Comments?
// Compile-time variables 1.0 by Goran Mitrovic (gmit_at_[hidden]) in 2004
#ifndef _compile_time_vars_h_
#define _compile_time_vars_h_
#define CTV_MAX_COUNTER_VALUE 1024
//#define CTV_MAX_COUNTER_STEP 128
#define ctv_namespace
::gmit::compile_time_vars
#define ctv_namespace_internal ctv_namespace::internal
namespace gmit {
namespace compile_time_vars {
namespace internal {
typedef unsigned int uint;
}
#ifdef CTV_MAX_COUNTER_VALUE
const ctv_namespace_internal::uint max_counter_value =
CTV_MAX_COUNTER_VALUE;
#else
const ctv_namespace_internal::uint max_counter_value = 1024;
#endif
#ifdef CTV_MAX_COUNTER_STEP
const ctv_namespace_internal::uint max_counter_step =
CTV_MAX_COUNTER_STEP;
#else
const ctv_namespace_internal::uint max_counter_step = 128;
#endif
namespace internal {
template<uint N>
class size {
char _size[N];
};
template<uint I>
class counter : public counter<I - 1> {
};
template<>
class counter<0> {
};
template<template<uint> class TYPE, int LAST, int STEP>
class bottom_up_explicit_instantiation : private
bottom_up_explicit_instantiation<TYPE, ((LAST - STEP) <= 0) ? 0 : (LAST -
STEP), STEP> {
TYPE<LAST> aa;
};
template<template<uint> class TYPE, int STEP>
class bottom_up_explicit_instantiation<TYPE, 0, STEP> {
};
#ifndef CTV_NO_BOTTOM_UP_EXPLICIT_INSTANTIATION
template class
bottom_up_explicit_instantiation<counter, max_counter_value, max_counter_step>;
#endif
typedef counter<max_counter_value> *probe;
}
}
}
#define form_counter_name(name) detect_##name
#define form_counter_init_name(name) init_##name
#define declare_counter_init(name, value)
\
namespace gmit {
\
namespace compile_time_vars {
\
namespace internal {
\
namespace {
\
size<1> form_counter_name(name)
(counter<0> *); \
const int form_counter_init_name(name)
= (value); \
}
\
}
\
}
\
}
#define declare_counter(name)
\
declare_counter_init(name, 0)
#define current_counter_unbiased(name)
\
(sizeof(ctv_namespace_internal::form_counter_name(name)
(ctv_namespace_internal::probe())) - 1)
#define current_counter(name)
\
((int)(current_counter_unbiased(name) +
ctv_namespace_internal::form_counter_init_name(name)))
#define hit_counter_by(name, value)
\
namespace gmit {
\
namespace compile_time_vars {
\
namespace internal {
\
namespace {
\
size<current_counter_unbiased(name) +
1 + (value)> form_counter_name(name)(counter<current_counter_unbiased(name) +
(value)> *); \
}
\
}
\
}
\
}
#define hit_counter(name)
\
hit_counter_by(name, 1)
#define form_ctv_name(name) compile_time_var_##name
#define form_ctv_counter_name(name) form_counter_name(__ctv_##name)
#define update_var(name, value)
\
namespace gmit {
\
namespace compile_time_vars {
\
namespace internal {
\
namespace {
\
template<>
\
class form_ctv_name(name)
##<current_counter(form_ctv_counter_name(name)) + 1> { \
public:
\
enum {
\
my_value = (value)
\
};
\
};
\
}
\
}
\
}
\
}
\
hit_counter(form_ctv_counter_name(name))
#define offset_var(name, offset)
\
update_var(name, get_var(name) + offset)
#define invalid_var_internal(name)
\
namespace gmit {
\
namespace compile_time_vars {
\
namespace internal {
\
namespace {
\
template<>
\
class form_ctv_name(name)
##<current_counter(form_ctv_counter_name(name))> { \
};
\
}
\
}
\
}
\
}
#define invalid_var(name)
\
hit_counter(form_ctv_counter_name(name))
\
invalid_var_internal(name)
#define get_var(name)
\
((int)(ctv_namespace_internal::form_ctv_name(name)##<current_counter
(form_ctv_counter_name(name))>::my_value))
#define declare_var(name)
\
declare_counter(form_ctv_counter_name(name))
\
namespace gmit {
\
namespace compile_time_vars {
\
namespace internal {
\
namespace {
\
template<int ID>
\
class form_ctv_name(name) {
\
};
\
}
\
}
\
}
\
}
#define declare_var_init(name, value)
\
declare_var(name)
\
update_var(name, value)
#endif // !_compile_time_vars_h_
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk