]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/module.cpp
Use C++11 features with containers
[libs/gl.git] / source / core / module.cpp
index 2474feaeb4cfffc8907caf4c1de730d40e5718cd..601aae99c8952e355016562d4589e333cfbd2798 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/algorithm.h>
 #include <msp/io/print.h>
 #include "module.h"
 #include "resources.h"
@@ -100,18 +101,18 @@ SpirVModule &SpirVModule::operator=(const SpirVModule &other)
 
 void SpirVModule::remap_pointers_from(const SpirVModule &other)
 {
-       for(vector<EntryPoint>::iterator i=entry_points.begin(); i!=entry_points.end(); ++i)
-               for(vector<const Variable *>::iterator j=i->globals.begin(); j!=i->globals.end(); ++j)
-                       *j = &variables[*j-&other.variables.front()];
+       for(EntryPoint &e: entry_points)
+               for(const Variable *&v: e.globals)
+                       v = &variables[v-&other.variables.front()];
 
-       for(vector<Variable>::iterator i=variables.begin(); i!=variables.end(); ++i)
-               if(i->struct_type)
-                       i->struct_type = &structs[i->struct_type-&other.structs.front()];
+       for(Variable &v: variables)
+               if(v.struct_type)
+                       v.struct_type = &structs[v.struct_type-&other.structs.front()];
 
-       for(vector<Structure>::iterator i=structs.begin(); i!=structs.end(); ++i)
-               for(vector<StructMember>::iterator j=i->members.begin(); j!=i->members.end(); ++j)
-                       if(j->struct_type)
-                               j->struct_type = &structs[j->struct_type-&other.structs.front()];
+       for(Structure &s: structs)
+               for(StructMember &m: s.members)
+                       if(m.struct_type)
+                               m.struct_type = &structs[m.struct_type-&other.structs.front()];
 }
 
 void SpirVModule::load_code(IO::Base &io)
@@ -144,8 +145,8 @@ void SpirVModule::reflect()
 
        if(code[0]==SPIRV_MAGIC_REVERSED)
        {
-               for(vector<UInt32>::iterator i=code.begin(); i!=code.end(); ++i)
-                       *i = ((*i&0xFF)<<24) || ((*i&0xFF00)<<8) | ((*i>>8)&0xFF00) | ((*i>>24)&0xFF);
+               for(UInt32 &c: code)
+                       c = ((c&0xFF)<<24) || ((c&0xFF00)<<8) | ((c>>8)&0xFF00) | ((c>>24)&0xFF);
        }
        else if(code[0]!=SPIRV_MAGIC)
                throw invalid_module("SPIR-V magic number not found");
@@ -154,38 +155,38 @@ void SpirVModule::reflect()
        reflection.reflect_code(code);
 
        map<const Constant *, unsigned> spec_indices;
-       for(map<unsigned, Constant>::const_iterator i=reflection.constants.begin(); i!=reflection.constants.end(); ++i)
-               if(i->second.constant_id>=0)
+       for(const auto &kvp: reflection.constants)
+               if(kvp.second.constant_id>=0)
                {
-                       spec_indices[&i->second] = spec_constants.size();
-                       spec_constants.push_back(i->second);
+                       spec_indices[&kvp.second] = spec_constants.size();
+                       spec_constants.push_back(kvp.second);
                }
 
        map<const Structure *, unsigned> struct_indices;
        structs.reserve(reflection.structs.size());
-       for(map<unsigned, Structure>::const_iterator i=reflection.structs.begin(); i!=reflection.structs.end(); ++i)
+       for(const auto &kvp: reflection.structs)
        {
-               struct_indices[&i->second] = structs.size();
-               structs.push_back(i->second);
+               struct_indices[&kvp.second] = structs.size();
+               structs.push_back(kvp.second);
        }
 
