home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 April B / Pcwk4b98.iso / Borland / Dbase50w / EXTERN.PAK / DBASEVAR.H next >
C/C++ Source or Header  |  1994-08-02  |  7KB  |  297 lines

  1. //============================================================================
  2. //
  3. // DBaseVar.h
  4. //
  5. // Header for dBASE for Windows EXTERN examples.
  6. //
  7. // This file defines the DBaseVar and DVar classes. DBaseVar member
  8. // functions make the actual calls into the EXTERN system.  The DVar
  9. // class is a "smart pointer" wrapper around the DBaseVar class to
  10. // facilitate usage of local DBaseVar objects.
  11. //
  12. //============================================================================
  13.  
  14. // Definitions of the DBaseVar and DVar classes.
  15.  
  16. #ifndef DBASEVAR_H
  17.  
  18.    // Used to test programs including DBaseVar.h under DOS.
  19. #ifndef DOSTEST
  20.  
  21. #include <windows.h>
  22. #include "dbaseext.h"
  23.  
  24.    // End of DOSTEST.
  25. #endif
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <assert.h>
  31. #include <time.h>
  32. #include <io.h>
  33. #include <fcntl.h>
  34. #include <limits.h>
  35.  
  36.    // If you want to test the C++ class under DOS, define DOSTEST.
  37. #ifndef DOSTEST
  38.  
  39. //
  40. // Macro to use what ever kind of exception handling that is desired.
  41. //
  42. #ifndef DBASETHROW
  43. #define DBASETHROW  DBase()->ReturnError()   // Throw to dBASE, show error
  44. #endif
  45.  
  46.  
  47. // DBaseVar class.
  48.  
  49. class DBaseVar {
  50.  
  51. public:
  52.  
  53.    ///////// Routines to get parts of a DBaseVar object. /////////
  54.  
  55.    BOOL Logical(){
  56.       BOOL b;
  57.       if( DBase()->VarGetLogical(this,&b) != 0){
  58.          DBASETHROW;
  59.       }
  60.       return b;
  61.    }
  62.  
  63.    DoubleType Double(){
  64.       DoubleType d;
  65.       if( DBase()->VarGetDouble(this,&d) != 0){
  66.          DBASETHROW;
  67.       }
  68.       return d;
  69.    }
  70.  
  71.    long Long(){
  72.       long l;
  73.       if( DBase()->VarGetLong(this,&l) != 0){
  74.          DBASETHROW;
  75.       }
  76.       return l;
  77.    }
  78.  
  79.    char *String(){
  80.       char *pStr;
  81.       if( DBase()->VarGetString(this,&pStr) != 0){
  82.          DBASETHROW;
  83.       }
  84.       return pStr;
  85.    }
  86.  
  87.    int StringLen(){
  88.       int Len;
  89.       if( DBase()->VarGetStringLen(this,&Len) != 0){
  90.          DBASETHROW;
  91.       }
  92.       return Len;
  93.    }
  94.  
  95.    char *StringBuffer(){
  96.       char *pStr;
  97.       if( (pStr = DBase()->VarGetStringBuffer(this)) == NULL){
  98.          DBASETHROW;
  99.       }
  100.       return pStr;
  101.    }
  102.  
  103.    void Property(char *PropName, DBaseVar *Result){
  104.  
  105.          // Convert PropName to upper case.
  106.       strupr( PropName );
  107.  
  108.       if( DBase()->VarGetProperty(this,PropName,Result) != 0){
  109.          DBASETHROW;
  110.       }
  111.    }
  112.  
  113.    void Element(DBaseVar *Result, int Size, DBaseVar **Index){
  114.       if( DBase()->VarGetElement(this,Result,Size,Index) != 0 ){
  115.          DBASETHROW;
  116.       }
  117.    }
  118.  
  119.    char Type(){
  120.       return DBase()->VarGetType(this);
  121.    }
  122.  
  123.  
  124.    ///////// Routines to set parts of a DBaseVar object. /////////
  125.  
  126.    void Set( DBaseVar *Original ){
  127.       DBase()->VarSetVar( this, Original );
  128.    }
  129.  
  130.    BOOL SetLogical(BOOL b){
  131.       DBase()->VarSetLogical(this,b);
  132.       return b;
  133.    }
  134.  
  135.    DoubleType SetDouble(DoubleType d){
  136.       DBase()->VarSetDouble(this,d);
  137.       return d;
  138.    }
  139.  
  140.    long SetLong(long l){
  141.       DBase()->VarSetLong(this,l);
  142.       return l;
  143.    }
  144.  
  145.    char *SetZString( char *pStr ){
  146.       DBase()->VarSetZString(this,pStr);
  147.       return pStr;
  148.    }
  149.  
  150.    void SetStringLen(int Len){
  151.       DBase()->VarSetStringLen(this,Len);
  152.    }
  153.  
  154.    void SetProperty(char *PropName, DBaseVar *pVar){
  155.  
  156.          // Convert PropName to upper case.
  157.       strupr( PropName );
  158.  
  159.       if(DBase()->VarSetProperty(this,PropName,pVar)){
  160.          DBASETHROW;
  161.       }
  162.    }
  163.  
  164.    void SetElement(int cDim, DBaseVar **ppVars, DBaseVar *pValue){
  165.       if(DBase()->VarSetElement(this,cDim,ppVars,pValue)){
  166.          DBASETHROW;
  167.       }
  168.    }
  169.  
  170.    void SetCodeBlock(char* code){
  171.       if(DBase()->VarSetCodeBlock(this,code)){
  172.          DBASETHROW;
  173.       }
  174.    }
  175.    
  176.  
  177.    ///////// Routine to execute a codeblock of a DBaseVar object. /////////
  178.  
  179.    void RunCodeBlock(DBaseVar *pDest, int iParaCount, DBaseVar** ppParas){
  180.       if(DBase()->VarRunCode(this,pDest,iParaCount,ppParas)){
  181.          DBASETHROW;     
  182.       }
  183.    }
  184. };
  185.  
  186. //
  187. //  A DVar is a smart pointer to a DBaseVar.  When used in a structure or 
  188. //  as a local variable, the variable's construction and destruction is 
  189. //  managed.
  190. //  There are conversion and access operators to automatically manage the 
  191. //  variable as a DBaseVar.
  192. //
  193. //  NOTE: DVARs can't be used as parameters of functions that are called
  194. //        directly from dBASEWin.  DVar parameters trigger a destructor
  195. //        call at the end of the function because the function is assumed
  196. //        to be called with temporary DVar objects as arguments.  Functions 
  197. //        called from dBASEWin however are called with DBaseVar *'s as 
  198. //        arguments.  
  199.  
  200.  
  201.    // Property is used to detect if a DVAR is not used as a parameter
  202.    // of a function called by the dBASEWin side.
  203. #ifndef NDEBUG
  204. #define SETPROPERTY Property = 12345
  205. #else
  206. #define SETPROPERTY 
  207. #endif
  208.  
  209. class DVar {
  210. private:
  211.    DBaseVar *pVar;
  212.    long      Property;     // Used to ensure it's really a DVar
  213. public:
  214.    // 
  215.    // Member access treats DVar's and DBaseVars the same.
  216.    //
  217.    DBaseVar * operator ->(){
  218.       return pVar;
  219.    }
  220.  
  221.    // 
  222.    // Any function that expects a DBaseVar* can also accept a DVar
  223.    //
  224.    operator DBaseVar*(){
  225.       return pVar;
  226.    }
  227.  
  228.    //
  229.    // Utility function to force conversion to a DBaseVar (in ... cases)
  230.    //
  231.    DBaseVar* Var(){
  232.       return pVar;
  233.    }
  234.  
  235.    //
  236.    // Constructors
  237.    //
  238.    DVar(){                           // Default constructor
  239.       pVar = DBase()->MakeVar();
  240.       SETPROPERTY;
  241.    }
  242.  
  243.    DVar(DBaseVar *original){         // Construct from DBaseVar *
  244.       pVar = DBase()->MakeVar();
  245.       pVar->Set(original);
  246.       SETPROPERTY;
  247.    }
  248.  
  249.    DVar(BOOL b){                     // Construct from BOOL
  250.       pVar = DBase()->MakeVar();
  251.       pVar->SetLogical(b);
  252.       SETPROPERTY;
  253.    }
  254.  
  255.    DVar(DoubleType d){              // Construct from double
  256.       pVar = DBase()->MakeVar();
  257.       pVar->SetDouble(d);
  258.       SETPROPERTY;
  259.    }
  260.  
  261.    DVar(double d){                    // Construct from double
  262.       pVar = DBase()->MakeVar();
  263.       pVar->SetDouble(d);
  264.       SETPROPERTY;
  265.    }
  266.  
  267.  
  268.    DVar(long l){                     // Construct from long
  269.       pVar = DBase()->MakeVar();
  270.       pVar->SetLong(l);
  271.       SETPROPERTY;
  272.    }
  273.  
  274.    DVar(char *pVal){                 // Construct from char *
  275.       pVar = DBase()->MakeVar();
  276.       pVar->SetZString(pVal);
  277.       SETPROPERTY;
  278.    }
  279.  
  280.    //
  281.    // Destructor
  282.    //
  283.    ~DVar(){
  284. #ifndef NDEBUG
  285.       assert( Property == 12345 );
  286. #endif
  287.       DBase()->DestroyVar(pVar);
  288.  
  289.    }
  290. };
  291.  
  292.    // End of DOSTEST.
  293. #endif
  294.  
  295. #define DBASEVAR_H
  296. #endif
  297.