VAPOR3 3.9.4
MyBase.h
Go to the documentation of this file.
1//
2// $Id$
3//
4//************************************************************************
5// *
6// Copyright (C) 2004 *
7// University Corporation for Atmospheric Research *
8// All Rights Reserved *
9// *
10//************************************************************************/
11//
12// File:
13//
14// Author: John Clyne
15// National Center for Atmospheric Research
16// PO 3000, Boulder, Colorado
17//
18// Date: Wed Sep 29 15:40:23 MDT 2004
19//
20// Description: A collection of general purpose utilities - things that
21// probably should be in the STL but aren't.
22//
23
24#ifndef _MyBase_h_
25#define _MyBase_h_
26
27#include <cmath>
28#include <cstdarg>
29#include <string>
30#include <cstring>
31#include <vector>
32#include <vapor/common.h>
33
34#ifdef NEW_DEBUG
35 #include <new>
36void *operator new(size_t sz);
37void *operator new[](size_t sz);
38#endif
39
40#ifdef WIN32
41 // Silence an annoying and unnecessary compiler warning
42 #pragma warning(disable : 4251)
43#endif
44using namespace std;
45
46namespace Wasp {
47
53
57
58// default values used for variables outside valid grid
59const float ABOVE_GRID = 0.f;
60const float BELOW_GRID = 0.f;
61
62//
63// The MyBase base class provides a simple error reporting mechanism
64// that can be used by derrived classes. N.B. the error messages/codes
65// are stored in static class members.
66//
68public:
69 typedef void (*ErrMsgCB_T)(const char *msg, int err_code);
70 typedef void (*DiagMsgCB_T)(const char *msg);
71
73 const string &getClassName() const { return (_className); };
74
76 //
83 //
84 static void SetErrMsg(const char *format, ...);
85
87 //
95 //
96 static void SetErrMsg(int errcode, const char *format, ...);
97
105 //
106 static const char *GetErrMsg() { return (ErrMsg); }
107
109 //
113 //
114 static void SetErrCode(int err_code) { ErrCode = err_code; }
115
117 //
122 //
123 static int GetErrCode() { return (ErrCode); }
124
133 //
134 static void SetErrMsgCB(ErrMsgCB_T cb) { ErrMsgCB = cb; };
135
144 //
145 static ErrMsgCB_T GetErrMsgCB() { return (ErrMsgCB); };
146
155 //
156 static void SetErrMsgFilePtr(FILE *fp) { ErrMsgFilePtr = fp; };
157
164 //
165 static const FILE *SetErrMsgFilePtr() { return (ErrMsgFilePtr); };
166
168 //
176 //
177 static void SetDiagMsg(const char *format, ...);
178
185 //
186 static const char *GetDiagMsg() { return (DiagMsg); }
187
196 //
197 static void SetDiagMsgCB(DiagMsgCB_T cb) { DiagMsgCB = cb; };
198
207 //
208 static DiagMsgCB_T GetDiagMsgCB() { return (DiagMsgCB); };
209
218 //
219 static void SetDiagMsgFilePtr(FILE *fp) { DiagMsgFilePtr = fp; };
220
227 //
228
238 static bool EnableErrMsg(bool enable)
239 {
240 bool prev = Enabled;
241 Enabled = enable;
242 return (prev);
243 };
244
245 static bool GetEnableErrMsg() { return Enabled; }
246
247 // N.B. the error codes/messages are stored in static class members!!!
248 static char * ErrMsg;
249 static int ErrCode;
250 static int ErrMsgSize;
251 static FILE * ErrMsgFilePtr;
252 static ErrMsgCB_T ErrMsgCB;
253
254 static char * DiagMsg;
255 static int DiagMsgSize;
256 static FILE * DiagMsgFilePtr;
257 static DiagMsgCB_T DiagMsgCB;
258 static bool Enabled;
259
260protected:
261 void SetClassName(const string &name) { _className = name; };
262
263private:
264 static void _SetErrMsg(char **msg, int *sz, const char *format, va_list args);
265 string _className; // name of class
266};
267
268COMMON_API inline int IsOdd(int x) { return (x % 2); };
269
271//
275COMMON_API int IsPowerOfTwo(unsigned int x);
276
277COMMON_API inline int Min(int a, int b) { return (a < b ? a : b); };
278COMMON_API inline int Max(int a, int b) { return (a > b ? a : b); };
279
280COMMON_API inline size_t Min(size_t a, size_t b) { return (a < b ? a : b); };
281COMMON_API inline size_t Max(size_t a, size_t b) { return (a > b ? a : b); };
282
283COMMON_API inline float Min(float a, float b) { return (a < b ? a : b); };
284COMMON_API inline float Max(float a, float b) { return (a > b ? a : b); };
285
286COMMON_API inline double Min(double a, double b) { return (a < b ? a : b); };
287COMMON_API inline double Max(double a, double b) { return (a > b ? a : b); };
288
289COMMON_API inline double LogBaseN(double x, double n) { return (log(x) / log(n)); };
290
291// Find integer log, base 2:
292COMMON_API int ILog2(int n);
293
295//
298//
299COMMON_API int StrCmpNoCase(const string &s, const string &t);
300
302//
306
308//
315//
316COMMON_API void StrToWordVec(const string &s, vector<string> &v);
317
318COMMON_API std::vector<std::string> &SplitString(const std::string &s, char delim, std::vector<std::string> &elems);
319COMMON_API std::vector<size_t> &SplitString(const std::string &s, char delim, std::vector<size_t> &elems);
320COMMON_API std::vector<int> &SplitString(const std::string &s, char delim, std::vector<int> &elems);
321COMMON_API std::vector<float> &SplitString(const std::string &s, char delim, std::vector<float> &elems);
322COMMON_API std::vector<double> &SplitString(const std::string &s, char delim, std::vector<double> &elems);
323
332//
333COMMON_API unsigned long long GetBits64(unsigned long long targ, int pos, int n);
334
343//
344COMMON_API unsigned long long SetBits64(unsigned long long targ, int pos, int n, unsigned long long src);
345
346// ran1 function declaration (from numerical recipes in C)
347COMMON_API double ran1(long *);
348}; // namespace Wasp
349
350//
351// Handle OS differences in 64-bit IO operators
352//
353
354// 64-bit fseek
355//
356
357#ifdef FSEEK64
358 #undef FSEEK64
359#endif
360
361#if defined(WIN32)
362 // Note: win32 won't seek beyond 32 bits
363 #define FSEEK64 fseek
364#endif
365
366#if defined(Linux) || defined(AIX)
367 #define FSEEK64 fseeko64
368#endif
369
370#if defined(Darwin)
371 #define FSEEK64 fseeko
372#endif
373
374#ifndef FSEEK64
375 #define FSEEK64 fseek64
376#endif
377
378// 64-bit fopen
379//
380
381#ifdef FOPEN64
382 #undef FOPEN64
383#endif
384
385#if defined(WIN32) || defined(Darwin)
386 #define FOPEN64 fopen
387#endif
388
389#ifndef FOPEN64
390 #define FOPEN64 fopen64
391#endif
392
393// 64-bit stat
394//
395
396#ifdef STAT64
397 #undef STAT64
398#endif
399
400#ifdef STAT64_T
401 #undef STAT64_T
402#endif
403
404#if defined(WIN32)
405 #define STAT64_T _stat
406 #define STAT64 _stat
407#endif
408
409#if defined(Darwin)
410 #define STAT64_T stat
411 #define STAT64 stat
412#endif
413
414#if defined(__CYGWIN__)
415 #define STAT64_T stat
416 #define STAT64 stat
417#endif
418
419#ifndef STAT64
420 #define STAT64_T stat64
421 #define STAT64 stat64
422#endif
423
424#ifndef TIME64_T
425 #ifdef WIN32
426 #define TIME64_T __int64
427 #else
428 #define TIME64_T int64_t
429 #endif
430#endif
431
432#endif // MYBASE_H
Wasp base class.
Definition: MyBase.h:67
static const char * GetErrMsg()
Definition: MyBase.h:106
static int ErrCode
Definition: MyBase.h:249
static void SetDiagMsgCB(DiagMsgCB_T cb)
Definition: MyBase.h:197
static bool Enabled
Definition: MyBase.h:258
static ErrMsgCB_T ErrMsgCB
Definition: MyBase.h:252
static void SetErrCode(int err_code)
Record an error code.
Definition: MyBase.h:114
static void SetErrMsgCB(ErrMsgCB_T cb)
Definition: MyBase.h:134
static int DiagMsgSize
Definition: MyBase.h:255
static void SetErrMsgFilePtr(FILE *fp)
Definition: MyBase.h:156
static DiagMsgCB_T DiagMsgCB
Definition: MyBase.h:257
static char * ErrMsg
Definition: MyBase.h:248
void SetClassName(const string &name)
Definition: MyBase.h:261
static int GetErrCode()
Retrieve the current error code.
Definition: MyBase.h:123
static DiagMsgCB_T GetDiagMsgCB()
Definition: MyBase.h:208
static ErrMsgCB_T GetErrMsgCB()
Definition: MyBase.h:145
static const FILE * SetErrMsgFilePtr()
Definition: MyBase.h:165
static FILE * DiagMsgFilePtr
Definition: MyBase.h:256
static void SetErrMsg(int errcode, const char *format,...)
Record a formatted error message and an error code.
static void SetDiagMsg(const char *format,...)
Record a formatted diagnostic message.
const string & getClassName() const
Definition: MyBase.h:73
static FILE * ErrMsgFilePtr
Definition: MyBase.h:251
static bool EnableErrMsg(bool enable)
Definition: MyBase.h:238
static void SetDiagMsgFilePtr(FILE *fp)
Definition: MyBase.h:219
static char * DiagMsg
Definition: MyBase.h:254
static int ErrMsgSize
Definition: MyBase.h:250
static const char * GetDiagMsg()
Definition: MyBase.h:186
static void SetErrMsg(const char *format,...)
Record a formatted error message.
static bool GetEnableErrMsg()
Definition: MyBase.h:245
#define COMMON_API
Definition: common.h:72
Definition: CFuncs.h:31
COMMON_API void StrToWordVec(const string &s, vector< string > &v)
Parse a string, returning a vector of words.
COMMON_API int IsPowerOfTwo(unsigned int x)
Return true if power of two.
COMMON_API double LogBaseN(double x, double n)
Definition: MyBase.h:289
COMMON_API int ILog2(int n)
COMMON_API unsigned long long GetBits64(unsigned long long targ, int pos, int n)
COMMON_API void StrRmWhiteSpace(string &s)
Remove white space from a string.
COMMON_API double ran1(long *)
COMMON_API std::vector< std::string > & SplitString(const std::string &s, char delim, std::vector< std::string > &elems)
const float ABOVE_GRID
Definition: MyBase.h:59
COMMON_API int Max(int a, int b)
Definition: MyBase.h:278
COMMON_API int Min(int a, int b)
Definition: MyBase.h:277
COMMON_API int IsOdd(int x)
Definition: MyBase.h:268
COMMON_API unsigned long long SetBits64(unsigned long long targ, int pos, int n, unsigned long long src)
const float BELOW_GRID
Definition: MyBase.h:60
COMMON_API int StrCmpNoCase(const string &s, const string &t)
Case-insensitive string comparison.