ARB
global.h
Go to the documentation of this file.
1 // =============================================================== //
2 // //
3 // File : global.h //
4 // Purpose : //
5 // //
6 // Institute of Microbiology (Technical University Munich) //
7 // http://www.arb-home.de/ //
8 // //
9 // =============================================================== //
10 
11 #ifndef GLOBAL_H
12 #define GLOBAL_H
13 
14 #ifndef ARB_ASSERT_H
15 #include <arb_assert.h>
16 #endif
17 #ifndef ARB_DEFS_H
18 #include <arb_defs.h>
19 #endif
20 #ifndef ARBTOOLS_H
21 #include <arbtools.h>
22 #endif
23 #ifndef SMARTPTR_H
24 #include <smartptr.h>
25 #endif
26 #ifndef ARB_STRING_H
27 #include <arb_string.h>
28 #endif
29 #ifndef ARB_GLOBAL_DEFS_H
30 #include <arb_global_defs.h>
31 #endif
32 
33 #define ca_assert(cond) arb_assert(cond)
34 
35 // --------------------
36 
38  static Convaln_exception *thrown;
39 
40  int code;
41  mutable char *msg;
42 
43 public:
44  Convaln_exception(int error_code, const char *error_msg)
45  : code(error_code),
46  msg(ARB_strdup(error_msg))
47  {
48  ca_assert(!thrown); // 2 exceptions at the same time ? very exceptional! :)
49  thrown = this;
50  }
52  : code(other.code),
53  msg(ARB_strdup(other.msg))
54  {}
57  ca_assert(thrown);
58  thrown = NULp;
59  free(msg);
60  }
61 
62  int get_code() const { return code; }
63  const char *get_msg() const { return msg; }
64  void replace_msg(const char *new_msg) const { freedup(msg, new_msg); }
65 
66  void catched() {
67  ca_assert(thrown == this);
68  thrown = NULp;
69  }
70 
71  static const Convaln_exception *exception_thrown() { return thrown; }
72 };
73 
74 // --------------------
75 
76 class Warnings : virtual Noncopyable {
77  static bool show_warnings;
78  bool old_state;
79 public:
80  static bool shown() { return show_warnings; }
81 
83  old_state = shown();
84  show_warnings = false;
85  }
86  ~Warnings() { show_warnings = old_state; }
87 };
88 
89 
90 // --------------------
91 
92 CONSTEXPR_INLINE int min(int t1, int t2) { return t1<t2 ? t1 : t2; }
93 CONSTEXPR_INLINE int max(int t1, int t2) { return t1>t2 ? t1 : t2; }
94 
95 
96 // Note: str_equal not accepted as constexpr by OSX (because strcmp() isnt)
97 CONSTEXPR_INLINE_NC bool str_equal(const char *s1, const char *s2) { return strcmp(s1, s2) == 0; }
98 CONSTEXPR_INLINE bool str_iequal(const char *s1, const char *s2) { return strcasecmp(s1, s2) == 0; }
99 
100 CONSTEXPR_INLINE int str0len(const char *str) {
101  return str ? strlen(str) : 0;
102 }
103 
104 inline char *strndup(const char *str, int len) {
105  char *result = ARB_alloc<char>(len+1);
106  memcpy(result, str, len);
107  result[len] = 0;
108  return result;
109 }
110 
111 CONSTEXPR_INLINE int count_spaces(const char *str) { return strspn(str, " "); }
112 
113 inline bool occurs_in(char ch, const char *in) { ca_assert(ch != 0); return strchr(in, ch); }
114 
115 CONSTEXPR_INLINE bool is_end_mark(char ch) { return ch == '.' || ch == ';'; }
116 
117 CONSTEXPR_INLINE bool is_sequence_terminator(const char *str) { return str[0] == '/' && str[1] == '/'; }
118 
119 #define WORD_SEP ",.; ?:!)]}"
120 
121 inline bool is_gapchar(char ch) { return GAP::is_import_gap(ch); }
122 inline bool is_word_char(char ch) { return !occurs_in(ch, WORD_SEP); }
123 
124 CONSTEXPR_INLINE bool has_no_content(const char *field) {
125  return !field ||
126  !field[0] ||
127  (field[0] == '\n' && !field[1]);
128 }
129 CONSTEXPR_INLINE bool has_content(const char *field) { return !has_no_content(field); }
130 
131 inline char *no_content() {
132  char *nothing = ARB_alloc<char>(2);
133  nothing[0] = '\n';
134  nothing[1] = 0;
135  return nothing;
136 }
137 
138 inline bool copy_content(char*& entry, const char *content) {
139  bool copy = has_content(content);
140  if (copy) freedup(entry, content);
141  return copy;
142 }
143 
144 // --------------------
145 
146 #define lookup_keyword(keyword,table) ___lookup_keyword(keyword, table, ARRAY_ELEMS(table))
147 
148 #else
149 #error global.h included twice
150 #endif // GLOBAL_H
151 
CONSTEXPR_INLINE int str0len(const char *str)
Definition: global.h:100
string result
CONSTEXPR_INLINE bool str_iequal(const char *s1, const char *s2)
Definition: global.h:98
bool is_import_gap(const char c)
DECLARE_ASSIGNMENT_OPERATOR(Convaln_exception)
#define ca_assert(cond)
Definition: global.h:33
char * ARB_strdup(const char *str)
Definition: arb_string.h:27
#define CONSTEXPR_INLINE_NC
Definition: cxxforward.h:127
#define WORD_SEP
Definition: global.h:119
const char * get_msg() const
Definition: global.h:63
CONSTEXPR_INLINE bool has_content(const char *field)
Definition: global.h:129
~Convaln_exception()
Definition: global.h:56
CONSTEXPR_INLINE_NC bool str_equal(const char *s1, const char *s2)
Definition: global.h:97
bool copy_content(char *&entry, const char *content)
Definition: global.h:138
int get_code() const
Definition: global.h:62
Convaln_exception(int error_code, const char *error_msg)
Definition: global.h:44
bool is_word_char(char ch)
Definition: global.h:122
#define CONSTEXPR_INLINE
Definition: cxxforward.h:111
CONSTEXPR_INLINE bool is_end_mark(char ch)
Definition: global.h:115
void replace_msg(const char *new_msg) const
Definition: global.h:64
Convaln_exception(const Convaln_exception &other)
Definition: global.h:51
static void copy(double **i, double **j)
Definition: trnsprob.cxx:32
static const Convaln_exception * exception_thrown()
Definition: global.h:71
~Warnings()
Definition: global.h:86
CONSTEXPR_INLINE int max(int t1, int t2)
Definition: global.h:93
bool is_gapchar(char ch)
Definition: global.h:121
CONSTEXPR_INLINE bool has_no_content(const char *field)
Definition: global.h:124
CONSTEXPR_INLINE int min(int t1, int t2)
Definition: global.h:92
static bool shown()
Definition: global.h:80
#define NULp
Definition: cxxforward.h:116
CONSTEXPR_INLINE int count_spaces(const char *str)
Definition: global.h:111
char * no_content()
Definition: global.h:131
char * strndup(const char *str, int len)
Definition: global.h:104
Warnings()
Definition: global.h:82
CONSTEXPR_INLINE bool is_sequence_terminator(const char *str)
Definition: global.h:117
void catched()
Definition: global.h:66
bool occurs_in(char ch, const char *in)
Definition: global.h:113