[solved] C++ question / PQ::Element suddenly has no name

Programmers discuss here anything related to FreeOrion programming. Primarily for the developers to discuss.

Moderator: Committer

Post Reply
Message
Author
Ophiuchus
Programmer
Posts: 3433
Joined: Tue Sep 30, 2014 10:01 am
Location: Wall IV

[solved] C++ question / PQ::Element suddenly has no name

#1 Post by Ophiuchus »

I'm working on the imperial stockpile and encounter a C++ behaviour I do not understand.

I have a for loop going through all queue elements looking for imperial
projects (i.e. projects which are allowed to use imperial stockpile)

In the loop the element has the ship design name.
As soon as I leave the loop, the ship design name is not there anymore.

From the log you can see that the name: Comsat is gone in update 4 (which is directly after leaving the scope of the for-loop:

Code: Select all

ProductionQueue::Update: 3      seems imperial project was saved: ProductionQueue::Element (ProductionItem: BT_SHIP name: Comsatid: 1) (1) x1  (remaining: 1)
ProductionQueue::Update: 3 last seems imperial project was saved: ProductionQueue::Element (ProductionItem: BT_SHIP name: Comsatid: 1) (1) x1  (remaining: 1)
ProductionQueue::Update: 4      seems imperial project was saved: ProductionQueue::Element (ProductionItem: BT_SHIP id: 1) (1) x1 (remaining: 1)
ProductionQueue::Update: 4 last seems imperial project was saved: ProductionQueue::Element (ProductionItem: BT_SHIP id: 1) (1) x1 (remaining: 1)

the relevant code part:

Code: Select all

// type of QueueType is std::deque<ProductionQueue::Element>
QueueType                   dpsim_queue = sim_queue;
...

std::vector<std::reference_wrapper<ProductionQueue::Element>> imperial_projects;
...

Element* last_one;
for (auto queue_element : dpsim_queue) {
    if (queue_element.allowed_imperial_stockpile_use) {
        imperial_projects.push_back(std::ref(queue_element));
    }
    last_one = &queue_element;
    if (imperial_projects.size() > 0)    DebugLogger() << "ProductionQueue::Update: 3      seems imperial project was saved: " << imperial_projects.back().get().Dump();
    if (last_one)    DebugLogger() << "ProductionQueue::Update: 3 last seems imperial project was saved: " << (*last_one).Dump();
}
if (imperial_projects.size() > 0) DebugLogger() << "ProductionQueue::Update: 4      seems imperial project was saved: " << imperial_projects.back().get().Dump();
if (last_one)    DebugLogger() << "ProductionQueue::Update: 4 last seems imperial project was saved: " << (*last_one).Dump();
full code in the following commit:
https://github.com/agrrr3/freeorion/com ... 5d72b65c04

I'm baffled, can one explain it to me?
Last edited by Ophiuchus on Fri Jun 09, 2017 8:22 am, edited 1 time in total.
Any code or patches in anything posted here is released under the CC and GPL licences in use for the FO project.

Look, ma... four combat bouts!

User avatar
Geoff the Medio
Programming, Design, Admin
Posts: 13587
Joined: Wed Oct 08, 2003 1:33 am
Location: Munich

Re: C++ question / ProductionQueue::Element suddenly has no

#2 Post by Geoff the Medio »

Should it be "auto& queue_element : dpsim_queue" so that you're taking the address of the Element in the queue rather than of a copy of it?

Ophiuchus
Programmer
Posts: 3433
Joined: Tue Sep 30, 2014 10:01 am
Location: Wall IV

Re: C++ question / ProductionQueue::Element suddenly has no

#3 Post by Ophiuchus »

Geoff the Medio wrote:Should it be "auto& queue_element : dpsim_queue" so that you're taking the address of the Element in the queue rather than of a copy of it?
Phew, thanks:!: Don't know how I missed that :)
Any code or patches in anything posted here is released under the CC and GPL licences in use for the FO project.

Look, ma... four combat bouts!

Post Reply