Lumiera  0.pre.03
»edit your freedom«
special-job-fun.hpp
Go to the documentation of this file.
1 /*
2  SPECIAL-JOB-FUN.hpp - a one-time render job to do something special
3 
4  Copyright (C)
5  2023, 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 VAULT_GEAR_SPECIAL_JOB_FUN_H
38 #define VAULT_GEAR_SPECIAL_JOB_FUN_H
39 
40 
41 
42 #include "lib/handle.hpp"
44 #include "lib/time/timevalue.hpp"
45 #include "lib/format-string.hpp"
46 #include "lib/format-obj.hpp"
47 #include "lib/meta/util.hpp"
48 
49 #include <string>
50 #include <memory>
51 #include <utility>
52 
53 
54 namespace vault{
55 namespace gear {
56  namespace err = lumiera::error;
57 
58  using util::_Fmt;
60  using std::move;
61  using std::forward;
62  using std::make_shared;
63 
64 
69  : public NopJobFunctor
70  {
71  public:
72  virtual uint remainingInvocations() const =0;
73  };
74 
75 
76 
77 
82  : protected lib::Handle<SpecialFunPrototype>
83  {
85 
86  template<class FUN>
89  {
90  FUN fun_;
91  uint lives_{1};
92  _Handle selfHook_;
93 
94  public:
95  explicit
96  SpecialExecutor (FUN theFun)
97  : fun_{move (theFun)}
98  , selfHook_(this)
99  { }
100 
103  friend _Handle&
105  {
106  REQUIRE (instance->selfHook_);
107  REQUIRE (instance->remainingInvocations());
108  // expose the self-managing handle
109  return instance->selfHook_;
110  }
111 
112 
113  uint
114  remainingInvocations() const
115  {
116  return lives_;
117  }
118 
119 
120  /* === JobFunctor Interface === */
121 
122  void
123  invokeJobOperation (JobParameter param) override
124  {
125  if (not remainingInvocations())
126  throw err::Logic{"invoking deceased SpecialJobFun"
127  ,err::LUMIERA_ERROR_LIFECYCLE};
128  fun_(param);
129 
130  if (not --lives_)
131  selfHook_.close();
132  } // Suicide.
133 
134  std::string
135  diagnostic() const override
136  {
137  return _Fmt{"SpecialJob(%d)-%s"}
138  % lives_
139  % util::showHash(size_t(this), 2);
140  }
141  };
142 
143 
144 
145  public:
146  SpecialJobFun() = default;
147 
158  template<class FUN, typename =disable_if_self<SpecialJobFun, FUN>>
159  explicit
160  SpecialJobFun (FUN&& someFun)
161  : _Handle{selfAttached (new SpecialExecutor(forward<FUN> (someFun)))}
162  { }
163 
164  // standard copy operations acceptable
165 
166  explicit
167  operator bool() const
168  {
169  return 0 < remainingInvocations();
170  }
171 
172  operator JobClosure&() const
173  {
174  REQUIRE (operator bool());
175  return _Handle::impl();
176  }
177 
178  uint
179  remainingInvocations() const
180  {
181  return _Handle::isValid()? _Handle::impl().remainingInvocations()
182  : 0;
183  }
184 
185  long
186  use_count() const
187  {
188  return _Handle::smPtr_.use_count();
189  }
190  };
191 
192 
193 }} // namespace vault::gear
194 #endif /*VAULT_GEAR_SPECIAL_JOB_FUN_H*/
Generic opaque reference counting handle, for accessing a service and managing its lifecycle...
Definition: handle.hpp:63
Interface: JobFunctor configured to invoke a function a limited number of times.
Generic implementation of a JobFunctor to perform no calculations.
Simple and lightweight helpers for metaprogramming and type detection.
Front-end for printf-style string template interpolation.
SpecialJobFun(FUN &&someFun)
Establish a new SpecialJobFun variation directly by wrapping a given functor.
Front-end to configure a special job functor for one-time use.
A front-end for using printf-style formatting.
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:190
void close()
deactivate this handle, so it isn&#39;t tied any longer to the associated implementation or service objec...
Definition: handle.hpp:140
Simple functions to represent objects, for debugging and diagnostics.
Interface of the closure for frame rendering jobs.
Definition: job.h:235
friend _Handle & selfAttached(SpecialExecutor *instance)
A generic opaque handle to an implementation entity, including lifecycle management.
a family of time value like entities and their relationships.
disable_if< std::is_same< std::remove_cv_t< std::remove_reference_t< extractFirst_t< ARGS... > >>, SELF > > disable_if_self
helper to prevent a template constructor from shadowing inherited copy ctors
Definition: meta/util.hpp:151
Vault-Layer implementation namespace root.
Stub/Test implementation of the JobFunctor interface for a render job to do nothing at all ...