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 #include <experimental/source_location>
31 
32 namespace std
33 {
35 }
36 
37 namespace armarx::detail
38 {
39  template<class, class = void>
40  struct HasGetMember : std::false_type {};
41 
42  template<class T>
43 struct HasGetMember<T, std::void_t<decltype(&T::get)>> : std::true_type {};
44 }
45 
46 namespace armarx
47 {
48  template<class T>
49  bool isNullptr(const T& p)
50  {
51  if constexpr(detail::HasGetMember<T>::value)
52  {
53  return p.get() == nullptr;
54  }
55  else
56  {
57  return p == nullptr;
58  }
59  }
60 
61  template<class T, class ExceptionType = std::invalid_argument>
62  auto CheckedDeref(const T& ptr, const std::source_location& loc = std::source_location::current())
63  {
64  if (isNullptr(ptr))
65  {
66  std::stringstream s;
67  s << "Ptr passed to CheckedDeref is NULL"
68  << "\nfile : " << loc.file_name()
69  << "\nline : " << loc.line()
70  << "\nfunction: " << loc.function_name();
71  throw ExceptionType {s.str()};
72  }
73  return *ptr;
74  }
75  template<class T, class M, class ExceptionType = std::invalid_argument>
76  auto CheckedDeref(const T& ptr, M && msg, const std::source_location& loc = std::source_location::current())
77  {
78  if (isNullptr(ptr))
79  {
80  std::stringstream s;
81  s << "Ptr passed to CheckedDeref is NULL"
82  << "\nfile : " << loc.file_name()
83  << "\nline : " << loc.line()
84  << "\nfunction: " << loc.function_name()
85  << "\nmessage :\n" << msg;
86  throw ExceptionType {s.str()};
87  }
88  return *ptr;
89  }
90 }
91 
std::source_location
experimental::source_location source_location
Definition: Pointer.h:34
armarx::isNullptr
bool isNullptr(const T &p)
Definition: Pointer.h:49
armarx::detail::HasGetMember
Definition: Pointer.h:40
armarx::CheckedDeref
auto CheckedDeref(const T &ptr, const std::source_location &loc=std::source_location::current())
Definition: Pointer.h:62
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