Re: Design question: best way for initializing many small objects
michael wrote:
a) Equipping each and every object with a X509_STORE* member and
initialize them accordingly?
If for most of the X509_Certificate the X509_STORE * represents a
const * which is identical in contents and behavior this is definitely
a waste of space.
b) Make X509_STORE* a static member of X509_Certificate and initialize
it at program startup?
Why not make it a static member ? They aren't so evil. You have
one instance per class and even can fine grain access control via
inheritance and friendship. If you're afraid of the "static
initialization order fiasco" you could make it a static NULL
pointer which gets initialized the first time a X509_Certificate
is created (it will however never be destructed).
Additionally, if the program evolves in a way,
that
more than one X509_STORE* can be used by those Certificates, this
solution
is a dead end.
I don't know much about the details but a way to handle this could
be like this:
<snip>
class X509_Store {
public:
int a;
int b;
};
class X509_Cert_Base {
public:
X509_Cert_Base() : _local_STORE(NULL) {
// initialize the global store for all
// Certs
if(_global_STORE==NULL) {
_global_STORE=new X509_Store();
}
}
~X509_Cert_Base() {
if(_local_STORE!=NULL) {
delete _local_STORE;
}
}
protected:
X509_Store * store() {
if(_local_STORE==NULL) {
_local_STORE = new X509_Store(*_global_STORE);
}
return _local_STORE;
}
const X509_Store * store() const {
return _local_STORE!=NULL ? _local_STORE : _global_STORE;
}
private:
static X509_Store * _global_STORE;
X509_Store * _local_STORE;
};
X509_Store * X509_Cert_Base::_global_STORE = NULL;
class X509_Certificate : public X509_Cert_Base {
private:
typedef X509_Cert_Base super;
public:
X509_Certificate() : super() {}
// accesses _global_STORE if _local_STORE
// has not been allocated
int foo() {
return store()->a*2;
}
// triggers creation of _local_STORE
// if not allocated already
void bar() {
store()->a=90;
}
};
int main() {
X509_Certificate c;
c.foo();
c.bar();
}
</snip>
So X509_Store can only be accessed via the protected function
store() in the base class. All X509_Certificates initially work
on the same object. store() creates a private instance as a copy
of the global one whenever data members of X509_Store have to
be modified. This might be a solution.
HTH
O.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]