-       for(vector<Structure>::iterator i=structs.begin(); i!=structs.end(); ++i)
+       for(Structure &s: structs)
        {
-               for(vector<StructMember>::iterator j=i->members.begin(); j!=i->members.end(); ++j)
+               for(StructMember &m: s.members)
                {
-                       if(j->struct_type)
+                       if(m.struct_type)
                        {
-                               map<const Structure *, unsigned>::const_iterator k = struct_indices.find(j->struct_type);
-                               j->struct_type = (k!=struct_indices.end() ? &structs[k->second] : 0);
+                               auto i = struct_indices.find(m.struct_type);
+                               m.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
                        }
-                       if(j->array_size_spec)
+                       if(m.array_size_spec)
                        {
-                               map<const Constant *, unsigned>::const_iterator k = spec_indices.find(j->array_size_spec);
-                               j->array_size_spec = (k!=spec_indices.end() ? &spec_constants[k->second] : 0);
+                               auto i = spec_indices.find(m.array_size_spec);
+                               m.array_size_spec = (i!=spec_indices.end() ? &spec_constants[i->second] : 0);
                        }
                }
 
-               const StructMember *last_member = &i->members.back();
+               const StructMember *last_member = &s.members.back();
                unsigned last_offset = last_member->offset;
                while(last_member->struct_type)
                {
@@ -198,51 +199,47 @@ void SpirVModule::reflect()
                        last_member = lm;
                }
                
-               i->size = last_offset+get_type_size(last_member->type);
-               i->size = (i->size+15)&~15;
+               s.size = last_offset+get_type_size(last_member->type);
+               s.size = (s.size+15)&~15;
        }
 
        map<const Variable *, unsigned> var_indices;
        variables.reserve(reflection.variables.size());
-       for(map<unsigned, Variable>::const_iterator i=reflection.variables.begin(); i!=reflection.variables.end(); ++i)
+       for(const auto &kvp: reflection.variables)
        {
-               int dup_index = -1;
-               for(vector<Variable>::const_iterator j=variables.begin(); (dup_index<0 && j!=variables.end()); ++j)
-                       if(*j==i->second)
-                               dup_index = j-variables.begin();
-
-               if(dup_index>=0)
-                       var_indices[&i->second] = dup_index;
+               auto i = find_if(variables, [&kvp](const Variable &v){ return v==kvp.second; });
+               if(i!=variables.end())
+                       var_indices[&kvp.second] = i-variables.begin();
                else
                {
-                       var_indices[&i->second] = variables.size();
-                       variables.push_back(i->second);
+                       var_indices[&kvp.second] = variables.size();
+                       variables.push_back(kvp.second);
                }
        }
 
-       for(vector<Variable>::iterator i=variables.begin(); i!=variables.end(); ++i)
+       for(Variable &v: variables)
        {
-               if(i->struct_type)
+               if(v.struct_type)
                {
-                       map<const Structure *, unsigned>::const_iterator j = struct_indices.find(i->struct_type);
-                       i->struct_type = (j!=struct_indices.end() ? &structs[j->second] : 0);
+                       auto i = struct_indices.find(v.struct_type);
+                       v.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
                }
-               if(i->array_size_spec)
+               if(v.array_size_spec)
                {
-                       map<const Constant *, unsigned>::const_iterator j = spec_indices.find(i->array_size_spec);
-                       i->array_size_spec = (j!=spec_indices.end() ? &spec_constants[j->second] : 0);
+                       auto i = spec_indices.find(v.array_size_spec);
+                       v.array_size_spec = (i!=spec_indices.end() ? &spec_constants[i->second] : 0);
                }
        }
 
        entry_points.reserve(reflection.entry_points.size());
-       for(map<unsigned, EntryPoint>::const_iterator i=reflection.entry_points.begin(); i!=reflection.entry_points.end(); ++i)
+       for(const auto &kvp: reflection.entry_points)
        {
-               entry_points.push_back(i->second);
+               entry_points.push_back(kvp.second);
                EntryPoint &entry = entry_points.back();
-               for(vector<const Variable *>::iterator j=entry.globals.begin(); j!=entry.globals.end(); ++j)
+               for(const Variable *&v: entry.globals)
                {
-                       map<const Variable *, unsigned>::const_iterator k = var_indices.find(*j);
-                       *j = (k!=var_indices.end() ? &variables[k->second] : 0);
+                       auto i = var_indices.find(v);
+                       v = (i!=var_indices.end() ? &variables[i->second] : 0);
                }
        }
 }
@@ -479,7 +476,7 @@ void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
        op += 2;
 
        strct.members.resize(op_end-op);
-       vector<StructMember>::iterator mem = strct.members.begin();
+       auto mem = strct.members.begin();
        for(; op!=op_end; ++op, ++mem)
        {
                TypeInfo &type = types[*op];