Array of Base Class Pointers (Templated)

J

Jeffrey Walton

Hi All,

Bear with the newbie question. It has been years since I had this in
college.

I'm working with Crypto++. I'm trying to build a base class array, but
the class is templated. Is there anyway to make it happen? I'm hoping
it is a syntax problem on my part.

template<class BASE>
class CipherModeFinalTemplate_ExternalCipher< BASE >

CipherModeFinalTemplate_ExternalCipher* Ciphers[] = {
new CipherModeFinalTemplate_ExternalCipher< ECB_Mode >,
...
}

I want to get to SetCipher() of the class so I can dynamically change
it at runtime. Sample code is at http://cryptopp.pastebin.com/d3a117c6c

Thanks,
Jeff
Jeffrey Walton

Full library documentation is at
http://www.cryptopp.com/docs/ref/cl..._cipher.html#b183bb3665c750c4555c3d9c8ef9f914
 
M

Michael DOUBEZ

Jeffrey Walton a écrit :
Hi All,

Bear with the newbie question. It has been years since I had this in
college.

I'm working with Crypto++. I'm trying to build a base class array, but
the class is templated. Is there anyway to make it happen? I'm hoping
it is a syntax problem on my part.

template<class BASE>
class CipherModeFinalTemplate_ExternalCipher< BASE >

CipherModeFinalTemplate_ExternalCipher* Ciphers[] = {

CipherModeFinalTemplate_ExternalCipher is a template, you cannot create
an array from it; you must specialize it.
new CipherModeFinalTemplate_ExternalCipher< ECB_Mode >,
...
}

The template is defined as follow:
template <class BASE>
class CipherModeFinalTemplate_ExternalCipher : public BASE
{
//...
};

Therefore, the templates specicialisations are likely to be orthogonal
classes (ie. they do not share a common ancestor you can use).
I want to get to SetCipher() of the class so I can dynamically change
it at runtime. Sample code is at http://cryptopp.pastebin.com/d3a117c6c

A solution is to create a holder making them inheriting from a common
interface:

struct CipherModeInterface
{
virtual void SetCipher (BlockCipher &cipher)=0;
};

template<class BASE>
struct CipherModeHolder: public CipherModeInterface
{

virtual void SetCipher (BlockCipher &cipher)
{
value.SetCipher(cipher);
}

CipherModeFinalTemplate_ExternalCipher<BASE> value;
};

And then you can create;

CipherModeInterface* Ciphers[]={
new CipherModeHolder<ECB_MODE>(),
//...
};

I don't know where you want to use the
CipherModeFinalTemplate_ExternalCipher<> instances but you cannot get
them back unless you know their type and do a dynamic_cast or unless you
they can be used through the common interface CipherModeInterface.

My impression is that your design is not in line with the intent of the
library.

Michael
 
J

Jeffrey Walton

Hi Michael,

Sorry about the top post... It's basically a thank you (why scroll
when we do't have to?)
My impression is that your design is not in line with
the intent of the library. Thanks

A solution is to create a holder making them inheriting
from a common interface...
I considered this, but I did not want to recompile the library.

Jeff

Jeffrey Walton a écrit :
Bear with the newbie question. It has been years since I had this in
college.
I'm working with Crypto++. I'm trying to build a base class array, but
the class is templated. Is there anyway to make it happen? I'm hoping
it is a syntax problem on my part.
template<class BASE>
class CipherModeFinalTemplate_ExternalCipher< BASE >
CipherModeFinalTemplate_ExternalCipher* Ciphers[] = {

CipherModeFinalTemplate_ExternalCipher is a template, you cannot create
an array from it; you must specialize it.
new CipherModeFinalTemplate_ExternalCipher< ECB_Mode >,
...
}

The template is defined as follow:
template <class BASE>
class CipherModeFinalTemplate_ExternalCipher : public BASE
{
//...

};

Therefore, the templates specicialisations are likely to be orthogonal
classes (ie. they do not share a common ancestor you can use).
I want to get to SetCipher() of the class so I can dynamically change
it at runtime. Sample code is athttp://cryptopp.pastebin.com/d3a117c6c

A solution is to create a holder making them inheriting from a common
interface:

struct CipherModeInterface
{
virtual void SetCipher (BlockCipher &cipher)=0;
};

template<class BASE>
struct CipherModeHolder: public CipherModeInterface
{

virtual void SetCipher (BlockCipher &cipher)
{
value.SetCipher(cipher);
}

CipherModeFinalTemplate_ExternalCipher<BASE> value;

};

And then you can create;

CipherModeInterface* Ciphers[]={
new CipherModeHolder<ECB_MODE>(),
//...
};

I don't know where you want to use the
CipherModeFinalTemplate_ExternalCipher<> instances but you cannot get
them back unless you know their type and do a dynamic_cast or unless you
they can be used through the common interface CipherModeInterface.

My impression is that your design is not in line with the intent of the
library.

Michael
 
M

Michael DOUBEZ

Jeffrey Walton a écrit :
Hi Michael,

Sorry about the top post... It's basically a thank you (why scroll
when we do't have to?)

I considered this, but I did not want to recompile the library.

You don't have to recompile the library. You just create your own
structures to use in your program (the way I've shown).

After a quick look at CipherModeFinalTemplate_ExternalCipher, I think it
would be easy to make a stategy based one. But again, I am not sure it
is the right design since its intent is already to be a strategy. I
guess there is a higher levelclass fitting your needs
Jeffrey Walton a écrit :
Hi All,
Bear with the newbie question. It has been years since I had this in
college.
I'm working with Crypto++. I'm trying to build a base class array, but
the class is templated. Is there anyway to make it happen? I'm hoping
it is a syntax problem on my part.
template<class BASE>
class CipherModeFinalTemplate_ExternalCipher< BASE >
CipherModeFinalTemplate_ExternalCipher* Ciphers[] = {
CipherModeFinalTemplate_ExternalCipher is a template, you cannot create
an array from it; you must specialize it.
new CipherModeFinalTemplate_ExternalCipher< ECB_Mode >,
...
}
The template is defined as follow:
template <class BASE>
class CipherModeFinalTemplate_ExternalCipher : public BASE
{
//...

};

Therefore, the templates specicialisations are likely to be orthogonal
classes (ie. they do not share a common ancestor you can use).
I want to get to SetCipher() of the class so I can dynamically change
it at runtime. Sample code is athttp://cryptopp.pastebin.com/d3a117c6c
A solution is to create a holder making them inheriting from a common
interface:

struct CipherModeInterface
{
virtual void SetCipher (BlockCipher &cipher)=0;
};

template<class BASE>
struct CipherModeHolder: public CipherModeInterface
{

virtual void SetCipher (BlockCipher &cipher)
{
value.SetCipher(cipher);
}

CipherModeFinalTemplate_ExternalCipher<BASE> value;

};

And then you can create;

CipherModeInterface* Ciphers[]={
new CipherModeHolder<ECB_MODE>(),
//...
};

I don't know where you want to use the
CipherModeFinalTemplate_ExternalCipher<> instances but you cannot get
them back unless you know their type and do a dynamic_cast or unless you
they can be used through the common interface CipherModeInterface.

My impression is that your design is not in line with the intent of the
library.
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,979
Members
47,579
Latest member
CharaS3188

Latest Threads

Top