[PATCH] Fix Dynamic Attribute Generation

Matthias Goldhoorn matthias.goldhoorn at dfki.de
Mon Sep 2 10:44:14 CEST 2013


This commit fixed bug https://rock.opendfki.de/ticket/272
---
 lib/orogen/gen/tasks.rb         |   42 ++++++++++++++++++++++++---------------
 lib/orogen/spec/task_context.rb |    8 ++++++++
 2 files changed, 34 insertions(+), 16 deletions(-)

diff --git a/lib/orogen/gen/tasks.rb b/lib/orogen/gen/tasks.rb
index e886b91..7fb6150 100644
--- a/lib/orogen/gen/tasks.rb
+++ b/lib/orogen/gen/tasks.rb
@@ -59,6 +59,12 @@ def register_for_generation
                     "RTT::Attribute< #{type.cxx_name} >").
                     initializer("_#{name}(\"#{name}\")").
                     constructor(constructor.join("\n"))
+                
+                if dynamic? && (setter_operation.task == task)
+                    task.add_code_to_base_method_before "updateDynamicAttributes","\tif(!set#{name.capitalize}(_#{name}.get())) return false;\n"
+                    setter_operation.base_body = "\t//Updates the classical value of this Attribute\n\t_#{name}.set(value); \n\treturn true;"
+                    setter_operation.body = "\t//TODO Add your code here \n\n  \t//Call the base function, DO-NOT Remove\n\treturn(#{task.name}Base::set#{name.capitalize}(value));"
+                end
             end
         end
 
@@ -468,6 +474,22 @@ def validate_constructors(filename, taskname)
                 end
             end
 
+            def create_dynamic_updater(name, function)
+                if self.send(function)
+                    add_base_method("bool",name)
+
+                    if superclass.send(function)
+                        #call the superclass method if needed, returning false if they failed. Otherwise check our dynamic properties
+                        #they are generated in register_for_generation, or returning true in the end
+                        add_code_to_base_method_after name,"\tif(!#{superclass.name}::#{name}()) return false;\n"
+                    end
+                    
+                    #Add the return true in any case
+                    add_code_to_base_method_after name,"\treturn true;\n"
+                end
+            end
+
+
 	    # Generate the code files for this task. This builds to classes:
 	    #
 	    # * a #{task.name}Base class in .orogen/tasks/#{task.name}Base.{cpp,hpp}
@@ -493,25 +515,13 @@ def generate
                         body("    return \"#{name}\";")
                 end
 
-                
-                if has_dynamic_properties?
-                    add_base_method("bool","updateDynamicProperties")
-
-                    if superclass.has_dynamic_properties?
-                        #call the superclass method if needed, returning false if they failed. Otherwise check our dynamic properties
-                        #they are generated in register_for_generation, or returning true in the end
-                        add_code_to_base_method_after "updateDynamicProperties","\tif(!#{superclass.name}::updateDynamicProperties()) return false;\n"
-                    end
-                    
-                    #Add the return true in any case
-                    add_code_to_base_method_after "updateDynamicProperties","\treturn true;\n"
-                end
-
-
+            
+                create_dynamic_updater("updateDynamicProperties","has_dynamic_properties?")
+                create_dynamic_updater("updateDynamicAttributes","has_dynamic_attributes?")
 
                 self_properties.each(&:register_for_generation) #needs to be called before operations, because it adds code to them
-                new_operations.each(&:register_for_generation)
                 self_attributes.each(&:register_for_generation)
+                new_operations.each(&:register_for_generation)
                 self_ports.each(&:register_for_generation)
                 extensions.each do |ext|
                     if ext.respond_to?(:register_for_generation)
diff --git a/lib/orogen/spec/task_context.rb b/lib/orogen/spec/task_context.rb
index 551ebc3..c19eaf1 100644
--- a/lib/orogen/spec/task_context.rb
+++ b/lib/orogen/spec/task_context.rb
@@ -1057,6 +1057,14 @@ def has_dynamic_properties?
                 end
                 return false
             end
+            
+            # Return true if this task interface has an dynamic property.
+            def has_dynamic_attributes?
+                self_attributes.each do |p|
+                    return true if p.dynamic?
+                end
+                return false
+            end
 
             # call-seq:
             #   dynamic_input_port name_regex, typename
-- 
1.7.10.4


--------------050804030901040202040102--


More information about the Rock-dev mailing list