source: finroc_plugins_data_ports/type_traits.h @ 49:718cc4437521

13.10
Last change on this file since 49:718cc4437521 was 49:718cc4437521, checked in by Michael Arndt <m_arndt@…>, 6 years ago

Adapted to changes in libstdc++ for GCC 4.8

File size: 5.1 KB
Line 
1//
2// You received this file as part of Finroc
3// A framework for intelligent robot control
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License along
18// with this program; if not, write to the Free Software Foundation, Inc.,
19// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20//
21//----------------------------------------------------------------------
22/*!\file    plugins/data_ports/type_traits.h
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2012-10-21
27 *
28 * \brief
29 *
30 * Various type traits relevant for data ports.
31 * May be specialized for certain types.
32 */
33//----------------------------------------------------------------------
34#ifndef __plugins__data_ports__type_traits_h__
35#define __plugins__data_ports__type_traits_h__
36
37//----------------------------------------------------------------------
38// External includes (system with <>, local with "")
39//----------------------------------------------------------------------
40#include "core/port/tEdgeAggregator.h"
41
42//----------------------------------------------------------------------
43// Internal includes with ""
44//----------------------------------------------------------------------
45
46//----------------------------------------------------------------------
47// Namespace declaration
48//----------------------------------------------------------------------
49namespace finroc
50{
51namespace data_ports
52{
53
54//----------------------------------------------------------------------
55// Forward declarations / typedefs / enums
56//----------------------------------------------------------------------
57namespace numeric
58{
59class tNumber;
60}
61
62//----------------------------------------------------------------------
63// Function declarations
64//----------------------------------------------------------------------
65
66/*!
67 * This type-trait is used to determine whether a type is a 'cheaply copied' type.
68 *
69 * In this case 'value' is true.
70 *
71 * Cheaply copied never block or allocate memory during copying.
72 * As a rule of thumb, all types that could be copied by using memcpy and that are not too big (max. 256 bytes)
73 * are 'cheaply copied' types.
74 *
75 * Interestingly, std::has_trivial_destructor is a pretty good heuristic whether a type is a 'cheaply copied' type.
76 */
77template <typename T>
78struct tIsCheaplyCopiedType
79{
80#if __GNUC__ == 4 && __GNUC_MINOR__ >= 8
81  enum { value = std::is_trivially_destructible<T>::value && (sizeof(T) <= 256) };
82#else
83  enum { value = std::has_trivial_destructor<T>::value && (sizeof(T) <= 256) };
84#endif
85};
86
87/*!
88 * Equivalent for runtime "cheaply copied" type identification
89 */
90inline bool IsCheaplyCopiedType(const rrlib::rtti::tType& dt)
91{
92  return dt.GetSize() <= 256 && ((dt.GetTypeTraits() & rrlib::rtti::trait_flags::cHAS_TRIVIAL_DESTRUCTOR) != 0);
93}
94
95/*!
96 * \return True, if the provided type is a data flow type
97 */
98inline static bool IsDataFlowType(const rrlib::rtti::tType& type)
99{
100  return core::tEdgeAggregator::IsDataFlowType(type);
101}
102
103/*!
104 * Type-trait for numeric types.
105 * This includes all built-in numeric types, as well as all
106 * types that can be implicitly casted to and from a built-in numeric type.
107 * For the latter, this template needs to be specialized.
108 */
109template <typename T>
110struct tIsNumeric
111{
112  enum { value = std::is_integral<T>::value || std::is_floating_point<T>::value || std::is_same<T, numeric::tNumber>::value };
113};
114template <>
115struct tIsNumeric<bool>
116{
117  enum { value = 0 };
118};
119static_assert(!tIsNumeric<bool>::value, "Bool should not be handled as numeric type");
120
121/*!
122 * This type-trait is used to determine whether a type supports operator '<' .
123 */
124template <typename T>
125struct tHasSmallerThanOperator
126{
127  template <typename U = T>
128  static int16_t Test(decltype((*(U*)(NULL)) < (*(U*)(NULL))))
129  {
130    return 0;
131  }
132
133  static int32_t Test(...)
134  {
135    return 0;
136  }
137
138  enum { value = sizeof(Test(true)) == sizeof(int16_t) };
139};
140
141/*!
142 * This type-trait determines whether a type is boundable in ports
143 */
144template <typename T>
145struct tIsBoundable
146{
147  enum { value = tIsCheaplyCopiedType<T>::value && tHasSmallerThanOperator<T>::value && (!std::is_same<bool, T>::value) };
148};
149
150/*!
151 * This type-trait determines whether a type is a string type
152 */
153template <typename T>
154struct tIsString
155{
156  enum { value = std::is_same<T, std::string>::value || std::is_same<T, tString>::value || std::is_same<T, char*>::value || std::is_same<T, const char*>::value || std::is_same<typename std::remove_extent<T>::type, char>::value };
157};
158
159//----------------------------------------------------------------------
160// End of namespace declaration
161//----------------------------------------------------------------------
162}
163}
164
165
166#endif
Note: See TracBrowser for help on using the repository browser.