home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / c2cpp.zip / C2CPP.TXT
Text File  |  1993-12-12  |  23KB  |  643 lines

  1.                          C2C++(TM) Paper
  2.      Copyright(c) 1993. All rights reserved worldwide.
  3.  
  4.                                              December 1993
  5. _________________________________________________________________
  6.  
  7. 1.    Introduction
  8.  
  9. This paper describes an automated suite of tools named C2C++(TM) 
  10. which provides the conversion of C code into object oriented C++
  11. code. I will attempt to make this paper short and punchy rather
  12. than long- winded since time is money.
  13.  
  14. This paper is targeted to those who have an existing investment
  15. in C and would like to embrace object orientation through the
  16. latest ANSI draft C++ language. It is expected that you are
  17. familiar with object oriented terminology since you would have
  18. done some up-front investigation as to what you should do with
  19. your existing investment in C code.
  20.  
  21. I believe it is widely accepted amongst the programming
  22. fraternity that object orientation offers enormous productivity
  23. gains in both large and small projects, for the creation and
  24. maintenance of code and believe me, no matter what the critics
  25. say, it does. 
  26.  
  27. For those who would like to become more familiar with object
  28. oriented terminology and methodology, and those who have yet to
  29. accept object orientation, I recommend reading in the following
  30. order some of the most thought provoking titles available on the
  31. subject specifically for C programmers:
  32.  
  33.      "C++ Inside and Out "
  34.           by Bruce Eckel. 
  35.           (This is for those who want to get up to speed quickly)
  36.  
  37.      "Effective C++" 
  38.           by Scott Meyers, 
  39.           (This is for those who want to find out why their code
  40.           does not work)
  41.      
  42.      "The Annotated C++ Reference Manual" (also known as the
  43.           ARM) 
  44.           by Bjarne Stroustrup and Margaret Ellis,
  45.           (This for those who think they know it all)
  46.  
  47.      "Advanced C++"
  48.           by James Coplien
  49.           (This is for those who realize "we aren't worthy" see
  50.           the movie "Waynes World", then you'll understand)
  51.  
  52.  
  53. _________________________________________________________________
  54.  
  55. 2.   Protecting Your Existing Investment in C code
  56.  
  57. With this new wave of technology, comes many questions and
  58. challenges. One with the most economic and technical impact, is
  59. what to do with your existing investment in C code, since this
  60. will directly affect your bottom line? 
  61.  
  62. It is simply not viable to throw out your existing C code which
  63. has been debugged and proven reliable over its development life
  64. cycle. 
  65.  
  66. There are many "challenges" (this is the word people use when it
  67. is extremely difficult) which prove painful and time consuming in
  68. interfacing C code to C++ code if you really want to take
  69. advantage of the object oriented paradigm (which is obviously why
  70. C++ was created). 
  71.  
  72. Having experienced the "challenges" and pain on the conversion of
  73. a number of commercial projects, the smallest of which has been
  74. no less than 400, 000 lines of code, with more than 1000 modules
  75. in more than 100 sub directories, I have developed a  strong
  76. foundation of a suite of programs called C2C++(TM).
  77.  
  78. I have decided to market C2C++(TM) which will provide your
  79. organization with the same competitive advantage that many other
  80. commercial publishers now have through the leveraging of their
  81. existing investment in C code. 
  82.  
  83. Simply put, if you want to convert your C code to C++ code, I
  84. believe, having gone through the exercise a number of times, that
  85. C2C++(TM) is the least painful and most economical way to do it. 
  86.  
  87. _________________________________________________________________
  88.  
  89. 3 C2C++(TM) conversion will:
  90.  
  91.      1.   Preservation and enhance your existing investment in C
  92.           code.
  93.      
  94.           This has already been discussed above.
  95.      
  96.      2.   Automate the migration of your procedural C world to
  97.           the object oriented C++ world.
  98.      
  99.           Manual migration is error prone and not economical.
  100.      
  101.      3.   Separate the interface description from the       
  102.           implementation of classes
  103.      
  104.           This is a classic benefit of object orientation which
  105.           is achieved through organization and structuring with
  106.           C++.
  107.       
  108.      4.   Simplify maintenance 
  109.           
  110.           For example: declarations are no longer scattered
  111.           throughout your code.
  112.  
  113.      5.   Make extensions easier
  114.      
  115.           You can code entirely with C++ without having to worry
  116.           about burdensome interfaces between C code and C++
  117.           code, making further extension simpler and easier.
  118.  
  119.      6.   Provide a rapid development process of C++ classes to   
  120.           establish a working base from code that you are already
  121.           familiar with.
  122.      
  123.           There cannot be any quicker way for your organization
  124.           to exploit this new technology with minimized risk.
  125.      
  126.      7.   Enable your programmers through a faster learning curve
  127.           by observing their existing code, which they are        
  128.           familiar with, transformed into well organized object 
  129.           oriented C++ code.
  130.      
  131.           There is minimal time lag between deciding upon         
  132.           exploiting object orientation and experiencing its      
  133.           benefits, without abandoning your existing code.
  134.  
  135. _________________________________________________________________
  136.  
  137. 4.   Technical consideration of the C2C++(TM) conversion:
  138.  
  139.      1.   C2C++(TM) considers your program from a global
  140.           perspective,not just one module or one directory of
  141.           modules in isolation. 
  142.      
  143.           This is achieved through a multi-pass program analyzer
  144.           that creates its own database to describe your code.
  145.  
  146.      2.   Converts existing C code (data and functions) into
  147.           object oriented C++ code and objects.
  148.           
  149.           C2C++(TM) makes full use of classes, structures and     
  150.           organizes your code through the process.
  151.  
  152.      3.   Automatic conversion of structs into classes.
  153.      
  154.           Fast, painless and error-free.
  155.  
  156.      4.   Automatic detection of  member functions and static
  157.           functions of classes.
  158.      
  159.           Structures your functions by collecting them into
  160.           groups which manipulate the same type of data making
  161.           your code more understandable and therefore simpler to
  162.           maintain.
  163.  
  164.      5.   Automatically classifies functions into            
  165.           private,protected or public given simple rules.
  166.      
  167.           This will allow you to take advantage of the facilities
  168.           that class inheritance provides.
  169.  
  170.      6.   Automatically formats your class declarations and
  171.           header files into a standard layout.
  172.      
  173.           This provides an orthodox canonical format (a better
  174.           structure from within which to work) and therefore
  175.           makes your code more readable for better maintenance
  176.           and bug finding.
  177.      
  178.      7.   Automatically produces nested classes.
  179.      
  180.           This will reduce global name space crowding. ( You can
  181.           achieve this manually, but you might work up a sweat)
  182.  
  183.      8.   Automatically detects normal and static data members of
  184.           classes.
  185.      
  186.           This is simply good C++ programming practice because it
  187.           collects related information together.
  188.  
  189.      10.  Automatically generates new translated header and       
  190.           source code files.
  191.           
  192.           This is why you are doing the conversion in the first
  193.           place.
  194.  
  195.  
  196. _________________________________________________________________
  197.  
  198. 5.   A simple example of the C2C++(TM) conversion for the
  199.      uninitiated
  200.  
  201.      Some of the features shown in this example includes:
  202.  
  203.      1.   Classification of a function into its class.
  204.  
  205.      2.   Simplification of a function name through renaming.
  206.  
  207.      3.   Removal of the first parameter of a class member        
  208.           function.
  209.  
  210.      4.   Translation of references to the member object within
  211.           expressions within a member function.
  212.  
  213.      5.   Translation of member function call expressions.
  214.      
  215. Comments that I have inserted afterwards for better reading
  216. regarding the conversion process are inserted into the example
  217. code and are recognizable by their prefix of: // **
  218.  
  219.  
  220. _________________________________________________________________
  221.      
  222. 6. Original C source code:
  223.  
  224. BOOL  IsControlVisible(Form* pForm, int idControl)
  225. {
  226.    Control*    pControl;
  227.  
  228.    if (idControl < 0 || idControl > pForm->cControls) return FALSE;
  229.  
  230.    pControl = pForm->pControls[idControl];
  231.  
  232.    return ControlIsVisible(pControl);
  233. }
  234.  
  235. _________________________________________________________________
  236.  
  237. 7. Converted C++ source code:
  238.  
  239. // ** The function was classified to be a member of the Form class.
  240. // ** Its first parameter was removed because it is now implied.
  241.  
  242. BOOL  Form::IsControlVisible(int idControl)
  243. {
  244.    Control*    pControl;
  245.  
  246.    // ** Member object reference pForm removed.
  247.    if (idControl < 0 || idControl > cControls) return FALSE;
  248.  
  249.    // ** Member object reference pForm removed.
  250.    pControl = pControls[idControl];
  251.  
  252.    // ** The expression was translated to put its first parameter in front
  253.    //    ** of the function call brackets.
  254.    // ** The function call uses the new function name.
  255.    return pControl->IsVisible();
  256. }
  257.  
  258. _________________________________________________________________
  259.  
  260. 8.   A more complex example of C2C++(TM) conversion
  261.  
  262.      Some of the features shown in this example includes:
  263.  
  264.      1.   Classification of a function into its class.
  265.  
  266.      2.   Simplification of a function name through renaming.
  267.  
  268.      3.   Removal of the first parameter of a class member        
  269.           function.
  270.  
  271.      4.   Translation of references to the member object within
  272.           expressions within a member function.
  273.  
  274.      5.   Translation of member function call expressions.
  275.  
  276.      6.   Creation of a nested class.
  277.  
  278.      7.   Classification of generic POBJECT pointers into         
  279.           pointers to specific classes.
  280.  
  281.      8.   Usage of C++ templates to handle reference counted
  282.           class pointers.
  283.  
  284.      9.   One method of detection of static data members of a
  285.           class.
  286.  
  287.      10.  Classification of a function as being a static member
  288.           of a class.
  289.  
  290.      11.  Standardized layout of header files.
  291.  
  292.      12.  Automatic header file inclusion of reference classes
  293.           within a class declaration.
  294.  
  295.      13.  Creation of default assignment and copy constructors.
  296.  
  297.      14.  Automatic protection against including a header file
  298.           multiple times.
  299.  
  300.      15.  Consolidation of multiple related header files.
  301.      
  302. _________________________________________________________________
  303.  
  304. 9.   Original C header file 'Pane.h':
  305.  
  306. typedef  struct   {
  307.    HWND           hwnd;          // window handle (client if in
  308. MDI frame)
  309. }  VIEWPANE, *PVIEWPANE;
  310.  
  311. _________________________________________________________________
  312.  
  313. 10.   Original C header file 'Control.h':
  314.  
  315. typedef  struct   {
  316.    // Window interface related information.
  317.    FLAG           fHidden;       // whether Control should be allowed to be visible
  318.    USHORT         cViews;        // number of views of this object
  319.    PVIEWPANE      pViews;        // definition of each view
  320.  
  321.    // Relationships.
  322.    POBJECT        pParent;       // parent control (for nesting within dialogs)
  323.    POBJECT        pForm;         // Form containing the Control
  324.    USHORT         cSub;          // number of related subordinate Controls
  325.    PPOBJECT       ppSub;         // related Controls subordinate to this one
  326.  
  327.    // Actions in response to standard events.
  328.    POBJECT        apAction[EVENT_LAST];
  329. }  CONTROL, *PCONTROL;
  330.  
  331. typedef  struct   {
  332.    // status variables for controlling mouse dragging
  333.    USHORT         usButton;      // button 1 or 2 or none or both.
  334.    FLAG           fAlt;          // whether Alt pressed when drag started
  335.    FLAG           fControl;      // whether Control pressed when drag started
  336.    FLAG           fShift;        // whether Shift pressed when drag started
  337. }  CONTROL_STATIC;
  338.  
  339. _________________________________________________________________
  340.  
  341. 11.  Original C source code file 'Control.c':
  342.  
  343. // --------------------------------------------------------------------------
  344.  
  345. HWND     ViewPaneQueryHWND(PVIEWPANE pViewPane)
  346. {
  347.    return pViewPane->hwnd;
  348. }
  349.  
  350. // --------------------------------------------------------------------------
  351. // Finalise all updating of views of the Control.
  352.  
  353. FLAG     ControlViewsUpdate(PCONTROL pControl)
  354. {
  355.    USHORT   i;
  356.  
  357.    for (i=0; i<pControl->cViews; i++) {
  358.       WinUpdateWindow(ViewPaneQueryHWND(&pControl->pViews[i]));
  359.    }
  360.  
  361.    return TRUE;
  362. }
  363.  
  364. // --------------------------------------------------------------------------
  365.  
  366. PVOID    ControlWait(FLAG fWait, PVOID hptrOld)
  367. {
  368.    if (fWait) {
  369.       hptrOld = (PVOID) WinQueryPointer(HWND_DESKTOP);
  370.       WinSetPointer(HWND_DESKTOP, WinQuerySysPointer(HWND_DESKTOP, SPTR_WAIT, FALSE));
  371.    }
  372.    else {
  373.       WinSetPointer(HWND_DESKTOP, (HPOINTER) hptrOld);
  374.    }
  375.  
  376.    return hptrOld;
  377. }
  378.  
  379. _________________________________________________________________
  380.  
  381. 12.   Generated C++ header file 'Control.hpp':
  382.  
  383. // ** Automatic protection against including a header file multiple times
  384.  
  385. #ifndef  _CONTROL_
  386.  
  387. #define  _CONTROL_
  388.  
  389. // -------------------------------------------------------------------------
  390. // INCLUDE FILE DEPENDENCIES:
  391.  
  392. // ** Automatically includes headers for common and reference classes. 
  393.  
  394. #ifndef  _Common_
  395.    #include "Common.hpp"
  396. #endif
  397. #ifndef  _Handle_
  398.    #include "Handle.h"
  399. #endif
  400. #ifndef  _Object_
  401.    #include "Object.hpp"
  402. #endif
  403.  
  404. // -------------------------------------------------------------------------
  405. // FORWARD DECLARATIONS OF ALL NON-NESTED CLASSES DEFINED WITHIN THIS MODULE:
  406.  
  407. // ** Predeclares all classes declared in this module to allow cross-references from one class to another
  408. // ** Uses a typedef to simplify access to a template class used for managing Control pointers.
  409.  
  410. class    Control;
  411. typedef  Control* PCONTROL;
  412. typedef  ObjectHandle < Control >   HandleControl;
  413.  
  414. // =======================================================================
  415.  
  416. // ** You can define rules on the database which establish the parent class of a class
  417.  
  418. class EXPORT   Control  : public SharedObject   {
  419.  
  420.    // ** Automatically creates nested classes where appropriate
  421.    // ** and insert then into the enclosing class
  422.  
  423.    // ----------------------------------------------------------------------
  424.    // NESTED CLASSES:
  425.  
  426.    // --------------------------------------------------------------------
  427.  
  428.    class EXPORT   ViewPane : public Object   {
  429.  
  430.       // ** Automatically labels each section of the class declaration for better
  431.       // ** readability and maintainability.
  432.  
  433.       // ----------------------------------------------------------------------
  434.       // DATA MEMBERS:
  435.  
  436.    public:
  437.  
  438.       HWND           hwnd;          // window handle (client if in MDI frame)
  439.  
  440.       // ----------------------------------------------------------------------
  441.       // FUNCTION MEMBERS:
  442.  
  443.    private:
  444.  
  445.       // ** Automatically inserts these declarations so that your C++ compiler
  446.       // ** won't surprise you
  447.       // ** by quietly creating its own code for them.
  448.       // ** It also serves as a reminder for you top implement these functions
  449.  
  450.       // Declared private and not defined in order to trap unwanted usage.
  451.  
  452.       ViewPane(const ViewPane& rItem);
  453.       ViewPane operator=   (const ViewPane& rItem);
  454.  
  455.    public:
  456.  
  457.       HWND     QueryHWND();
  458.    };
  459.  
  460.    typedef  Control::ViewPane    VIEWPANE;
  461.    typedef  Control::ViewPane*   PVIEWPANE;
  462.  
  463.    // ** The banner and indentation tells us we are back to the main class' definition of Control.
  464.  
  465.    // ----------------------------------------------------------------------
  466.    // DATA MEMBERS:
  467.  
  468. public:
  469.  
  470.    // Window interface related information.
  471.    FLAG           fHidden;       // whether Control should be allowed to be visible
  472.    USHORT         cViews;        // number of views of this object
  473.    PVIEWPANE      pViews;        // definition of each view
  474.  
  475.    // ** This section shows a special feature of C2C++(TM) which allows for improved
  476.     // ** conversion of quasi object oriented C code.
  477.    // ** In this case the old C code uses a generic "POBJECT" which is a pointer
  478.     // ** to a member of an unknown class, but
  479.    // ** C2C++(TM) was able to transform this pointer into a managed pointer
  480.     // ** (a handle) to the correct class of object, as determined
  481.    // ** by the application of rules defined to C2C++(TM).
  482.  
  483.    // Relationships.
  484.    HandleControl  pParent;       // parent control (for nesting within dialogs)
  485.    HandleForm     pForm;         // Form containing the Control
  486.    USHORT         cSub;          // number of related subordinate Controls
  487.    HandleControl* ppSub;         // related Controls subordinate to this one
  488.  
  489.    // Actions in response to standard events.
  490.    HandleAction   apAction[EVENT_LAST];
  491.  
  492.    // ----------------------------------------------------------------------
  493.    // FUNCTION MEMBERS:
  494.  
  495. private:
  496.  
  497.       // ** Automatically inserts these declarations so that your C++ compiler 
  498.       // ** won't surprise you by quietly creating its own code for them.
  499.       // ** It also serves as a reminder for you top implement these functions
  500.  
  501.    // Declared private and not defined in order to trap unwanted usage.
  502.  
  503.    Control(const Control& rItem);
  504.    Control  operator=   (const Control& rItem);
  505.  
  506. public:
  507.  
  508.    FLAG     ViewsUpdate();
  509.  
  510.    // ** Notice how the class declaration is ordered according to 
  511.    // ** standard layout which allows for quick finding of declarations and
  512.    // ** easy detection of missing functions.
  513.  
  514.    // ----------------------------------------------------------------------
  515.    // STATIC DATA MEMBERS:
  516.  
  517. private:
  518.  
  519.    // ** These variables were known to be static because of the naming convention of the struct.
  520.  
  521.    // status variables for controlling mouse dragging
  522.    static   USHORT         usButton;      // button 1 or 2 or none or both.
  523.    static   FLAG           fAlt;          // whether Alt pressed when drag started
  524.    static   FLAG           fControl;      // whether Control pressed when drag started
  525.    static   FLAG           fShift;        // whether Shift pressed when drag started
  526.  
  527.    // ----------------------------------------------------------------------
  528.    // STATIC FUNCTION MEMBERS:
  529.  
  530.    static   PVOID    Wait(FLAG fWait, PVOID hptrOld);
  531. };
  532.  
  533. #endif
  534.  
  535.  
  536. _________________________________________________________________
  537.  
  538. 13.   Converted C++ source code file 'Control.cpp':
  539.  
  540. // --------------------------------------------------------------------------
  541. // ** The function was classified into the nested Control::ViewPane class.
  542. // ** The first parameter was removed because it is now implied.
  543.  
  544. HWND     Control::ViewPane::QueryHWND()
  545. {
  546.    // ** The expression was simplified because this is a member function.
  547.    return hwnd;
  548. }
  549.  
  550. // --------------------------------------------------------------------------
  551. // Finalise all updating of views of the Control.
  552.  
  553. FLAG     Control::ViewsUpdate()
  554. {
  555.    USHORT   i;
  556.  
  557.    // ** Superfluous references to the member function object are removed
  558.    // ** from all expressions.
  559.    for (i=0; i<cViews; i++) {
  560.       // ** Note that more complicated expressions that involve multiple
  561.       // ** translations are also translated correctly.
  562.       WinUpdateWindow(pViews[i].QueryHWND());
  563.    }
  564.  
  565.    return TRUE;
  566. }
  567.  
  568. // --------------------------------------------------------------------------
  569. // ** Static functions are detected and categorised into classes by rules.
  570. // ** Notice how the function name was also simplified by removal of the
  571. // ** prefix 'Control'.
  572.  
  573. PVOID    Control::Wait(FLAG fWait, PVOID hptrOld)
  574. {
  575.    if (fWait) {
  576.       hptrOld = (PVOID) WinQueryPointer(HWND_DESKTOP);
  577.       WinSetPointer(HWND_DESKTOP, WinQuerySysPointer(HWND_DESKTOP, SPTR_WAIT, FALSE));
  578.    }
  579.    else {
  580.       WinSetPointer(HWND_DESKTOP, (HPOINTER) hptrOld);
  581.    }
  582.  
  583.    return hptrOld;
  584. }
  585.  
  586. _________________________________________________________________
  587.  
  588. 14.   Summary
  589.  
  590. The effectiveness of C2C++(TM)    has been proven on commercial
  591. projects of over 400,000 lines of C code. Migrating a project of
  592. this size, without automation, is a mammoth proposition, and was
  593. only made feasible (time-wise and financially) through the use of
  594. C2C++(TM).
  595.  
  596. This above project was estimated to cost between $80,000 and
  597. $150,000 based on a conversion rate of 100 to 150 lines per hour
  598. at a cost of $30 to $40 per hour, and still does not take into
  599. consideration the learning curve or the cost of gaining
  600. sufficient familiarity with the original source code to be able
  601. to reliably and correctly convert the code to C++ ( In other
  602. words it does not take debugging into consideration).
  603.  
  604. Even a small project of 20,000 lines of code would cost between
  605. $4,000 and $8,000 to convert, again not taking into consideration
  606. the learning curve or the cost of gaining sufficient familiarity
  607. with the original source code to be able to reliably and
  608. correctly convert the code to C++.
  609.  
  610. Furthermore the drawback of a manual conversion is that further
  611. costs are involved for every new conversion project, whereas with
  612. C2C++(TM) there is a single up-front investment. This makes
  613. C2C++(TM) a serious economic consideration for all projects.
  614.  
  615. By ensuring an automated migration path from C to C++, C2C++(TM) 
  616. allows organizations to embark upon new object oriented projects
  617. without throwing away their investment in C code. They can
  618. achieve the same gains in application quality, development
  619. productivity and maintenance productivity that are being
  620. experienced by organizations already using C++.
  621.  
  622. _________________________________________________________________
  623.  
  624. 15.   Orders and Enquiries:
  625.  
  626. Post me a message on CompuServe including your FAX number and
  627. CompuServe address. This will enable me to fax your order form or
  628. liaise with you directly.
  629.  
  630. My CompuServe address is: 
  631.  
  632. 70732,3352
  633.  
  634. My international head office fax number is:
  635.  
  636. international-27-21-72-8005
  637.  
  638. Thankfully yours.
  639. John Viveiros 
  640.  
  641. _________________________________________________________________
  642.  
  643.