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 /*
2  Copyright 2017 Larry Gritz and the other authors and contributors.
3  All Rights Reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8  * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  * Neither the name of the software's owners nor the names of its
14  contributors may be used to endorse or promote products derived from
15  this software without specific prior written permission.
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28  (This is the Modified BSD License)
29 */
30 
31 // Portions of the code in this file is a derived work based on the
32 // FunctionRef class in LLVM:
33 //
34 // University of Illinois/NCSA Open Source License
35 //
36 // Copyright (c) 2003-2018 University of Illinois at Urbana-Champaign.
37 // All rights reserved.
38 //
39 // Developed by:
40 // LLVM Team, University of Illinois at Urbana-Champaign, http://llvm.org
41 //
42 // Permission is hereby granted, free of charge, to any person obtaining a copy of
43 // this software and associated documentation files (the "Software"), to deal with
44 // the Software without restriction, including without limitation the rights to
45 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
46 // of the Software, and to permit persons to whom the Software is furnished to do
47 // so, subject to the following conditions:
48 // * Redistributions of source code must retain the above copyright notice,
49 // this list of conditions and the following disclaimers.
50 // * Redistributions in binary form must reproduce the above copyright notice,
51 // this list of conditions and the following disclaimers in the
52 // documentation and/or other materials provided with the distribution.
53 // * Neither the names of the LLVM Team, University of Illinois at
54 // Urbana-Champaign, nor the names of its contributors may be used to
55 // endorse or promote products derived from this Software without specific
56 // prior written permission.
57 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
58 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
59 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
60 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
61 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
62 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
63 // SOFTWARE.
64 
65 
66 #pragma once
67 
68 #include <functional>
69 #include <type_traits>
70 
71 #include <OpenImageIO/export.h>
73 #include <OpenImageIO/platform.h>
74 
75 
77 
78 
79 /// function_view<R(T...)> is a lightweight non-owning generic callable
80 /// object view, similar to a std::function<R(T...)>, but with much less
81 /// overhead.
82 ///
83 /// A function_view invocation should have the same cost as a function
84 /// pointer (which it basically is underneath). Similar in spirit to a
85 /// string_view or span, the function-like object that the function_view
86 /// refers to MUST have a lifetime that outlasts any use of the
87 /// function_view.
88 ///
89 /// In contrast, a full std::function<> is an owning container for a
90 /// callable object. It's more robust, especially with restpect to object
91 /// lifetimes, but the call overhead is quite high. So use a function_view
92 /// when you can.
93 ///
94 /// This implementation comes from LLVM:
95 /// https://github.com/llvm-mirror/llvm/blob/master/include/llvm/ADT/STLExtras.h
96 
97 template<typename Fn> class function_view;
98 
99 template<typename Ret, typename... Params> class function_view<Ret(Params...)> {
100  Ret (*callback)(intptr_t callable, Params... params) = nullptr;
101  intptr_t callable;
102 
103  template<typename Callable>
104  static Ret callback_fn(intptr_t callable, Params... params)
105  {
106  return (*reinterpret_cast<Callable*>(callable))(
107  std::forward<Params>(params)...);
108  }
109 
110 public:
111  function_view() = default;
112  function_view(std::nullptr_t) {}
113 
114  template<typename Callable>
116  Callable&& callable,
117  typename std::enable_if<
118  !std::is_same<typename std::remove_reference<Callable>::type,
119  function_view>::value>::type* = nullptr)
120  : callback(callback_fn<typename std::remove_reference<Callable>::type>)
121  , callable(reinterpret_cast<intptr_t>(&callable))
122  {
123  }
124 
125  Ret operator()(Params... params) const
126  {
127  return callback(callable, std::forward<Params>(params)...);
128  }
129 
130  operator bool() const { return callback; }
131 };
132 
133 
function_view(Callable &&callable, typename std::enable_if< !std::is_same< typename std::remove_reference< Callable >::type, function_view >::value >::type *=nullptr)
Platform-related macros.
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1253
const GLfloat * params
Definition: glew.h:1531
#define OIIO_NAMESPACE_END
Definition: oiioversion.h:66
GLsizei const GLfloat * value
Definition: glew.h:1849
type
Definition: core.h:528
#define OIIO_NAMESPACE_BEGIN
Definition: oiioversion.h:65
Ret operator()(Params...params) const