VisionWorks Toolkit Reference

December 18, 2015 | 1.2 Release

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Utility.hpp
Go to the documentation of this file.
1 /*
2 # Copyright (c) 2014-2015, NVIDIA CORPORATION. All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions
6 # are met:
7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution.
12 # * Neither the name of NVIDIA CORPORATION nor the names of its
13 # contributors may be used to endorse or promote products derived
14 # from this software without specific prior written permission.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
17 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 # 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 
29 #ifndef NVXIO_UTILITY_HPP
30 #define NVXIO_UTILITY_HPP
31 
32 #include <iostream>
33 #include <memory>
34 #include <sstream>
35 #include <stdexcept>
36 #include <utility>
37 
38 #include <NVX/nvx.h>
39 
45 namespace nvxio
46 {
47 
55 // Auxiliary macros
56 
63 #define NVXIO_THROW_EXCEPTION(msg) \
64  do { \
65  std::ostringstream ostr_; \
66  ostr_ << msg; \
67  throw std::runtime_error(ostr_.str()); \
68  } while(0)
69 
77 #define NVXIO_SAFE_CALL(vxOp) \
78  do \
79  { \
80  vx_status status = (vxOp); \
81  if (status != VX_SUCCESS) \
82  { \
83  NVXIO_THROW_EXCEPTION(# vxOp << " failure [status = " << status << "]" << " in file " << __FILE__ << " line " << __LINE__); \
84  } \
85  } while (0)
86 
93 #define NVXIO_ASSERT(cond) \
94  do \
95  { \
96  bool status = (cond); \
97  if (!status) \
98  { \
99  NVXIO_THROW_EXCEPTION(# cond << " failure in file " << __FILE__ << " line " << __LINE__); \
100  } \
101  } while (0)
102 
109 #define NVXIO_CHECK_REFERENCE(ref) \
110  NVXIO_ASSERT(ref != 0 && vxGetStatus((vx_reference)ref) == VX_SUCCESS)
111 
117 template <typename T, vx_size N>
118 vx_size dimOf(T (&)[N]) { return N; }
119 
120 // Common constants
121 
127 const vx_float64 PI = 3.1415926535897932;
133 const vx_float32 PI_F = 3.14159265f;
134 
135 // Auxiliary functions
136 
146 void VX_CALLBACK stdoutLogCallback(vx_context context, vx_reference ref, vx_status status, const vx_char string[]);
147 
154 void checkIfContextIsValid(vx_context context);
155 
163 {
164  ContextGuard() : context(vxCreateContext()) {
165  checkIfContextIsValid(context);
166  }
167  ContextGuard(const ContextGuard &) = delete;
168  ContextGuard &operator = (const ContextGuard &) = delete;
170  vxReleaseContext(&context);
171  }
172  operator vx_context() { return context; }
173 
174 private:
175  vx_context context;
176 };
177 
183 template <typename T, typename... Args>
184 std::unique_ptr<T> makeUP(Args &&... args)
185 {
186  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
187 }
188 
189 }
190 
191 #endif // NVXIO_UTILITY_HPP
#define VX_CALLBACK
Definition: vx_types.h:78
size_t vx_size
A wrapper of size_t to keep the naming convention uniform.
Definition: vx_types.h:166
NVIDIA VisionWorks Framework and Primitives API.
ContextGuard is a wrapper for vx_context.
Definition: Utility.hpp:162
char vx_char
An 8 bit ASCII character.
Definition: vx_types.h:85
float vx_float32
A 32-bit float value.
Definition: vx_types.h:138
void VX_CALLBACK stdoutLogCallback(vx_context context, vx_reference ref, vx_status status, const vx_char string[])
The callback for OpenVX error logs, which prints messages to standard output.
const vx_float64 PI
Double-precision PI.
Definition: Utility.hpp:127
vx_status VX_API_CALL vxReleaseContext(vx_context *context)
Releases the OpenVX object context.
vx_enum vx_status
A formal status type with known fixed size.
Definition: vx_types.h:421
struct _vx_context * vx_context
An opaque reference to the implementation context.
Definition: vx_types.h:222
struct _vx_reference * vx_reference
A generic opaque reference to any object within OpenVX.
Definition: vx_types.h:154
ContextGuard & operator=(const ContextGuard &)=delete
std::unique_ptr< T > makeUP(Args &&...args)
make_unique function.
Definition: Utility.hpp:184
double vx_float64
A 64-bit float value (aka double).
Definition: vx_types.h:143
void checkIfContextIsValid(vx_context context)
Checks whether the context is valid and throws an exception in case of failure.
const vx_float32 PI_F
Float-precision PI.
Definition: Utility.hpp:133
vx_context VX_API_CALL vxCreateContext()
Creates a vx_context.
vx_size dimOf(T(&)[N])
Returns the size of an array (the N template argument).
Definition: Utility.hpp:118