Enhancement #622
Allow MathVector::CWise constants for use in Mappings
Status: | Resolved | Start date: | 10/12/2011 | |
---|---|---|---|---|
Priority: | Normal | Due date: | ||
Assignee: | M. Rolf | % Done: | 100% | |
Category: | - | |||
Target version: | NemoMath 0.2 |
Description
One might want to use expressions like this:
VectorMapping v1 = RealVector(1.0, 2.0).cwise() * arg<RealVector>();
Therefore the CWise object must be copyable. What happens with the internal reference to the MathVector object.
The MathVector must be prevented to be deleted. On the other hand, just copying the MathVector object (holding
by value, bot reference) breaks in-place operations like
v1.cwise() *= v2;
History
#1 Updated by M. Rolf over 12 years ago
- Target version set to NemoMath 0.2
#2 Updated by M. Rolf over 12 years ago
- Status changed from New to In Progress
- % Done changed from 0 to 10
Possible solution:
1. CWise should hold a reference plus a value of a Vector. If the CWise is created it assigns NULL to the pointer.
2. The operations make a check: if the pointer is NULL, they operate on the reference. If it is not NULL, they operate on the pointer.
3. If a CWise is copy-constructed or assigned, it sets the pointer to a newly heap-contructed copy of the reference
For this to work, the RealVector::cwise() functions must be STRICTLY inlined in order to prevent copy construction right during the 'getting' of a CWise object. If it would be copy-constructed, then vec.cwise *= otherVec
would not work anymore.
Also, the operators to not seem to be found correctly at the moment. The intended test case for Mappings does not yet compile:
https://code.cor-lab.org/projects/nemomath/repository/revisions/134/entry/trunk/nemomath/test/MappingArithmetics.cpp
#3 Updated by M. Rolf over 12 years ago
- % Done changed from 10 to 20
#4 Updated by M. Rolf over 12 years ago
- Status changed from In Progress to Resolved
- % Done changed from 20 to 100
The solution with combined referance/value logic as in the last comment does not work. The compiler's return-value-optimization lets the class not always see when copies are made. Consequence: memory violations!
Solution:
1. CWise now always behaves like a value, i.e. it holds a MathVector object.
2. In-place operations are removed from the CWise API: you cannot write vec.cwise() *= otherVec
anymore
3. In-place operations are now in the MathVector API: you can write vec *= otherVec.cwise()
Fixed problem for operator use with mappings:
1. CWise needs a default-constructor!!! Necessary for the type-inference in mapping operators