Appendix B. Utility Headers

This appendix presents the contents of “sputil.h” (in Section B.1), the header used in the examples in Chapter 2; “mathutil.h” (in Section B.2), the header used in the examples in Chapter 12; and “rgxutil.h” (in Section B.3), the header used in the exercises in Chapter 15.

B.1. “sputil.h”

#ifndef SPUTIL_H
#define SPUTIL_H

#include <ostream>
#include <iostream>
#include <memory>

struct resource
  { // simple struct to demonstrate resource handling
  resource (int i0 = 0) : i(i0) {}
  int i;
  };

template <class Elem, class Tr>
std::basic_ostream<Elem, Tr>& operator<<(
  std::basic_ostream<Elem, Tr>& str,
  const resource& res)
  { // insert resource contents into stream
  return str << res.i;
  }

class d_res : public resource
  { // simple derived class
public:
  d_res (int i0) : resource (i0) {}

  };
static void show_rc(unsigned long count)
  { // show a reference count
  std::cout << " reference count: "
         << count << ' ';
  }

template <class Sp>
void show_refs (const std::tr1::shared_ptr<Sp>& sp)
  { // show reference count for shared ptr_objects
  show_rc (sp.use_count());
  }

template <class Sp>
void show_refs (const std::tr1::weak_ptr<Sp>& wp)
  { // show reference count for weak ptr_objects
  show_rc(wp.use_count());
  }

template <class Sp>
void show_refs(const Sp&)
  { // show reference count for other types (do nothing)
  }

template <class Sp>
void do_show(const Sp& sp)
  { // show contents of smart pointer
  std::cout << " pointer: "
         << (void*) sp.get() << ' ';
  if (sp.get())
    std::cout << " value: " << *sp << ' ';
  }

template <class Sp>
void show (const char *title, const Sp& sp)
  { // show title and contents of smart pointer
  std::cout << title << ' ';
  show_refs(sp);
  do_show(sp);
  }

template <class Ty>
void show (const char *title,
  const std::tr1::weak_ptr<Ty>& wp)

  { // show title and contents of weak_ptr object
  std::cout << title << ' ';
  if (wp.expired())
    std::cout << " expired  ";
  else
    { // show reference count before creating
      // temporary shared_ptr object
    show_rc(wp.use_count());
    do_show(wp.lock());
    }
  }

struct instrumented
  { // struct to report construction and destruction
  instrumented()
    { std::cout << " constructing instrumented "; }
  instrumented(const instrumented&)
    { std::cout << " copy constructing instrumented "; }
  instrumented& operator=(const instrumented&)
    { std::cout << " copying instrumented "; }
  ~ instrumented()
    { std::cout << " destroying instrumented "; }
  };
# endif   // SPUTIL H

B.2. “mathutil.h”

#ifndef MATHUTIL_H
#define MATHUTIL_H

#include <fenv.h>
#include <iostream>

void show_exceptions(const char *title)
  { // show raised exceptions then clear all
  std::cout << title << "  raised exceptions:";
  if (fetestexcept(FE_DIVBYZERO))
    std::cout << " divide-by-zero";
  if (fetestexcept(FE_INEXACT))
    std::cout << " inexact";
  if (fetestexcept(FE_INVALID))
    std::cout << " invalid";
  if (fetestexcept(FE_OVERFLOW))
    std::cout << " overflow";
  if (fetestexcept(FE_UNDERFLOW))

    std::cout << " underflow";
  std::cout << ' ';
  feclearexcept(FE_ALL_EXCEPT);
  }

# endif  // MATHUTIL_H

B.3. “rgxutil.h”

#ifndef RGXUTIL_H
#define RGXUTIL_H

#include <iostream>
#include <iomanip>
#include <regex>

static const char intro[] = "Trying to match `";
static const unsigned intro_chrs = sizeof(intro) - 1;

static const char *error(
  const std::tr1::regex_error& err)
  { // return description of error
  switch (err.code())
    { // select description
    case std::tr1::regex_constants::error_badbrace:
      return "invalid repeat count";
    case std::tr1::regex_constants::error_badrepeat:
      return "repeat not preceded by expression";
    case std::tr1::regex_constants::error_brace:
      return "unmatched curly brace";
    case std::tr1::regex_constants::error_brack:
      return "unmatched square bracket";
    case std::tr1::regex_constants::error_collate:
      return "invalid collating element name";
    case std::tr1::regex_constants::error_complexity:
      return "match too complex";
    case std::tr1::regex_constants::error_ctype:
      return "invalid character class name";
    case std::tr1::regex_constants::error_escape:
      return "invalid escape sequence";
    case std::tr1::regex_constants::error_paren:
      return "unmatched parenthesis";
    case std::tr1::regex_constants::error_range:
      return "invalid character range specifier";
    case std::tr1::regex_constants::error_space:
      return "out of resources";

      case std::tr1::regex_constants::error_stack:
      return "out of memory";
    case std::tr1::regex_constants::error_backref:
      return "invalid back reference";
    default:
      return "unknown error";;
    }
  }

static void do_match(const char *str, const char *tgt,
  std::tr1::regex_constants::syntax_option_type flags)
  {
  std::tr1::regex rgx;
  std::cout << "Regular expression `" << str << "`: ";
  try
    { // compile regular expression
    rgx.assign(str, flags);
    std::cout << "okay  ";
    }
  catch(const std::tr1::regex_error& err)
    { // report regular expression error
    std::cout << "invalid   " << error(err) << ' ';
    return;
    }
  if (tgt)
    { // check for match
    std::tr1::cmatch mr;
    std::cout << intro << tgt << "`, ";
    try
      { // try to match
      if (std::tr1::regex_match(tgt, mr, rgx))
          { // report successful match
          std::cout << "success ";
          for (int i = 0; i < mr.size(); ++ i)
            std::cout << std::setw(intro_chrs - 2)
             << i << ':'
             << std::setw(mr.position(i) + 1) << ' '
             << mr[i] << ' ';
          }
      else
        std::cout << "no match ";
      }
    catch(const std::tr1::regex_error& err)
      { // report match error
      std::cout << "match failed   "

             << error(err) << ' ' ;
      }
    }
  }

static void match_ECMA(
  const char *str, const char *tgt = 0)
  {
  do_match(str, tgt,
    std::tr1::regex_constants::ECMAScript);
  }

static void match_grep(
  const char * str, const char * tgt = 0)
  {
  do_match(str, tgt,
    std::tr1::regex_constants::grep);
  }

static void match_ere(
  const char *str, const char *tgt = 0)
  {
  do_match(str, tgt,
    std::tr1::regex_constants::extended);
  }

# endif // RGXUTIL_H

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.181.154