FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
July 01, 1997

A Memory-Constrained Image-Processing Architecture

(Page 5 of 5)
Dr. Dobb's Journal July 1997: A Macro Technique for Managing Types

A Macro Technique for Managing Types

Dr. Dobb's Journal July 1997

By Mayur Patel


In the image-processing architecture presented here, I use a macro technique to encapsulate data type support. Like any macro technique, it requires disciplined use. However, it provides a convenient way to make branching decisions based on type information.

C/C++ programmers are familiar with the source-header file combination. To implement this macro technique, I need another kind of file. I call this a macro file, and I use a ".m" extension. Some choose to call it an include file, with a ".i" extension.

There are three things to notice in Listing Four, which is an example macro file I call "Type.m." First, all macros are undefined by the end of the file. Second, the macro TYPEMACRO is not defined within this file, even though it is used. TYPESEPER and TYPEENDOFLIST are defined only if they haven't already been instantiated. Third, one of the elements in the TYPEMACRO is a formal data type.

The parameters to the TYPEMACRO calls encapsulate a table of information. Depending on your application, you will need different information in this table. The trick is to keep this table minimal. In the image-processing architecture, I had to include function names, numeric values, and precedence information.

Listing Five shows one way you can use the macro file in Listing Four. This bit of code declares an enumeration that can be used to identify and manage types in a cleaner way in other parts of the code. The TYPEMACRO definition allows information to be extracted from the table in the Type.m file and used in the current context. The TYPESEPER and TYPEENDOFLIST elements allow the included table entries to adapt to the syntax of their context. Listing Six illustrates another way to use the macro file in Listing Four.

The application uses the enumerated type declared earlier to provide type information when the function is invoked. In this case, data is available through void pointers, but in general, any interface that supports easy typecasting will work. The macro definition and include statement fill the switch with a variety of alternative behaviors, depending upon the specified type.

I originally learned this technique while implementing databases, but it clearly can be applied to a wide variety of situations. With sparing, disciplined use, it can be a powerful tool for managing types in C/C++.

-- M.P.

Dr. Dobb's Journal July 1997


Copyright © 1997, Dr. Dobb's Journal

Previous Page | 1 | 2 | 3 | 4 | 5
RELATED ARTICLES
No Related Articles
TOP 5 ARTICLES
No Top Articles.
DR. DOBB'S CAREER CENTER
Ready to take that job and shove it? open | close
Search jobs on Dr. Dobb's TechCareers
Function:

Keyword(s):

State:  
  • Post Your Resume
  • Employers Area
  • News & Features
  • Blogs & Forums
  • Career Resources

    Browse By:
    Location | Employer | City
  • Most Recent Posts:
    MEDIA CENTER  more
    NetSeminar
    Modernize your Development by Moving Build and Code Quality Upstream
    Moderated by Jon Erickson, Editor-in-Chief of Dr. Dobb's, this interactive panel discussion brings industry experts Anders Wallgren, CTO of Electric Cloud and Gwyn Fisher, CTO of Klocwork together for a candid discussion of the cost savings, productivity and quality benefits that can be achieved by stabilizing builds and code quality as early in the development cycle as possible.

    The reality of today's development environment - geographically distributed teams, the use of Agile development practices, increasing application complexity, etc. - is straining the viability of the traditional coding, build and release process. To stay ahead of the curve, development teams are modernizing their approach to dealing with these issues, and as a result are achieving new levels of development productivity. Register for the webcast.
    Date: Wednesday, July 15, 2009
    Time: 11 am PT/2 pm ET
    Modernize your Development by Moving Build and Code Quality Upstream
    Moderated by Jon Erickson, Editor-in-Chief of Dr. Dobb's, this interactive panel discussion brings industry experts Anders Wallgren, CTO of Electric Cloud and Gwyn Fisher, CTO of Klocwork together for a candid discussion of the cost savings, productivity and quality benefits that can be achieved by stabilizing builds and code quality as early in the development cycle as possible.

    The reality of today's development environment - geographically distributed teams, the use of Agile development practices, increasing application complexity, etc. - is straining the viability of the traditional coding, build and release process. To stay ahead of the curve, development teams are modernizing their approach to dealing with these issues, and as a result are achieving new levels of development productivity. Register for the webcast.
    Date: Wednesday, July 15, 2009
    Time: 11 am PT/2 pm ET
                                   
    INFO-LINK

    Resource Links: