In my application I got many instances of class CDbaOciNotifier. They all share a pointer to only one instance of class OCIEnv.
What I like to achieve is that allocation and deallocation of the resource class OCIEnv will be handled automatically inside class CDbaOciNotifier.
The desired behaviour is, with the first instance of class CDbaOciNotifier the environment will be created, after that all following notifiers use that same environment. With the destruction of the last notifier, the environment will be destroyed too (call to custom deleter). Later on, this cycle can start again with the creation of a new environment.
What I've got so far (using a static factory method to create notifiers):
#pragma once
#include <string>
#include <memory>
#include "boost\noncopyable.hpp"
class CDbaOciNotifier : private boost::noncopyable
{
public:
virtual ~CDbaOciNotifier(void);
static std::auto_ptr<CDbaOciNotifier> createNotifier(const std::string &tnsName, const std::string &user, const std::string &password);
private:
CDbaOciNotifier(OCIEnv* envhp);
// All notifiers share one environment
static OCIEnv* m_ENVHP;
// Custom deleter
static void freeEnvironment(OCIEnv *env);
OCIEnv* m_envhp;
};
CPP:
#include "DbaOciNotifier.h"
using namespace std;
OCIEnv* CDbaOciNotifier::m_ENVHP = 0;
CDbaOciNotifier::~CDbaOciNotifier(void)
{
}
CDbaOciNotifier::CDbaOciNotifier(OCIEnv* envhp)
:m_envhp(envhp)
{
}
void CDbaOciNotifier::freeEnvironment(OCIEnv *env)
{
OCIHandleFree((dvoid *) env, (ub4) OCI_HTYPE_ENV);
*env = null;
}
auto_ptr<CDbaOciNotifier> CDbaOciNotifier::createNotifier(const string &tnsName, const string &user, const string &password)
{
if(!m_ENVHP)
{
OCIEnvCreate( (OCIEnv **) &m_ENVHP, OCI_EVENTS|OCI_OBJECT, (dvoid *)0,
(dvoid * (*)(dvoid *, size_t)) 0,
(dvoid * (*)(dvoid *, dvoid *, size_t))0,
(void (*)(dvoid *, dvoid *)) 0,
(size_t) 0, (dvoid **) 0 );
}
//shared_ptr<OCIEnv> spEnvhp(m_ENVHP, freeEnvironment); ...got so far...
return auto_ptr<CDbaOciNotifier>(new CDbaOciNotifier(m_ENVHP));
}
I'd like to avoid counting references (notifiers) myself, and use something like shared_ptr.
Do you see an easy solution to m开发者_运维技巧y problem?
There is a lot going on in your code. Here is the solution, but simplified to just the bare essentials.
class CDbaOciNotifier
{
public:
CDbaOciNotifier() :
m_resource(get_env())
{ }
private:
shared_ptr<OCIEnv> m_env;
struct Delete_env
{
void operator()(OCIEnv* env)
{
OCIHandleFree( ... );
}
};
static shared_ptr<OCIEnv> get_env()
{
// make sure a mutex is involved if CDbaOciNotifier
// can be constructed concurrently.
static weak_ptr<OCIEnv> s_env;
shared_ptr<OCIEnv> env = s_env.lock();
if( ! env )
{
OCIEnv* env_ptr = OCIEnvCreate( ... );
env.reset( env_ptr, Delete_env() );
s_env = env;
}
return env;
}
};
As written you cannot construct CDbaOciNotifier
concurrently. You'll need a static mutex to protect s_env
if you want that ability.
The weak_ptr
needs to be a function local static otherwise you're app might explode if a global or static CDbaOciNotifier
is created (static initialization order is undefined).
Does this work for you?
// In .h file
class CDbaOciNotifier
{
// ...
private:
static shared_ptr<OCIEnv> envPtr;
};
// In .cpp file
// Write freeEnvironment as a free function.
shared_ptr<OCIEnv> CDbaOciNotifier::envPtr(new OCIEnv, freeEnvironment);
精彩评论