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