473,395 Members | 1,377 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,395 software developers and data experts.

Macro and Template

Hello,

I have a singleton class that I'm using through macros:

//snippet
#define DO_SOMETHING ( myArgument) \
{ \
MyClass *myObject = MyClass::getInstance(); \
myObject->doSomething(myArgument); \
}; \

class MyClass
{
public:
MyClass() {}
static MyClass *getInstance ()
{
static MyClass *myObject = new MyClass();
return myObject;
}

void doSomething(MyArgument myArgument) { whatever(); }
};
// snippet

Now, I would like to template MyClass by another class, and don't
know how to do that! I guess I need some kind of initialization macro
where I give the template class to use, but how getInstance will
remember it for the DO_SOMETHING call ? Is there a way to store a
class in a variable or something ?

This code here:

//Snippet
#define DO_SOMETHING ( myArgument) \
{ \
MyClass<TemplateClass*myObject =
MyClass<TemplateClass>::getInstance(); \
myObject->doSomething(myArgument); \
}; \

template <class T>
class MyClass
{
public:
MyClass() { _t = new T(); }
static MyClass *getInstance ()
{
static MyClass<T*myObject = new MyClass<T>();
return myObject;
}

void doSomething(MyArgument myArgument) { _t->whatever(); }
T *_t;
};
// snippet

Obviously doesn't work, but can help you understand my pb. Is there
an elegant solution ?

Thank you.

Feb 3 '07 #1
9 2017
* je********@yahoo.co.uk:
>
I have a singleton class that I'm using through macros:

//snippet
#define DO_SOMETHING ( myArgument) \
{ \
MyClass *myObject = MyClass::getInstance(); \
myObject->doSomething(myArgument); \
}; \
Why are you using a macro?

--
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?
Feb 3 '07 #2
On Feb 3, 10:26 am, "Alf P. Steinbach" <a...@start.nowrote:
* jedi200...@yahoo.co.uk:
I have a singleton class that I'm using through macros:
//snippet
#define DO_SOMETHING ( myArgument) \
{ \
MyClass *myObject = MyClass::getInstance(); \
myObject->doSomething(myArgument); \
}; \

Why are you using a macro?
I designing a "logger". I have hundreds of source file, and I'm just
including an header file containg MyClass. Anywhere in my source file
I'm just addind: LOG("Warning : Db connection lost") and that's it, I
have my log. Using a macro is the simplest way to do that. I wan't to
template my logger so I can just output in a text form, or in XML, or
whatever ...

Feb 3 '07 #3
* je********@yahoo.co.uk:
On Feb 3, 10:26 am, "Alf P. Steinbach" <a...@start.nowrote:
>* jedi200...@yahoo.co.uk:
>> I have a singleton class that I'm using through macros:
//snippet
#define DO_SOMETHING ( myArgument) \
{ \
MyClass *myObject = MyClass::getInstance(); \
myObject->doSomething(myArgument); \
}; \
Why are you using a macro?

I designing a "logger". I have hundreds of source file, and I'm just
including an header file containg MyClass. Anywhere in my source file
I'm just addind: LOG("Warning : Db connection lost") and that's it, I
have my log. Using a macro is the simplest way to do that. I wan't to
template my logger so I can just output in a text form, or in XML, or
whatever ...
OK, in order to pick up logging call locations you need a macro.

However, you don't need that macro to know anything about the singleton:
just make it call a logging function that does the Real Stuff.

Now, what's this about "templating"? Can you give a short example of
the problem to be solved? Is it perhaps that you want the macro to
invoke different singletons (of different types) depending on something?

--
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?
Feb 3 '07 #4
On Feb 3, 12:06 pm, "Alf P. Steinbach" <a...@start.nowrote:
* jedi200...@yahoo.co.uk:
On Feb 3, 10:26 am, "Alf P. Steinbach" <a...@start.nowrote:
* jedi200...@yahoo.co.uk:
> I have a singleton class that I'm using through macros:
//snippet
#define DO_SOMETHING ( myArgument) \
{ \
MyClass *myObject = MyClass::getInstance(); \
myObject->doSomething(myArgument); \
}; \
Why are you using a macro?
I designing a "logger". I have hundreds of source file, and I'm just
including an header file containg MyClass. Anywhere in my source file
I'm just addind: LOG("Warning : Db connection lost") and that's it, I
have my log. Using a macro is the simplest way to do that. I wan't to
template my logger so I can just output in a text form, or in XML, or
whatever ...

