Pointer.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarXCore::core
19 * @author Raphael Grimm (raphael dot grimm at kit dot edu)
20 * @date 2019
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 
25 #pragma once
26 
27 #include <sstream>
28 #include <stdexcept>
29 #include <type_traits>
30 
31 #if __has_include(<source_location>)
32 #include <source_location>
33 #else
34 #include <experimental/source_location>
35 namespace std
36 {
38 }
39 #endif
40 
41 
42 namespace armarx::detail
43 {
44  template<class, class = void>
45  struct HasGetMember : std::false_type {};
46 
47  template<class T>
48 struct HasGetMember<T, std::void_t<decltype(&T::get)>> : std::true_type {};
49 }
50 
51 namespace armarx
52 {
53  template<class T>
54  bool isNullptr(const T& p)
55  {
56  if constexpr(detail::HasGetMember<T>::value)
57  {
58  return p.get() == nullptr;
59  }
60  else
61  {
62  return p == nullptr;
63  }
64  }
65 
66  template<class T, class ExceptionType = std::invalid_argument>
67  auto CheckedDeref(const T& ptr, const std::source_location& loc = std::source_location::current())
68  {
69  if (isNullptr(ptr))
70  {
71  std::stringstream s;
72  s << "Ptr passed to CheckedDeref is NULL"
73  << "\nfile : " << loc.file_name()
74  << "\nline : " << loc.line()
75  << "\nfunction: " << loc.function_name();
76  throw ExceptionType {s.str()};
77  }
78  return *ptr;
79  }
80  template<class T, class M, class ExceptionType = std::invalid_argument>
81  auto CheckedDeref(const T& ptr, M && msg, const std::source_location& loc = std::source_location::current())
82  {
83  if (isNullptr(ptr))
84  {
85  std::stringstream s;
86  s << "Ptr passed to CheckedDeref is NULL"
87  << "\nfile : " << loc.file_name()
88  << "\nline : " << loc.line()
89  << "\nfunction: " << loc.function_name()
90  << "\nmessage :\n" << msg;
91  throw ExceptionType {s.str()};
92  }
93  return *ptr;
94  }
95 }
96 
std::source_location
experimental::source_location source_location
Definition: Pointer.h:37
armarx::isNullptr
bool isNullptr(const T &p)
Definition: Pointer.h:54
armarx::detail::HasGetMember
Definition: Pointer.h:45
armarx::CheckedDeref
auto CheckedDeref(const T &ptr, const std::source_location &loc=std::source_location::current())
Definition: Pointer.h:67
armarx::detail
Definition: ApplicationNetworkStats.cpp:34
armarx::meta::void_t
void void_t
Helper for sfinae (is added in c++17)
Definition: TemplateMetaProgramming.h:59
std
Definition: Application.h:66
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28