dynamic-graph  4.1.0-8-gdab7-dirty
Dynamic graph library
signal-array.h
1 // -*- mode: c++ -*-
2 // Copyright 2010, François Bleibel, Thomas Moulard, Olivier Stasse,
3 // JRL, CNRS/AIST.
4 //
5 
6 #ifndef DYNAMIC_GRAPH_SIGNAL_ARRAY_H
7 #define DYNAMIC_GRAPH_SIGNAL_ARRAY_H
8 #include <dynamic-graph/dynamic-graph-api.h>
9 #include <dynamic-graph/signal-base.h>
10 #include <stdio.h>
11 #include <vector>
12 
13 namespace dynamicgraph {
14 
18 template <class Time> class SignalArray_const {
19 
20 public:
21  static const int DEFAULT_SIZE = 20;
22 
23 protected:
24  std::vector<const SignalBase<Time> *> const_array;
25  unsigned int size, rank;
26 
27 public:
28  SignalArray_const<Time>(const unsigned int &sizeARG = DEFAULT_SIZE)
29  : const_array(sizeARG), size(sizeARG), rank(0) {}
30 
31  SignalArray_const<Time>(const SignalBase<Time> &sig)
32  : const_array(DEFAULT_SIZE), size(DEFAULT_SIZE), rank(0) {
33  addElmt(&sig);
34  }
35 
36  SignalArray_const<Time>(const SignalArray<Time> &siga)
37  : const_array(siga.getSize()), size(siga.getSize()),
38  rank(siga.getSize()) {
39  for (unsigned int i = 0; i < rank; ++i)
40  const_array[i] = &siga[i];
41  }
42 
43  SignalArray_const<Time>(const SignalArray_const<Time> &siga)
44  : const_array(siga.getSize()), size(siga.getSize()),
45  rank(siga.getSize()) {
46  for (unsigned int i = 0; i < rank; ++i)
47  const_array[i] = &siga[i];
48  }
49 
50  virtual ~SignalArray_const<Time>() {}
51 
52 protected:
53  void addElmt(const SignalBase<Time> *el) {
54  if (rank >= size) {
55  size += DEFAULT_SIZE;
56  const_array.resize(size);
57  }
58  const_array[rank++] = el;
59  }
60 
61 public:
62  virtual SignalArray_const<Time> &operator<<(const SignalBase<Time> &sig) {
63  addElmt(&sig);
64  return *this;
65  }
66 
67 public:
68  virtual const SignalBase<Time> &operator[](const unsigned int &idx) const {
69  return *const_array[idx];
70  }
71  virtual unsigned int getSize() const { return rank; }
72 };
73 
74 template <class Time>
75 SignalArray_const<Time> operator<<(const SignalBase<Time> &sig1,
76  const SignalBase<Time> &sig2) {
77  SignalArray_const<Time> res(sig1);
78  res << sig2;
79  return res;
80 }
81 
85 template <class Time> class SignalArray : public SignalArray_const<Time> {
86 public:
87  using SignalArray_const<Time>::DEFAULT_SIZE;
88  using SignalArray_const<Time>::size;
89  using SignalArray_const<Time>::rank;
90 
91 protected:
92  mutable std::vector<SignalBase<Time> *> array;
93 
94 public:
95  SignalArray<Time>(const unsigned int &sizeARG = DEFAULT_SIZE)
96  : SignalArray_const<Time>(0), array(sizeARG) {
97  size = sizeARG;
98  }
99 
100  SignalArray<Time>(SignalBase<Time> &sig)
101  : SignalArray_const<Time>(0), array(DEFAULT_SIZE) {
102  size = DEFAULT_SIZE;
103  addElmt(&sig);
104  }
105 
106  SignalArray<Time>(const SignalArray<Time> &siga)
107  : SignalArray_const<Time>(siga.getSize()), array(siga.getSize()) {
108  rank = siga.getSize();
109  for (unsigned int i = 0; i < rank; ++i)
110  array[i] = &siga[i];
111  }
112 
113  virtual ~SignalArray<Time>() {}
114 
115 protected:
116  void addElmt(SignalBase<Time> *el) {
117  if (rank >= size) {
118  size += DEFAULT_SIZE;
119  array.resize(size);
120  }
121  array[rank++] = el;
122  }
123 
124 public:
125  virtual SignalArray<Time> &operator<<(SignalBase<Time> &sig) {
126  addElmt(&sig);
127  return *this;
128  }
129 
130  virtual SignalArray_const<Time>
131  operator<<(const SignalBase<Time> &sig) const {
132  SignalArray_const<Time> res(*this);
133  res << sig;
134  return res;
135  }
136 
137  virtual SignalBase<Time> &operator[](const unsigned int &idx) const {
138  return *array[idx];
139  }
140 };
141 
142 template <class Time>
143 SignalArray<Time> operator<<(SignalBase<Time> &sig1, SignalBase<Time> &sig2) {
144  SignalArray<Time> res(sig1);
145  res << sig2;
146  return res;
147 }
148 
149 DYNAMIC_GRAPH_DLLAPI extern SignalArray<int> sotNOSIGNAL;
150 
151 } // end of namespace dynamicgraph.
152 
153 #endif