forked from lgsvl/ROS2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ros2Utils.cs
51 lines (45 loc) · 1.59 KB
/
Ros2Utils.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
/**
* Copyright(c) 2019 LG Electronics, Inc.
*
* This software contains code licensed as described in LICENSE.
*
*/
using System;
using System.Collections.Generic;
namespace Simulator.Bridge.Ros2
{
static class Ros2Utils
{
static readonly Dictionary<Type, string> BuiltinMessageTypes = new Dictionary<Type, string>
{
{ typeof(bool), "std_msgs/Bool" },
{ typeof(sbyte), "std_msgs/Int8" },
{ typeof(short), "std_msgs/Int16" },
{ typeof(int), "std_msgs/Int32" },
{ typeof(long), "std_msgs/Int64" },
{ typeof(byte), "std_msgs/UInt8" },
{ typeof(ushort), "std_msgs/UInt16" },
{ typeof(uint), "std_msgs/UInt32" },
{ typeof(ulong), "std_msgs/UInt64" },
{ typeof(float), "std_msgs/Float32" },
{ typeof(double), "std_msgs/Float64" },
{ typeof(string), "std_msgs/String" },
};
public static string GetMessageType<BridgeType>()
{
var type = typeof(BridgeType);
string name;
if (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;
}
}
}