Sign in to follow this  
nlbs

overloading macro

Recommended Posts

I've a macro called $_SESSION() and if one argument is passed to it it will invoke the getter method and if two arguments has been passed it will invoke the setter

Share this post


Link to post
Share on other sites
The '$' prefix leads me to believe that you're dealing with PERL, PHP or an SH variant. Since PHP and SH have no macros and PERL 6 macros don't look like that, I'm left to wonder, what are you talking about?

Share this post


Link to post
Share on other sites
Right. That's not much better, but at least we got something to work with:
function session()
{
$args = func_get_args();
if (count($args) == 1) return get_value($args[0]);
if (count($args) == 2) return set_value($args[0], $args[1]);
assert (false);
}

// Warning: not a superglobal!
$_MY_SESSION = 'session';

Share this post


Link to post
Share on other sites
Man why you are not understanding

#define $_SESSION(k) session::get(k)
#define $_SESSIOn(k, v) session::add(k, v)

but compiler warns that multiple defenition of $_SESSION macro

C++ doesn't forbids macros starting with $

Share this post


Link to post
Share on other sites
Quote:
Original post by nlbs
Man why you are not understanding
Well, it's been five messages already in this thread, and even though I mentioned in the first message that you didn't specify the language you were enquiring about, you've waited until now to mention C++. Why have you not mentioned that earlier, thereby saving me the wasted time of proposing a PHP solution?

Seriously, you're just wasting everyone's time. The reason why you don't get the correct answer is because you suck at asking questions.

Quote:
but compiler warns that multiple defenition of $_SESSION macro

Macros cannot be overloaded. Use a different solution, for instance create a class with an overloaded operator[] returning an object with an impicit conversion (that calls get) and an assignment operator (that calls set).

Quote:
C++ doesn't forbids macros starting with $
It does, '$' is only allowed as a microsoft extension.

Share this post


Link to post
Share on other sites
Sorry as its mainly a C++ forum I thought its not required to say C++ explecitely

ok so if it cant be overloaded how can i make a macro that can accept one argument as well as two argument too.

I am using g++ and I've 4-5 macro's already running well who's name starts with $ sign.

Share this post


Link to post
Share on other sites
Quote:

Man why you are not understanding

Don't be rude to people trying to help you. You're original questions are far too vague; we don't read minds here.

Quote:

but compiler warns that multiple defenition of $_SESSION macro

Because you cannot overload macros. They are part of the preprocessor, and exist for simple, naive textual substitution.

Quote:

ok so if it cant be overloaded how can i make a macro that can accept one argument as well as two argument too.

You can't, the macros have to have different names. You can also use a function (an actual, honest C++ function) which can be overloaded.

Quote:

C++ doesn't forbids macros starting with $...I am using g++ and I've 4-5 macro's already running well who's name starts with $ sign.

Yes it does, '$' is not in the basic source character set. Visual C++ and some other compilers (including GCC) will let you use the character in an identifier as a language extension. If you disable extensions then it should provide you with an error (if it doesn't, the compiler is non-compliant with the C++ standard on that point).

Share this post


Link to post
Share on other sites
ok so if it cant be overloaded how can i make a macro that can accept one argument as well as two arguments too. with teh same name e.g. one optional argument

Share this post


Link to post
Share on other sites
You can't. We've already said this a number of times. A macro isn't going to cut it here, you need to use something else, like a function.

The closest you might be able to get with a macro is the nonstandard, compiler-specific support for varadic macros, but this generally only gives you something like _VA_ARGS_ that is expanded; to get this to work, you'd need a function anyway.

Share this post


Link to post
Share on other sites
Using macros here is wrong (no ifs or buts).

A PHP equivalent for $SEESION in C++ is std::map<std::string, std::string>.

<?php
session_start();
$_SESSION['views'] = 1; // store session data
echo "Pageviews = ". $_SESSION['views']; //retrieve data
?>

C++ equivalent:
typedef std::map<std::string, std::string> Session;

Session session;

session["Views"] = "1"; // C++ doesn't support dynamic types
std::cout << "Pageviews = " << session["Views"];


You can however get fancy, and try (not sure if it works), the boost or cdiggins 'any' type.

In which case:
typedef std::map<std::string, boost::any> Session;

Session session;

session["Views"] = 1; // Can now store int, string or any other type
std::cout << "Pageviews = " << session["Views"];
But this is untested, I don't know if implicit conversion work in combination with map.

Share this post


Link to post
Share on other sites
(I might be mistaking what the OP is trying to do, I'm not familiar with $_SESSION or any of PHP)

Quote:
Original post by nlbs
ok so if it cant be overloaded how can i make a macro that can accept one argument as well as two arguments too. with teh same name e.g. one optional argument
You could fake it, like this:

int myMacroFunction(int oneParam, int twoParam)
{
return whatever;
}

int myMacroFunction(int oneParam)
{
return thatOtherThing;
}

#define MY_OVERLOADED_MACRO myMacroFunction

...

int myFirstParam, mySecondParam;

MY_OVERLOADED_MACRO(myFirstParam);

MY_OVERLOADED_MACRO(myFirstParam, mySecondParam);


What do you gain from doing it like that? Nothing, really. It complicates your code, and just by reading the name of the macro, '$_SESSION' or 'SESSION', it does not explain what the function does, which is the primary concern of naming any function or variable.

I've offered that solution, because you've presented a problem, and I (think) I had an answer. I'm warning you, however, that there are some ways of writing code that improves the readability of your programs, and some ways of writing code that obscures what the program is doing. The method I show above, obscures your code.

It'd be far better to just use myGetFunction(), and mySetFunction(), but it's your code, do what you like. [smile]


$_SESSION(userID); //Uh, what's this doing?

GetSessionData(userID); //Oh, this function is easier to understand. I don't know PHP, but I can see that this is retrieving data of some kind.

$_SESSION(userID, data); //Huh? Seriously, if I saw this, I wouldn't know what to think.

SetSessionData(userID, data); //While I still don't know what 'session' is, the name of this function clearly tells me it's storing data.


Naming functions, constants/enums, and variables are an incredibly important part of programming. Good names makes a program easier to add to later, easier to see mistakes, and easier to work cooperatively with someone else.

Also, GameDev.net has people working with C, C++, C#, Python, Java, Perl, and many programming laungages I've never even heard of. Alot of the people here even know ASM. [smile]
(I personally know only C++)

Share this post


Link to post
Share on other sites
Quote:
Original post by Servant of the Lord
(I might be mistaking what the OP is trying to do, I'm not familiar with $_SESSION or any of PHP)


From manual:
Quote:
When you want to store user data in a session use the $_SESSION associative array.


It is std::map equivalent.

It really is std::map<std::string, any>, where any is one of 'any' arbitrary type containers.

None of the void * or BaseObject * or similar.

If you use the above approach, you get functional and *syntactic* equivalent.

And it even works if one wants to use static (sigh) session object. Just use: static/extern Session session; which matches the OP's apparently static implementation.

Share this post


Link to post
Share on other sites
@Servant of the Lord
Thats what I was asking for.

actually I am making a CGI application so I thought to make a common Shared librery before doing that so it will need less memory.

and Session functionality is already done I just need an user friendly macro that function method is of work.

I can use Session::add() Session::get() etc.....

I am mainly a Web developer
(I personaly know PHP and C++)

I thought its only C++ Coders forum LOL

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this