Wrapper object

Hi,

I am trying to combine objects defined in third party's lib.

1) First of all, I want to perform the conditional operation like this:
1
2
3
4
5
        if(SelectionBox_Ind == 1){ //Rectangular
            sRfMSat = new WrapRF_Rect_DK (&sRfMSat_RECT);
        }else{ // SIEMENS' GAUSSIAN
            sRfMSat = new WrapRF_Gauss_DK (&sRfMSat_GAUSS);
        }



2-A) and then somewhere I would like to replace
1
2
3
4
5
        if(SelectionBox_Ind == 1){ //Rectangular
            fRTEI(lT, &sRfMSatSet, &sRfMSat_RECT, 0, 0, 0, 0, 0);
        }else{ // SIEMENS' GAUSSIAN
            fRTEI(lT, &sRfMSatSet, &sRfMSat_GAUSS, 0, 0, 0, 0, 0);
        }



2-B) by a single statement like this:
 
        fRTEI(lT, &sRfMSatSet, reinterpret_cast<RFBaseDK&>(*sRfMSat), 0, 0, 0, 0, 0);   

then I am getting this error
FLASH.cpp(3450) : error C2665: 'fRTEI' : none of the 8 overloads can convert parameter 3 from type 'class RFBaseDK'

Obviusly 2-A and 2-B are not similar as I wanted.

The part where I defined wrapper object look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
	class RFBaseDK{
		public:
			//virtual RFBaseDK* returnPointer2Img() = 0;  // "=0" means abstract methods - MUST be 
			//virtual int getStatus() = 0;

			// CEST : Prepare option for any type of pulse
			void setTypeUndefined(){};
			int setDuration(double Duration){MTPulse_Duration = Duration;		return 0;} ;	// CEST in us
			int setSamples(long Samples){No_of_Samples = Samples;		return 0;};		// CEST
			int setFlipAngle(int Ang){setFlipAngle(Ang);		return 0;}
			int setInitialPhase(int Pha){InitialPha = Pha;		return 0;};
			int setThickness(double Thickness){SliceThickness = Thickness;		return 0;};	
			//long virtual getDuration() = 0;
		private:
			double MTPulse_Duration;
			long No_of_Samples;
			int InitialPha;
			double SliceThickness;

	};

	class WrapRF_Gauss_DK: public RFBaseDK{
		public:
			WrapRF_Gauss_DK(sRF_PULSE_GAUSS* B){A = B;}
			~WrapRF_Gauss_DK(){delete A;}

			/*
			long getDuration(){
				return (A->getDuration());
			}*/

			/*WrapRF_Gauss_DK* returnPointer2Img() {
        		return this;
			}*/
		private:
			sRF_PULSE_GAUSS* A;
	};


	class WrapRF_Rect_DK: public RFBaseDK{
		public:
			WrapRF_Rect_DK(sRF_PULSE_RECT* B){A = B;}
			~WrapRF_Rect_DK(){delete A;}

			long getDuration(){
				return (A->getDuration());
			}

			/*WrapRF_Gauss_DK* returnPointer2Img() {
        		return this;
			}*/
		private:
			sRF_PULSE_RECT* A;
	};

	RFBaseDK* sRfMSat;


I do not have much experience with C++ and any help would be highly appreciated.
Last edited on
<nolyc> You're trying to do X, and you thought of solution Y. So you're asking about solution Y, without even mentioning X. The problem is, there might be a better solution, but we can't know that unless you describe what X is.


