unused_return_value_monitor Class Template Reference
[Utility Library]

#include <stlsoft/util/unused_return_value_monitor.hpp>

List of all members.


Detailed Description

template<typename V, typename M, typename R = V>
class stlsoft::unused_return_value_monitor< V, M, R >

Return value adaptor for monitoring whether return values are used.

Parameters:
V The value type. This is the type that is to be returned by the function whose return type is to be monitored
M The monitor function type. If the return value is not used, the monitor function instance will be called.
R The reference type. This is the reference type, used in the constructor of the monitor, and the type of the member variable storing the value
If the value type is a simple type, you can just allow the reference type R to be defaulted, as in:

struct int_monitor
{
public:
  void operator ()(void const* instance, int value) const
  {
    printf("Unused return value %d from object instance %p\n", value, instance);
  }
};

class X
{
public:
  unused_return_value_monitor<int, int_monitor>   fn()
  {
    return 10; // A copy is taken, but copies of ints usually cost the same as references to ints
  }
};

Where the return type is a complex type, you can make efficiency savings by using a (C++) reference for the reference type. However, this must only be done when the returned value will persist for the duration of the calling expression.

struct string_monitor
{
public:
  void operator ()(void const* instance, string const& value) const
  {
    printf("Unused return value %s from object instance %p\n", value.c_str(), instance);
  }
};

class Y1
{
public:
  Y2()
    : m_str("What's new, Pussycat?")
  {}

public:
  // Must store return value by value, since the returned value will not persist for the duration of the calling expression
  unused_return_value_monitor<string, string_monitor> fn_by_value()
  {
    return "Hello sailor";
  }

  // Must store return value by (const) reference, since the returned value will persist for the duration of the calling expression
  unused_return_value_monitor<string, string_monitor, string const&> fn_by_ref()
  {
    return m_str;
  }

private:
  string const  m_str;
};

Public Types

typedef V value_type
typedef M monitor_function
typedef R reference_type
typedef
unused_return_value_monitor< V,
M, R > 
class_type

Public Member Functions

Construction
 unused_return_value_monitor (reference_type value)
 unused_return_value_monitor (reference_type value, monitor_function monitorFn)
 unused_return_value_monitor (class_type const &rhs)
 ~unused_return_value_monitor () throw ()
 Destructor.
Conversion
 operator value_type () const

Constructor & Destructor Documentation

~unused_return_value_monitor (  )  throw () [inline]

Destructor.

Note:
The destructor for unused_return_value_monitor deliberately does not declare a throw() clause because it may indeed throw an exception.

This requires that unused_return_value_monitor is *never* used inside a catch block.


The documentation for this class was generated from the following file:

Generated on Thu Jun 10 08:59:03 2010 for STLSoft by  doxygen 1.5.6