Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

A look at the Boost Bind and Function libraries

, 9 Feb 2006
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
	 boost::function<void ()> _f;
	 command() {;}
	 command(boost::function<void ()> f) : _f(f) {;}

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

/// 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));
  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)));


	// 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.


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


About the Author

Gary J. Kuehn
United States United States
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 9 Feb 2006
Article Copyright 2006 by Gary J. Kuehn
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid