OSVR-Core
UseSendGuard.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_UseSendGuard_h_GUID_FEAB5647_E86B_4BA2_0A29_CB5665678CCB
26 #define INCLUDED_UseSendGuard_h_GUID_FEAB5647_E86B_4BA2_0A29_CB5665678CCB
27 
28 // Internal Includes
29 #include <osvr/Util/Verbosity.h>
30 
31 // Library/third-party includes
32 // - none
33 
34 // Standard includes
35 #include <exception>
36 
39 template <typename InterfaceType, typename F>
40 inline OSVR_ReturnCode useSendGuard(InterfaceType &iface, F &&func) {
41  try {
42  auto guard = iface->getSendGuard();
43  if (guard->lock()) {
44  return func();
45  }
46  } catch (std::exception const &e) {
47  OSVR_DEV_VERBOSE("Caught exception: " << e.what());
48  return OSVR_RETURN_FAILURE;
49  } catch (...) {
50  OSVR_DEV_VERBOSE("Caught non-standard exception!");
51  return OSVR_RETURN_FAILURE;
52  }
53  return OSVR_RETURN_FAILURE;
54 }
55 
58 template <typename InterfaceType, typename F>
59 inline OSVR_ReturnCode useSendGuardVoid(InterfaceType &iface, F &&func) {
60  return useSendGuard(iface, [&func]() -> OSVR_ReturnCode {
61  func();
62  return OSVR_RETURN_SUCCESS;
63  });
64 }
65 #endif // INCLUDED_UseSendGuard_h_GUID_FEAB5647_E86B_4BA2_0A29_CB5665678CCB
OSVR_ReturnCode useSendGuardVoid(InterfaceType &iface, F &&func)
Calls a void function using the send guard, returning success if it completes without exception...
Definition: UseSendGuard.h:59
#define OSVR_RETURN_FAILURE
The "failure" value for an OSVR_ReturnCode.
Definition: ReturnCodesC.h:47
#define OSVR_RETURN_SUCCESS
The "success" value for an OSVR_ReturnCode.
Definition: ReturnCodesC.h:45
Definition: benchGeometry.cpp:23
OSVR_ReturnCode useSendGuard(InterfaceType &iface, F &&func)
Calls a function using the send guard, returning the return value of the function if it completes wit...
Definition: UseSendGuard.h:40