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) Lumiera.org
5  2009, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
23 
46 #ifndef CONTROL_COMMAND_IMPL_CLONE_BUILDER_H
47 #define CONTROL_COMMAND_IMPL_CLONE_BUILDER_H
48 
51 #include "lib/opaque-holder.hpp"
52 #include "lib/nocopy.hpp"
53 
54 
55 namespace steam {
56 namespace control {
57 
59  using lib::InPlaceBuffer;
60 
61 
62  namespace impl { // Helper: type erased context for creating command clones
63 
64  struct CloneContext
65  {
66  virtual ~CloneContext() {}
67 
68  virtual UndoMutation const& getUndoFunc() { NOTREACHED(); }
69  virtual PClo const& getClosure() { NOTREACHED(); }
70  virtual bool isValid() { return false; }
71  };
72 
74  : public CloneContext
75  {
76  PClo newClosure_;
77  UndoMutation newUndoFunctor_;
78 
79  virtual UndoMutation const& getUndoFunc() { return newUndoFunctor_; }
80  virtual PClo const& getClosure() { return newClosure_; }
81  virtual bool isValid() { return true; }
82 
83 
89  template<typename ARG>
90  ARG&
92  {
93  REQUIRE (INSTANCEOF (ARG, newClosure_.get()));
94 
95  return static_cast<ARG&> (*newClosure_);
96  }
97 
98 
99  public:
100 
101  template<typename ARG>
102  ClonedContext ( ARG const& origArgHolder
103  , TypedAllocationManager& allocator
104  )
105  : newClosure_(allocator.create<ARG> (origArgHolder))
106  , newUndoFunctor_(downcast<ARG>().getMementoWiring())
107  { }
108  };
109 
110  }//(End) impl namespace
111 
112 
113 
114 
115 
125  {
127 
128  TypedAllocationManager& allocator_;
129  ContextHolder newContext_;
130 
131  public:
133  : allocator_(allo)
134  { }
135 
136 
146  template<typename ARG>
147  void
148  buildCloneContext (ARG const& origArgHolder)
149  {
150  REQUIRE (!newContext_->isValid(), "Lifecycle-Error");
151 
152  newContext_.create<impl::ClonedContext> (origArgHolder, allocator_);
153  }
154 
155 
156 
158  UndoMutation const&
160  {
161  REQUIRE (newContext_->isValid());
162  return newContext_->getUndoFunc();
163  }
164 
165 
168  PClo const&
170  {
171  REQUIRE (newContext_->isValid());
172  return newContext_->getClosure();
173  }
174 
175  };
176 
177 
178 
179 
180 
181 }} // namespace steam::control
182 #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:492
Any copy and copy construction prohibited.
Definition: nocopy.hpp:46
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.