summaryrefslogtreecommitdiff
path: root/contrib/utility/Utility/Hetero/Shell.hpp
blob: 0d37a6ba09d4c0c9eab43c843441da32ef47e022 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// file      : Utility/Hetero/Shell.hpp
// author    : Boris Kolpackov <boris@kolpackov.net>
// copyright : Copyright (c) 2002-2003 Boris Kolpackov
// license   : http://kolpackov.net/license.html

#ifndef UTILITY_HETERO_SHELL_HPP
#define UTILITY_HETERO_SHELL_HPP

namespace Utility
{
  namespace Hetero
  {

    //
    //
    //
    class ShellCore
    {
    protected:
      template <typename F, typename T0>
      static typename F::RetType
      apply (F& f, TypedContainer<TypeList<T0> >& c)
      {
        if (type_check <T0> (c)) return type_apply<T0> (f, c);

        throw Utility::Hetero::Typing ();
      }

      template <typename F, typename T0, typename T1>
      static typename F::RetType
      apply (F& f, TypedContainer<TypeList<T0, T1> >& c)
      {
        if (type_check <T0> (c)) return type_apply<T0> (f, c);
        if (type_check <T1> (c)) return type_apply<T1> (f, c);

        throw Utility::Hetero::Typing ();
      }

      template <typename F, typename T0, typename T1, typename T2>
      static typename F::RetType
      apply (F& f, TypedContainer<TypeList<T0, T1, T2> >& c)
      {
        if (type_check <T0> (c)) return type_apply<T0> (f, c);
        if (type_check <T1> (c)) return type_apply<T1> (f, c);
        if (type_check <T2> (c)) return type_apply<T2> (f, c);

        throw Utility::Hetero::Typing ();
      }

    private:
      template <typename T>
      static bool
      type_check (Container& c)
      {
        return c.type () == typeid (T);
      }

      template <typename T, typename F>
      static typename F::RetType
      type_apply (F& f, Container& c)
      {
        return f (c. template value<T> ());
      }
    };


    //
    //
    //
    template <typename F>
    struct Shell : F, ShellCore
    {
      using F::operator ();

      template <typename T>
      typename F::RetType
      operator () (TypedContainer<T>& p)
      {
        return apply (*this, p);
      }
    };
  }
}

#endif  // UTILITY_HETERO_SHELL_HPP
//$Id$