Archived

This topic is now archived and is closed to further replies.

I need code tested on other compilers...

This topic is 5047 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can you try compiling this code. Then post the compiler name, compiler version and any errors/warnings (if any) you get.
#include <iostream>
#include <cstdlib>

using namespace std;

#define smart(clazz) clazz##_smart_
#define SMART_CLASS(clazz)                    \
    class smart(clazz);                       \
    typedef SmartObject<smart(clazz)> clazz;  \
    class smart(clazz)


class NullPointerException { public: NullPointerException() {} };
template<class T>
class PointerReferences
{
	long count;
	T p;
public:
	PointerReferences() : count(0) {}
	void incRef() { count++; }
	void decRef() { delete this; }
	void operator delete (void *o) {
		PointerReferences<T> *t = static_cast<PointerReferences<T>* >(o);
		if (!t) return;
		if (t->count > 0)
			t->count--;
		if (t->count <= 0)
			free(t);
	}
	T *getPointer() { return &p; }
};
template<class T>
class SmartObject
{
	PointerReferences<T> *p;
	void _assign(PointerReferences<T> *o)
	{
		if (p)
			p->decRef();
		p = o;
		if (p)
			p->incRef();
	}
public:
	SmartObject() : p(0) {}
	//SmartObject(T *o) : p(0) { this->_assign(o); }

	SmartObject(const SmartObject<T> &o) : p(0) { *this = o; }
	SmartObject(const SmartObject<T> *o) : p(0) { *this = o; }
	virtual ~SmartObject() { *this = 0; }

	//Assignment

	SmartObject<T> &operator = (const SmartObject<T> &o) {
		if (o.p)
			this->_assign(const_cast<PointerReferences<T>* >(o.p));
		else
			this->_assign((PointerReferences<T> *)0);
		return *this;
	}
	SmartObject<T> &operator = (const SmartObject<T> *o) {
		//HACK: Theoretically SmartObject<T>* it is actually a PointerReferences<T>* in disguise.

		if (o)
			this->_assign(reinterpret_cast<PointerReferences<T>*>
				(
					const_cast<SmartObject<T>* >(o)
				));
		else
			this->_assign((PointerReferences<T> *)0);
		return *this;
	}
	operator T *() { return p->getPointer(); }

	//Pointer to member

	T &operator *() { if (p && p->getPointer()) return *(p->getPointer()); else throw NullPointerException(); }
	T *operator ->() { if (p && p->getPointer()) return p->getPointer(); else throw NullPointerException(); }

	//Comparison

	bool operator !() { return !p; }
	bool operator ==(const T *o) { return (p->getPointer() == o); }
	bool operator !=(const T *o) { return (p->getPointer() != o); }
	operator bool() { return (p != 0); }

	//New operator

	void *operator new (size_t sz) { return new PointerReferences<T>(); }
};

SMART_CLASS(testClass)
{
public:
    smart(testClass)() {
        std::cout << "\nAllocating 65000 integers.";
    }
    virtual ~smart(testClass)() {
        std::cout << "\nDestroying 65000 integers.";
    }
    void run() {
        std::cout << "\nRunning...";
    }
};
SMART_CLASS(class2)
{
public:
    smart(class2)() {
        std::cout << "\nCreating new class2";
    }
    ~smart(class2)() {
        std::cout << "\nDestroying class2";
    }
};

int main(int argc, char **argv)
{
    try
    {
        testClass t = new testClass;
        t->run();
        t = new testClass;
        t->run();
        t->run();
        t = 0;
        //only condition that I can''t handle

        new class2;
    }
    catch (NullPointerException)
    {
        cout << "\nNullPointerException!";
    }
    catch (...)
    {
        cout << "\nUnknown Exception!";
    }
    cin.ignore();
    return 0;
}
Thanks. btw, does this code prove I''m crazy?!!
<!-- ................................ --> u r what u bcome My (XHTML 1.0 Strict) Website

Share this post


Link to post
Share on other sites
I''m just trying to make a way to simplifying managing pointers (not that I have a big problem with that...), by using automatic memory managment with very little overhead.

Share this post


Link to post
Share on other sites
boost::shared_ptr
boost::scoped_ptr
boost::weak_ptr
et. al.

- Magmai Kai Holmlor

Not For Rent

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]
[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ | wxWindows| Spirit(xBNF)]
[Free C Libraries | zlib ]

Share this post


Link to post
Share on other sites