Search code examples
c#typelistc++-loki

C# Typelist analog to Loki::Typelist with Hierarchy Generator


I loved Loki's C++ HierarchyGenerator and want to do the same in C#.

What I want in the end is a class that has a virtual function per argument in a given typelist.

C++ code I'd like to convert:

template <class T>
class SenderV 
{
public: 
    virtual void Send(T t) = 0;
};
template <int i>
class Foo // Just to make it easy to show typelist, it's not interesting. 
{ /* doIt definition */ };
typedef TYPELIST_2(Foo<1>,Foo<2>) FooSendables;

template <typename TList=FooSendables>
class FooSend : public Loki::GenScatterHierarchy <TList,SenderV>
{
public:
    void Send(Foo<1> f) {f.doIt();std::cout<<"Sending Foo1."<<std::endl;};
    void Send(Foo<2> f) {f.doIt();std::cout<<"Sending Foo2."<<std::endl;};
};

in C#. If you aren't familiar with Loki, the FooSend class above would default to:

class FooSend : SenderV<Foo<1> >, SenderV<Foo<2> >//including every type in TList
{ /*... as above */};

But when a new TList is given, it'd be a different Hierarchy based on the Types in the TList.

I'm also interested in a GenLinearHierarchy in Loki if it exists out there.

I could always try to translate between the languages, but I'm not a big fan of trying that as I'm new to C# and just want to do my work, not learn the subtle difference between templates and generics.


Solution

  • using t4:

    <#@ template debug="false" hostspecific="false" language="C#" #>
    <#@ output extension=".cs" #>
    
    <#@ import namespace="System.Text" #>
    <#@ import namespace="System.IO" #>
    <#@ import namespace="System" #>
    
    
    namespace SomeNamespace 
    {
    
        public interface Sender<T> 
        {
            void Send<T>(T t);
        };
        <# string[] strings={"Foo1","Foo2","Foo3"};
            foreach (String node in strings) 
            { #> partial class <#= node #> {}
            <# } #>
        class z {}
        public class FooSend: Sender<z><# 
             foreach (String node in strings) 
             { #>, Sender<<#= node #>> <# } #>
        {
        }
    }
    

    I can't get the formatting the way I'd like (and, regardless, the t4 formatting is always going to be ugly), but this solves my problem.

    The code above produces:

    namespace SomeNamespace 
    {
    
        public interface Sender<T> 
        {
            void Send<T>(T t);
        };
         partial class Foo1 {}
         partial class Foo2 {}
         partial class Foo3 {}
         class z {}
        public class ParentClass : Sender<z>, Sender<Foo1> , Sender<Foo2> , Sender<Foo3>  {
        }    
    }
    

    Which fits my needs.