From: Mikko Rasa Date: Wed, 17 Nov 2021 15:12:19 +0000 (+0200) Subject: Use emplace_back when a new object is being constructed X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=5871764de7aa23d2c40cac03ad9d07088fb57e06;p=libs%2Fgl.git Use emplace_back when a new object is being constructed --- diff --git a/source/animation/animation.cpp b/source/animation/animation.cpp index 76d9552e..a79e1f8d 100644 --- a/source/animation/animation.cpp +++ b/source/animation/animation.cpp @@ -164,7 +164,7 @@ void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, bool for(const auto &kvp: kf_uniforms) { if(find_member(uniforms, kvp.first, &UniformInfo::name)==uniforms.end()) - uniforms.push_back(UniformInfo(kvp.first, kvp.second.size)); + uniforms.emplace_back(kvp.first, kvp.second.size); } } @@ -246,17 +246,17 @@ void Animation::create_curve(CurveTarget target, int component, const T &extract { typename Knot::Value cv = knots.back().y; knots.back().y = (knots[knots.size()-2].y+cv*2.0)/3.0; - knots.push_back(Knot(x, (dvalue+cv*2.0)/3.0)); + knots.emplace_back(x, (dvalue+cv*2.0/3.0)); } else if(n_control==0 && !knots.empty()) { typename Knot::Value prev = knots.back().y; - knots.push_back(Knot(knots.back().x, (prev*2.0+dvalue)/3.0)); - knots.push_back(Knot(x, (prev+dvalue*2.0)/3.0)); + knots.emplace_back(knots.back().x, (prev*2.0+dvalue)/3.0); + knots.emplace_back(x, (prev+dvalue*2.0/3.0)); } n_control = 0; } - knots.push_back(Knot(x, value)); + knots.emplace_back(x, value); } } diff --git a/source/animation/animationplayer.cpp b/source/animation/animationplayer.cpp index 42605bea..44b8e466 100644 --- a/source/animation/animationplayer.cpp +++ b/source/animation/animationplayer.cpp @@ -31,7 +31,7 @@ AnimationPlayer::Target &AnimationPlayer::play_(Placeable &obj, const Animation target.stacked = stacked; // TODO check for incompatible armature target.armature = anim.get_armature(); - target.animations.push_back(PlayingAnimation(anim, speed)); + target.animations.emplace_back(anim, speed); return target; } diff --git a/source/animation/armature.cpp b/source/animation/armature.cpp index d8512d82..acfffbf7 100644 --- a/source/animation/armature.cpp +++ b/source/animation/armature.cpp @@ -8,7 +8,7 @@ namespace GL { Armature::Link &Armature::add_link() { - links.push_back(Link(string(), links.size())); + links.emplace_back(string(), links.size()); return links.back(); } diff --git a/source/backends/opengl/program_backend.cpp b/source/backends/opengl/program_backend.cpp index 685f1542..a9328488 100644 --- a/source/backends/opengl/program_backend.cpp +++ b/source/backends/opengl/program_backend.cpp @@ -274,7 +274,7 @@ void OpenGLProgram::query_uniforms() if(len>3 && !strcmp(name+len-3, "[0]")) name[len-3] = 0; - rd.uniforms.push_back(ReflectData::UniformInfo()); + rd.uniforms.emplace_back(); ReflectData::UniformInfo &info = rd.uniforms.back(); info.name = name; info.tag = name; @@ -296,7 +296,7 @@ void OpenGLProgram::query_uniforms() query_uniform_blocks(uniforms_by_index); } - rd.uniform_blocks.push_back(ReflectData::UniformBlockInfo()); + rd.uniform_blocks.emplace_back(); ReflectData::UniformBlockInfo &default_block = rd.uniform_blocks.back(); for(ReflectData::UniformInfo &u: rd.uniforms) @@ -331,7 +331,7 @@ void OpenGLProgram::query_uniform_blocks(const vector3 && !strcmp(name+len-3, "[0]")) name[len-3] = 0; - rd.attributes.push_back(ReflectData::AttributeInfo()); + rd.attributes.emplace_back(); ReflectData::AttributeInfo &info = rd.attributes.back(); info.name = name; info.location = glGetAttribLocation(id, name); diff --git a/source/builders/sequencetemplate.cpp b/source/builders/sequencetemplate.cpp index 3f96f0bb..2886e0a8 100644 --- a/source/builders/sequencetemplate.cpp +++ b/source/builders/sequencetemplate.cpp @@ -185,7 +185,7 @@ void SequenceTemplate::ClearLoader::init_actions() void SequenceTemplate::ClearLoader::color(float r, float g, float b, float a) { - obj.clear_colors.push_back(Color(r, g, b, a)); + obj.clear_colors.emplace_back(r, g, b, a); } void SequenceTemplate::ClearLoader::depth(float d) diff --git a/source/core/program.cpp b/source/core/program.cpp index bc61e1db..16b2fa79 100644 --- a/source/core/program.cpp +++ b/source/core/program.cpp @@ -113,7 +113,7 @@ void Program::collect_visited_blocks(const vector void Program::collect_uniforms(const SpirVModule &mod, const vector &used_variables) { // Prepare the default block - reflect_data.uniform_blocks.push_back(ReflectData::UniformBlockInfo()); + reflect_data.uniform_blocks.emplace_back(); vector > block_uniform_names(1); const vector &variables = mod.get_variables(); @@ -126,7 +126,7 @@ void Program::collect_uniforms(const SpirVModule &mod, const vector &us const SpirVModule::Variable &v = variables[i]; if((v.storage==SpirVModule::UNIFORM || v.storage==SpirVModule::PUSH_CONSTANT) && v.struct_type) { - reflect_data.uniform_blocks.push_back(ReflectData::UniformBlockInfo()); + reflect_data.uniform_blocks.emplace_back(); ReflectData::UniformBlockInfo &info = reflect_data.uniform_blocks.back(); info.name = v.struct_type->name; if(v.storage==SpirVModule::PUSH_CONSTANT) @@ -144,13 +144,13 @@ void Program::collect_uniforms(const SpirVModule &mod, const vector &us string prefix; if(!v.name.empty()) prefix = v.struct_type->name+"."; - block_uniform_names.push_back(vector()); + block_uniform_names.emplace_back(); collect_block_uniforms(*v.struct_type, prefix, 0, block_uniform_names.back()); } else if(v.storage==SpirVModule::UNIFORM_CONSTANT && (v.location>=0 || v.binding>=0)) { block_uniform_names[0].push_back(v.name); - reflect_data.uniforms.push_back(ReflectData::UniformInfo()); + reflect_data.uniforms.emplace_back(); ReflectData::UniformInfo &info = reflect_data.uniforms.back(); info.name = v.name; info.tag = v.name; @@ -208,7 +208,7 @@ void Program::collect_block_uniforms(const SpirVModule::Structure &strct, const { string name = prefix+m.name; uniform_names.push_back(name); - reflect_data.uniforms.push_back(ReflectData::UniformInfo()); + reflect_data.uniforms.emplace_back(); ReflectData::UniformInfo &info = reflect_data.uniforms.back(); info.name = name; info.tag = name; @@ -230,7 +230,7 @@ void Program::collect_attributes(const SpirVModule &mod, const vector & for(const SpirVModule::Variable *v: e.globals) if(v->storage==SpirVModule::INPUT && used_variables[v-variables.data()]) { - reflect_data.attributes.push_back(ReflectData::AttributeInfo()); + reflect_data.attributes.emplace_back(); ReflectData::AttributeInfo &info = reflect_data.attributes.back(); info.name = v->name; info.location = v->location; diff --git a/source/glsl/debug.cpp b/source/glsl/debug.cpp index 5b142019..60ac76f2 100644 --- a/source/glsl/debug.cpp +++ b/source/glsl/debug.cpp @@ -305,11 +305,11 @@ void DumpTree::visit(BasicTypeDeclaration &type) vector branches; if(type.base_type) - branches.push_back(format("%s: %%%d %s", (type.kind==BasicTypeDeclaration::ALIAS ? "Alias of" : "Base"), get_label(*type.base_type), type.base_type->name)); + branches.emplace_back(format("%s: %%%d %s", (type.kind==BasicTypeDeclaration::ALIAS ? "Alias of" : "Base"), get_label(*type.base_type), type.base_type->name)); if(type.kind==BasicTypeDeclaration::VECTOR) - branches.push_back(format("Vector: %d", type.size)); + branches.emplace_back(format("Vector: %d", type.size)); else if(type.kind==BasicTypeDeclaration::MATRIX) - branches.push_back(format("Matrix: %dx%d", type.size&0xFFFF, type.size>>16)); + branches.emplace_back(format("Matrix: %dx%d", type.size&0xFFFF, type.size>>16)); append_subtree(branches); } @@ -320,11 +320,11 @@ void DumpTree::visit(ImageTypeDeclaration &type) append(type, format("%%%d typedef %s", get_label(type), type.name)); vector branches; - branches.push_back(format("Dimensions: %s%s", dims[type.dimensions-1], (type.array ? " array" : ""))); + branches.emplace_back(format("Dimensions: %s%s", dims[type.dimensions-1], (type.array ? " array" : ""))); if(type.base_type) - branches.push_back(format("Element type: %%%d %s", get_label(*type.base_type), type.base_type->name)); + branches.emplace_back(format("Element type: %%%d %s", get_label(*type.base_type), type.base_type->name)); if(type.shadow) - branches.push_back("Shadow"); + branches.emplace_back("Shadow"); append_subtree(branches); } @@ -362,7 +362,7 @@ void DumpTree::visit(VariableDeclaration &var) if(var.array) { if(var.array_size) - branches.push_back(Branch("Array []", var.array_size.get())); + branches.emplace_back("Array []", var.array_size.get()); else branches.push_back("Array []"); } @@ -421,10 +421,10 @@ void DumpTree::visit(Conditional &cond) append(cond, "if()"); vector branches; - branches.push_back(cond.condition.get()); - branches.push_back(Branch("then", &cond.body)); + branches.emplace_back(cond.condition.get()); + branches.emplace_back("then", &cond.body); if(!cond.else_body.body.empty()) - branches.push_back(Branch("else", &cond.else_body)); + branches.emplace_back("else", &cond.else_body); append_subtree(branches); } @@ -434,12 +434,12 @@ void DumpTree::visit(Iteration &iter) vector branches; if(iter.init_statement) - branches.push_back(Branch("Initialization", iter.init_statement.get())); + branches.emplace_back("Initialization", iter.init_statement.get()); if(iter.condition) - branches.push_back(Branch("Condition", iter.condition.get())); + branches.emplace_back("Condition", iter.condition.get()); if(iter.loop_expression) - branches.push_back(Branch("Loop", iter.loop_expression.get())); - branches.push_back(&iter.body); + branches.emplace_back("Loop", iter.loop_expression.get()); + branches.emplace_back(&iter.body); append_subtree(branches); } diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index ea0f96f0..da951589 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -449,7 +449,7 @@ void ExpressionInliner::visit(Assignment &assign) if(targets_overlap(i->first, assign.target)) i->second->blocked = true; - expressions.push_back(ExpressionInfo()); + expressions.emplace_back(); ExpressionInfo &info = expressions.back(); info.target = assign.target; // Self-referencing assignments can't be inlined without additional work. @@ -496,7 +496,7 @@ void ExpressionInliner::visit(VariableDeclaration &var) analyze and non-trivial expressions could be expensive to inline. */ if((current_block->parent || (constant && r_trivial)) && var.interface.empty()) { - expressions.push_back(ExpressionInfo()); + expressions.emplace_back(); ExpressionInfo &info = expressions.back(); info.target = &var; /* Assume variables declared in an iteration initialization statement @@ -1420,7 +1420,7 @@ void UnusedVariableRemover::visit(FunctionCall &call) void UnusedVariableRemover::record_assignment(const Assignment::Target &target, Node &node) { - assignments.push_back(AssignmentInfo()); + assignments.emplace_back(); AssignmentInfo &assign_info = assignments.back(); assign_info.node = &node; assign_info.target = target; diff --git a/source/materials/rendermethod.cpp b/source/materials/rendermethod.cpp index 5c838ef9..ddbe4f1e 100644 --- a/source/materials/rendermethod.cpp +++ b/source/materials/rendermethod.cpp @@ -81,7 +81,7 @@ void RenderMethod::set_texture(Tag tag, const Texture *tex, const Sampler *samp) auto i = find_member(textures, tag, &TextureSlot::tag); if(i==textures.end()) { - textures.push_back(TextureSlot(tag)); + textures.emplace_back(tag); i = textures.end()-1; } i->texture = tex; @@ -216,7 +216,7 @@ void RenderMethod::Loader::texture(const string &n) auto i = find_member(obj.textures, Tag(n), &TextureSlot::tag); if(i==obj.textures.end()) { - obj.textures.push_back(TextureSlot(n)); + obj.textures.emplace_back(n); i = obj.textures.end()-1; } TextureSlot::Loader ldr(*i, n, coll); diff --git a/source/render/object.cpp b/source/render/object.cpp index e2e95a9b..65e9a7ef 100644 --- a/source/render/object.cpp +++ b/source/render/object.cpp @@ -112,7 +112,7 @@ void Object::update_bounding_sphere() for(unsigned j=0; j(vertices[j]+offset); - points.push_back(Vector3(v[0], v[1], (three ? v[2] : 0.0f))); + points.emplace_back(v[0], v[1], (three ? v[2] : 0.0f)); } } diff --git a/source/render/renderer.cpp b/source/render/renderer.cpp index 58a56ff2..45ad1200 100644 --- a/source/render/renderer.cpp +++ b/source/render/renderer.cpp @@ -32,7 +32,7 @@ void Renderer::begin() if(current_state) throw invalid_operation("Renderer::begin"); - state_stack.push_back(State()); + state_stack.emplace_back(); current_state = &state_stack.back(); RendererBackend::begin(); @@ -163,7 +163,7 @@ void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp) break; } - texture_stack.push_back(BoundTexture()); + texture_stack.emplace_back(); BoundTexture &bound_tex = texture_stack.back(); bound_tex.tag = tag; bound_tex.texture = tex; diff --git a/source/render/sequence.cpp b/source/render/sequence.cpp index 6c98b402..74b8bc3b 100644 --- a/source/render/sequence.cpp +++ b/source/render/sequence.cpp @@ -65,7 +65,7 @@ void Sequence::set_clear_stencil(int s) Sequence::Step &Sequence::add_step(Tag tag, Renderable &r) { - steps.push_back(Step(tag, &r)); + steps.emplace_back(tag, &r); return steps.back(); } diff --git a/source/resources/resourcemanager.cpp b/source/resources/resourcemanager.cpp index c72b361c..91b37424 100644 --- a/source/resources/resourcemanager.cpp +++ b/source/resources/resourcemanager.cpp @@ -403,7 +403,7 @@ void ResourceManager::LoadingThread::main() catch(const exception &e) { MutexLock lock(queue_mutex); - error_queue.push_back(resource_load_error(managed->location.name, e)); + error_queue.emplace_back(managed->location.name, e); managed->state = ManagedResource::LOAD_ERROR; }