views:

88

answers:

2

I want to add a operator override to perform assignments/__set__s inline.

Template :-

class CBase {
    public :
        static void SetupVmeInterface(CVmeInterface *in);

    protected :
        static CVmeInterface *pVmeInterface;
};


template <class T> class TCVmeAccess : public CBase {
    public:
        TCVmeAccess(int address);

        T get()
        {
            unsigned long temp = pVmeInterface->ReadAddress(Address);
            T ret = *reinterpret_cast<T*>(&temp);
            return ret;
        };

        T *operator->();
        unsigned long asLong();

        bool set(T data)
        {
            unsigned long write_data = *reinterpret_cast<unsigned long*>(&data);
            return pVmeInterface->WriteAddress(Address, write_data);
        };

        // void operator->(T);
        void operator=(T data)
        { set(data); }

    private :
        int Address;
};

A struct that will be used in the template :-

typedef struct
{
    int a: 1; // 0
    int b: 1; // 1
    int c: 1; // 2
    int d: 1; // 3
    int NotUsed : 28; // 31-4
} _HVPSUControl;

Code body :-

TCVmeAccess<_HVPSUControl> HVPSUControl(constHVPSUControlBlock);
_HVPSUControl hvpsu = HVPSUControl.get(); // Yep, good, but not as nice as...
int a = HVPSUControl2.get().OperationalRequestPort; // yep, also good, but...
int b = HVPSUControl->a; // works, and is all go so far

HVPSUControl.set(hvpsu); // works, but need _HVPSUControl type
HVPSUControl = hvpsu;    // also works, as operator = is used, but still need type

// this line does not work!
// as the = assignment is redirected into a copy of the struct, not the template
HVPSUControl->a = 1; // this line

So, is there a way to get this line above to work?

Edit: As in, I want "this line" to perform as a "set" as does in the template class.

Edit: 1. Assign a value directly in-line to a member of the struct that the template is formed of.
2. Cause that assignment to go though a template accessor.

So that I dont have to do this on assignments :-

// HVPSUControl is predefined and used many times.
_HVPSUControl hvpsu;
hvpsu.a = 1;
HVPSUControl.set(hvpsu);

I want to do

HVPSUControl.a = 1; // or 
HVPSUControl->a = 1; // or ?

As gets work on line :

if (HVPSUControl->a)

+1  A: 

Instead of overwriting the "->" and the "=" operator, you could derive from the template struct.

template <class T> class TCVmeAccess : public CBase, public T {
    public:
        TCVmeAccess(int address);

        T get();
        // T *operator->();
        unsigned long asLong();

        bool set(T);
        // void operator->(T);
        // void operator=(T);

    private :
        int Address;
};

HVPSUControl.a = 1; // and use this for setting a bitfield.

Edit: If you want to use an custom assignment operator, you should declare it in HVPSUControl, or a even a base class of it, if you have more of this control-like structures.

struct _HVPSUControl
{
    int a: 1; // 0
    int b: 1; // 1
    int c: 1; // 2
    int d: 1; // 3
    int NotUsed : 28; // 31-4
    void operator = (int x);
};

or

struct _HVPSUBase {
    void operator = (int x);
}
struct _HVPSUControl: public _HVPSUBase
{
    int a: 1; // 0
    int b: 1; // 1
    int c: 1; // 2
    int d: 1; // 3
    int NotUsed : 28; // 31-4
};
drhirsch
The problem with this is that it will assign "1" locally to the instance of "HVPSUControl", whereas I need that assignment to go though an accessor method in my class (i need to do some processing on it)
IanVaughan
Ah, just read the ", public T" in the "dervied" part of your text..., will play...
IanVaughan
Nope, it will just set the local instance of the struct. I need an assignment to go through an accessor in the template class.
IanVaughan
Close, although its a shame to have to derive all my (40+) structs from a base struct, it would be a small price to pay to save the inline code... if it would work! I will play with it...
IanVaughan
the assignment doesnt know which struct member has been assigned a value. I think what I want cannot be done!
IanVaughan
With your idea of deriving the template class from the struct ": public CBase, public T {"... If the template class could have an operator overridden then that would be used when accessing struct members? Cant override ".", but maybe an odd char. "void operator~(int x); .... inline "HVPSUControl~a = 1;"??
IanVaughan
A: 

You can't get this line to work any other way. You call TCVmeAccess::operator-> which returns a _HVPSUControl* and then you access _HVPSUControl*::a.

Or do you want to assign integer 1 to the complete _HVPSUControl struct?

template <class T> class TCVmeAccess : public CBase {
public:
    // ...

    void operator=(T data) { set(data); }
    void operator=(int n) { operator=(T(n)); }

    // ...
};


typedef struct
{
    _HVPSUControl(int n) {
       *this = reinterpret_cast<_HVPSUControl>(n);
    }

    int a: 1; // 0
    int b: 1; // 1
    int c: 1; // 2
    int d: 1; // 3
    int NotUsed : 28; // 31-4
} _HVPSUControl;

and then do

HVPSUControl = 1;
Sebastian
Quick reply on "do I want to assign integer 1 to all" = nope!I want to extract the struct member from the template, assign some data to it. But that assignment needs to go though the template class so I can do stuff with it.
IanVaughan