|
-
February 15th, 2008, 07:25 AM
#1
Unique identifiers
Hey.
Is there a way to generate unique identifiers for a program. For example here's how it can be done with Visual C++ in a file.
Code:
#define UNIQ(s) s##__COUNTER__
then
Code:
int UNIQ(a) = 5; //a0
int UNIQ(a) = 6; //a1
etc...
Unfortunately the counter is restarted per file. Is there a way to make something global for the program.
Thanks in advance for any help.
(Visual C++ specific solutions are ok)
It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames
-
February 15th, 2008, 09:59 AM
#2
Re: Unique identifiers
 Originally Posted by SeventhStar
Is there a way to generate unique identifiers for a program?
You can use global object Mutexes to set a unique identifiers.
To create an identifier for a single process, simply pass a NULL to lpName.
You can create a mutex that refers to the same mutex in MORE than one process, by providing lpName.
HANDLE CreateMutex(LPSECURITY_ATTRIBUTES lpMutexAttributes,
BOOL bInitialOwner,
LPCWSTR lpName);
Consider the following code:
Code:
HANDLE hMutex;
hMutex=CreateMutex(NULL,true, NULL);
if (hMutex==NULL)
{
std::cout<<"Failed to create mutex";
return 0;
}
if (getLastError() == ERROR_ALREADY_EXISTS)
{
// ... Do Something ...
}
EDIT: The code provided will only run for a single process, if you have more than one process that shares the same resource, you should provide the function a unique lpName.
Regards
Doron Moraz
Last edited by Doron Moraz; February 16th, 2008 at 06:23 AM.
-
February 15th, 2008, 10:15 AM
#3
Re: Unique identifiers
Why do they need to be unique between files? - putting them into a no-name namespace or marking them static will prevent the linker treating them as the same.
-
February 15th, 2008, 10:21 AM
#4
Re: Unique identifiers
Do you need the unique identifier at compile-time or at runtime?
At runtime you can create and use LUIDs (Locally Unique Identifiers) that are guaranteed to be unique across the OS (until you restart the system).
- petter
-
February 15th, 2008, 10:23 AM
#5
Re: Unique identifiers
 Originally Posted by SeventhStar
Code:
int UNIQ(a) = 5; //a0
int UNIQ(a) = 6; //a1
This looks awful to me. How would it be useful?
1) The point of named variables is that you can (and should) give a meaningful name to a piece of data. If you have a group of related variables where it would make sense to give them all the same name, in addition to a unique number, guess what... that's an array. For example:
Code:
vector<int> a;
a.push_back(5);
a.push_back(6);
// or
int b[] = { 5, 6 /*, etc */ };
// or, if you must
unsigned int counter = 0;
int c[SOME_SUFFICIENT_NUMBER];
c[counter++] = 5;
c[counter++] = 6;
2) Say you're debugging your code, and the variable a## looks suspicious. Where is this variable declared? Is it UNIQ(a), UNIQ(a), or UNIQ(a), or ...? Also, how do you refer to these variables in your code anyway?
- Alon
-
February 15th, 2008, 10:34 AM
#6
Re: Unique identifiers
 Originally Posted by Hermit
This looks awful to me. How would it be useful?
Ahh. Now I realized what the question was all about. Hermit is correct. It's awful and never any reason for 'randomly' naming variables.
- petter
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|