Algorithm-CRF

 view release on metacpan or  search on metacpan

common.h  view on Meta::CPAN

  template <class T> inline void itoa(T val, char *s) {
    char *t;
    T mod;

    if (val < 0) {
      *s++ = '-';
      val = -val;
    }
    t = s;

    while (val) {
      mod = val % 10;
      *t++ = static_cast<char>(mod)+ '0';
      val /= 10;
    }

    if (s == t) *t++ = '0';
    *t = '\0';
    std::reverse(s, t);

    return;
  }

  template <class T>
  inline void uitoa(T val, char *s) {
    char *t;
    T mod;
    t = s;

    while (val) {
      mod = val % 10;
      *t++ = static_cast<char>(mod) + '0';
      val /= 10;
    }

    if (s == t) *t++ = '0';
    *t = '\0';
    std::reverse(s, t);

    return;
  }

#define _ITOA(_n) do { \
char buf[64]; \
itoa(_n, buf); \
append(buf); \
return *this; } while (0)

#define _UITOA(_n) do { \
char buf[64]; \
uitoa(_n, buf); \
append(buf); \
return *this; } while (0)

#define _DTOA(_n) do { \
char buf[64]; \
dtoa(_n, buf); \
append(buf); \
return *this; } while (0)

  class string_buffer: public std::string {
  public:
    string_buffer& operator<<(double _n)             { _DTOA(_n); }
    string_buffer& operator<<(short int _n)          { _ITOA(_n); }
    string_buffer& operator<<(int _n)                { _ITOA(_n); }
    string_buffer& operator<<(long int _n)           { _ITOA(_n); }
    string_buffer& operator<<(unsigned short int _n) { _UITOA(_n); }
    string_buffer& operator<<(unsigned int _n)       { _UITOA(_n); }
    string_buffer& operator<<(unsigned long int _n)  { _UITOA(_n); }
    string_buffer& operator<<(char _n) {
      push_back(_n);
      return *this;
    }
    string_buffer& operator<<(const char* _n) {
      append(_n);
      return *this;
    }
    string_buffer& operator<<(const std::string& _n) {
      append(_n);
      return *this;
    }
  };

  class die {
  public:
    die() {}
    virtual ~die() {
      std::cerr << std::endl;
      exit(-1);
    }
    int operator&(std::ostream&) { return 0; }
  };

  class warn {
  public:
    warn() {}
    virtual ~warn() { std::cerr << std::endl; }
    int operator&(std::ostream&) { return 0; }
  };

  struct whatlog {
    std::ostrstream stream_;
    const char *str() {
      stream_ << std::ends;
      return stream_.str();
    }
    jmp_buf cond_;
  };

  class wlog {
  public:
    whatlog *l_;
    explicit wlog(whatlog *l): l_(l) { l_->stream_.clear(); }
    ~wlog() { longjmp(l_->cond_, 1); }
    int operator&(std::ostream &) { return 0; }
  };
}

#define WHAT what_.stream_

#define CHECK_RETURN(condition, value) \
if (!(condition)) \
  if (setjmp(what_.cond_) == 1) { \
     return value;  \
  } else \
    wlog(&what_) & what_.stream_ << \
    __FILE__ << "(" << __LINE__ << ") [" << #condition << "] "

#define CHECK_0(condition)      CHECK_RETURN(condition, 0)
#define CHECK_FALSE(condition)  CHECK_RETURN(condition, false)

#define CHECK_CLOSE_FALSE(condition) \
if (!(condition)) \
  if (setjmp(what_.cond_) == 1) { \
     close(); \
     return false;  \
  } else \
    wlog(&what_) & what_.stream_ << \
    __FILE__ << "(" << __LINE__ << ") [" << #condition << "] "

#define CHECK_DIE(condition) \
(condition) ? 0 : die() & std::cerr << __FILE__ << \
"(" << __LINE__ << ") [" << #condition << "] "

#define CHECK_WARN(condition) \
(condition) ? 0 : warn() & std::cerr << __FILE__ << \
"(" << __LINE__ << ") [" << #condition << "] "
#endif



( run in 0.489 second using v1.01-cache-2.11-cpan-39bf76dae61 )