post fRTEI prototype, I guess that you are choosing an overload so you should wrap it.
Also ¿what is `sRfMSat_RECT'? your memory management seems odd (you shouldn't delete what you didn't new)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class RFBaseDK{
public:
   virtual void fRTEI( /**/ ) = 0;
};
class WrapRF_Gauss_DK: public RFBaseDK{
public:
   virtual void fRTEI( /**/ ){
      fRTEI( this, A /**/);
   }
};
class WrapRF_Rect_DK: public RFBaseDK{
public:
   virtual void fRTEI( /**/ ){
      fRTEI( this, A /**/); //it is resolved because A is a different type in each case
   }
};

template<class T>
class Wrap : public RFBaseDK{
public:
   typedef T pulse_type;
   virtual void fRTEI( /**/ ){
      fRTEI( this, this->A /**/); //it is resolved because A is a different type in each case
   }
private:
   pulse_type *A;
};

sRfMSat = new Wrap<sRF_PULSE_RECT> (&sRfMSat_RECT);
sRfMSat->fRTEI(/**/);

My apologies for not posing the problem properly.

Actually, fRTEI(...) is a function from a third party library and so, I can not really modify that.

This function uses reference to different objects: sRfMSat_RECT and sRfMSat_GAUSS depending on the following condition:

1
2
3
4
5
       if(SelectionBox_Ind == 1){ //Rectangular
            fRTEI(lT, &sRfMSatSet, &sRfMSat_RECT, 0, 0, 0, 0, 0);
        }else{ // SIEMENS' GAUSSIAN
            fRTEI(lT, &sRfMSatSet, &sRfMSat_GAUSS, 0, 0, 0, 0, 0);
        }


Alos, sRfMSat_RECT and sRfMSat_GAUSS are objects from third party libraries. So, I can not modify them as well.

However, I would like to have a single statement like:
1
2
RFBaseDK* sRfMSat;
fRTEI(lT, &sRfMSatSet, reinterpret_cast<RFBaseDK&>(*sRfMSat), 0, 0, 0, 0, 0);


Ideally, rather than RFBaseDK* sRfMSat;, I would like something like
RFBaseDK sRfMSat; and then I can use the function fRTEI.

Also, declaration of fRTEI is as:
1
2
3
4
5
6
7
8
9
10
11
__IMP_EXP NLSStatus fRTEI
(
  long                   lRelTime,      // IMP: relative start time of event
  sFREQ_PHASE *          pFREQ_PHASE,   // IMP: freq/phase definition
  sRF_PULSE *            pRF_PULSE,     // IMP: RF definition
  sREADOUT *             pREADOUT,      // IMP: readout definition
  sGRAD_PULSE_BASE *     pGRAD_PULSEGP, // IMP: gradient pulse definition for GP
  sGRAD_PULSE_BASE *     pGRAD_PULSEGR, // IMP: gradient pulse definition for GR
  sGRAD_PULSE_BASE *     pGRAD_PULSEGS, // IMP: gradient pulse definition for GS
  sSYNC *                pSYNC          // IMP: synchonization definition
);


I tried to modify my wrapper classes : WrapRF_Gauss_DK and WrapRF_Rect_DK around sRF_PULSE_GAUSS and sRF_PULSE_RECT respectively. However, I could not succeed and any help would be appreciated.

Regards,
Dushyant
Last edited on
OK, I think that I get it now.
You don't need to modify fRTEI, simply encapsulate it.

Given that sRfMSat_{RECT,GAUSS} are from the same type (sRF_PULSE) you don't really need different wrapper types
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class wrapper{
public:
   wrapper(){} //default constructor (¿invalid?)
   //copying the pulse, if that is expensive consider pointers without ownership
   wrapper(sRF_PULSE pulse): pulse(pulse){} 
   NLSStatus fRTEI(/**/){ almost the same as the function prototype
      fRTEI(/*forwarding parameters*/, &pulse, /**/); //calling the global function
  } 
   sRF_PULSE pulse;
};

//usage
        wrapper sRfMSat;
        if(SelectionBox_Ind == 1){ //Rectangular
            sRfMSat = wrapper (sRfMSat_RECT); //using assignment
        }else{ // SIEMENS' GAUSSIAN
            sRfMSat = wrapper (sRfMSat_GAUSS);
        }
        sRfMSat.fRTEI( lT, &sRfMSatSet, 0, 0, 0, 0, 0 );

//if you don't want to provide a default constructor
wrapper select(int SelectionBox_Ind){
        if(SelectionBox_Ind == 1){ //Rectangular
            return wrapper (sRfMSat_RECT);
        }else{ // SIEMENS' GAUSSIAN
            return wrapper (sRfMSat_GAUSS);
        }
}
wrapper sRfMSat = select(SelectionBox_Ind);
sRfMSat.fRTEI( lT, &sRfMSatSet, 0, 0, 0, 0, 0 );

Last edited on
Thanks for help ne55.

I appreciate it.
Topic archived. No new replies allowed.