www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Q: Help with compile time function

reply Myron Alexander <someone somewhere.com> writes:
Hello.

I want to check that a structure contains only the supported types for a 
fetch from the database. I want to check this at compile time and issue 
an assert. I have come up with the following:

 /**
  * Compile time check that structure field types match supported types for
  * structure fetch.
  *
  * Supported types :-
  *    int (int32), long (int64), char[] (utf-8), void[] (blob), float, double.
  *
  * Unsupported types :-
  *    wchar[] (utf-16)
  *    void*   (null)
  *
  * It does not make sense to support null as a value return type so it will
  * never be supported. The utf16 type is not supported at this time as I do
  * not know how to support it. Only partial support for utf16 in the system.
  */
 static void chkStrucOkForFetch(T) () {
    foreach (i, f; FieldTypeTuple!(T)) {
       static assert (
          is (typeof(f) == int)    || // int32
          is (typeof(f) == long)   || // int64
          is (typeof(f) == char[]) || // utf-8
          is (typeof(f) == void[]) || // blob
          is (typeof(f) == float)  ||
          is (typeof(f) == double)
          , "Invalid field type for structure fetch. Field ("~str(i)~
            ") not one of int, long, char[], void[], float, double."
       );
    }
 }
This code performs the check but I cannot get it to print the name of the structure and the field. Please can you help me with the following: - How can I get the name of the structure (T) and field type at compile time? - If I call this from a normal run-time function, will it get executed at run-time as well as in compile-time? Thanks and best regards, Myron.
Jun 23 2007
parent reply Jari-Matti =?ISO-8859-1?Q?M=E4kel=E4?= <jmjmak utu.fi.invalid> writes:
Myron Alexander wrote:

 Hello.
 
 I want to check that a structure contains only the supported types for a
 fetch from the database. I want to check this at compile time and issue
 an assert. I have come up with the following:
 
 /**
  * Compile time check that structure field types match supported types
  for * structure fetch.
  *
  * Supported types :-
  *    int (int32), long (int64), char[] (utf-8), void[] (blob), float,
  double. *
  * Unsupported types :-
  *    wchar[] (utf-16)
  *    void*   (null)
  *
  * It does not make sense to support null as a value return type so it
  will * never be supported. The utf16 type is not supported at this time
  as I do * not know how to support it. Only partial support for utf16 in
  the system. */
 static void chkStrucOkForFetch(T) () {
    foreach (i, f; FieldTypeTuple!(T)) {
       static assert (
          is (typeof(f) == int)    || // int32
          is (typeof(f) == long)   || // int64
          is (typeof(f) == char[]) || // utf-8
          is (typeof(f) == void[]) || // blob
          is (typeof(f) == float)  ||
          is (typeof(f) == double)
          , "Invalid field type for structure fetch. Field ("~str(i)~
            ") not one of int, long, char[], void[], float, double."
       );
    }
 }
This code performs the check but I cannot get it to print the name of the structure and the field. Please can you help me with the following: - How can I get the name of the structure (T) and field type at compile time?
You mean T.stringof and f.stringof ?
    - If I call this from a normal run-time function, will it get
 executed at run-time as well as in compile-time?
I'm afraid that function above doesn't do anything on runtime. You need to use the RTTI system to get the type at runtime.
 
 Thanks and best regards,
 
 Myron.
--
Jun 23 2007
parent Myron Alexander <someone somewhere.com> writes:
Jari-Matti Mäkelä wrote:
 
 You mean T.stringof and f.stringof ?
Exactly what I need.
 
    - If I call this from a normal run-time function, will it get
 executed at run-time as well as in compile-time?
I'm afraid that function above doesn't do anything on runtime. You need to use the RTTI system to get the type at runtime.
Perfect. That is what I want. Thanks, you have helped alot. Regards, Myron.
Jun 24 2007