I am wondering what the best pattern/approach for a data structure that may contain either a reference or a copy of some data used in a constructor. I would love the opportunity to have the data structure capable of conditionally storing the data for the data copy on the stack. Are there any fancy generic tricks/patterns to do this using boost::mpl, etc.?
So to put in some pseudocode notes for discussion to get the point across of what I think I want:
boost::array< boost::array<double, M>, N> myaxis; //creates axis in a stack allocated data structure.
regular_grid<M, N, COPYDATA> my_grid(axis_values);
regular_grid<M, N, REFDATA> my_grid(axis_values);
template<int M, int N, int ConstructionPolicy>
class regular_grid
{
boost::array< boost::array<double, M>, N>& axis_ref_; //Reference to the axis, either static or actual reference. All code in the class would use this, not axis_
STATIC_IF(ConstructionPolicy == COPYDATA)
{
boost::array< boost::array<double, M>, N> axis_;
regular_grid(const boost::array< boost::array<double, M>, N>& axis) : axis_(axis), axis_ref_(axis_) //constructor makes copy, sets ref to copy.
{
}
}
STATIC_ELSE(ConstructionPolicy == REFDATA)
{
regular_grid(const boost::array< boost::array<double, M>, N>& axis) : axis_ref_(axis) {} //constructor just uses the ref.
}
};
Is this possible? any ideas on code to look at to see how to do it?