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) Lumiera.org
5  2023, 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 VAULT_GEAR_SPECIAL_JOB_FUN_H
47 #define VAULT_GEAR_SPECIAL_JOB_FUN_H
48 
49 
50 
51 #include "lib/handle.hpp"
53 #include "lib/time/timevalue.hpp"
54 #include "lib/format-string.hpp"
55 #include "lib/format-obj.hpp"
56 #include "lib/meta/util.hpp"
57 
58 #include <string>
59 #include <memory>
60 #include <utility>
61 
62 
63 namespace vault{
64 namespace gear {
65  namespace err = lumiera::error;
66 
67  using util::_Fmt;
69  using std::move;
70  using std::forward;
71  using std::make_shared;
72 
73 
78  : public NopJobFunctor
79  {
80  public:
81  virtual uint remainingInvocations() const =0;
82  };
83 
84 
85 
86 
91  : protected lib::Handle<SpecialFunPrototype>
92  {
94 
95  template<class FUN>
98  {
99  FUN fun_;
100  uint lives_{1};
101  _Handle selfHook_;
102 
103  public:
104  explicit
105  SpecialExecutor (FUN theFun)
106  : fun_{move (theFun)}
107  , selfHook_(this)
108  { }
109 
112  friend _Handle&
114  {
115  REQUIRE (instance->selfHook_);
116  REQUIRE (instance->remainingInvocations());
117  // expose the self-managing handle
118  return instance->selfHook_;
119  }
120 
121 
122  uint
123  remainingInvocations() const
124  {
125  return lives_;
126  }
127 
128 
129  /* === JobFunctor Interface === */
130 
131  void
132  invokeJobOperation (JobParameter param) override
133  {
134  if (not remainingInvocations())
135  throw err::Logic{"invoking deceased SpecialJobFun"
136  ,err::LUMIERA_ERROR_LIFECYCLE};
137  fun_(param);
138 
139  if (not --lives_)
140  selfHook_.close();
141  } // Suicide.
142 
143  std::string
144  diagnostic() const override
145  {
146  return _Fmt{"SpecialJob(%d)-%s"}
147  % lives_
148  % util::showHash(size_t(this), 2);
149  }
150  };
151 
152 
153 
154  public:
155  SpecialJobFun() = default;
156 
167  template<class FUN, typename =disable_if_self<SpecialJobFun, FUN>>
168  explicit
169  SpecialJobFun (FUN&& someFun)
170  : _Handle{selfAttached (new SpecialExecutor(forward<FUN> (someFun)))}
171  { }
172 
173  // standard copy operations acceptable
174 
175  explicit
176  operator bool() const
177  {
178  return 0 < remainingInvocations();
179  }
180 
181  operator JobClosure&() const
182  {
183  REQUIRE (operator bool());
184  return _Handle::impl();
185  }
186 
187  uint
188  remainingInvocations() const
189  {
190  return _Handle::isValid()? _Handle::impl().remainingInvocations()
191  : 0;
192  }
193 
194  long
195  use_count() const
196  {
197  return _Handle::smPtr_.use_count();
198  }
199  };
200 
201 
202 }} // namespace vault::gear
203 #endif /*VAULT_GEAR_SPECIAL_JOB_FUN_H*/
Generic opaque reference counting handle, for accessing a service and managing its lifecycle...
Definition: handle.hpp:72
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:199
void close()
deactivate this handle, so it isn&#39;t tied any longer to the associated implementation or service objec...
Definition: handle.hpp:149
Simple functions to represent objects, for debugging and diagnostics.
Interface of the closure for frame rendering jobs.
Definition: job.h:244
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:160
Vault-Layer implementation namespace root.
Stub/Test implementation of the JobFunctor interface for a render job to do nothing at all ...