OK, in order to pick up logging call locations you need a macro.
Exactly.
However, you don't need that macro to know anything about the singleton:
just make it call a logging function that does the Real Stuff.
Well, a simple function will not make the thing.
Now, what's this about "templating"? Can you give a short example of
the problem to be solved? Is it perhaps that you want the macro to
invoke different singletons (of different types) depending on something?
I want my logger to be extensible, so someone can write a back-end,
deriving from a abstract class that I define, and my logger, when
invoked from the macro, will use a standarized function in my backend
abstract class, which will output the log in different format.
Basically it's a line of text, but can be XML, JSON, or other text
format, eventually crypted or compressed format. This template thing
allow me to do that.

Feb 3 '07 #5
* je********@yahoo.co.uk:
On Feb 3, 12:06 pm, "Alf P. Steinbach" <a...@start.nowrote:
>* jedi200...@yahoo.co.uk:
>>On Feb 3, 10:26 am, "Alf P. Steinbach" <a...@start.nowrote:
* jedi200...@yahoo.co.uk:
I have a singleton class that I'm using through macros:
//snippet
#define DO_SOMETHING ( myArgument) \
{ \
MyClass *myObject = MyClass::getInstance(); \
myObject->doSomething(myArgument); \
}; \
Why are you using a macro?
I designing a "logger". I have hundreds of source file, and I'm just
including an header file containg MyClass. Anywhere in my source file
I'm just addind: LOG("Warning : Db connection lost") and that's it, I
have my log. Using a macro is the simplest way to do that. I wan't to
template my logger so I can just output in a text form, or in XML, or
whatever ...
OK, in order to pick up logging call locations you need a macro.

Exactly.
>However, you don't need that macro to know anything about the singleton:
just make it call a logging function that does the Real Stuff.

Well, a simple function will not make the thing.
Consider

#define LOG(s) makeLogEntry( whatever, whatever2, ... )

where only makeLogEntry() knows about the singleton.

>Now, what's this about "templating"? Can you give a short example of
the problem to be solved? Is it perhaps that you want the macro to
invoke different singletons (of different types) depending on something?

I want my logger to be extensible, so someone can write a back-end,
deriving from a abstract class that I define, and my logger, when
invoked from the macro, will use a standarized function in my backend
abstract class, which will output the log in different format.
Basically it's a line of text, but can be XML, JSON, or other text
format, eventually crypted or compressed format. This template thing
allow me to do that.
OK, assuming that the argument types for LOG do not depend on type of
the active logger object, then that's not a template, that's an
installable object or function pointer. With polymorphism (virtual
functions), not templating. For example, you can use the same scheme as
with e.g. std::set_new_handler (client code is responsible for
installing the logger), or you can require client code to provide a
function that creates the singleton object and returns a pointer to it
(your code is responsible for installing the logger), or whatever.

--
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?
Feb 3 '07 #6
On Feb 3, 9:11 am, "Alf P. Steinbach" <a...@start.nowrote:
* jedi200...@yahoo.co.uk:


On Feb 3, 12:06 pm, "Alf P. Steinbach" <a...@start.nowrote:
* jedi200...@yahoo.co.uk:
>On Feb 3, 10:26 am, "Alf P. Steinbach" <a...@start.nowrote:
* jedi200...@yahoo.co.uk:
I have a singleton class that I'm using through macros:
//snippet
#define DO_SOMETHING ( myArgument) \
{ \
MyClass *myObject = MyClass::getInstance(); \
myObject->doSomething(myArgument); \
}; \
Why are you using a macro?
I designing a "logger". I have hundreds of source file, and I'm just
including an header file containg MyClass. Anywhere in my source file
I'm just addind: LOG("Warning : Db connection lost") and that's it, I
have my log. Using a macro is the simplest way to do that. I wan't to
template my logger so I can just output in a text form, or in XML, or
whatever ...
OK, in order to pick up logging call locations you need a macro.
Exactly.
However, you don't need that macro to know anything about the singleton:
just make it call a logging function that does the Real Stuff.
Well, a simple function will not make the thing.

Consider

#define LOG(s) makeLogEntry( whatever, whatever2, ... )

where only makeLogEntry() knows about the singleton.
Now, what's this about "templating"? Can you give a short example of
the problem to be solved? Is it perhaps that you want the macro to
invoke different singletons (of different types) depending on something?
I want my logger to be extensible, so someone can write a back-end,
deriving from a abstract class that I define, and my logger, when
invoked from the macro, will use a standarized function in my backend
abstract class, which will output the log in different format.
Basically it's a line of text, but can be XML, JSON, or other text
format, eventually crypted or compressed format. This template thing
allow me to do that.

OK, assuming that the argument types for LOG do not depend on type of
the active logger object, then that's not a template, that's an
installable object or function pointer. With polymorphism (virtual
functions), not templating. For example, you can use the same scheme as
with e.g. std::set_new_handler (client code is responsible for
installing the logger), or you can require client code to provide a
function that creates the singleton object and returns a pointer to it
(your code is responsible for installing the logger), or whatever.

--
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?- Hide quoted text -

- Show quoted text -
Would it be a bad Idea to use streams-like scheme (<< operator) ?
For example:
MyLogStream logger; // <-- declared somewhere in the program...

logger << "This is a log line, value=" << value << endl;

a user defined type could use the scheme by defining a friend function
like is done with std::ostream:

// snippet
class UserDefinedClass {
friend MyLogStream& operator<<(MyLogStream& ls, const
UserDefinedClass& udc) {
// insert data to log stream
ls << udc.data;
Feb 4 '07 #7
On Feb 4, 4:38 pm, "naoki" <nelson.a...@gmail.comwrote:
On Feb 3, 9:11 am, "Alf P. Steinbach" <a...@start.nowrote:
* jedi200...@yahoo.co.uk:
On Feb 3, 12:06 pm, "Alf P. Steinbach" <a...@start.nowrote:
>* jedi200...@yahoo.co.uk:
>>On Feb 3, 10:26 am, "Alf P. Steinbach" <a...@start.nowrote:
>>>* jedi200...@yahoo.co.uk:
>>>> I have a singleton class that I'm using through macros:
>>>>//snippet
>>>>#define DO_SOMETHING ( myArgument) \
>>>>{ \
>>>> MyClass *myObject = MyClass::getInstance(); \
>>>> myObject->doSomething(myArgument); \
>>>>}; \
>>>Why are you using a macro?
>>I designing a "logger". I have hundreds of source file, and I'm just
>>including an header file containg MyClass. Anywhere in my source file
>>I'm just addind: LOG("Warning : Db connection lost") and that's it, I
>>have my log. Using a macro is the simplest way to do that. I wan't to
>>template my logger so I can just output in a text form, or in XML, or
>>whatever ...
>OK, in order to pick up logging call locations you need a macro.
Exactly.
>However, you don't need that macro to know anything about the singleton:
>just make it call a logging function that does the Real Stuff.
Well, a simple function will not make the thing.
Consider
#define LOG(s) makeLogEntry( whatever, whatever2, ... )
where only makeLogEntry() knows about the singleton.
>Now, what's this about "templating"? Can you give a short example of
>the problem to be solved? Is it perhaps that you want the macro to
>invoke different singletons (of different types) depending on something?
I want my logger to be extensible, so someone can write a back-end,
deriving from a abstract class that I define, and my logger, when
invoked from the macro, will use a standarized function in my backend
abstract class, which will output the log in different format.
Basically it's a line of text, but can be XML, JSON, or other text
format, eventually crypted or compressed format. This template thing
allow me to do that.
OK, assuming that the argument types for LOG do not depend on type of
the active logger object, then that's not a template, that's an
installable object or function pointer. With polymorphism (virtual
functions), not templating. For example, you can use the same scheme as
with e.g. std::set_new_handler (client code is responsible for
installing the logger), or you can require client code to provide a
function that creates the singleton object and returns a pointer to it
(your code is responsible for installing the logger), or whatever.
--
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?- Hide quoted text -
- Show quoted text -

Would it be a bad Idea to use streams-like scheme (<< operator) ?
For example:
MyLogStream logger; // <-- declared somewhere in the program...

logger << "This is a log line, value=" << value << endl;

a user defined type could use the scheme by defining a friend function
like is done with std::ostream:

// snippet
class UserDefinedClass {
friend MyLogStream& operator<<(MyLogStream& ls, const
UserDefinedClass& udc) {
// insert data to log stream
ls << udc.data;
.
.
.
return ls;
}
.
.
.}

// snippet
Thanks for your answer!
I'm not happy with this solution, first because I need this log
library to be simple... no, not simple, simplistic! There is hundreds
of source files and lots of developers, and I want my macros to handle
the job of allocating the logger. Moreover, it allows me to track the
logs (source filename and line number using __FILE__ and __LINE__).
Then, using a function as a friend requires you to modify you logger
each time you are adding a back end, which is inelegant IMHO.

Feb 5 '07 #8
On Feb 3, 1:11 pm, "Alf P. Steinbach" <a...@start.nowrote:
>
OK, assuming that the argument types for LOG do not depend on type of
the active logger object, then that's not a template, that's an
installable object or function pointer. With polymorphism (virtual
functions), not templating. For example, you can use the same scheme as
with e.g. std::set_new_handler (client code is responsible for
installing the logger), or you can require client code to provide a
function that creates the singleton object and returns a pointer to it
(your code is responsible for installing the logger), or whatever.
Ok so if I get you right, if the user want a logger that outputs in
XML, he just create its own class derived from MyClass, like:

// snippet
class MyClassXML : public MyClass
{
virtual void doDomething(MyArgument myArgument) { whateverXML(); }
};
// snippet

So my macros now would be like:

// snippet
MyClass *g_myObject = NULL;

#define DO_SOMETHING_FIRST( myArgument, myObject ) \
{ \
assert(g_myObject == NULL);
g_myObject = myObject;
DO_SOMETHING( myArgument ); \
}

#define DO_SOMETHING ( myArgument ) \
{ \
g_myObject->doSomething( myArgument ); \
}
// snippet

So this still means that the logger allocation would have to be
handled by the client (client = user) and it's something I'm trying to
avoid. This getInstance thing is something I don't want to loose.
Moreover here, there is a risk to call DO_SOMETHING_FIRST more than
once, then a risk of asserting or leaking memory depending on what you
do in that case... So I can pass a function pointer to the client-
defined getInstance function. Still, I got to perform an
initialization, and now the client to provide this getInstance
function.

Your solutions are great, but I'm sure my macro/template thing can be
solved by some wonderful elegant c++ trick a la BOOST :)))
Still looking for this trick!

Feb 5 '07 #9
* je********@yahoo.co.uk:
On Feb 3, 1:11 pm, "Alf P. Steinbach" <a...@start.nowrote:
>OK, assuming that the argument types for LOG do not depend on type of
the active logger object, then that's not a template, that's an
installable object or function pointer. With polymorphism (virtual
functions), not templating. For example, you can use the same scheme as
with e.g. std::set_new_handler (client code is responsible for
installing the logger), or you can require client code to provide a
function that creates the singleton object and returns a pointer to it
(your code is responsible for installing the logger), or whatever.
[snip]
Your solutions are great, but I'm sure my macro/template thing can be
solved by some wonderful elegant c++ trick a la BOOST :)))
Still looking for this trick!
The solutions I mentioned are not mine. Exactly what the perceived
problem is, is unclear. Perhaps just study existing implementations of
logging facilities (John Torjo proposed a logging facility for Boost, it
is available at <url: http://torjo.com/code/logging.zip>, but keep in
mind that it evidently didn't make it into Boost).

--
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?
Feb 5 '07 #10

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

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
0
by: Colin Steadman | last post by:
Apoligies if this post appears twice, but my first attempt at posting it was hours ago and there is still no sign of it in the forum (says five minutes in the confirmation message). ...
2
by: foo | last post by:
I'm creating a debug class called debug_mem_allocation for the purpose of finding memory leaks. I used macro's to replace the new and delete operators. My problem is with trying to replace the...
27
by: TheDD | last post by:
Hello all, right now, i'm using the following macro to automatically add informations to exceptions: #define THROW(Class, args...) throw Class(__FILE__, __LINE__, ## args) but AFAIK, it's...
0
by: Phlip | last post by:
Newsgroupies: This sample code tests a WTL dialog box. (Fear not, topic police, we see little Windows Template Library code here, and the actual TEST_() macro itself is quite portable.) I...
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
12
by: swellfr | last post by:
Hi if have simple macro defined this way: #define M_OPplus( classname ) typedef classname operator+(const classname& rhs); and a template class : template<typename X, typename Y> class...
2
by: Christof Warlich | last post by:
Hi macro experts, in a variadic macro, i.e. in a macro with a variable parameter list, is there any way to access single parameters of the list? __VA_ARGS__ only expands to the whole list. ...
2
by: Alex Vinokur | last post by:
Hi, Here is my program -------------- Program -------------- #define ADD(x,t) (x + t) template <typename T, int N> int foo() {
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.