FreeOrion

Forums for the FreeOrion project
It is currently Sun Oct 22, 2017 6:08 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue May 30, 2017 7:59 am 
Offline
Space Kraken

Joined: Tue Sep 30, 2014 10:01 am
Posts: 196
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:
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:
// 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/commit/b75ce6f56a34d384429b04a13680485d72b65c04

I'm baffled, can one explain it to me?

_________________
Any code or patches in anything posted here is released under the CC and GPL licences in use for the FO project.


Last edited by Ophiuchus on Fri Jun 09, 2017 8:22 am, edited 1 time in total.

Top
 Profile  
 
PostPosted: Tue May 30, 2017 9:29 am 
Offline
Programming, Design, Admin
User avatar

Joined: Wed Oct 08, 2003 1:33 am
Posts: 12004
Location: Munich
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?


Top
 Profile  
 
PostPosted: Tue May 30, 2017 1:32 pm 
Offline
Space Kraken

Joined: Tue Sep 30, 2014 10:01 am
Posts: 196
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group