home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Visual Database / Visual dBase v5.5 / EXTERN.PAK / DBASEVAR.H next >
Encoding:
C/C++ Source or Header  |  1995-07-18  |  6.9 KB  |  308 lines

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