Templete with operator overloading

This is a discussion on Templete with operator overloading within the C++ Programming forums, part of the General Programming Boards category; Code: class CBase { public : static void SetupVmeInterface(CVmeInterface *in); protected : static CVmeInterface *pVmeInterface; }; template <class T> class ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    1

    Templete with operator overloading

    Code:
    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(); // similar to operator->
            T *operator->()
            {
                unsigned long temp = pVmeInterface->ReadAddress(Address);
                T *ret = reinterpret_cast<T*>(&temp);
                return ret;
            }
            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)
            // void operator->(int);
            // void operator=(int);
    
        private :
            int Address;
    };
    A struct that will be used in the template (the fact that its bit-field'ed is not an issue):-
    Code:
    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 :-
    Code:
    TCVmeAccess<_HVPSUControl> HVPSUControl(constHVPSUControlBlock);
    _HVPSUControl hvpsu = HVPSUControl.get(); // works - but not as nice as...
    int a = HVPSUControl2.get().OperationalRequestPort; // works - but...
    int b = HVPSUControl->a; // works, and is all the nicest for reads, good so far
    
    // writes are my question :-
    HVPSUControl.set(hvpsu); // works, but need a _HVPSUControl type!
    
    HVPSUControl->a = 1; // this line does not work!
    I want the assignment of the struct member to go through the set method in the template class?

    Because my writes have to be Read/Modify/Write, I have to do this this code in-line everywhere on assignments :-
    Code:
    // HVPSUControl is predefined and used many times.
    _HVPSUControl hvpsu;
    hvpsu.a = 1;
    HVPSUControl.set(hvpsu);
    
    // This would save on those lines (and I can also perform extra logic in my template)
    HVPSUControl.a = 1;
    Cheers, Ian.
    Last edited by IanVaughan; 12-10-2009 at 03:56 AM. Reason: added Read/Modify/Write comment.

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    -> returns a pointer and you're trying to assign it's value without dereferencing. do you perhaps want to return a T&?

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Code:
    unsigned long temp = pVmeInterface->ReadAddress(Address);
    T *ret = reinterpret_cast<T*>(&temp);
    You're returning the address of a local object. Reading HVPSUControl->a is undefined behavior. There are some tricks to get around this, but they won't help you with writing.

    If I understand correctly, this code accesses some control register, right? If so, I'm afraid there's no way to make the nice write syntax work in the generic case.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21