473,769 Members | 8,305 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(BucketO fLocks& newOwner) { owner = &newOwner; }
};

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

BucketOfLocks:: BucketOfLocks(c onst vector<Lockable *>& lockables)
{
for_each(lockab les.begin(), lockables.end() ,
???(&BucketOfLo cks::acquire,*t his));
}
Oct 13 '07 #1
4 1747
"Alf P. Steinbach" <al***@start.no wrote in news:13h04ulpeu pb8f2
@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*****@comcas t.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.c omwrote in message
news:Xn******** *************** ***@216.196.97. 136...
"Chris Thomasson" <cr*****@comcas t.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.no wrote in news:13h04ulpeu pb8f2
@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
1977
by: trash | last post by:
I do this kindda stuff often: class CVisitableObjectVector : public std::vector<Loki::BaseVisitable<>*> { void Enable(bool enable); .... };
11
2812
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 derived class circle, rectangle and triangle. i have also written this method for each shape object called 'draw_all_seq_inside2' which takes two values. i want to call this method using for_each statement in another method but it doesnt seem to...
8
1439
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 and call the passed in member function. I can do this in my own loop, but I am looking to call for_each. I am a little confused on the syntax to use. Any help would be appreciated. look at the following code and at the comment in
5
2690
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
2778
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 fun() {
9
1846
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 thought it might be instructive to write a 'deletePointer' which can be used in an algorithm or standalone. (code at end of mail) I discovered I could not simply for_each(v.begin(),v.end(),deletePointer);
15
2623
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 allocate a Tile object for each pointer in the 2d-array. Anyway, I often need to traverse the entire array and perform operations so until now I've used alot of constructs like this: for (int y = 0; y < num_rows; ++y) {
3
1575
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 presence of this member variable prevents me from using a static or global method to be passed as a third parameter to for_each, and mem_fun doesn't seem to work for me either as I am not going to execute a method of an iterator but pass an iterator...
9
4011
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) : i(j) {}
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.