OSVR-Core
DeduplicatingFunctionWrapper.h
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_DeduplicatingFunctionWrapper_h_GUID_56CC2963_C33D_4FAA_34AB_4562516FE97B
26 #define INCLUDED_DeduplicatingFunctionWrapper_h_GUID_56CC2963_C33D_4FAA_34AB_4562516FE97B
27 
28 // Internal Includes
29 // - none
30 
31 // Library/third-party includes
32 // - none
33 
34 // Standard includes
35 #include <functional>
36 #include <type_traits>
37 #include <stdexcept>
38 
39 namespace osvr {
40 namespace common {
43  template <typename ArgumentType, typename StorageType = void>
45  public:
47  using argument_type = ArgumentType;
48 
51  using storage_type = typename std::conditional<
52  std::is_same<StorageType, void>::value,
53  typename std::remove_const<
54  typename std::remove_reference<ArgumentType>::type>::type,
55  StorageType>::type;
56 
58  typedef void return_type;
59 
61  typedef std::function<return_type(argument_type)> function_type;
62 
64  explicit DeduplicatingFunctionWrapper(function_type const &f)
65  : m_func(f), m_beenCalled(false) {}
66 
68  DeduplicatingFunctionWrapper() : m_beenCalled(false) {}
69 
71  void setFunction(function_type const &f) { m_func = f; }
72 
79  return_type operator()(argument_type arg, ...) {
80  if (!m_func) {
81  throw std::logic_error("Must set the function in "
82  "DeduplicatingFunctionWrapper before "
83  "calling it!");
84  }
85  if (!m_beenCalled) {
86  // First call - always pass through.
87  m_beenCalled = true;
88  return m_do(arg);
89  }
90  if (arg != m_last) {
91  // Not first call, but argument has changed
92  return m_do(arg);
93  }
94  return m_defaultReturn();
95  }
96 
97  private:
100  static return_type m_defaultReturn() { return return_type(); }
101 
104  return_type m_do(argument_type arg) {
105  m_last = arg;
106  return m_func(arg);
107  }
108 
110  function_type m_func;
111 
113  bool m_beenCalled;
114 
116  storage_type m_last;
117  };
118 } // namespace common
119 } // namespace osvr
120 
121 #endif // INCLUDED_DeduplicatingFunctionWrapper_h_GUID_56CC2963_C33D_4FAA_34AB_4562516FE97B
Handles spatial transformations.
Definition: SerializationTraitExample_Complicated.h:40
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
return_type operator()(argument_type arg,...)
Function call operator: passes call along to contained function if and only if (argument != last argu...
Definition: DeduplicatingFunctionWrapper.h:79
ArgumentType argument_type
Argument type: always must be supplied via template parameter.
Definition: DeduplicatingFunctionWrapper.h:47
void return_type
Return type - presently fixed.
Definition: DeduplicatingFunctionWrapper.h:58
DeduplicatingFunctionWrapper()
Default constructor - must have function set before calling.
Definition: DeduplicatingFunctionWrapper.h:68
std::function< return_type(argument_type)> function_type
std::function type corresponding to what is being wrapped.
Definition: DeduplicatingFunctionWrapper.h:61
void setFunction(function_type const &f)
Set/replace the function.
Definition: DeduplicatingFunctionWrapper.h:71
DeduplicatingFunctionWrapper(function_type const &f)
Constructor from a function.
Definition: DeduplicatingFunctionWrapper.h:64
typename std::conditional< std::is_same< StorageType, void >::value, typename std::remove_const< typename std::remove_reference< ArgumentType >::type >::type, StorageType >::type storage_type
Storage type: if not supplied via template parameter, inferred by removing const& from the argument t...
Definition: DeduplicatingFunctionWrapper.h:55
A wrapper for a unary function that only calls its contained function when the input has changed...
Definition: DeduplicatingFunctionWrapper.h:44