Syntax of passing lambda causing hair loss (pulling out)

Posted by Astara on Programmers See other posts from Programmers or by Astara
Published on 2014-06-08T02:59:28Z Indexed on 2014/06/08 3:38 UTC
Read the original article Hit count: 244

Filed under:
|

Right now, I'm working on refactoring a program that calls its parts by polling to a more event-driven structure.

I've created sched and task classes with the sced to become a base class of the current main loop. The tasks will be created for each meter so they can be called off of that instead of polling.

Each of the events main calls are a type of meter that gather info and display it. When the program is coming up, all enabled meters get 'constructed' by a main-sub. In that sub, I want to store off the "this" pointer associated with the meter, as well as the common name for the "action routine.

void MeterMaker::Meter_n_Task (Meter * newmeter,) {
  push(newmeter);      // handle non-timed draw events
  Task t  = new Task(now() + 0.5L);
  t.period={0,1U};
  t.work_meter = newmeter;      
  t.work  = [&newmeter](){newmeter.checkevent();};<<--attempt at lambda
  t.flags = T_Repeat;
  t.enable_task();
  _xos->sched_insert(t);
}

A sample call to it:

Meter_n_Task(new CPUMeter(_xos, "CPU "));

've made the scheduler a base class of the main routine (that handles the loop), and I've tried serveral variations to get the task class to be a base of the meter class, but keep running into roadblocks. It's alot like "whack-a-mole" -- pound in something to fix something one place, and then a new probl pops out elsewhere.

Part of the problem, is that the sched.h file that is trying to hold the Task Q, includes the Task header file. The task file Wants to refer to the most "base", Meter class.

The meter class pulls in the main class of the parent as it passes a copy of the parent to the children so they can access the draw routines in the parent.

Two references in the task file are for the 'this' pointer of the meter and the meter's update sub (to be called via this).

void *this_data= NULL;
void (*this_func)() = NULL;

Note -- I didn't really want to store these in the class, as I wanted to use a lamdba in that meter&task routine above to store a routine+context to be used to call the meter's action routine.

Couldn't figure out the syntax.

But am running into other syntax problems trying to store the pointers...such as

  g++: COMPILE lsched.cc
In file included from meter.h:13:0,
                 from ltask.h:17,
                 from lsched.h:13,
                 from lsched.cc:13:
xosview.h:30:47: error: expected class-name before ‘{’ token
 class XOSView : public XWin, public Scheduler {

Like above where it asks for a class, where the classname "Scheduler" is. !?!? Huh? That IS a class name.

I keep going in circles with things that don't make sense... Ideally I'd get the lamba to work right in the Meter_n_Task routine at the top. I wanted to only store 1 pointer in the 'Task' class that was a pointer to my lambda that would have already captured the "this" value ... but couldn't get that syntax to work at all when I tried to start it into a var in the 'Task' class.

This project, FWIW, is my teething project on the new C++... (of course it's simple!.. ;-))... I've made quite a bit of progress in other areas in the code, but this lambda syntax has me stumped...its at times like thse that I appreciate the ease of this type of operation in perl. Sigh.

Not sure the best way to ask for help here, as this isn't a simple question. But thought I'd try!... ;-)

Too bad I can't attach files to this Q.

© Programmers or respective owner

Related posts about c++

Related posts about syntax