Author Topic: ResbufPointer Class  (Read 2403 times)

0 Members and 1 Guest are viewing this topic.

Chuck Gabriel

  • Guest
ResbufPointer Class
« on: April 13, 2007, 04:02:00 PM »
I thought somebody else might find this useful.  It is a quick little smart pointer class I threw together today to help me avoid forgetting to free resbuf pointers:

Code: [Select]
class ResbufPointer {
resbuf* pHead; // Maintain a pointer to the head of the resource chain for de-allocation
resbuf* pBuffer;
public:
ResbufPointer(resbuf* ptr) : pHead(ptr), pBuffer(ptr) {}
~ResbufPointer() {
acutRelRb(pHead); // release the buffer
pHead = pBuffer = 0;
}
resbuf* operator->() { return pBuffer; }  // so you can do things like buf->resval.rstring
operator resbuf*() { return pBuffer; } // so you can pass the smart pointer to functions that expect a resbuf*
bool isNull() { return pBuffer == 0; } // null pointer check
void start() { pBuffer = pHead; } // in case you need to return to the head of the resource chain
void next() { pBuffer = pBuffer->rbnext; } // try to move the pointer to the next item in the resource chain
};

It seemed silly to separate the implementation from the interface on something so trivial, so I just lumped it all together.

<edit>Added some comments.  Changed silly variable names.</edit>
« Last Edit: April 13, 2007, 09:36:29 PM by Chuck Gabriel »

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6940
  • AKA Daniel
Re: ResbufPointer Class
« Reply #1 on: April 15, 2007, 09:41:29 AM »
I like it, I was wondering if resbuf had a smart  pointer, I guess it does  8-)
Thanks Chuck

Alexander Rivilis

  • Bull Frog
  • Posts: 212
  • Programmer from Kyiv (Ukraine)
Re: ResbufPointer Class
« Reply #2 on: April 15, 2007, 03:06:20 PM »
Another sample of class wrapper of struct resbuf is ArxDbgRbList (from ArxDbg sample in ObjectARX SDK)
« Last Edit: April 15, 2007, 11:17:37 PM by Alexander Rivilis »

Chuck Gabriel

  • Guest
Re: ResbufPointer Class
« Reply #3 on: April 15, 2007, 09:59:29 PM »
Thanks Alexander.  I hadn't ever noticed that in the samples.