Remember 3December
Find out where to celebrate your 3D CG art...
Theme color:
  • 1/3
You are here: Homepage /  Blogs /  The 3ds Max SDK Blog / Reducing Cutting and Pasting of Code in your Max SDK Plug-ins
Reducing Cutting and Pasting of Code in your Max SDK Plug-ins
Posted: Oct 08, 2009
Category:
Social Media:
Bookmark and Share

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.

In order to post any comments, you must be logged in!
Newest users comments View All 1 Comments
Posted by EnderJSC on Oct 29, 2009 at 08:07 PM
I was just considering implementing this exactly. Thanks.