473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Factory specialization

Hello all,

Please see my question embedded in comment form below.

Thanks,
Dave

#include <iostream>
#include <boost/shared_ptr.hpp>

using namespace std;
using namespace boost;

// A base class
class Deleter
{
public:
Deleter() {}
virtual ~Deleter() {}
};

// A derived class
class MyClass: public Deleter
{
public:
MyClass(int data): m_data(data) {}
virtual ~MyClass() {}

void DoIt() const
{
cout << m_data << endl;
}

private:
int m_data;
};

// A factory for objects that are constructed with 0 or 1 parameters.
template <class T>
class Factory
{
public:
static shared_ptr<T> Create()
{
cout << "Primary Factory invoked" << endl;
return shared_ptr<T>(new T());
}

template <class P1>
static shared_ptr<T> Create(P1 p1)
{
cout << "Primary Factory invoked" << endl;
return shared_ptr<T>(new T(p1));
}
};

// Here's where I don't have things right. I'd like to specialize Factory
for types derived from Deleter.
// How may I do so?
template <class T> // ?????????????
class Factory<Deleter> // ?????????????
{
static shared_ptr<T> Create()
{
cout << "Specialized Factory invoked" << endl;
return shared_ptr<T>(new T());
}

template <class P1>
static shared_ptr<T> Create(P1 p1)
{
cout << "Specialized Factory invoked" << endl;
return shared_ptr<T>(new T(p1));
}
};

int main()
{
shared_ptr<MyClass> ptr(Factory<MyClass>::Create(42));

ptr->DoIt();
}
Nov 30 '05 #1
6 1581
* Dave:

#include <iostream>
#include <boost/shared_ptr.hpp>

using namespace std;
using namespace boost;

// A base class
class Deleter
{
public:
Deleter() {}
virtual ~Deleter() {}
};
What's the purpose of Deleter?

// A derived class
class MyClass: public Deleter
{
public:
MyClass(int data): m_data(data) {}
virtual ~MyClass() {}

void DoIt() const
{
cout << m_data << endl;
}

private:
int m_data;
};

// A factory for objects that are constructed with 0 or 1 parameters.
template <class T>
class Factory
{
public:
static shared_ptr<T> Create()
{
cout << "Primary Factory invoked" << endl;
return shared_ptr<T>(new T());
}

template <class P1>
static shared_ptr<T> Create(P1 p1)
{
cout << "Primary Factory invoked" << endl;
return shared_ptr<T>(new T(p1));
}
};
And what's your purpose in using a factory?

Depending on your purpose there may be a better alternative.

// Here's where I don't have things right. I'd like to specialize Factory
for types derived from Deleter.
// How may I do so?


What's the purpose of that?

Anyway if you really must you have at least three options: add a
template parameter that you can use for partial specialization, or use a
derived class, or let the general implementation defer to a specific
implementation if one exists.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 30 '05 #2
Dave wrote:
Hello all,

Please see my question embedded in comment form below.

Thanks,
Dave

#include <iostream>
#include <boost/shared_ptr.hpp>

using namespace std;
using namespace boost;

// A base class
class Deleter
{
public:
Deleter() {}
virtual ~Deleter() {}
};

// A derived class
class MyClass: public Deleter
{
public:
MyClass(int data): m_data(data) {}
virtual ~MyClass() {}

void DoIt() const
{
cout << m_data << endl;
}

private:
int m_data;
};

// A factory for objects that are constructed with 0 or 1 parameters.
template <class T>
class Factory
{
public:
static shared_ptr<T> Create()
{
cout << "Primary Factory invoked" << endl;
return shared_ptr<T>(new T());
}

template <class P1>
static shared_ptr<T> Create(P1 p1)
{
cout << "Primary Factory invoked" << endl;
return shared_ptr<T>(new T(p1));
}
};

// Here's where I don't have things right. I'd like to specialize Factory
for types derived from Deleter.
// How may I do so?
template <class T> // ?????????????
class Factory<Deleter> // ?????????????
{
static shared_ptr<T> Create()
{
cout << "Specialized Factory invoked" << endl;
return shared_ptr<T>(new T());
}

template <class P1>
static shared_ptr<T> Create(P1 p1)
{
cout << "Specialized Factory invoked" << endl;
return shared_ptr<T>(new T(p1));
}
};

int main()
{
shared_ptr<MyClass> ptr(Factory<MyClass>::Create(42));

ptr->DoIt();
}


To get the effect you want, put a compile-time check for inheritance
from Deleter in your Create() functions. Using Boost Typetraits and
static asserts, it would look something like:

static shared_ptr<T> Create()
{
BOOST_ASSERT( (is_base_of<Deleter, T>::value) );
// ...
}

Note the extra set of parentheses to overcome the preprocessor's
ignorance of templates. Using this code, you'd get a compile-time error
if T is not derived from Deleter.

