home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 31 / CDASC_31_1996_juillet_aout.iso / vrac / cuj0796.zip / LETTERS.ZIP / DECLCPAR.CPP next >
C/C++ Source or Header  |  1996-05-06  |  1KB  |  54 lines

  1. // Copy and paste would be worse, so the macro is Ok
  2. #define  DECLCPAR(type,name)  \
  3.     public:  PARMS &name##Set(type x)\
  4.         {name = x; return *this;}\
  5.     private: type  name; 
  6.  
  7. // system calls to allocate and free the resource 
  8. // refered to by SYSRES::handle
  9. int CreateSysRes(int, int, float);
  10. void DestroySysRes(int);
  11.  
  12. // SYSRES encapsulates some system resource
  13. // (i.e. file, window, port)
  14. class SYSRES
  15. {
  16. public:
  17.    class PARMS
  18.    {       
  19.    public:        
  20.       // default parameters
  21.       PARMS(void):
  22.          ival1(-1),
  23.          ival2(0),         
  24.          fval(99.9)  {}
  25.    private:             
  26.    friend class SYSRES;    
  27.       
  28.       DECLCPAR(int,  ival1)      
  29.       DECLCPAR(int,  ival2)      
  30.       DECLCPAR(float,fval)
  31.    };           
  32.  
  33.    SYSRES(const PARMS &parms = PARMS()):
  34.       handle(CreateSysRes(parms.ival1,parms.ival2,parms.fval))
  35.       {}             
  36.    ~SYSRES()
  37.       {DestroySysRes(handle);}
  38.  
  39. private:
  40.    int   handle;
  41. }; 
  42.  
  43. void fun(const SYSRES &);
  44.  
  45. int main()
  46. {
  47.    SYSRES   res   = SYSRES::PARMS().ival1Set(20).fvalSet(0.7);
  48.  
  49.    fun(SYSRES::PARMS().ival2Set(9));   
  50.    
  51.    return 1;
  52. }                       
  53.  
  54.