Commit 9084b680 authored by Davis King's avatar Davis King

Improved this example program.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403047
parent dcd322dd
...@@ -83,6 +83,7 @@ public: ...@@ -83,6 +83,7 @@ public:
// Here we declare our pipe object. It will contain our messages. // Here we declare our pipe object. It will contain our messages.
pipe_type message_pipe; pipe_type message_pipe;
private:
// When we call apply_to_contents() below these are the // When we call apply_to_contents() below these are the
// functions which get called. // functions which get called.
...@@ -101,8 +102,6 @@ public: ...@@ -101,8 +102,6 @@ public:
cout << "got a string: " << val << endl; cout << "got a string: " << val << endl;
} }
private:
void thread () void thread ()
{ {
tsu_type msg; tsu_type msg;
...@@ -110,14 +109,23 @@ private: ...@@ -110,14 +109,23 @@ private:
// Here we loop on messages from the message_pipe. // Here we loop on messages from the message_pipe.
while (message_pipe.dequeue(msg)) while (message_pipe.dequeue(msg))
{ {
// Tell the msg type_safe_union object to take whatever object // Here we call the apply_to_contents() function on our type_safe_union.
// it contains and call (*this)(contained_object); So what // It takes a function object and applies that function object
// happens here is one of the three above functions gets called // to the contents of the union. In our case we have setup
// with the message we just got. // the pipe_example class as our function object and so below we
// tell the msg object to take whatever it contains and
// call (*this)(contained_object); So what happens here is
// one of the three above functions gets called with the message
// we just got.
msg.apply_to_contents(*this); msg.apply_to_contents(*this);
} }
} }
// Finally, note that since we declared the operator() member functions
// private we need to declare the type_safe_union as a friend of this
// class so that it will be able to call them.
friend class type_safe_union<int, float, std::string>;
}; };
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment