iCub-main
BoardList.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 RobotCub Consortium
3  * Author: Alessandro Scalzo alessandro.scalzo@iit.it
4  * CopyPolicy: Released under the terms of the GNU GPL v2.0.
5  *
6  */
7 
8 #ifndef __ETHUPDATER_BOARDLIST_H__
9 #define __ETHUPDATER_BOARDLIST_H__
10 
11 #include "BoardInfo.h"
12 
13 #include <vector>
14 using namespace std;
15 
16 class BoardList
17 {
18 public:
20  {
21  mnBoards=0;
22 
23  for (int i=0; i<256; ++i)
24  {
25  mpBoards[i]=NULL;
26  }
27  }
28 
29  virtual ~BoardList()
30  {
31  empty();
32  }
33 
34  int size(){ return mnBoards; }
35 
37  {
38  int n=0;
39  for (int i=0; i<mnBoards; ++i)
40  {
41  if (mpBoards[i])
42  {
43  if (mpBoards[i]->mSelected) ++n;
44  }
45  }
46 
47  return n;
48  }
49 
50  void empty()
51  {
52  for (int i=0; i<mnBoards; ++i)
53  {
54  if (mpBoards[i])
55  {
56  delete mpBoards[i];
57  mpBoards[i]=NULL;
58  }
59  }
60 
61  mnBoards=0;
62  }
63 
64  bool addBoard(BoardInfo *pBoard)
65  {
66  if (mnBoards>=256) return false;
67 
68  mpBoards[mnBoards++]=pBoard;
69 
70  return true;
71  }
72 
73  vector<BoardInfo *> getBoards(ACE_UINT32 address)
74  {
75  vector<BoardInfo *> ret;
76  ret.resize(0);
77 
78  for(int i=0; i<mnBoards; i++)
79  {
80  bool found = false;
81  if(0 == address)
82  { // searching for the selected boards
83  if(true == mpBoards[i]->mSelected)
84  {
85  found = true;
86  }
87  }
88  else if(mpBoards[i]->mAddress == address)
89  { // searching for a given ip address
90  found = true;
91  }
92 
93  if(true == found)
94  {
95  ret.push_back(mpBoards[i]);
96  }
97  }
98 
99  return ret;
100  }
101 
102 
103  bool replaceBoard(BoardInfo *pBoard)
104  {
105  // search for the board with the same mac
106 
107  for(int i=0; i<mnBoards; i++)
108  {
109  if(mpBoards[i]->mMac == pBoard->mMac)
110  {
111  // found ...
112  delete mpBoards[i];
113  mpBoards[i] = pBoard;
114  return true;
115  }
116  }
117 
118  delete pBoard;
119 
120  return true;
121  }
122 
123  void selectAll(bool sel)
124  {
125  for (int i=0; i<mnBoards; ++i)
126  {
127  if (mpBoards[i])
128  {
129  mpBoards[i]->mSelected=sel;
130  }
131  }
132  }
133 
134  BoardInfo& operator[](int i){ return *mpBoards[i]; }
135 
136 protected:
137  int mnBoards;
138  BoardInfo *mpBoards[256];
139 };
140 
141 #endif
ACE_UINT64 mMac
Definition: BoardInfo.h:113
void empty()
Definition: BoardList.h:50
bool addBoard(BoardInfo *pBoard)
Definition: BoardList.h:64
virtual ~BoardList()
Definition: BoardList.h:29
int numSelected()
Definition: BoardList.h:36
BoardList()
Definition: BoardList.h:19
bool replaceBoard(BoardInfo *pBoard)
Definition: BoardList.h:103
vector< BoardInfo * > getBoards(ACE_UINT32 address)
Definition: BoardList.h:73
void selectAll(bool sel)
Definition: BoardList.h:123
BoardInfo & operator[](int i)
Definition: BoardList.h:134
int size()
Definition: BoardList.h:34
int mnBoards
Definition: BoardList.h:137
int n