SM_ADC  1.0
Приём данных АЦП через разделяемую память
utils.h
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 //
3 // This file is part of easyLink Library.
4 //
5 // Copyright (c) 2014 FEMTO-ST, ENSMM, UFC, CNRS.
6 //
7 // License: GNU General Public License 3
8 // Author: Guillaume J. Laurent
9 //
10 //------------------------------------------------------------------------------
11 #ifndef EASYLINK_UTILS_H
12 #define EASYLINK_UTILS_H
13 
14 //------------------------------------------------------------------------------
15 #include <iostream>
16 #include <string>
17 #include <sstream>
18 #include <math.h>
19 #include <vector>
20 using namespace std;
21 
22 //------------------------------------------------------------------------------
23 #ifdef _MSC_VER
24 #include <windows.h>
25 #undef IN
26 #undef OUT
27 #endif
28 
29 //------------------------------------------------------------------------------
30 static char ERROR_MSG_BUFFER[512];
31 
32 /** \ingroup utils
33  * Converts a double, int or char to a String.
34  */
35 template <typename T> inline string toString(T value)
36 {
37  ostringstream os ;
38  os << value ;
39  return os.str() ;
40 }
41 
42 /** \ingroup utils
43  * Returns true if the string is a valid MATLAB variable identifier.
44  */
45 inline bool isIdentifier(string str)
46 {
47  unsigned int i;
48  bool valid;
49  valid = ( ( (str[0]>='a')&&(str[0]<='z') )
50  || ( (str[0]>='A')&&(str[0]<='Z') ) );
51  i=1;
52  while ((i<str.size()) && valid)
53  {
54  valid = ( ( (str[i]>='a')&&(str[i]<='z') )
55  || ( (str[i]>='A')&&(str[i]<='Z') )
56  || ( (str[i]>='0')&&(str[i]<='9') )
57  || (str[i]=='_') );
58  i++;
59  }
60  return valid;
61 }
62 
63 /** \ingroup utils
64  * Returns the current computer time in milliseconds.
65  */
66 inline unsigned long getComputerTime()
67 {
68 #ifdef _MSC_VER
69  return (unsigned long)(GetTickCount());
70 #else
71  return (unsigned long)(clock()/1000);
72 #endif
73 }
74 
75 #ifdef _MSC_VER
76 inline int _round(double x)
77 {
78  if (x > -0.5)
79  return((int)(x + 0.5));
80  else
81  return((int)(x - 0.5));
82 }
83 #endif
84 
85 #define EQUALITY_TOLERANCE 0.00000000000001
86 
87 /** \ingroup utils
88  * Returns true if x are equal to y. The test takes into account the
89  * precision of the mantissa.
90  */
91 inline bool areEqual(double x,double y,double tolerance = EQUALITY_TOLERANCE)
92 {
93  if (fabs(x)>1.0)
94  {
95  double mx,my;
96  int ex,ey;
97  mx=frexp(x,&ex);
98  my=frexp(y,&ey);
99  return ( (ex==ey) && (mx<my+tolerance) && (mx>my-tolerance) ) ;
100  }
101  else
102  return (fabs(x-y)<2*tolerance);
103 }
104 
105 //------------------------------------------------------------------------------
106 #endif
bool areEqual(double x, double y, double tolerance=EQUALITY_TOLERANCE)
Definition: utils.h:91
#define EQUALITY_TOLERANCE
Definition: utils.h:85
string toString(T value)
Definition: utils.h:35
static char ERROR_MSG_BUFFER[512]
Definition: utils.h:30
unsigned long getComputerTime()
Definition: utils.h:66
bool isIdentifier(string str)
Definition: utils.h:45