blitzdg
an open-source project aiming to implement parallel discontinuous Galerkin (dg) solvers for common partial differential equations systems using blitz++ for array and tensor manipulations and MPI for distributed parallelism.
contextprovider.h
1 
2 // Copyright Joakim Karlsson & Kim Gräsman 2010-2013.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 #ifndef IGLOO_CONTEXTPROVIDER_H_
8 #define IGLOO_CONTEXTPROVIDER_H_
9 
10 namespace igloo {
11 
12  namespace detail {
13  //
14  // Check if the context is marked as "only"
15  //
16  template <typename T>
17  inline bool IsContextMarkedAsOnly()
18  {
19  return T::IsContextMarkedAsOnly();
20  }
21 
22  //
23  // Specialization for root context. Is never marked "only".
24  //
25  template <>
26  inline bool IsContextMarkedAsOnly<ContextWithAttribute<void> >()
27  {
28  return false;
29  }
30 
31  //
32  // Check if the context is marked as "skip"
33  //
34  template <typename T>
35  inline bool IsContextMarkedAsSkip()
36  {
37  return T::IsMarkedAsSkip();
38  }
39 
40  //
41  // Specialization for root context. Is never marked as "skip".
42  //
43  template <>
44  inline bool IsContextMarkedAsSkip<ContextWithAttribute<void> >()
45  {
46  return false;
47  }
48  }
49 
50  //
51  // This class provides functionality for nested contexts.
52  //
53  // Template arguments:
54  // InnerContext - this is the type of the current context.
55  // OuterContext - the type of the outer (parent) context.
56  // For root contexts, this is 'void'.
57  // ISONLY - Marks this context as 'only', which means
58  // this context and its children are the only
59  // contexts to be executed.
60  // ISSKIP - Marks this context as 'skip', which means
61  // this context and its children should not
62  // be executed.
63  //
64  template <typename InnerContext, typename OuterContext, bool ISONLY, bool ISSKIP>
66  {
67  //
68  // By storing the current context in 'IGLOO_CURRENT_CONTEXT', the macros for
69  // registering contexts nested to this will be created with this context as
70  // outer context.
71  //
72  typedef InnerContext IGLOO_CURRENT_CONTEXT;
73 
74  static bool IsContextMarkedAsOnly()
75  {
76  return ISONLY ||
78  detail::IsContextMarkedAsOnly<OuterContext>();
79  }
80 
81  static bool IsMarkedAsSkip()
82  {
83  return ISSKIP || detail::IsContextMarkedAsSkip<OuterContext>();
84  }
85 
86  virtual OuterContext& Parent()
87  {
88  if(m_outerContext.get() == 0)
89  {
90  m_outerContext = std::auto_ptr<OuterContext>(CreateIglooContext<OuterContext>());
91  }
92  return *(m_outerContext.get());
93  }
94 
95  virtual void IglooFrameworkSetUp()
96  {
97  Parent().IglooFrameworkSetUp();
98  this->SetUp();
99  }
100 
101  virtual void IglooFrameworkTearDown()
102  {
103  this->TearDown();
104  Parent().IglooFrameworkTearDown();
105  }
106 
107  private:
108  template <typename ContextType>
109  ContextType* CreateIglooContext()
110  {
111  return new ContextType();
112  }
113 
114  std::auto_ptr<OuterContext> m_outerContext;
115  };
116 
117  //
118  // This class makes it possible for contexts to retrieve the root context.
119  //
120  template <typename InnerContext, typename OuterContext, typename RootContext, bool ISONLY, bool ISSKIP>
121  struct ContextProvider : public NestedContextProvider<InnerContext, OuterContext, ISONLY, ISSKIP>
122  {
123  //
124  // For non root contexts, we ask our parent to return the root context.
125  // This will work recursively up to the root context.
126  //
127  RootContext& Root()
128  {
130  }
131  };
132 
133  //
134  // Specialization for the root contexts.
135  //
136  template <typename InnerContext, bool ISONLY, bool ISSKIP>
137  struct ContextProvider<InnerContext, ContextWithAttribute<void>, void, ISONLY, ISSKIP> : public NestedContextProvider<InnerContext, ContextWithAttribute<void>, ISONLY, ISSKIP>
138  {
139  //
140  // By setting this, nested contexts will have access to the type
141  // of the root context so that their 'Root()' method can return
142  // the correct type.
143  //
144  typedef InnerContext IGLOO_ROOT_CONTEXT;
145 
146  //
147  // For the root context, we return a reference to this
148  // context.
149  //
150  InnerContext& Root()
151  {
152  return *(static_cast<InnerContext*>(this));
153  }
154  };
155 
156 }
157 #endif
Definition: contextprovider.h:121
Definition: context.h:51
Definition: context.h:13
Definition: contextregistry.h:26
Definition: contextprovider.h:65