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) Lumiera.org
5  2015, 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 
51 #ifndef STAGE_MODEL_ELEMENT_ACCESS_H
52 #define STAGE_MODEL_ELEMENT_ACCESS_H
53 
54 
55 #include "lib/error.hpp"
56 #include "lib/nocopy.hpp"
57 #include "lib/result.hpp"
58 #include "include/limits.hpp"
59 #include "lib/variant.hpp"
61 #include "lib/access-casted.hpp"
63 
64 #include <string>
65 
66 
67 namespace Gtk {
68  class Widget;
69 }
70 namespace stage {
71 namespace model {
72  namespace error = lumiera::error;
73 
74  using interact::UICoord;
75  using lib::meta::Types;
76  using std::string;
77 
78  class Tangible;
79 
88  {
89 
90  public:
91  virtual ~ElementAccess () { }
92 
93 
94  /* == Access by Location == */
95 
96  template<class TAR>
97  lib::Result<TAR&> access (UICoord const& destination);
98 
99  UICoord locate_or_create (UICoord const& destination, size_t limitCreation = LUMIERA_MAX_ORDINAL_NUMBER);
100 
101 
102  protected:
104 
106  virtual RawResult performAccessTo (UICoord::Builder &, size_t limitCreation) =0;
107 
108  private:
109  template<class TAR>
111  };
112 
113 
114 
115 
132  template<class TAR>
135  {
136  lib::Result<TAR&> result{error::Invalid{"not convertible to desired target widget"}};
137 
138  template<typename X> // note the "backward" use. We pick that base interface
139  using canUpcast = std::is_convertible<TAR*, X>; // into which our desired result type can be upcast, because
140  // we know the then following dynamic_cast (downcast) can succeed
141  using Base = typename RawResult::FirstMatching<canUpcast>::Type;
142 
143  virtual void
144  handle (Base& pb) override
145  {
146  if (pb)
147  {
148  TAR* downcasted = dynamic_cast<TAR*> (pb);
149  if (downcasted)
150  result = *downcasted;
151  }
152  else
153  result = lib::Result<TAR&>{error::State{"access returns empty answer"}};
154  }
155  };
156 
157 
158 
159 
160 
161 
170  template<class TAR>
171  inline lib::Result<TAR&>
172  ElementAccess::access (UICoord const& destination)
173  {
174  UICoord::Builder targetLocation{destination.rebuild()};
175  TypeConverter<TAR> converter;
176  RawResult targetElm = performAccessTo (targetLocation, 0);
177  targetElm.accept (converter);
178  return converter.result;
179  }
180 
181 
189  inline UICoord
190  ElementAccess::locate_or_create (UICoord const& destination, size_t limitCreation)
191  {
192  UICoord::Builder targetLocation{destination.rebuild()};
193  performAccessTo (targetLocation, limitCreation);
194  return targetLocation;
195  }
196 
197 
198 
199 
200 
201 }} // namespace stage::model
202 #endif /*STAGE_MODEL_ELEMENT_ACCESS_H*/
Describe a location within the UI through structural/topological coordinates.
Definition: ui-coord.hpp:138
Representation of the result of some operation, EITHER a value or a failure.
Definition: result.hpp:106
Any copy and copy construction prohibited.
Definition: nocopy.hpp:46
Typesafe union record.
Definition: variant.hpp:224
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:165
Interface: access UI elements by navigating the UI topology.
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:199
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:46
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:241
Metaprogramming: Helpers for manipulating lists-of-types.
Helper for accessing a value, employing either a conversion or downcast, depending on the relation of...