Lumiera  0.pre.03
»edit your freedom«
element-access.hpp
Go to the documentation of this file.
1 /*
2  ELEMENT-ACCESS.hpp - access to generic elements in the UI
3 
4  Copyright (C)
5  2015, 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 
42 #ifndef STAGE_MODEL_ELEMENT_ACCESS_H
43 #define STAGE_MODEL_ELEMENT_ACCESS_H
44 
45 
46 #include "lib/error.hpp"
47 #include "lib/nocopy.hpp"
48 #include "lib/result.hpp"
49 #include "include/limits.hpp"
50 #include "lib/variant.hpp"
52 #include "lib/access-casted.hpp"
54 
55 #include <string>
56 
57 
58 namespace Gtk {
59  class Widget;
60 }
61 namespace stage {
62 namespace model {
63  namespace error = lumiera::error;
64 
65  using interact::UICoord;
66  using lib::meta::Types;
67  using std::string;
68 
69  class Tangible;
70 
79  {
80 
81  public:
82  virtual ~ElementAccess () { }
83 
84 
85  /* == Access by Location == */
86 
87  template<class TAR>
88  lib::Result<TAR&> access (UICoord const& destination);
89 
90  UICoord locate_or_create (UICoord const& destination, size_t limitCreation = LUMIERA_MAX_ORDINAL_NUMBER);
91 
92 
93  protected:
95 
97  virtual RawResult performAccessTo (UICoord::Builder &, size_t limitCreation) =0;
98 
99  private:
100  template<class TAR>
102  };
103 
104 
105 
106 
123  template<class TAR>
126  {
127  lib::Result<TAR&> result{error::Invalid{"not convertible to desired target widget"}};
128 
129  template<typename X> // note the "backward" use. We pick that base interface
130  using canUpcast = std::is_convertible<TAR*, X>; // into which our desired result type can be upcast, because
131  // we know the then following dynamic_cast (downcast) can succeed
132  using Base = typename RawResult::FirstMatching<canUpcast>::Type;
133 
134  virtual void
135  handle (Base& pb) override
136  {
137  if (pb)
138  {
139  TAR* downcasted = dynamic_cast<TAR*> (pb);
140  if (downcasted)
141  result = *downcasted;
142  }
143  else
144  result = lib::Result<TAR&>{error::State{"access returns empty answer"}};
145  }
146  };
147 
148 
149 
150 
151 
152 
161  template<class TAR>
162  inline lib::Result<TAR&>
163  ElementAccess::access (UICoord const& destination)
164  {
165  UICoord::Builder targetLocation{destination.rebuild()};
166  TypeConverter<TAR> converter;
167  RawResult targetElm = performAccessTo (targetLocation, 0);
168  targetElm.accept (converter);
169  return converter.result;
170  }
171 
172 
180  inline UICoord
181  ElementAccess::locate_or_create (UICoord const& destination, size_t limitCreation)
182  {
183  UICoord::Builder targetLocation{destination.rebuild()};
184  performAccessTo (targetLocation, limitCreation);
185  return targetLocation;
186  }
187 
188 
189 
190 
191 
192 }} // namespace stage::model
193 #endif /*STAGE_MODEL_ELEMENT_ACCESS_H*/
Describe a location within the UI through structural/topological coordinates.
Definition: ui-coord.hpp:129
Representation of the result of some operation, EITHER a value or a failure.
Definition: result.hpp:97
Any copy and copy construction prohibited.
Definition: nocopy.hpp:37
Typesafe union record.
Definition: variant.hpp:215
Intermediary value object to represent »either« an operation result or a failure. ...
hard wired safety limits.
Helper to pick the first type from a type sequence, which fulfils the predicate (meta function) given...
Definition: variant.hpp:156
Interface: access UI elements by navigating the UI topology.
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:190
Mix-Ins to allow or prohibit various degrees of copying and cloning.
A typesafe union record to carry embedded values of unrelated type.
virtual ~ElementAccess()
this is an interface
Lumiera GTK UI implementation root.
Definition: guifacade.cpp:37
A topological addressing scheme to designate structural locations within the UI.
Lumiera error handling (C++ interface).
to be implemented by the client for visitation
Definition: variant.hpp:232
Metaprogramming: Helpers for manipulating lists-of-types.
Helper for accessing a value, employing either a conversion or downcast, depending on the relation of...