SimplePluginManager.cpp 4.31 KB
Newer Older
Thomas Moulard's avatar
Thomas Moulard committed
1
/*
2
 * Copyright 2005, 2006, 2007, 2008, 2009, 2010,
Thomas Moulard's avatar
Thomas Moulard committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * Paul       Evrard
 * Andrei     Herdt
 * Francois   Keith
 * Nicolas    Perrin
 * Olivier    Stasse
 *
 * JRL, CNRS/AIST
 *
 * This file is part of walkGenJrl.
 * walkGenJrl is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * walkGenJrl is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License
 * along with walkGenJrl.  If not, see <http://www.gnu.org/licenses/>.
 *
25
 *  Research carried out within the scope of the
Thomas Moulard's avatar
Thomas Moulard committed
26
27
28
 *  Joint Japanese-French Robotics Laboratory (JRL)
 */

29
30
/*! Abstract class to handle generic calls for the
  Pattern Generator Interface.*/
Thomas Moulard's avatar
Thomas Moulard committed
31
32
33
34

#include <iostream>

using namespace std;
35
#include <Debug.hh>
36
37
#include <SimplePlugin.hh>
#include <SimplePluginManager.hh>
Thomas Moulard's avatar
Thomas Moulard committed
38
39
40
41
#include <assert.h>

using namespace PatternGeneratorJRL;

42
SimplePluginManager::SimplePluginManager() { RESETDEBUG5("PgDebug.txt"); }
Thomas Moulard's avatar
Thomas Moulard committed
43

44
SimplePluginManager::~SimplePluginManager() {
45
  std::multimap<std::string, SimplePlugin *, ltstr>::iterator it_SP;
46

47
48
49
50
51
  it_SP = m_SimplePlugins.begin();
  while (it_SP != m_SimplePlugins.end()) {
    it_SP->second->m_SimplePluginManager = 0;
    it_SP++;
  }
Thomas Moulard's avatar
Thomas Moulard committed
52
53
}

54
55
56
void SimplePluginManager::UnregisterPlugin(SimplePlugin *aSimplePlugin) {
  std::multimap<std::string, SimplePlugin *, ltstr>::iterator it_SP,
      it_ToBeRemoved;
57

58
59
  it_SP = m_SimplePlugins.begin();
  while (it_SP != m_SimplePlugins.end()) {
60

61
62
63
    if (it_SP->second == aSimplePlugin) {
      it_ToBeRemoved = it_SP;
      it_SP++;
Thomas Moulard's avatar
Thomas Moulard committed
64

65
66
      m_SimplePlugins.erase(it_ToBeRemoved);
    } else
Thomas Moulard's avatar
Thomas Moulard committed
67
      it_SP++;
68
  }
Thomas Moulard's avatar
Thomas Moulard committed
69
70
}

71
72
73
74
75
76
77
78
79
void SimplePluginManager::Print() {
  std::multimap<std::string, SimplePlugin *, ltstr>::iterator it_SP;

  it_SP = m_SimplePlugins.begin();
  while (it_SP != m_SimplePlugins.end()) {
    cout << it_SP->first << endl;
    it_SP++;
  }
}
Thomas Moulard's avatar
Thomas Moulard committed
80
81
82

/*! \name Register the method for which this object can be called
  by a higher parser. */
83
84
bool SimplePluginManager::RegisterMethod(string &MethodName,
                                         SimplePlugin *aSP) {
Thomas Moulard's avatar
Thomas Moulard committed
85
86
87
  /* We have to copy the name, because we don't know
     how is handle the memory towards MethodName.c_str() */

88
  m_SimplePlugins.insert(pair<string, SimplePlugin *>(MethodName, aSP));
Thomas Moulard's avatar
Thomas Moulard committed
89

90
91
  ODEBUG5("Registered method " << MethodName << " for plugin " << aSP << endl,
          "PgDebug.txt");
Thomas Moulard's avatar
Thomas Moulard committed
92
  //  Print();
93
  return true;
Thomas Moulard's avatar
Thomas Moulard committed
94
95
96
}

/*! \name Call the method from the Method name. */
97
98
99
100
bool SimplePluginManager::CallMethod(string &MethodName, istringstream &istrm) {
  pair<std::multimap<std::string, SimplePlugin *, ltstr>::iterator,
       std::multimap<std::string, SimplePlugin *, ltstr>::iterator>
      RangeOfPlugins = m_SimplePlugins.equal_range(MethodName);
101

102
  std::multimap<std::string, SimplePlugin *, ltstr>::iterator CurrentPlugin;
Thomas Moulard's avatar
Thomas Moulard committed
103

104
  unsigned int NbPlugins = 0;
Thomas Moulard's avatar
Thomas Moulard committed
105
  for (CurrentPlugin = RangeOfPlugins.first;
106
107
       CurrentPlugin != RangeOfPlugins.second; ++NbPlugins, ++CurrentPlugin)
    ;
108

109
110
111
  ODEBUG5("Size of SimplePlugins: " << m_SimplePlugins.size() << " Found for "
                                    << MethodName << " : " << NbPlugins,
          "PgDebug.txt");
Thomas Moulard's avatar
Thomas Moulard committed
112
113
114
115
116
  bool FoundAPlugin = false;

  stringbuf *pbuf;
  pbuf = istrm.rdbuf();

Olivier Stasse's avatar
Olivier Stasse committed
117
  std::streamsize  size = pbuf->in_avail();
Thomas Moulard's avatar
Thomas Moulard committed
118
119
120
  char aBuffer[65636];
  assert(size < 65635);

121
  memset(aBuffer, 0, size + 1);
Olivier Stasse's avatar
Olivier Stasse committed
122
  for (std::streamsize i = 0; i < size; i++)
Thomas Moulard's avatar
Thomas Moulard committed
123
    aBuffer[i] = (char)pbuf->sbumpc();
124
  ODEBUG5(aBuffer, "PgDebug.txt");
125

126
  pbuf->pubsetbuf(aBuffer, size);
Thomas Moulard's avatar
Thomas Moulard committed
127
128

  for (CurrentPlugin = RangeOfPlugins.first;
129
130
131
132
133
134
135
136
137
138
139
       CurrentPlugin != RangeOfPlugins.second; ++CurrentPlugin) {
    istringstream iss(aBuffer);
    SimplePlugin *aSP = CurrentPlugin->second;
    ODEBUG5("Found the method " << MethodName << " for plugin :" << aSP << endl
                                << " Buffer: " << aBuffer,
            "PgDebug.txt");
    if (aSP != 0) {
      aSP->CallMethod(MethodName, iss);
      FoundAPlugin = true;
    } else {
      cout << "SimplePlugin empty " << endl;
Thomas Moulard's avatar
Thomas Moulard committed
140
    }
141
  }
Thomas Moulard's avatar
Thomas Moulard committed
142
143
144

  return FoundAPlugin;
}