Click here to Skip to main content
15,893,814 members
Articles / Desktop Programming / MFC

A look at the Boost Bind and Function libraries

Rate me:
Please Sign up or sign in to vote.
4.34/5 (26 votes)
9 Feb 20064 min read 98.9K   607   31  
A small introduction to the Boost Bind and Function libraries.
// BoostCommandPattern.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

#include "boost\function.hpp"
#include "boost\bind.hpp"
#include "boost\shared_ptr.hpp"

///////////////////////////////////////////////////////////
/// Final Approach
struct Final
{
	void incAlt() {cout << "Throttle up..." << endl;}
	void decAlt() {cout << "Throttle down..." << endl;}
	void incSpeed() {cout << "Decrease back pressure. yoke forward..." << endl;}
	void decSpeed() {cout << "Increase back pressure..." << endl;}
	void comm(string s) {cout << "This is N123456, " << s << endl;}
};

///////////////////////////////////////////////////////////
/// command object
class command
{
 private:
	 boost::function<void ()> _f;
 public:
	 command() {;}
	 command(boost::function<void ()> f) : _f(f) {;}

	 template <typename T> void setFunction (T t) {_f = t;}
	 void execute()
	 {
	  if(!_f.empty())
			_f();
	 }
};

///////////////////////////////////////////
/// main
int _tmain(int argc, _TCHAR* argv[])
{
	/// prepare for final
	boost::shared_ptr<Final> final(new Final);

	string s = "request landing One-Four with information Papa.";

	// PART 1
	/////////////////////////////////
	/// create commands and execute
	command c;
	c.setFunction(boost::bind(&Final::comm, final, s));

	command da(boost::bind(&Final::decAlt, final));
	command ds(boost::bind(&Final::decSpeed, final));
	
	c.execute();
	da.execute();
  ds.execute();
	
  cout << endl << "Part2" << endl;
	// PART 2
	/////////////////////////////////////////////////////////////////////////////////////
	//create a series of commands and stuff them into a container to be sequenced later
	vector<boost::shared_ptr<command> > v;

	boost::shared_ptr<command> spCmd1(new command(boost::bind(&Final::incAlt, final)));
  boost::shared_ptr<command> spCmd2(new command(boost::bind(&Final::decSpeed, final)));
  boost::shared_ptr<command> spCmd3(new command(boost::bind(&Final::decAlt, final)));

	v.push_back(spCmd1);
	v.push_back(spCmd2);
	v.push_back(spCmd3);

	// run the command sequence
	for_each(v.begin(), v.end(), boost::bind(&command::execute, _1));
	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions