dtl


cb_ptr_fun() and cb_ptr_fun_w_ret()

Category: utilities Component type: concept

Description

Use the cb_ptr_fun() and cb_ptr_fun_w_ret() template helper functions to wrap a raw function pointer or a pointer-to-member method into a function object. This mechanism allows you write regular C++ functions for use as a BCA , BPA, InsVal, SelVal, IOHandler, or SetParamsFn in an IndexedDBView. The single argument versions of cb_ptr_fun() and cb_ptr_fun_w_ret() accept a raw function pointer as their only argument while the two argument overloads also require an argument to the object to bind to the functor. Use cb_ptr_fun() for functions that do not return a value (return void) and cb_ptr_fun_w_ret() for functions that do return a value. You may wrap any C++ function that contains up to and including 4 arguments.

Definition

Defined in the variant_cc.h header file.

Refinement of

None.

Associated types

BPA, BCA, InsVal, SelVal, DBView, IndexedDBView, , IOHandler

Example 1:

//Simple use of cb_ptr_fun() with a BCA


// a BCA written as a simple function 
void BCAExample(BoundIOs &cols, Example &row)
{
  cols["INT_VALUE"]    == row.exampleInt;
  cols["STRING_VALUE"] == row.exampleStr;
  cols["DOUBLE_VALUE"] == row.exampleDouble;
  cols["EXAMPLE_LONG"] == row.exampleLong;
  cols["EXAMPLE_DATE"] == row.exampleDate;
}

// Read the contents of the DB_EXAMPLE table and return a vector of the
// resulting rows
vector<Example> ReadData()
{
 vector<Example> results;

 // the call to cb_ptr_fun() wraps BCAExample in a function object
 // so DTL may use it as the BCA for the DBView
 DBView<Example> view("DB_EXAMPLE", cb_ptr_fun(BCAExample));

 DBView<Example>::select_iterator read_it = view.begin();
 for ( ; read_it != view.end(); ++read_it)
 {
  results.push_back(*read_it);
 }

 return results;
}

 

Example 2:

//Simple use of cb_ptr_fun_w_ret() as a SelVal



// a typical SelVal function that we want to use in DTL
// an Example object is valid if all columns have a value
bool ExampleSelValidate(boundIOs &boundIOs, Example &rowbuf)
{
	for (BoundIOs::iterator b_it = boundIOs.begin();
				b_it != boundIOs.end(); b_it++)
	{
		BoundIO &boundIO = (*b_it).second;
		if (boundIO.IsColumn() && boundIO.IsNull())
			return false;  // found null column ... data is invalid
	}

	return true;	// no nulls found ... data is OK
}

// Read the contents of the DB_EXAMPLE table and return a vector of the
// resulting rows
vector<Example> ReadData()
{
 vector<Example> results;

 // the call to cb_ptr_fun_w_ret() wraps ExampleSelValidate in a function object
 // so DTL may use it as the SelVal for the DBView
 DBView<Example> view("DB_EXAMPLE", DefaultBCA<Example>(),
	"", DefaultBPA<Example>(), cb_ptr_fun_w_ret(ExampleSelValidate));

 DBView<Example>::select_iterator read_it = view.begin();
 for ( ; read_it != view.end(); ++read_it)
 {
  results.push_back(*read_it);
 }

 return results;
}

Notation:

X A type that is a model of BoundIO
a Object of type X

Expression semantics

Name Expression Precondition Semantics Postcondition
Wrap function ptr. for functions returning void
cb_ptr_fun(func_ptr)
  Wraps the raw function pointer func_ptr into a function object in the CBFunctor family. See Note [1].  
Wrap function ptr. for functions returning a value
cb_ptr_fun_w_ret(func_ptr)
  Wraps the raw function pointer func_ptr into a function object in the CBFunctor family.See Note [1].    
Wrap pointer-to-member methods returning void cb_ptr_fun(obj, ptr_to_member)   Wraps the pointer-to-member method into a function object in the CBFunctor family.See Note [1]. The type of the bound object does not figure in the signature of the functor ... only the method arguments and return type matter.    
Wrap pointer-to-member methods returning a value cb_ptr_fun_w_ret(obj, ptr_to_member)   Wraps the pointer-to-member method into a function object in the CBFunctor family.See Note [1]. The type of the bound object does not figure in the signature of the functor ... only the method arguments and return type matter.    

Notes

[1] The CBFunctor family of functions is defined in the C++ Callback Library created by Rich Hickey. For information, go to http://www.bestweb.net/~rhickey/. Much of the DTL implementation uses a modified and extended form of this library wherever function objects are required.

See also

BPA, BCA, InsVal, SelVal, DBView, IndexedDBView, , IOHandler


[DTL Home]

Copyright © 2002, Michael Gradman and Corwin Joy.

Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appears in all copies and that both that copyright notice and this permission notice appear in supporting documentation. Corwin Joy and Michael Gradman make no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.

This site written using the ORB. [The ORB]

1