help-glpk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-glpk] Add a hook for xassert


From: Robbie Morrison
Subject: Re: [Help-glpk] Add a hook for xassert
Date: Fri, 04 Jul 2008 19:39:32 +0200
User-agent: Thunderbird 1.5.0.14ubu (X11/20080306)

Hi all

Andrew Makhorin recently wrote:

> As to catching the xassert message, you can use glp_term_hook;
> all glpk terminal output goes through it.

Some comments on 'glp_term_hook' and C++.

With regard to the following section in the 4.28 manual:

    3.3.4  Install hook to intercept terminal output

and the example function:

    static int hook(void *info, const char *s).

An explicit cast is required to compile under GNU g++
(and probably all C++ compilers).

Given that the GLPK client code is often a C++ program,
you might like to consider one of the following changes
to the documentation:

    FILE *foo = info;                       /* C compiler */
    FILE *foo = static_cast<FILE*>(info);   /* C++ compiler */

or:

    FILE *foo = (FILE*)info;   /* C++ compiler requires explicit cast */

---

For the record, one can also pass through a C++ ostream
(instead of a UNIX-style file handle) as follows
(I presume this code is reasonable style, given that
the use of void* is unavoidable):

  #include <iostream>                  // standard io for C++

  namespace                            // anonymous namespace better than
  {                                    //   'static' storage class
    int
    termHook
    (      void* info,                 // pointer to 'ostream' (as it happens)
     const char* s)                    // C-string to be output
    {
      std::ostream* os = static_cast<std::ostream*>(info);  // explicit cast

      *os << s;                        // need to dereference 'os' (the *)

      return 1;                        // non-zero suppresses normal reporting
    }
  }

  int
  main()
  {
    std::ostream* info = &std::clog;   // set redirect, here the C++ 'clog'
                                       //   stream, note too the need to obtain
                                       //   the address (the &)

    glp_term_hook(termHook, info);     // place terminal output under
                                       //   control of 'termHook'

    // make a GLPK solver call

    // can now remap 'info' to 'std::cout' or an open
    // 'std::ofstream' log file, if you wish!
  }

Or more directly but less flexibly:

  int
  main()
  {
    glp_term_hook(termHook, &std::clog);
  }

with best wishes
Robbie Morrison
PhD student -- policy-oriented energy system simulation
Technical University of Berlin (TU-Berlin), Germany
University email (redirected) : address@hidden
Webmail (preferred)           : address@hidden
[from IMAP client]





reply via email to

[Prev in Thread] Current Thread [Next in Thread]