A C++ Class to handle Graphics Group Assignment

I want to assign a graphic group number to several elements in my MDL application. The application is written in C++, so I'm able to benefit from some aspects of C++ constructors and destructors.

The current graphic group number is available in global variable tcb->graphic. You apply it to an element using mdlElement_setProperties. Once you've assigned a graphic group number to one or more elements, you should increment the graphic group number ready for the next assignment.

MDL function mdlSystem_updateGraphicGroup updates the graphic group number. The MDL documentation about that function states: After mdlSystem_updateGraphicGroup returns, the value in tcb->graphic is one greater than the value returned. A program should not use tcb->graphic unless it calls mdlSystem_updateGraphicGroup afterwards.

I'm pretty good at forgetting to do things, so that last statement raised a warning flag in my mind. How could I be sure always to increment the graphic group number in the TCB? The solution, with a C++ class, is very simple: we design a class to handle graphic group assignment that calls mdlSystem_updateGraphicGroup in its destructor.

We should be able to use the class something like this …

void SomeFunctionThatAssignsGraphicGroup (MSElement* el)
{
    GraphicGroup gg;
    gg.Assign (el);
    //  GraphicGroup destructor calls mdlSystem_updateGraphicGroup
}

Here's the source code of the GraphicGroup class. Feel free to add this to your own C++ projects. GraphicGroup's constructor gets the current value of tcb->graphic. Its Assign member sets the graphic group property of an element. Its destructor calls mdlSystem_updateGraphicGroup automatically.

GraphicGroup Header File

GraphicGroup.h

#if !defined(GRAPHIC_GROUP_H_INCLUDED)
#define GRAPHIC_GROUP_H_INCLUDED

//////////////////////////////////////////////////////////////////////
//	GraphicGroup example provided by LA Solutions Ltd
//	You may freely copy this code for use in your own C++ projects
//	whether commercial or otherwise.
//	http://www.la-solutions.co.uk
//////////////////////////////////////////////////////////////////////
//	GraphicGroup class is a helper to ensure that graphic group setting
//	is handled correctly and automatically.  Instantiate this class
//	before assigning a graphic group no. to one or more elements.
//	The destructor advances the TCB graphic group no. automatically
//////////////////////////////////////////////////////////////////////
class GraphicGroup
{
public:
    //////////////////////////////////////////////////////////////////////
    //	Construction
    GraphicGroup	();
    ~GraphicGroup	();
    //////////////////////////////////////////////////////////////////////
    //	Implementation
    bool            Assign   (MSElement*  el) const;

private:
    UInt32          gg_;    //	Gets tcb->graphic
    GraphicGroup	(const GraphicGroup&  NoCopyConstructor			);
    GraphicGroup& operator=	(const GraphicGropu&  NoAssignment			);
};

#endif //   GRAPHIC_GROUP_H_INCLUDED

GraphicGroup Implementation File

GraphicGroup.cpp

//    Standard include for MFC projects
#include "stdafx.h"
//    Our header
#include "GraphicGroup.h"
//    MDL header files
#include <mselemen.fdf>
#include <mssystem.fdf>
#include <tcb.h>
//////////////////////////////////////////////////////////////////////
//	GraphicGroup example provided by LA Solutions Ltd
//	You may freely copy this code for use in your own C++ projects
//	whether commercial or otherwise.
//	http://www.la-solutions.co.uk
//////////////////////////////////////////////////////////////////////
//  GraphicGroup constructor
GraphicGroup::GraphicGroup      ()
   : gg_                        (tcb->graphic)
{
}
//////////////////////////////////////////////////////////////////////
//  GraphicGroup destructor
GraphicGroup::~GraphicGroup     ()
{
    mdlSystem_updateGraphicGroup ();
}
//////////////////////////////////////////////////////////////////////
//	GraphicGroup's sole method: assign the graphic group number to an element
bool	GraphicGroup::Assign	(MSElement*		el)	const
{
    _ASSERT (NULL != el);
    mdlElement_setProperties (el, NULL, const_cast(&gg_), NULL, NULL, NULL, NULL, NULL, NULL);
    return true;
}

Constness

The GraphicGroup class encourages you to use const. It's only member function Assign is declared const. This means you can pass an instance of GraphicGroup to other functions by const reference, like this …

//////////////////////////////////////////////////////////////////////
void SomeFunctionThatAssignsGraphicGroup (MSElement* el1, MSElement* el2, )
{
    GraphicGroup gg;
    //  Assign graphic group number to one element
    AnotherFunctionThatAssignsGraphicGroup (el1, gg);
    //  Assign same graphic group to another element
    AnotherFunctionThatAssignsGraphicGroup (el2, gg);
    //  GraphicGroup destructor calls mdlSystem_updateGraphicGroup
}
//////////////////////////////////////////////////////////////////////
void AnotherFunctionThatAssignsGraphicGroup (MSElement* el, const GraphicGroup& gg)
{
    gg.Assign (el);
}