Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Design

A Memory-Constrained Image-Processing Architecture


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


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.