Example #1
0
 public void Add(RosMessageConstantDescriptor constantDescriptor)
 {
     if (constantDescriptor == null)
     {
         throw new ArgumentNullException(nameof(constantDescriptor));
     }
     _constants.Add(constantDescriptor);
 }
Example #2
0
        public static RosMessageDescriptor Create(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            var messageTypeAttribute = type.GetCustomAttributes(false)
                                       .OfType <RosMessageBaseAttribute>()
                                       .FirstOrDefault();

            if (messageTypeAttribute == null)
            {
                throw new NotSupportedException();
            }

            var descriptorBuilder = new RosMessageDescriptorBuilder();

            var rosType = RosType.Parse(messageTypeAttribute.MessageType);

            descriptorBuilder.SetRosType(rosType);

            // Get Fields from properties
            foreach (var property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                var rosFieldAttribute = GetRosMessageFieldAttribute(property);

                if (rosFieldAttribute == null)
                {
                    continue;
                }

                var rosFieldType  = GetRosFieldType(rosFieldAttribute.RosType, rosType.PackageName);
                var rosIdentifier = rosFieldAttribute.RosIdentifier ?? property.Name;

                var fieldDescriptor = new PropertyRosMessageFieldDescriptor(rosFieldAttribute.Index, rosFieldType,
                                                                            rosIdentifier, property);

                descriptorBuilder.Add(fieldDescriptor);
            }

            // Get Fields from fields
            foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.Public))
            {
                var rosFieldAttribute = GetRosMessageFieldAttribute(field);

                if (rosFieldAttribute == null)
                {
                    continue;
                }

                var rosFieldType  = GetRosFieldType(rosFieldAttribute.RosType, rosType.PackageName);
                var rosIdentifier = rosFieldAttribute.RosIdentifier ?? field.Name;

                var fieldDescriptor = new FieldRosMessageFieldDescriptor(rosFieldAttribute.Index, rosFieldType,
                                                                         rosIdentifier, field);

                descriptorBuilder.Add(fieldDescriptor);
            }

            // Get Constants
            foreach (var constant in GetConstants(type))
            {
                var rosConstantAttribute = constant.GetCustomAttributes(false)
                                           .OfType <RosMessageFieldAttribute>()
                                           .FirstOrDefault();

                if (rosConstantAttribute == null)
                {
                    continue;
                }

                var rosConstantType    = RosType.Parse(rosConstantAttribute.RosType);
                var constantValue      = constant.GetValue(null);
                var constantDescriptor = new RosMessageConstantDescriptor(rosConstantAttribute.Index, rosConstantType,
                                                                          rosConstantAttribute.RosIdentifier, constantValue);

                descriptorBuilder.Add(constantDescriptor);
            }


            return(descriptorBuilder.Build());
        }