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

for_each and invoking self's member pointer

I'm trying to create a class that represents a bucket of locks, and I
want to lock a set of objects specified by a container of pointers. I
can't seem to find the right combination of function adapters to invoke
the bucket's member function on each pointer in the argument container.
What's the right thing to use here, at the "???"?

#include <vector>
#include <functional>
#include <algorithm>

using namespace std; // keeps example simple

class BucketOfLocks;

class Lockable
{
BucketOfLocks* owner;
public:
void acquire(BucketOfLocks& newOwner) { owner = &newOwner; }
};

class BucketOfLocks
{
public:
BucketOfLocks(const vector<Lockable*>& lockables);
private:
// this is what I want to invoke in the for_each
void acquire(Lockable& lockable);
};

BucketOfLocks::BucketOfLocks(const vector<Lockable*>& lockables)
{
for_each(lockables.begin(), lockables.end(),
???(&BucketOfLocks::acquire,*this));
}
Oct 13 '07 #1
4 1720
"Alf P. Steinbach" <al***@start.nowrote in news:13h04ulpeupb8f2
@corp.supernews.com:
By the way, why are you using for_each, why not simply use a for loop?
Flexibility. I can change the container type without changing the code that
iterates over it.

Alas, I need to continue to use VC6 to support some customers and will need
to cripple my STL template usage a bit for awhile.
Oct 13 '07 #2
"Chris Thomasson" <cr*****@comcast.netwrote in
news:I8******************************@comcast.com:
Okay. Sorry for all the questions, but, are your using traditional
two-phased locking based on try_lock/back-off logic?
No, I just throw a busy exception if the lock is already held, and the
application pops up an error. Scheduling of resources is supposed to be
handled elsewhere, and a lock failure at this level indicates a programming
error.
Oct 13 '07 #3
"Kenneth Porter" <sh*************@sewingwitch.comwrote in message
news:Xn**************************@216.196.97.136.. .
"Chris Thomasson" <cr*****@comcast.netwrote in
news:I8******************************@comcast.com:
>Okay. Sorry for all the questions, but, are your using traditional
two-phased locking based on try_lock/back-off logic?

No, I just throw a busy exception if the lock is already held, and the
application pops up an error. Scheduling of resources is supposed to be
handled elsewhere, and a lock failure at this level indicates a
programming
error.
Okay. I see.

Oct 13 '07 #4
Kenneth Porter wrote:
"Alf P. Steinbach" <al***@start.nowrote in news:13h04ulpeupb8f2
@corp.supernews.com:
>By the way, why are you using for_each, why not simply use a for loop?

Flexibility. I can change the container type without changing the code
that iterates over it.
[snip]

That can be achieved using just a typedef:

typedef std::vector< ticket ticket_sequence;
...
for ( ticket_sequence::iterator iter = ... ) {
...
}

Now you can change from vector to deque by modifying a single line.
In fact, one can consider using std::vector< whatever directly instead of
using a typedef to be the typesystem analogue of using a magic number like
12341 instead of a named constant

int const number_of_runs = 12341;
Best

Kai-Uwe Bux
Oct 14 '07 #5

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

Similar topics

1
by: trash | last post by:
I do this kindda stuff often: class CVisitableObjectVector : public std::vector<Loki::BaseVisitable<>*> { void Enable(bool enable); .... };
11
by: franklini | last post by:
hello people, just wanted to say thanks again for the help in the past. i have a new problem which am wondering if any body can help me with. i have written this abtract class shape and its...
8
by: pookiebearbottom | last post by:
Ok, this is what I am trying to do. I have a member function that as a parameter accepts a pointer to a different member function. In this 2nd function, I want to iterate over an STL container...
5
by: glen stark | last post by:
Hi Everyone So, I want to use a std::for_each in the following situation: class B{ //whatever } class A{ private:
22
by: ypjofficial | last post by:
Is there any possibility of invoking the member functions of a class without creating an object (or even a pointer to ) of that class. eg. #include <iostream.h> class test { public: void...
9
by: shaun | last post by:
I am working on code where I am handed a vector of pointers vector<T*> or a map<std::string, T*>, and I have to delete the objects and set the pointers to zero. I have been using a 'for' loop and...
15
by: Eric Lilja | last post by:
Hello, in one of my classes I have this member variable: Tile ***tiles_; where Tile is another one of my classes. dimensional array of Tile-pointers (dimensions are of equal sizes). Last, I...
3
by: Yan | last post by:
Hi, I don't seem to be able to use for_each if it should replace a 'for' loop in a method (constructor in my case) and inside that 'for' loop a class member variable is being accessed. The...
9
by: nguillot | last post by:
Hello I used to loop on a std::map<k, dto act on the data (d) like that (d being a class with setIntMember method): typedef std::map<k, dtMap; struct setIntMember { setIntMember(int j) :...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.