Dynamic types based on existing template code

Dynamic types based on existing template code

Post by gao_boli » Fri, 11 Feb 2005 00:32:29


I have a library that has a class Signal<T> whose data type is
templated. The class comes with a whole bunch of functions and objects
to do various processing on the signal.

What I would like to do is a DSignal class that does essentially the
same thing, but whose type is unknown at compile type. Since I have
already this template library with everything I want in it, I would
like to maximally reuse it and minimize overheads. It's the first time
I really sit and think about it and the more I do and the more it looks
like a nightmare.

I essentially see two parts in this process, that are not necessarily
completely distinct: (1), How would this DSignal class be build reusing
Signal<T>, and (2), How to import all goodies that were using Signal<T>
before.

At first, I thought I could save a lot of effort using the C++ build-in
dynamic type checking functionality, but now I realize that I won't go
very far with virtual functions, since declarations thereof should
match. E.g. I would have like to have a bottom-up approach, were the
only thing I would rewrite for DSignal would be accessors like
operator(),but that's just not possible, because the return type varies
for different signal precision.

So it seems that I would have to rewrite, for any function that exists,
an equivalent function taking a DSignal as an input and forking to
various implementation of this function with a given list of
types,using a switch, or a table of function pointers. Not really an
enthusiastic perspective.

For functors, maybe a template class could help, e.g. if I had an
inversion functor for Signal<T>, e.g. Inv()(s), I could have a functor
wrapper like

class Wrapper
{
public:
template < class Functor >
void operator()(const DSignal &s, const Functor &f)
{
if (s.getType() == DSignal::DOUBLE) f(s);
else ...
}
};

if I don't mind writting from now on Wrapper()(s, Inv()). Plus, I
cannot use the same Wrapper class to wrap Functors that have the same
arguments but different return values.

The problem of getting from Signal<T> to DSignal is also not that easy.
At first, I wanted not to have an enum in DSignal that would list all
the currently available types, and somehow use typeid instead, so that
maintenance and extension would be easier, but now I don't think that's
possible. In the same logic, I wanted to use inheritance to add
available types to DSignal -- but now I don't think I can. I would
rather not use a super class with each possible Signal<T> as a member,
because this looks super cumbersome. Union is probably not better. So
maybe some kind of smart pointer who would not be templated but uses a
void* and stores the type somewhere?

Any hint or pointer to hints/pieces of code/libraries addressing this
kind of issue would be highly appreciated.

Thanks

Bolin
 
 
 

1. Could not load type/Base type does not exist in source file woes

2. Dynamic generic type with base generic type definition

Hi,
I'm trying to define dynamic generic type definition, inherited from
another generic type definition (something like class B<T> : A<T>).
I'm using similar code to set base type (if this is wrong, all other
problems will be probably gone):

void SetBaseType(TypeBuilder builder, Type sourceType)
{
Type[] ga = sourceType.GetGenericArguments();
string[] gaNames = new string[ga.Length];

for (int i = 0; i < ga.Length; i++)
gaNames[i] = ga[i].Name;

GenericTypeParameterBuilder[] gaBuilders =
typeBuilder.DefineGenericParameters(gaNames);
// Setting constraints is here

Type baseType = sourceType.MakeGenericType(gaBuilders);
typeBuilder.SetParent(baseType);
}

Now, this works until i want to override any method. When I'm using
sourceType to get method that will be overridden, builder.CreateType()
throws this exception "System.TypeLoadException":
Type 'Foo.ListEx`1' from assembly 'Boo, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null' tried to override method
'GetObjectData' but does not implement or inherit that method.

(Note: I'm sure that IL code generation is fine)
I think that problem is that sourceType isn't actually new type parent,
however when any GetMethod (or similar) method on builder.BaseType fails
with exception "System.NotSupportedException: Specified method is not
supported." until type is created (thats probably because i used
non-built types as generic arguments).

Problem is that i'm stuck in the loop.. i can't get methods that should
be overriden until type is created, and can't override methods after
type is created :)
Any ideas?

Thanks
Michal Dvorak

3. Declare type in function template based on class t type

4. Modify existing code to dynamic code

5. Preventing a word document based on a template containing VBA code from the template

6. new head code in template, but doesn't show up in pages based on the template

7. attaching a dynamic web template to a dynamic web template

8. Template operator question (E2094 Operator 'operator' not implemented in type 'type' for arguments of type 'type')

9. Dynamic xpath in XQuery in XML Type Exist() function

10. Explicit specialization of member template when type is another template type.

11. Document Based upon an Existing Template

12. Making a Word template based on an existing paper note

13. ADDING BUSINESS TYPE TEMPLATE TO EXISTING COMPANY

14. How do I type in a template without existing words/lines moving?

15. New documents based on existing types