HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
function_view.h
Go to the documentation of this file.
1 // Copyright 2008-present Contributors to the OpenImageIO project.
2 // SPDX-License-Identifier: BSD-3-Clause
3 // https://github.com/OpenImageIO/oiio
4 
5 // Portions of the code in this file is a derived work based on the
6 // FunctionRef class in LLVM:
7 //
8 // University of Illinois/NCSA Open Source License
9 //
10 // Copyright (c) 2003-2018 University of Illinois at Urbana-Champaign.
11 // All rights reserved.
12 //
13 // Developed by:
14 // LLVM Team, University of Illinois at Urbana-Champaign, http://llvm.org
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining a copy of
17 // this software and associated documentation files (the "Software"), to deal with
18 // the Software without restriction, including without limitation the rights to
19 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
20 // of the Software, and to permit persons to whom the Software is furnished to do
21 // so, subject to the following conditions:
22 // * Redistributions of source code must retain the above copyright notice,
23 // this list of conditions and the following disclaimers.
24 // * Redistributions in binary form must reproduce the above copyright notice,
25 // this list of conditions and the following disclaimers in the
26 // documentation and/or other materials provided with the distribution.
27 // * Neither the names of the LLVM Team, University of Illinois at
28 // Urbana-Champaign, nor the names of its contributors may be used to
29 // endorse or promote products derived from this Software without specific
30 // prior written permission.
31 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
33 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
37 // SOFTWARE.
38 
39 
40 #pragma once
41 
42 #include <functional>
43 #include <type_traits>
44 
45 #include <OpenImageIO/export.h>
47 #include <OpenImageIO/platform.h>
48 
49 
51 
52 
53 /// function_view<R(T...)> is a lightweight non-owning generic callable
54 /// object view, similar to a std::function<R(T...)>, but with much less
55 /// overhead.
56 ///
57 /// A function_view invocation should have the same cost as a function
58 /// pointer (which it basically is underneath). Similar in spirit to a
59 /// string_view or span, the function-like object that the function_view
60 /// refers to MUST have a lifetime that outlasts any use of the
61 /// function_view.
62 ///
63 /// In contrast, a full std::function<> is an owning container for a
64 /// callable object. It's more robust, especially with respect to object
65 /// lifetimes, but the call overhead is quite high. So use a function_view
66 /// when you can.
67 ///
68 /// This implementation comes from LLVM:
69 /// https://github.com/llvm-mirror/llvm/blob/master/include/llvm/ADT/STLExtras.h
70 
71 template<typename Fn> class function_view;
72 
73 template<typename Ret, typename... Params> class function_view<Ret(Params...)> {
74  Ret (*callback)(intptr_t callable, Params... params) = nullptr;
75  intptr_t callable;
76 
77  template<typename Callable>
78  static Ret callback_fn(intptr_t callable, Params... params)
79  {
80  return (*reinterpret_cast<Callable*>(callable))(
81  std::forward<Params>(params)...);
82  }
83 
84 public:
85  function_view() = default;
86  function_view(std::nullptr_t) {}
87 
88  template<typename Callable>
90  Callable&& callable,
91  typename std::enable_if<
92  !std::is_same<typename std::remove_reference<Callable>::type,
93  function_view>::value>::type* = nullptr)
94  : callback(callback_fn<typename std::remove_reference<Callable>::type>)
95  , callable(reinterpret_cast<intptr_t>(&callable))
96  {
97  }
98 
99  Ret operator()(Params... params) const
100  {
101  return callback(callable, std::forward<Params>(params)...);
102  }
103 
104  operator bool() const { return callback; }
105 };
106 
107 
function_view(Callable &&callable, typename std::enable_if< !std::is_same< typename std::remove_reference< Callable >::type, function_view >::value >::type *=nullptr)
Definition: function_view.h:89
GLenum const GLfloat * params
Definition: glcorearb.h:105
Definition: core.h:1131
#define OIIO_NAMESPACE_END
Definition: oiioversion.h:94
type
Definition: core.h:1059
#define OIIO_NAMESPACE_BEGIN
Definition: oiioversion.h:93
Ret operator()(Params...params) const
Definition: function_view.h:99