Browse Source

Fixes class generation to only load used branches

Caleb 6 years ago
parent
commit
a83921cf4b
1 changed files with 13 additions and 7 deletions
  1. 13 7
      tools/generate_class.py

+ 13 - 7
tools/generate_class.py

@@ -16,6 +16,7 @@ class {obj_name}Collection {{
         iter operator++() {{ ++idx; return *this; }}
         iter operator++() {{ ++idx; return *this; }}
         bool operator!=(const iter & other) {{ return idx != other.idx; }}
         bool operator!=(const iter & other) {{ return idx != other.idx; }}
         const {obj_name} operator*() const;
         const {obj_name} operator*() const;
+        TrackingDataSet* tds;
       private:
       private:
         const {obj_name}Collection* collection;
         const {obj_name}Collection* collection;
         size_t idx;
         size_t idx;
@@ -27,15 +28,12 @@ class {obj_name}Collection {{
         name = field['name']
         name = field['name']
         type_ = field['type']
         type_ = field['type']
         src.append(f'    Value<vector<{type_}>>* val_{name};')
         src.append(f'    Value<vector<{type_}>>* val_{name};')
+        src.append(f'    bool {name}_loaded;')
 
 
     src.append(f'\n    {obj_name}Collection() {{ }}\n')
     src.append(f'\n    {obj_name}Collection() {{ }}\n')
 
 
-    src.append('    void init(TrackingDataSet& tds){')
-    for field in obj_attrs['fields']:
-        name = field['name']
-        type_ = field['type']
-        prefix = obj_attrs['treename_prefix']+'_'
-        src.append(f'        val_{name} = tds.track_branch_obj<vector<{type_}>>("{prefix}{name}");')
+    src.append('    void init(TrackingDataSet* tds){')
+    src.append('        this->tds = tds;')
     src.append('    }\n')
     src.append('    }\n')
     first_obj_name = list(obj_attrs['fields'])[0]['name']
     first_obj_name = list(obj_attrs['fields'])[0]['name']
     src.append(f'    size_t size() const {{ return (*val_{first_obj_name})().size();}}\n')
     src.append(f'    size_t size() const {{ return (*val_{first_obj_name})().size();}}\n')
@@ -55,7 +53,15 @@ struct {obj_name} {{
     for field in obj_attrs['fields']:
     for field in obj_attrs['fields']:
         name = field['name']
         name = field['name']
         type_ = field['type']
         type_ = field['type']
-        src.append(f'    const {type_}& {name}() const {{return (*collection->val_{name})().at(idx);}}')
+        prefix = obj_attrs['treename_prefix']+'_'
+        src.append(f'''\
+    const {type_}& {name}() const {{
+        if (!collection->{name}_loaded) {{
+            collection->val_{name} = collection->tds->track_branch_obj<vector<{type_}>>("{prefix}{name}");
+            collection->{name}_loaded = true;
+        }}
+        return (*collection->val_{name})().at(idx);}}
+''')
     src.append('};')
     src.append('};')
 
 
     src.append(f'''
     src.append(f'''