QDaq  0.2.6
Qt-based Data Aqcuisition
 All Classes Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
vectorprototype.cpp
1 #include "vectorprototype.h"
2 #include <QtScript/QScriptEngine>
3 
4 Q_DECLARE_METATYPE(QDaqVector*)
5 
6 VectorPrototype::VectorPrototype(QObject *parent)
7  : QObject(parent)
8 {
9 }
10 
11 VectorPrototype::~VectorPrototype()
12 {
13 }
14 
15 QDaqVector *VectorPrototype::thisVector() const
16 {
17  QDaqVector * V = qscriptvalue_cast<QDaqVector*>(thisObject().data());
18  Q_ASSERT(V);
19  return V;
20 }
21 
23 {
24  return *thisVector() == other;
25 }
26 
27 QString VectorPrototype::toString() const
28 {
29  return QString("[object Vector]");
30 }
31 
32 void VectorPrototype::push(const QScriptValue &val)
33 {
34  QDaqVector* V = thisVector();
35  if (V->isCircular() && V->capacity()==0) {
36  context()->throwError(QScriptContext::RangeError,"Pushing to a null capacity circular vector.");
37  return;
38  }
39  if (val.isNumber())
40  thisVector()->push(val.toNumber());
41  else if (val.isArray()) {
42  quint32 n = val.property("length").toUInt32();
43  QDaqVector* vec = thisVector();
44  for(quint32 i=0; i<n; i++) {
45  QScriptValue vi = val.property(i);
46  if (vi.isNumber()) vec->push(vi.toNumber());
47  else break;
48  }
49  }
50  else if (val.instanceOf(engine()->globalObject().property("Vector"))) {
51  QDaqVector * vec = thisVector();
52  QDaqVector * rhs = qscriptvalue_cast<QDaqVector*>(val.data());
53  if (rhs) vec->push(*rhs);
54  }
55 }
56 
58 {
59  thisVector()->pop();
60 }
61 
63 {
64  thisVector()->resize(n);
65 }
66 
67 QScriptValue VectorPrototype::toArray() const
68 {
69  QDaqVector* vec = thisVector();
70  QScriptValue v = engine()->newArray(vec->size());
71  for(int i=0; i<vec->size(); i++)
72  v.setProperty(quint32(i),qScriptValueFromValue(engine(),vec->get(i)));
73  return v;
74 }
75 
76 void VectorPrototype::clear()
77 {
78  thisVector()->clear();
79 }
80 
81 double VectorPrototype::min() const
82 {
83  return thisVector()->vmin();
84 }
85 double VectorPrototype::max() const
86 {
87  return thisVector()->vmax();
88 }
89 double VectorPrototype::mean() const
90 {
91  return thisVector()->mean();
92 }
93 double VectorPrototype::std() const
94 {
95  return thisVector()->std();
96 }
97 
98 QScriptValue VectorPrototype::valueOf() const
99 {
100  return thisObject().data();
101 }
102 
103 bool VectorPrototype::checkRange(int offset, int sz) const
104 {
105  int len = thisVector()->size();
106  if (offset<0 || len<sz || offset>len-sz)
107  {
108  context()->throwError(QScriptContext::RangeError,tr("Index out of range"));
109  return false;
110  } else return true;
111 }
112 
113 
double vmin() const
Minimum value in the buffer.
Definition: QDaqVector.h:102
double mean() const
Mean value in the buffer.
Definition: QDaqVector.h:106
double std() const
Standard deviation the buffer values.
Definition: QDaqVector.h:108
int size() const
Return the number of elememts stored in the buffer.
Definition: QDaqVector.h:64
bool equals(const QDaqVector &other)
Returns true if this Vector is equal to other.
The prototype for the Vector class.
A buffer for storing double numbers.
Definition: QDaqVector.h:40
double get(int i) const
Get the i-th element.
Definition: QDaqVector.h:79
QScriptValue valueOf() const
Return the data stored by the object.
void push(double v)
Append a value to the buffer.
Definition: QDaqVector.h:86
double vmax() const
Maximum value in the buffer.
Definition: QDaqVector.h:104
bool isCircular() const
Return true if Circular.
Definition: QDaqVector.h:69
void pop()
Remove the last element.
void clear()
Empty the buffer.
Definition: QDaqVector.h:77
int capacity() const
Return the currently allocated memory capacity (in number of elements).
Definition: QDaqVector.h:73
void push(const QScriptValue &val)
Push a number, a Vector or a numeric Array at the end.
void pop()
Remove the last point.
Definition: QDaqVector.h:92
void resize(int n)
Resize to n elements keeping the first n.