Sign in to follow this  
  • entries
    292
  • comments
    557
  • views
    153570

Code Dump

Sign in to follow this  
TANSTAAFL

94 views

The other day, a colleague and I were discussing the potential code for new system. This new system would need to operate on multiple (okay! two) platforms.

But we wanted to make most of the code usable on either platform, so we needed to write platform independent code, provide abstract interfaces for platform specific functionality, and then provide implementations, hidden, for each of the platforms.

One of these abstract interfaces is the MemoryManager:


//MemoryManager.h
class MemoryManager
{
private:
MemoryManager(const MemoryManager&);
static MemoryManager* s_ptrMemoryManager;
protected:
MemoryManager()
{
if(s_ptrMemoryManager)
{
//blow up!
}
else
{
s_ptrMemoryManager=this;
}
}
virtual void* AllocateMemory_impl(unsigned long size)=0;
virtual void FreeMemory_impl(void* ptr)=0;
public:
virtual ~MemoryManager(){}
static void* AllocateMemory(unsigned long size)
{
if(s_ptrMemoryManager)
{
return(s_ptrMemoryManager->AllocateMemory_impl(size));
}
else
{
//blow up!
}
}
static void FreeMemory(void* ptr)
{
if(s_ptrMemoryManager)
{
if(ptr)
{
s_ptrMemoryManager->FreeMemory_impl(ptr);
}
}
else
{
//blow up!
}
}
};

//MemoryManager.cpp
MemoryManager* MemoryManager::s_ptrMemoryManager=0;

//WIN32MemoryManager.cpp
class WIN32MemoryManager
:public MemoryManager
{
private:
static WIN32MemoryManager s_WIN32MemoryManager;
protected:
WIN32MemoryManager();
virtual void* AllocateMemory_impl(unsigned long size)
{
return(malloc(size));
}
virtual void FreeMemory_impl(void* ptr)
{
free(ptr);
}
};
WIN32MemoryManager WIN32MemoryManager::s_WIN32MemoryManager;



So, basically, MemoryManager is an abstract factory singleton.

And soon we found other places in the code that needed similar abstract factory singletons.

And since the code was all so similar, we decided to make it a template:


//Factory.h
template
class Factory
{
private:
Factory(const Factory&);
static Factory* s_ptrFactory;
protected:
Factory()
{
if(s_pFactory)
{
//blow up!
}
else
{
s_pFactory=this;
}
}
virtual product_t* Create_impl(const initializer_t& initializer)=0;
virtual void Destroy_impl(product_t* product)=0;
public:
static product_t* Create(const initializer_t& initializer=initializer_t())
{
if(s_ptrFactory)
{
return(s_ptrFactory->Create_impl(initializer));
}
else
{
//blow up!
}
}
static void Destroy(product_t* product)
{
if(s_ptrFactory)
{
if(product)
{
s_ptrFactory->Destroy_impl(product);
}
}
else
{
//blow up!
}
}
virtual ~Factory()
};



So now, WIN32MemoryManager is really a child class of Factory, rather than MemoryManager, and other types of objects, like Semaphores, Threads, Queues, and so on, are children of the appropriate Factory class.

Well, *I* thought it was neat, anyway.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now