Skip to content

Session: Traits VS Metafunction

Marcel edited this page Mar 21, 2017 · 2 revisions

Preparation

There will be a cpp-session tomorrow at 11 o'clock in K40. I don't know if Rene prepared "Algorithms Library", but I will give a short session on "Type Traits" vs Type Metafunctions". If you have used SeqAn a lot, you don't have to prepare anything, but if not I recommend looking at these pages before:

http://seqan.readthedocs.io/en/master/Tutorial/HowTo/Recipes/Metafunctions.html

http://seqan.readthedocs.io/en/master/Tutorial/DataStructures/Indices/StringIndices.html

(the last section makes use of metafunctions for changing a data structure's behaviour).

Session

Slides

Solution to task

#include <string>
#include <iostream>
#include <cctype>

struct dna_char_traits : public std::char_traits<char>
{
    static void assign(char & c1, char const & c2)
    {
         switch(c2)
         {
             case 'a': case 'A': c1 = 'A'; break;
             case 'c': case 'C': c1 = 'C'; break;
             case 'g': case 'G': c1 = 'G'; break;
             case 't': case 'T': case 'u': case 'U': c1 = 'T'; break;
             default : c1 = 'N';
         }
    }

    static char* copy(char * dest, char const * src, std::size_t count)
    {
        for (unsigned i = 0; i < count; ++i)
            assign(dest[i], src[i]);
        return dest;
    }
};

typedef std::basic_string<char, dna_char_traits> dna_string;

std::ostream& operator<<(std::ostream & os, dna_string const & str)
{
    return os.write(str.data(), str.size());
}

int main()
{
    dna_string s1 = "AaCcGgTtUuNn";
    s1.push_back('z');
    std::cout << s1 << "\n"; // shall print "AACCGGTTTTNNNNN"
}
Clone this wiki locally