Template data structure – Access to getters and setters of a template class deriving from an abstract class

  abstract, c++, database, templates

in a finite element code, I’ve a data structure to manipulate the value of a field (coordinates, temperature, …) at a point. This structure is basic and looks like this:

class point_data
{
public:
    void allocate(int ldim); // allocation of data;
    const double* get_data() const { return data; }
    void set_data(const double* ldata); // for example
    …
protected:
    double* data;
    …
}

I then use objects of type point_data* to evaluate expressions, for example to impose boundary conditions. To be concrete, if x, y are the coordinate fields, and I impose a temperature T equal to sin(y) on a boundary defined by x == 0, then I have a “sin” operator object, whose evaluate method takes a point_data* argument (in fact a point_data**) and return a point_data* containing the result. Same thing for my “==” object, which also takes a point_data** argument (array of two point_data* this time) and return the result.

As my structure for supporting data of global fields is already template, and since it is this structure that creates point_data objects, I wanted templatized the class point_data. Then, I tried:

class abstract_point_data
{
public:
    void allocate(int ldim) = 0;
    …
}

template <typename T>
class point_data : public abstract_point_data
{
public:
    void allocate(int ldim); 
    const T* get_data() const { return data; }
    void set_data(const T* ldata); // for example
    …
protected:
    T* data;
}

This way, the evaluate method of my operator objects can have an abstract_point_data** as an argument. In can also create the result object, because I know for example that a sine provide a double, or by cloning one of the arguments. But, that’s it. In general, I don’t know the type of my arguments in the evaluate function and therefore I can’t (of course) access the getters and setters. I’ve tried things like CRTP, decltype(auto), or even stupid stuff like std::tuple<bool,int,double> my_types; + decltype(std::get<get_my_index()>(my_types)) to store the type of my template “in” abstract_point_data, but nope, the C++ rules seem inviolable.

Can anyone give me an idea or reference to help me redefine (if possible) an acceptable templatized data structure? Thanks in advance.

Source: Windows Questions C++

LEAVE A COMMENT