home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR13 / TI_BC1.ZIP / TI1700.ZIP / TI1700.ASC
Text File  |  1993-10-07  |  7KB  |  265 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Borland C++                           NUMBER  :  1700
  9.   VERSION  :  All
  10.        OS  :  All
  11.      DATE  :  October 7, 1993                          PAGE  :  1/4
  12.  
  13.     TITLE  :  Using BI_IArrayAsVector as a class data member
  14.  
  15.  
  16.  
  17.  
  18.   /* ARRYADM.CPP: BI_IArrayAsVector as class data member
  19.  
  20.      This example illustrates the use of a template-based
  21.      container class as a data member of another class. It also
  22.      demonstrates the use of non-member functions pointers and
  23.      the use of variable argument lists in a class constructor.
  24.  
  25.      The OPERATIONS class might be useful in creating a batch-
  26.      like command executor.
  27.  
  28.      NOTES: (1) Tested with BC++ OS/2 1.0, BC++ 3.1
  29.   */
  30.  
  31.   #include <string.h>
  32.   #include <stdarg.h>
  33.   #include <arrays.h>
  34.  
  35.   // All functions submitted to the OPS class constructor must
  36.   // have a prototype that matches the following typedef. Editing
  37.   // the following code line is all that is necessary to change
  38.   // the function prototype expected by the OPS and OPERATIONS
  39.   // classes. For instance:
  40.  
  41.   //      typedef long (*AFUNC)(char*, char*);
  42.  
  43.   // would change the following code to expect a function that
  44.   // returns a 'long' and takes two character pointers. Naturally
  45.   // the example functions would have to be replaced with
  46.   // functions whose signatures match.
  47.   typedef int (*AFUNC)(void);
  48.  
  49.   //////////////////////////////////////////// OPS ///////////////
  50.   // This class provides template container class compatible
  51.   // function pointer objects. In this example, OPS objects are
  52.   // passed to the constructor of an OPERATIONS class object and
  53.   // are inserted into the OPERATIONS class object's ARRAY data
  54.   // member.
  55.   class OPS {
  56.     public:
  57.       OPS(const char *ids, const AFUNC af) {
  58.         idString = new char[strlen(ids)+1];
  59.         strcpy(idString, ids);
  60.         afunc = af;
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Borland C++                           NUMBER  :  1700
  75.   VERSION  :  All
  76.        OS  :  All
  77.      DATE  :  October 7, 1993                          PAGE  :  2/4
  78.  
  79.     TITLE  :  Using BI_IArrayAsVector as a class data member
  80.  
  81.  
  82.  
  83.  
  84.       }
  85.       ~OPS(void) {
  86.         delete idString;
  87.       }
  88.  
  89.       // Required by the BI_IArrayAsVector template
  90.       int operator ==(const OPS&) const {
  91.         return 0;
  92.       }
  93.       int isSortable(void) { return 0; }
  94.  
  95.       char *idString;
  96.       AFUNC afunc;
  97.   };
  98.   typedef OPS *POPS;
  99.  
  100.   typedef BI_IArrayAsVector<OPS> ARRAY;
  101.  
  102.   //////////////////////////////////////////// OPERATIONS ////////
  103.   // The OPERATIONS class is an example of a class which:
  104.   //   (1) uses variable argument lists in its constructor
  105.   //   (2) uses a template-based container class as a data
  106.   //       member.
  107.   struct OPERATIONS {
  108.       OPERATIONS(unsigned short slctd, ...)
  109.       : operations(5, 0, 5), functions(0) {
  110.         selected = slctd;
  111.         va_list ap;
  112.         POPS arg;
  113.         va_start(ap, slctd);
  114.         while((arg = va_arg(ap, POPS)) != 0) {
  115.           operations.add(arg);
  116.           ++functions;
  117.         }
  118.         va_end(ap);
  119.       }
  120.       void executeSelected(void) const {
  121.         operations[selected]->afunc();
  122.       }
  123.       char *selectedIs(void) const {
  124.         return operations[selected]->idString;
  125.       }
  126.       void changeSelected(int i) {
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.   PRODUCT  :  Borland C++                           NUMBER  :  1700
  141.   VERSION  :  All
  142.        OS  :  All
  143.      DATE  :  October 7, 1993                          PAGE  :  3/4
  144.  
  145.     TITLE  :  Using BI_IArrayAsVector as a class data member
  146.  
  147.  
  148.  
  149.  
  150.         selected = i;
  151.       }
  152.       operator int() {
  153.         return functions;
  154.       }
  155.  
  156.     private:
  157.       ARRAY operations;
  158.       int selected;
  159.       int functions;
  160.   };
  161.   typedef OPERATIONS *POPERATIONS;
  162.  
  163.   // Some gutless demo functions with the required prototype
  164.   //--------------------------------------------------------------
  165.   int nop(void) {
  166.     cout << "\nDoing nothing!";
  167.     return 0;
  168.   }
  169.  
  170.   //--------------------------------------------------------------
  171.   int deleteFile(void) {
  172.     cout << "\nDeleting file!";
  173.     return 0;
  174.   }
  175.  
  176.   //--------------------------------------------------------------
  177.   int renameFile(void) {
  178.     cout << "\nRenaming file!";
  179.     return 0;
  180.   }
  181.  
  182.   //--------------------------------------------------------------
  183.   int copyFile(void) {
  184.     cout << "\nCopying file!";
  185.     return 0;
  186.   }
  187.  
  188.   //**************************************************************
  189.   void main(void) {
  190.  
  191.     // Install our functions and function descriptions into
  192.     // OPS objects.
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.   PRODUCT  :  Borland C++                           NUMBER  :  1700
  207.   VERSION  :  All
  208.        OS  :  All
  209.      DATE  :  October 7, 1993                          PAGE  :  4/4
  210.  
  211.     TITLE  :  Using BI_IArrayAsVector as a class data member
  212.  
  213.  
  214.  
  215.  
  216.     POPS op1 = new OPS("NULL function", nop);
  217.     POPS op2 = new OPS("Copy file", copyFile);
  218.     POPS op3 = new OPS("Delete file", deleteFile);
  219.     POPS op4 = new OPS("Rename file", renameFile);
  220.  
  221.     // First argument in the creation of an OPERATIONS object is
  222.     // the array indici of the function to be initialially
  223.     // selected.  The last argument is always 0. In between is the
  224.     // variable list of OPS objects.
  225.  
  226.     // NOTE!!! The OPERATIONS class expects these objects to have
  227.     // been dynamically allocated. If they are local, static or
  228.     // global objects, this program will crash!!!! If static, local
  229.     // or global objects must be used, the OPERATIONS class will
  230.     // have to be modified to support a TShouldDelete::NoDelete
  231.     // facility.
  232.     OPERATIONS operations(2, op1, op2, op3, op4, 0L);
  233.  
  234.     // Lets see if initial function selection went as planned.
  235.     cout << "Initial selected function is "
  236.          << operations.selectedIs();
  237.     operations.executeSelected();
  238.  
  239.     // Note that the OPERATIONS class member function
  240.     // "operator int()" makes this particular for-loop
  241.     // work.
  242.     for (int i = 0; i < operations; i++) {
  243.       operations.changeSelected(i);
  244.       operations.executeSelected();
  245.     }
  246.   } // end of main()
  247.  
  248.  
  249.   DISCLAIMER: You have the right to use this technical information
  250.   subject to the terms of the No-Nonsense License Statement that
  251.   you received with the Borland product to which this information
  252.   pertains.
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.