Cheers! --M

Nov 30 '05 #3

"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Dave wrote:
Hello all,

Please see my question embedded in comment form below.

Thanks,
Dave

#include <iostream>
#include <boost/shared_ptr.hpp>

using namespace std;
using namespace boost;

// A base class
class Deleter
{
public:
Deleter() {}
virtual ~Deleter() {}
};

// A derived class
class MyClass: public Deleter
{
public:
MyClass(int data): m_data(data) {}
virtual ~MyClass() {}

void DoIt() const
{
cout << m_data << endl;
}

private:
int m_data;
};

// A factory for objects that are constructed with 0 or 1 parameters.
template <class T>
class Factory
{
public:
static shared_ptr<T> Create()
{
cout << "Primary Factory invoked" << endl;
return shared_ptr<T>(new T());
}

template <class P1>
static shared_ptr<T> Create(P1 p1)
{
cout << "Primary Factory invoked" << endl;
return shared_ptr<T>(new T(p1));
}
};

// Here's where I don't have things right. I'd like to specialize Factory for types derived from Deleter.
// How may I do so?
template <class T> // ?????????????
class Factory<Deleter> // ?????????????
{
static shared_ptr<T> Create()
{
cout << "Specialized Factory invoked" << endl;
return shared_ptr<T>(new T());
}

template <class P1>
static shared_ptr<T> Create(P1 p1)
{
cout << "Specialized Factory invoked" << endl;
return shared_ptr<T>(new T(p1));
}
};

int main()
{
shared_ptr<MyClass> ptr(Factory<MyClass>::Create(42));

ptr->DoIt();
}
To get the effect you want, put a compile-time check for inheritance
from Deleter in your Create() functions. Using Boost Typetraits and
static asserts, it would look something like:

static shared_ptr<T> Create()
{
BOOST_ASSERT( (is_base_of<Deleter, T>::value) );
// ...
}

Note the extra set of parentheses to overcome the preprocessor's
ignorance of templates. Using this code, you'd get a compile-time error
if T is not derived from Deleter.


But I don't want a compile-time error if T is not derived from Deleter. In
that case, I would just want the primary Factory template to be used rather
than the specialization.

Cheers! --M

Nov 30 '05 #4
Dave wrote:
"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Dave wrote:
Hello all,

Please see my question embedded in comment form below.

Thanks,
Dave

#include <iostream>
#include <boost/shared_ptr.hpp>

using namespace std;
using namespace boost;

// A base class
class Deleter
{
public:
Deleter() {}
virtual ~Deleter() {}
};

// A derived class
class MyClass: public Deleter
{
public:
MyClass(int data): m_data(data) {}
virtual ~MyClass() {}

void DoIt() const
{
cout << m_data << endl;
}

private:
int m_data;
};

// A factory for objects that are constructed with 0 or 1 parameters.
template <class T>
class Factory
{
public:
static shared_ptr<T> Create()
{
cout << "Primary Factory invoked" << endl;
return shared_ptr<T>(new T());
}

template <class P1>
static shared_ptr<T> Create(P1 p1)
{
cout << "Primary Factory invoked" << endl;
return shared_ptr<T>(new T(p1));
}
};

// Here's where I don't have things right. I'd like to specialize Factory for types derived from Deleter.
// How may I do so?
template <class T> // ?????????????
class Factory<Deleter> // ?????????????
{
static shared_ptr<T> Create()
{
cout << "Specialized Factory invoked" << endl;
return shared_ptr<T>(new T());
}

template <class P1>
static shared_ptr<T> Create(P1 p1)
{
cout << "Specialized Factory invoked" << endl;
return shared_ptr<T>(new T(p1));
}
};

int main()
{
shared_ptr<MyClass> ptr(Factory<MyClass>::Create(42));

ptr->DoIt();
}


To get the effect you want, put a compile-time check for inheritance
from Deleter in your Create() functions. Using Boost Typetraits and
static asserts, it would look something like:

static shared_ptr<T> Create()
{
BOOST_ASSERT( (is_base_of<Deleter, T>::value) );
// ...
}

Note the extra set of parentheses to overcome the preprocessor's
ignorance of templates. Using this code, you'd get a compile-time error
if T is not derived from Deleter.


But I don't want a compile-time error if T is not derived from Deleter. In
that case, I would just want the primary Factory template to be used rather
than the specialization.


Sorry. I didn't read it closely enough. Just use the compile-time
constant is_base_of<Deleter, T>::value to select an implementation.
Something like:

template<class T>
struct DefaultFactory
{
static shared_ptr<T> Create() { /*...*/ }
// ...
};

template<class T>
struct SpecialFactory
{
static shared_ptr<T> Create() { /*...*/ }
// ...
};

template<bool IsSpecial, class T> struct FactorySelector;

