|
[Sponsors] |
July 28, 2022, 02:00 |
How and when use tmp<vol****Fields>?
|
#1 |
Senior Member
Agustín Villa
Join Date: Apr 2013
Location: Alcorcón
Posts: 313
Rep Power: 15 |
Hi,
I know that this topic has been discussed previously: But it still not clear, at least ceraint aspects, for me. I am not a C++ developer but a OpenFOAM user that deals with turbulence model implementations. I had at the university a course where pointers were introduced (15 years ago), so please don't be so acid me if I don't get the idea inmediately. These are my ideas with respect tmp fields
|
|
August 2, 2022, 10:31 |
|
#2 |
Senior Member
Santiago Lopez Castano
Join Date: Nov 2012
Posts: 354
Rep Power: 15 |
tmp<> is an pre-STL version of a pointer Handle or weak_ptr<>. Is a means of moving (as opposed to copying) data across function calls while avoiding registering said data in the time selection mechanism (Factory) in FOAM. You use it when you don't want to duplicate data, for short lived templated datatypes that don't need be registered in the time selection mechanism, or when you are not sure of the lifetime of the original data (a reference can be deallocated, and become invalid, a tmp guarantees the lifetime of the underlying object by keeping a count).
There is another use: geometricFields (and many other things in FOAM) have no null-constructor. You may want to declare a tmp<geometricField<...> > in one scope and DEFINE it in some other scope, for example. |
|
August 2, 2022, 10:44 |
|
#3 |
Senior Member
Santiago Lopez Castano
Join Date: Nov 2012
Posts: 354
Rep Power: 15 |
ref() is a construct in new versions of FOAM: it returns a reference of what is inside the tmp, so you don't have to de-reference the tmp everytime you do operations on it. Earlier versions of FOAM used operator() for returning the reference.
BTW, the code you show has little context so it is difficult to say why tmp was used there. |
|
August 2, 2022, 10:49 |
|
#4 |
Senior Member
Santiago Lopez Castano
Join Date: Nov 2012
Posts: 354
Rep Power: 15 |
Don't want to be acid but the rest of the questions refer to basic C/C++ (scopes, lifetime, what is memory) so ill refer you to "accelerated c++" for details.
|
|
August 3, 2022, 00:04 |
|
#5 |
Senior Member
Agustín Villa
Join Date: Apr 2013
Location: Alcorcón
Posts: 313
Rep Power: 15 |
Thank you for your comments! I will check that reference about C++. I hope it helps me to get the idea behind using these concepts.
|
|
June 30, 2023, 05:37 |
|
#6 |
Senior Member
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,686
Rep Power: 40 |
Need to comment that there is yet another reason for the tmp - it can either hold a managed pointer (somewhat like a mix of unique_ptr and shared_ptr) but it can also hold a reference to an object (somewhat like std reference_wrapper, but can be nullptr initialized).
This duality is be particularly convenient. There is also a refPtr wrapper that is similar in many ways to tmp but does not require object to have embedded reference counting. |
|
|
|