Lumiera  0.pre.03
»edit your freedom«
command-impl-clone-builder.hpp
Go to the documentation of this file.
1 /*
2  COMMAND-IMPL-CLONE-BUILDER.hpp - Cloning command implementation without disclosing concrete type
3 
4  Copyright (C)
5  2009, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 */
13 
14 
37 #ifndef CONTROL_COMMAND_IMPL_CLONE_BUILDER_H
38 #define CONTROL_COMMAND_IMPL_CLONE_BUILDER_H
39 
42 #include "lib/opaque-holder.hpp"
43 #include "lib/nocopy.hpp"
44 
45 
46 namespace steam {
47 namespace control {
48 
50  using lib::InPlaceBuffer;
51 
52 
53  namespace impl { // Helper: type erased context for creating command clones
54 
55  struct CloneContext
56  {
57  virtual ~CloneContext() {}
58 
59  virtual UndoMutation const& getUndoFunc() { NOTREACHED(); }
60  virtual PClo const& getClosure() { NOTREACHED(); }
61  virtual bool isValid() { return false; }
62  };
63 
65  : public CloneContext
66  {
67  PClo newClosure_;
68  UndoMutation newUndoFunctor_;
69 
70  virtual UndoMutation const& getUndoFunc() { return newUndoFunctor_; }
71  virtual PClo const& getClosure() { return newClosure_; }
72  virtual bool isValid() { return true; }
73 
74 
80  template<typename ARG>
81  ARG&
83  {
84  REQUIRE (INSTANCEOF (ARG, newClosure_.get()));
85 
86  return static_cast<ARG&> (*newClosure_);
87  }
88 
89 
90  public:
91 
92  template<typename ARG>
93  ClonedContext ( ARG const& origArgHolder
94  , TypedAllocationManager& allocator
95  )
96  : newClosure_(allocator.create<ARG> (origArgHolder))
97  , newUndoFunctor_(downcast<ARG>().getMementoWiring())
98  { }
99  };
100 
101  }//(End) impl namespace
102 
103 
104 
105 
106 
116  {
118 
119  TypedAllocationManager& allocator_;
120  ContextHolder newContext_;
121 
122  public:
124  : allocator_(allo)
125  { }
126 
127 
137  template<typename ARG>
138  void
139  buildCloneContext (ARG const& origArgHolder)
140  {
141  REQUIRE (!newContext_->isValid(), "Lifecycle-Error");
142 
143  newContext_.create<impl::ClonedContext> (origArgHolder, allocator_);
144  }
145 
146 
147 
149  UndoMutation const&
151  {
152  REQUIRE (newContext_->isValid());
153  return newContext_->getUndoFunc();
154  }
155 
156 
159  PClo const&
161  {
162  REQUIRE (newContext_->isValid());
163  return newContext_->getClosure();
164  }
165 
166  };
167 
168 
169 
170 
171 
172 }} // namespace steam::control
173 #endif
UndoMutation const & clonedUndoMutation()
after visitation: use pre-built bits to provide a cloned UndoFunctor
ARG & downcast()
helper for accessing the new cloned closure through the specific concrete type.
Abstract foundation for building custom allocation managers.
void buildCloneContext(ARG const &origArgHolder)
to be executed from within the specifically typed context of a concrete command StorageHolder; alloca...
Core of a Steam-Layer command: functor containing the operation to be executed.
#define INSTANCEOF(CLASS, EXPR)
shortcut for subclass test, intended for assertions only.
Definition: util.hpp:514
Any copy and copy construction prohibited.
Definition: nocopy.hpp:37
PClo const & clonedClosuere()
after visitation: provide cloned StorageHolder, but already stripped down to the generic usage type ...
Steam-Layer implementation namespace root.
Specialised version of the command Mutation functor, used to implement the UNDO functionality.
Mix-Ins to allow or prohibit various degrees of copying and cloning.
Helper allowing type erasure while holding the actual object inline.
Visitor to support creating a CommandImpl clone.
Foundation for a custom allocation manager, tracking the created objects by smart-ptrs.
Buffer to place and maintain an object instance privately within another object.