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
36namespace std
37{
38 using source_location = experimental::source_location;
39}
40#endif
41
42
43namespace armarx::detail
44{
45 template <class, class = void>
46 struct HasGetMember : std::false_type
47 {
48 };
49
50 template <class T>
51 struct HasGetMember<T, std::void_t<decltype(&T::get)>> : std::true_type
52 {
53 };
54} // namespace armarx::detail
55
56namespace armarx
57{
58 template <class T>
59 bool
60 isNullptr(const T& p)
61 {
63 {
64 return p.get() == nullptr;
65 }
66 else
67 {
68 return p == nullptr;
69 }
70 }
71
72 template <class T, class ExceptionType = std::invalid_argument>
73 auto
74 CheckedDeref(const T& ptr, const std::source_location& loc = std::source_location::current())
75 {
76 if (isNullptr(ptr))
77 {
78 std::stringstream s;
79 s << "Ptr passed to CheckedDeref is NULL"
80 << "\nfile : " << loc.file_name() << "\nline : " << loc.line()
81 << "\nfunction: " << loc.function_name();
82 throw ExceptionType{s.str()};
83 }
84 return *ptr;
85 }
86
87 template <class T, class M, class ExceptionType = std::invalid_argument>
88 auto
89 CheckedDeref(const T& ptr,
90 M&& msg,
91 const std::source_location& loc = std::source_location::current())
92 {
93 if (isNullptr(ptr))
94 {
95 std::stringstream s;
96 s << "Ptr passed to CheckedDeref is NULL"
97 << "\nfile : " << loc.file_name() << "\nline : " << loc.line()
98 << "\nfunction: " << loc.function_name() << "\nmessage :\n"
99 << msg;
100 throw ExceptionType{s.str()};
101 }
102 return *ptr;
103 }
104} // namespace armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
bool isNullptr(const T &p)
Definition Pointer.h:60
auto CheckedDeref(const T &ptr, const std::source_location &loc=std::source_location::current())
Definition Pointer.h:74
experimental::source_location source_location
Definition Pointer.h:38