rosidl_runtime_cpp  master
Generate the rosidl interfaces in C++.
traits.hpp
Go to the documentation of this file.
1 // Copyright 2018 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ROSIDL_RUNTIME_CPP__TRAITS_HPP_
16 #define ROSIDL_RUNTIME_CPP__TRAITS_HPP_
17 
18 #include <codecvt>
19 #include <iomanip>
20 #include <string>
21 #include <type_traits>
22 
24 {
25 
26 inline void value_to_yaml(bool value, std::ostream & out)
27 {
28  out << (value ? "true" : "false");
29 }
30 
31 inline void character_value_to_yaml(unsigned char value, std::ostream & out)
32 {
33  auto flags = out.flags();
34  out << "0x" << std::hex << std::setw(2) << std::setfill('0') << \
35  static_cast<uint16_t>(value);
36  out.flags(flags);
37 }
38 
39 inline void character_value_to_yaml(char16_t value, std::ostream & out)
40 {
41  auto flags = out.flags();
42  out << "\"\\u" << std::hex << std::setw(4) << std::setfill('0') << \
43  static_cast<uint_least16_t>(value) << "\"";
44  out.flags(flags);
45 }
46 
47 inline void value_to_yaml(float value, std::ostream & out)
48 {
49  auto flags = out.flags();
50  out << std::showpoint << value;
51  out.flags(flags);
52 }
53 
54 inline void value_to_yaml(double value, std::ostream & out)
55 {
56  auto flags = out.flags();
57  out << std::showpoint << value;
58  out.flags(flags);
59 }
60 
61 inline void value_to_yaml(long double value, std::ostream & out)
62 {
63  auto flags = out.flags();
64  out << std::showpoint << value;
65  out.flags(flags);
66 }
67 
68 inline void value_to_yaml(uint8_t value, std::ostream & out)
69 {
70  out << +value;
71 }
72 
73 inline void value_to_yaml(int8_t value, std::ostream & out)
74 {
75  out << +value;
76 }
77 
78 inline void value_to_yaml(uint16_t value, std::ostream & out)
79 {
80  out << value;
81 }
82 
83 inline void value_to_yaml(int16_t value, std::ostream & out)
84 {
85  out << value;
86 }
87 
88 inline void value_to_yaml(uint32_t value, std::ostream & out)
89 {
90  out << value;
91 }
92 
93 inline void value_to_yaml(int32_t value, std::ostream & out)
94 {
95  out << value;
96 }
97 
98 inline void value_to_yaml(uint64_t value, std::ostream & out)
99 {
100  out << value;
101 }
102 
103 inline void value_to_yaml(int64_t value, std::ostream & out)
104 {
105  out << value;
106 }
107 
108 inline void value_to_yaml(const std::string & value, std::ostream & out)
109 {
110  out << "\"";
111  size_t index = 0;
112  while (index < value.size()) {
113  size_t pos = value.find_first_of("\\\"", index);
114  if (pos == std::string::npos) {
115  pos = value.size();
116  }
117  out.write(&value[index], pos - index);
118  if (pos >= value.size()) {
119  break;
120  }
121  out << "\\" << value[pos];
122  index = pos + 1;
123  }
124  out << "\"";
125 }
126 
127 inline void value_to_yaml(const std::u16string & value, std::ostream & out)
128 {
129  out << "\"";
131  auto flags = out.flags();
132  size_t index = 0;
133  while (index < value.size()) {
134  uint_least16_t character = static_cast<uint_least16_t>(value[index]);
135  if (!(character & 0xff80)) { // ASCII
136  std::string character_as_string = convert.to_bytes(character);
137  out << std::hex << character_as_string.c_str();
138  } else if (!(character & 0xff00)) { // only 1 byte set
139  out << "\\x" << std::hex << std::setw(2) << std::setfill('0') << \
140  character;
141  } else {
142  out << "\\u" << std::hex << std::setw(4) << std::setfill('0') << \
143  character;
144  }
145  index += 1;
146  }
147  out.flags(flags);
148  out << "\"";
149 }
150 
151 template<typename T>
152 inline const char * data_type();
153 
154 template<typename T>
155 inline const char * name();
156 
157 template<typename T>
159 
160 template<typename T>
162 
163 template<typename T>
165 
166 template<typename T>
168 
169 template<typename T>
171 
172 template<typename T>
174 
175 template<typename T>
177 
178 template<typename T>
180 
181 template<typename T>
183 
184 template<typename T>
186 
187 } // namespace rosidl_generator_traits
188 
189 #endif // ROSIDL_RUNTIME_CPP__TRAITS_HPP_
std::showpoint
T showpoint(T... args)
rosidl_generator_traits::data_type
const char * data_type()
std::false_type
rosidl_generator_traits::is_service
Definition: traits.hpp:167
rosidl_generator_traits
Definition: traits.hpp:23
std::string
rosidl_generator_traits::is_action_feedback
Definition: traits.hpp:185
rosidl_generator_traits::name
const char * name()
rosidl_generator_traits::is_action_result
Definition: traits.hpp:182
std::string::size
T size(T... args)
std::wstring_convert::to_bytes
T to_bytes(T... args)
std::wstring_convert
std::setfill
T setfill(T... args)
rosidl_generator_traits::is_action_goal
Definition: traits.hpp:179
rosidl_generator_traits::is_message
Definition: traits.hpp:164
std::ostream::write
T write(T... args)
std::hex
T hex(T... args)
std::ostream
std::string::c_str
T c_str(T... args)
rosidl_generator_traits::is_service_response
Definition: traits.hpp:173
std::ostream::flags
T flags(T... args)
rosidl_generator_traits::character_value_to_yaml
void character_value_to_yaml(unsigned char value, std::ostream &out)
Definition: traits.hpp:31
rosidl_generator_traits::value_to_yaml
void value_to_yaml(bool value, std::ostream &out)
Definition: traits.hpp:26
std::string::find_first_of
T find_first_of(T... args)
std::setw
T setw(T... args)
rosidl_generator_traits::has_fixed_size
Definition: traits.hpp:158
rosidl_generator_traits::has_bounded_size
Definition: traits.hpp:161
rosidl_generator_traits::is_action
Definition: traits.hpp:176
rosidl_generator_traits::is_service_request
Definition: traits.hpp:170