-
Notifications
You must be signed in to change notification settings - Fork 5
/
RosUtils.cs
60 lines (52 loc) · 1.94 KB
/
RosUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Copyright(c) 2019 LG Electronics, Inc.
*
* This software contains code licensed as described in LICENSE.
*
*/
using System;
using System.Text;
using System.Collections.Generic;
namespace Simulator.Bridge.Ros
{
static class RosUtils
{
public static bool IsGenericList(this Type type)
=> type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(List<>));
public static bool IsNullable(this Type type)
=> Nullable.GetUnderlyingType(type) != null;
public static void AppendEscaped(this StringBuilder sb, string text)
{
foreach (char c in text)
{
switch (c)
{
case '\\': sb.Append('\\'); sb.Append('\\'); break;
case '\"': sb.Append('\\'); sb.Append('"'); break;
case '\n': sb.Append('\\'); sb.Append('n'); break;
case '\r': sb.Append('\\'); sb.Append('r'); break;
case '\t': sb.Append('\\'); sb.Append('t'); break;
case '\b': sb.Append('\\'); sb.Append('b'); break;
case '\f': sb.Append('\\'); sb.Append('f'); break;
default: sb.Append(c); break;
}
}
}
public static string GetMessageType<BridgeType>()
{
var type = typeof(BridgeType);
string name;
if (RosSerialization.BuiltinMessageTypes.TryGetValue(type, out name))
{
return name;
}
object[] attributes = type.GetCustomAttributes(typeof(MessageTypeAttribute), false);
if (attributes == null || attributes.Length == 0)
{
throw new Exception($"Type {type.Name} does not have {nameof(MessageTypeAttribute)} attribute");
}
var attribute = attributes[0] as MessageTypeAttribute;
return attribute.Type;
}
}
}