• 1/2

Reducing Cutting and Pasting of Code in your Max SDK Plug-ins

Posted by Christopher Diggins, 8 October 2009 1:33 pm

I am obsessive about factoring out common code. One of the worse crimes a developer can commit in my opinion is cutting and pasting code. Cutting and pasting bloats your code, hurting performance, making debugging and maintenance a serious nightmare.

Anyway, assuming you accept that redundant code is bad. The problem remains: given several different classes that are plug-ins can derive from, how do we factor common code out into a common class. The trick I am going to show you is a technique of inheriting from a template parameter.

Say for example that I have a number of plug-ins of different kinds that all have one reference which is a single parameter block. We can write a common "base" class to derive these classes.

template<typename Base_Type>
class MySingleReferenceClass : Base_Type
{
protected:
   IParamBlock2* pblock;
public:
   MySharedClass()
       : pblock(NULL), Base_Type()
   { }
   virtual int NumRefs() {
       return 1;
   }
   virtual RefTargetHandle GetReference(int i) {
       return i == 0 ? pblock : NULL;
   }
   virtual void SetReference(int i, RefTargetHandle rtarg) {
       if (i == 0) pblock = dynamic_cast<rtarg>(rtarg);
   }
};

Now your new plug-ins can derive from this as follows:

class MyGeomObjectPlugin : MySingleReferenceClass<GeomObject>
{
    ...
};

class MyModifierPlugin : MySingleReferenceClass<Modifier>
{
    ...
};

This is a pretty simple example, I'm sure you can imagine ways to extend it to make your plug-in projects more powerful.Utilizing This technique can be a great way to reduce the amount of cutting and pasting that you have to do in your plug-in.

Comments

There are currently no comments for this post. Be the first to comment!

Add Your Comment

You must be logged in to post a comment.

Please only report comments that are spam or abusive.