Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Wrapping C with C++ in .NET


January 04:

Listing 2: Managed C++ code used to create the assembly naglib, which contains the namespace NAGLIB and wraps the NAG C library functions in the class NAG_FUNCTIONS.

extern "C" {
   #include <stdio.h>
   #include <nag.h>
   #include <nag_stdlib.h>
}
#using <mscorlib.dll>
using namespace System::Runtime::InteropServices;
using namespace System;
namespace NAGLIB {
   public __delegate double INTEGRAND_FUN_TYPE(Double x);
   public __delegate void OBJ_FUN_TYPE (Int32 n, double *x, 
                                    double *objf, double *g, Int32 comm);
   [DllImport("nagc")]
   extern "C" void e04dgc(Int32 n, OBJ_FUN_TYPE *f, Double *x, Double *objf,
               Double *g, Nag_E04_Opt *options, Int32 comm, NagError *flag);
   [DllImport("nagc")]
   extern "C" void e04xxc(Nag_E04_Opt *options);
   [DllImport("nagc")]
   extern "C" void d01ajc(INTEGRAND_FUN_TYPE *f, Double a, Double b,
        Double epsabs, Double epsrel, Int32 max_num_subint, Double* result,
        Double *abserr, Nag_QuadProgress *qp, NagError *flag);
   [DllImport("nagc")]
   extern "C" void f02aac(Int32 n, Double *a, Int32 tda, 
                         Double *r, NagError *eflag);
   [DllImport("nagc")]
   extern "C" Double s15abc(Double x);
   public __gc class NAG_FUNCTIONS
   {
      public:
      Double QUADRATURE_epsabs;
      Double QUADRATURE_epsrel;
      Int32 QUADRATURE_max_subint;
      NAG_FUNCTIONS()
      { // the constructor: set default values
      QUADRATURE_epsabs = 0.0;
      QUADRATURE_epsrel = 0.0001;
      QUADRATURE_max_subint = 200;
      }
      void REAL_SYMM_EIGEN (Int32 n, Double *a, Int32 tda, 
                                                 Double *r, Int32 *flag)
      {
         NagError eflag;
         INIT_FAIL(eflag);
         f02aac(n, a, tda, r, &eflag);
         *flag = (Int32)eflag.code;
     }
     Double CUM_NORM (Double x) {
        return s15abc(x);
     }
     void OPTIMIZE (Int32 n, Double *x, Double *g, Double *objf, 
                                       Int32 *flag, OBJ_FUN_TYPE *the_fun)
     {
        NagError eflag;
        Nag_E04_Opt options;
        INIT_FAIL(eflag);
        e04xxc(&options);
        options.print_level = Nag_NoPrint;
        options.list = 0;
        options.verify_grad = Nag_NoCheck;
        e04dgc(n, the_fun, x, objf, g, &options, (Int32)0, &eflag);
        *flag = (Int32)eflag.code;
     }
     void QUADRATURE (Double a, Double b, Double *result, 
                     Double *abserr, Int32 *flag, INTEGRAND_FUN_TYPE *the_fun)
     {
       Nag_QuadProgress qp;
       NagError eflag;
       INIT_FAIL(eflag);
       d01ajc(the_fun, a, b, QUADRATURE_epsabs, QUADRATURE_epsrel, 
                         QUADRATURE_max_subint, result, abserr, &qp, &eflag);
       *flag = (Int32)eflag.code;
     }
  };
}


	
		

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.