template<class T>
struct FactorySelector<true,T>
{
typedef SpecialFactory<T> Factory;
};

template<class T>
struct FactorySelector<false,T>
{
typedef DefaultFactory<T> Factory;
};

template<class T>
struct Factory
: public FactorySelector<
is_base_of<Deleter, T>::value, T >::Factory
{};
Cheers! --M

Nov 30 '05 #5

"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Dave wrote:
"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Dave wrote:
> Hello all,
>
> Please see my question embedded in comment form below.
>
> Thanks,
> Dave
>
> #include <iostream>
> #include <boost/shared_ptr.hpp>
>
> using namespace std;
> using namespace boost;
>
> // A base class
> class Deleter
> {
> public:
> Deleter() {}
> virtual ~Deleter() {}
> };
>
> // A derived class
> class MyClass: public Deleter
> {
> public:
> MyClass(int data): m_data(data) {}
> virtual ~MyClass() {}
>
> void DoIt() const
> {
> cout << m_data << endl;
> }
>
> private:
> int m_data;
> };
>
> // A factory for objects that are constructed with 0 or 1 parameters. > template <class T>
> class Factory
> {
> public:
> static shared_ptr<T> Create()
> {
> cout << "Primary Factory invoked" << endl;
> return shared_ptr<T>(new T());
> }
>
> template <class P1>
> static shared_ptr<T> Create(P1 p1)
> {
> cout << "Primary Factory invoked" << endl;
> return shared_ptr<T>(new T(p1));
> }
> };
>
> // Here's where I don't have things right. I'd like to specialize

Factory
> for types derived from Deleter.
> // How may I do so?
> template <class T> // ?????????????
> class Factory<Deleter> // ?????????????
> {
> static shared_ptr<T> Create()
> {
> cout << "Specialized Factory invoked" << endl;
> return shared_ptr<T>(new T());
> }
>
> template <class P1>
> static shared_ptr<T> Create(P1 p1)
> {
> cout << "Specialized Factory invoked" << endl;
> return shared_ptr<T>(new T(p1));
> }
> };
>
> int main()
> {
> shared_ptr<MyClass> ptr(Factory<MyClass>::Create(42));
>
> ptr->DoIt();
> }

To get the effect you want, put a compile-time check for inheritance
from Deleter in your Create() functions. Using Boost Typetraits and
static asserts, it would look something like:

static shared_ptr<T> Create()
{
BOOST_ASSERT( (is_base_of<Deleter, T>::value) );
// ...
}

Note the extra set of parentheses to overcome the preprocessor's
ignorance of templates. Using this code, you'd get a compile-time error if T is not derived from Deleter.


But I don't want a compile-time error if T is not derived from Deleter. In that case, I would just want the primary Factory template to be used rather than the specialization.


Sorry. I didn't read it closely enough. Just use the compile-time
constant is_base_of<Deleter, T>::value to select an implementation.
Something like:

template<class T>
struct DefaultFactory
{
static shared_ptr<T> Create() { /*...*/ }
// ...
};

template<class T>
struct SpecialFactory
{
static shared_ptr<T> Create() { /*...*/ }
// ...
};

template<bool IsSpecial, class T> struct FactorySelector;

template<class T>
struct FactorySelector<true,T>
{
typedef SpecialFactory<T> Factory;
};

template<class T>
struct FactorySelector<false,T>
{
typedef DefaultFactory<T> Factory;
};

template<class T>
struct Factory
: public FactorySelector<
is_base_of<Deleter, T>::value, T >::Factory
{};
Cheers! --M


Hopefully the last question...

Has is_base_of<> been replaced? I cannot find it for the life of me...
Nov 30 '05 #6
Dave wrote:
[snip]
Hopefully the last question...

Has is_base_of<> been replaced? I cannot find it for the life of me...


It's in the Boost.Typetraits documentation:

http://boost.org/doc/html/boost_type...its.is_base_of

but it looks like it has been renamed is_base_and_derived. Sorry about
the confusion.

Cheers! --M

Nov 30 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
6
by: jesse | last post by:
I am frustrated by class specialization. i don't think it helps me a lot. suppose we have template <class T> class Talkative { T& t; public:
8
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I...
19
by: Nicolas Fleury | last post by:
Hi everyone, I would to know what do you think of this PEP. Any comment welcomed (even about English mistakes). PEP: XXX Title: Specialization Syntax Version: $Revision: 1.10 $...
4
by: anonymous.user0 | last post by:
Using the dotnet v1.1 framework (so no generics possible). I'd like to create a bunch of Factory classes that all inherit from a single abstract base Factory class. Each Factory is responsible...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
8
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
6
by: abir | last post by:
i have a template as shown template<typename Sclass Indexer{}; i want to have a specialization for std::vector both const & non const version. template<typename T,typename Aclass...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.