Add compile time mutable variable type
Chang Long
changlon at gmail.com
Mon Jun 4 23:27:54 PDT 2012
On Tuesday, 5 June 2012 at 05:39:34 UTC, Ali Çehreli wrote:
> On 06/04/2012 06:22 PM, Chang Long wrote:
>
> > The previous two example is not correct, please see this one:
>
> Your post sounds interesting but there are lots of errors:
>
> > size_t line l; <-- ERROR
>
> > void this(string name) { <-- ERROR
>
> etc. Can you actually compile that code?
>
> It would be better if you can present your question with less
> code.
>
> Ali
this is what I mean, I can't make it more smaller.
-----------------------------------------------------------
abstract class Template_parameter_Base{
TypeInto ti;
string name ;
this(TypeInto ti, string name) {
this.ti = ti ;
this.name = name ;
}
}
class Template_parameter(T) : Template_parameter_Base {
T value ;
alias value this;
void assign(ref T t){
value = t ;
}
}
class Template_Engine {
string name ;
Template_parameter_Base[string] parameters ;
void this(string name) {
this.name = name ;
}
}
class Template(string name) {
static compile_time_mutable(Template_Engine) engine = new
Template_Engine!name ;
void assign(string name, T)(ref T t){
static if(name in vars) {
static const parameter = cast(Template_parameter!T)
engine.parameters[name];
static assert( parameter.ti == typeid(T) );
} else {
static const parameter = new Template_parameter!T(typeid(T),
name) ;
static engine.parameters[name] = parameter ;
}
parameter.assign(t);
}
}
void main(){
static tpl = new Template!"home_page" ;
auto is_login = false ;
tpl.assign!"is_login"( is_login) ) ;
if( is_login ) {
tpl.assign!"user"( new User() ) ;
}
auto string switch_user = "new_user_email" ;
if( switch_user !is null ) {
tpl.assign!"user"( new User(switch_user) ) ;
}
tpl.assign!"user"( true ) ; // static assert error
}
-----------------------------------------------------------
After think what is done, a better way is put the
Template_parameter on the annotation data section.
some things like this:
class Template_Engine(string name) {
static this_ti = typeid( typeof(this) ) ;
void assign(string name, T)(ref T t){
static const parameter = new Template_parameter!(T,
name) ;
ti.createAnnotation("Template_Engine_Parameter",
parameter);
parameter.assign(t);
}
void render(){
auto parameters =
this_ti.getParametersByName("Template_Engine_Parameter") ;
// compile template to dynamic link library by
template file and parameters
}
}
More information about the Digitalmars-d
mailing list