Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • cberge/dynamic-graph
  • ostasse/dynamic-graph
  • gsaurel/dynamic-graph
  • stack-of-tasks/dynamic-graph
4 results
Show changes
Showing
with 1007 additions and 3444 deletions
......@@ -5,265 +5,210 @@
*
* CNRS/AIST
*
* This file is part of dynamic-graph.
* dynamic-graph 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.
* dynamic-graph 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 Lesser General Public License for more details. You should
* have received a copy of the GNU Lesser General Public License along
* with dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
*/
/*! Local framework includes */
#include <dynamic-graph/command.h>
#include <dynamic-graph/debug.h>
#include <dynamic-graph/entity.h>
#include <dynamic-graph/pool.h>
#include <dynamic-graph/pool.h>
#include <dynamic-graph/debug.h>
/*! System includes */
#include <stdlib.h>
#include <sstream>
#include <sstream>
using namespace std;
using namespace dynamicgraph;
using dynamicgraph::command::Command;
const std::string Entity::CLASS_NAME = "Entity";
void Entity::
entityRegistration( void )
{
//sotPool.entity[name] = this;
g_pool.registerEntity(name,this);
void Entity::entityRegistration() {
PoolStorage::getInstance()->registerEntity(name, this);
}
void Entity::
entityDeregistration( void )
{
g_pool.deregisterEntity(name);
void Entity::entityDeregistration() {
PoolStorage::getInstance()->deregisterEntity(name);
}
Entity::
Entity( const string& name__ )
: name(name__)
{
dgDEBUG(15) << "New entity <"<<name__<<">"<<endl;
if( name.length()==0 )
{
stringstream oss; oss << rand();
name = CLASS_NAME;
name+="::";
name+=oss.str();
}
Entity::Entity(const string &name__) : name(name__) {
dgDEBUG(15) << "New entity <" << name__ << ">" << endl;
if (name.length() == 0) {
stringstream oss;
oss << rand();
// name = this->getClassName();
// Cannot call a virtual function from the constructor
name += "::";
name += oss.str();
}
entityRegistration();
}
Entity::
~Entity( void )
{
Entity::~Entity() {
dgDEBUG(25) << "# In (" << name << " { " << endl;
entityDeregistration();
for (std::map<const std::string, Command *>::iterator it = commandMap.begin();
it != commandMap.end(); ++it) {
delete it->second;
}
dgDEBUGOUT(25);
}
/* -------------------------------------------------------------------------- */
/* --- SIGNALS -------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void Entity::
signalRegistration( const SignalArray<int>& signals )
{
for( unsigned int i=0;i<signals.getSize();++i )
{
SignalBase<int>& sig = signals[i];
//const string& signame = sig.getName();
istringstream iss( sig.getName() );
const int SIZE = 128;
char buffer[SIZE];
while( iss.good() )
{ iss.getline(buffer,SIZE,':'); }
const string& signame( buffer );
SignalMap::iterator sigkey = signalMap.find(signame);
if( sigkey != signalMap.end() ) // key does exist
{
dgERRORF( "Key %s already exist in the signalMap.",signame.c_str() );
if( sigkey->second!=&sig )
{
throw ExceptionFactory( ExceptionFactory::SIGNAL_CONFLICT,
"Another signal already defined with the same name. ",
"Signame is <%s>.",signame.c_str() );
}
}
else
{
dgDEBUG(10) << "Register signal <"<< signame << "> for entity <"
<< getName() << "> ."<<endl;
signalMap[signame] = &sig;
}
void Entity::signalRegistration(const SignalArray<int> &signals) {
for (unsigned int i = 0; i < signals.getSize(); ++i) {
SignalBase<int> &sig = signals[i];
// const string& signame = sig.getName ();
istringstream iss(sig.getName());
const int SIZE = 128;
char buffer[SIZE];
while (iss.good()) {
iss.getline(buffer, SIZE, ':');
}
}
const string &signame(buffer);
void Entity::
signalDeregistration( const std::string& signame )
{
SignalMap::iterator sigkey = signalMap.find(signame);
if( sigkey == signalMap.end() ) // key does not exist
{
dgERRORF( "Key %s does not exist in the signalMap.",signame.c_str() );
throw ExceptionFactory( ExceptionFactory::UNREFERED_SIGNAL,
"No signal defined with the given name. ",
" (while erasing <%s>).",signame.c_str() );
}
else
SignalMap::iterator sigkey = signalMap.find(signame);
if (sigkey != signalMap.end()) // key does exist
{
dgDEBUG(10) << "Deregister signal <"<< signame << "> for entity <"
<< getName() << "> ."<<endl;
signalMap.erase(signame);
dgERRORF("Key %s already exist in the signalMap.", signame.c_str());
if (sigkey->second != &sig) {
throw ExceptionFactory(
ExceptionFactory::SIGNAL_CONFLICT,
"Another signal already defined with the same name. ",
"Signame is <%s>.", signame.c_str());
}
} else {
dgDEBUG(10) << "Register signal <" << signame << "> for entity <"
<< getName() << "> ." << endl;
signalMap[signame] = &sig;
}
}
}
void Entity::signalDeregistration(const std::string &signame) {
SignalMap::iterator sigkey = signalMap.find(signame);
if (sigkey == signalMap.end()) // key does not exist
{
dgERRORF("Key %s does not exist in the signalMap.", signame.c_str());
throw ExceptionFactory(ExceptionFactory::UNREFERED_SIGNAL,
"No signal defined with the given name. ",
" (while erasing <%s>).", signame.c_str());
} else {
dgDEBUG(10) << "Deregister signal <" << signame << "> for entity <"
<< getName() << "> ." << endl;
signalMap.erase(signame);
}
}
#define __DG_ENTITY_GET_SIGNAL__(ITER_TYPE) \
SignalMap::ITER_TYPE sigkey = signalMap.find(signame); \
if( sigkey == signalMap.end() ) /* key does NOT exist */ \
{ \
throw ExceptionFactory( ExceptionFactory::UNREFERED_SIGNAL,\
"The requested signal is not registered",\
": %s",signame.c_str() );\
}\
return *(sigkey ->second) ;
std::string Entity::getDocString() const {
std::string docString("No header documentation.");
return docString;
}
#define __DG_ENTITY_GET_SIGNAL__(ITER_TYPE) \
SignalMap::ITER_TYPE sigkey = signalMap.find(signame); \
if (sigkey == signalMap.end()) /* key does NOT exist */ \
{ \
throw ExceptionFactory(ExceptionFactory::UNREFERED_SIGNAL, \
"The requested signal is not registered", ": %s", \
signame.c_str()); \
} \
return *(sigkey->second);
bool Entity::hasSignal(const string &signame) const {
return (!(signalMap.find(signame) == signalMap.end()));
}
SignalBase<int>& Entity::
getSignal( const string & signame )
{
SignalBase<int> &Entity::getSignal(const string &signame) {
__DG_ENTITY_GET_SIGNAL__(iterator);
}
const SignalBase<int>& Entity::
getSignal( const string & signame ) const
{
const SignalBase<int> &Entity::getSignal(const string &signame) const {
__DG_ENTITY_GET_SIGNAL__(const_iterator);
}
std::ostream& Entity::
displaySignalList( std::ostream& os ) const
{
os << "--- <" << getName() << "> signal list: "<<endl;
const SignalMap::const_iterator iterend=signalMap.end();
for( SignalMap::const_iterator iter = signalMap.begin();iterend!=iter;++iter )
{
os << " "; if( (++iter)--==iterend ) os << "`"; else os <<"|";
os << "-- <" << *(iter->second) << endl;
}
std::ostream &Entity::displaySignalList(std::ostream &os) const {
os << "--- <" << getName() << "> signal list: " << endl;
const SignalMap::const_iterator iterend = signalMap.end();
for (SignalMap::const_iterator iter = signalMap.begin(); iterend != iter;
++iter) {
os << " ";
if ((++iter)-- == iterend)
os << "`";
else
os << "|";
os << "-- <" << *(iter->second) << endl;
}
return os;
}
std::ostream& Entity::
writeGraph( std::ostream& os ) const
{
const SignalMap::const_iterator iterend=signalMap.end();
for( SignalMap::const_iterator iter = signalMap.begin();iterend!=iter;++iter )
{
(*(iter->second)).writeGraph(os);
}
std::ostream &Entity::writeGraph(std::ostream &os) const {
const SignalMap::const_iterator iterend = signalMap.end();
for (SignalMap::const_iterator iter = signalMap.begin(); iterend != iter;
++iter) {
(*(iter->second)).writeGraph(os);
}
return os;
}
std::ostream& Entity::
writeCompletionList( std::ostream& os ) const
{
const SignalMap::const_iterator iterend=signalMap.end();
for( SignalMap::const_iterator iter = signalMap.begin();iterend!=iter;++iter )
{
os << getName() << "." << (*(iter->second)).shortName() << std::endl;
}
std::ostream &Entity::writeCompletionList(std::ostream &os) const {
const SignalMap::const_iterator iterend = signalMap.end();
for (SignalMap::const_iterator iter = signalMap.begin(); iterend != iter;
++iter) {
os << getName() << "." << (*(iter->second)).shortName() << std::endl;
}
os << getCommandList() << std::endl;
return os;
}
void Entity::
display( std::ostream& os ) const
{
os<<this->getClassName()<<": "<<name;
void Entity::display(std::ostream &os) const {
os << this->getClassName() << ": " << name;
}
std::ostream& dynamicgraph::operator<< (std::ostream& os, const Entity& ent )
{
std::ostream &dynamicgraph::operator<<(std::ostream &os, const Entity &ent) {
ent.display(os);
return os;
}
Entity::SignalMap Entity::getSignalMap() const { return signalMap; }
/* --- PARAMS --------------------------------------------------------------- */
/* --- PARAMS --------------------------------------------------------------- */
/* --- PARAMS --------------------------------------------------------------- */
static std::string Entity_COMMAND_LIST = "print\nsignals\nsignalDep";
const std::string& Entity::
getCommandList( void ) const
{
const std::string &Entity::getCommandList() const {
return Entity_COMMAND_LIST;
}
void Entity::
commandLine( const std::string& cmdLine,std::istringstream& cmdArgs,std::ostream& os )
{
if( cmdLine == "help" )
{
os << "Entity : " << std::endl
<< " - print\t\t\tWhat d'you think?"<<endl
<< " - signals\t\t\tDisplay the signals list."<<endl
<< " - signalDep <signame> \tDisplay the dependency graph for signal signame."<<endl;
}
else if( cmdLine == "print")
{
os << *this << std::endl;
}
else if( cmdLine == "signals")
{ displaySignalList(os); }
else if( cmdLine == "signalDep")
{
string sig; cmdArgs>>sig;
cmdArgs >> ws; int depth=-1;
if( cmdArgs.good() ) { cmdArgs >> depth; }
getSignal(sig) .displayDependencies( os,depth ); os<<endl;
}
else
{
try{
SignalBase<int> & sig = getSignal( cmdLine );
/// Add a command to Entity
void Entity::addCommand(const std::string &inName, Command *command) {
if (commandMap.count(inName) != 0) {
DG_THROW ExceptionFactory(
ExceptionFactory::OBJECT_CONFLICT,
"Command " + inName + " already registered in Entity.");
}
std::pair<const std::string, Command *> item(inName, command);
commandMap.insert(item);
}
int time; cmdArgs>>ws;
if( cmdArgs.good() )
{cmdArgs >> time;} else {time=0;}
sig.recompute( time );
/// Return the list of command objects
std::map<const std::string, Command *> Entity::getNewStyleCommandMap() {
return commandMap;
}
os << cmdLine << " = "; sig.get( os );
} catch( ExceptionFactory& e ) {
switch( e.getCode() )
{
case ExceptionFactory::UNREFERED_SIGNAL:
DG_THROW ExceptionFactory( ExceptionFactory::UNREFERED_FUNCTION,
"The requested function/signal :","<%s> is "
"not registered.",cmdLine.c_str() );
break;
default:
throw;
}
} catch( ... ) { throw; }
}
Command *Entity::getNewStyleCommand(const std::string &commandName) {
if (commandMap.count(commandName) != 1) {
DG_THROW ExceptionFactory(
ExceptionFactory::UNREFERED_FUNCTION,
"Command <" + commandName + "> is not registered in Entity.");
}
return commandMap[commandName];
}
void Entity::sendMsg(const std::string &msg, MsgType t,
const std::string &lineId) {
logger_.stream(t, lineId) << "[" << name << "]" << msg << '\n';
}
/*
* Copyright 2010,
* François Bleibel,
* Olivier Stasse,
*
* CNRS/AIST
*
* This file is part of dynamic-graph.
* dynamic-graph 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.
* dynamic-graph 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 Lesser General Public License for more details. You should
* have received a copy of the GNU Lesser General Public License along
* with dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
*/
/* --------------------------------------------------------------------- */
/* --- INCLUDE --------------------------------------------------------- */
/* --------------------------------------------------------------------- */
/* --- DYNAMIC-GRAPH --- */
#include <dynamic-graph/debug.h>
#include <dynamic-graph/factory.h>
// Copyright 2010, François Bleibel, Thomas Moulard, Olivier Stasse,
// JRL, CNRS/AIST.
//
#include "dynamic-graph/factory.h"
#include <boost/foreach.hpp>
#include "dynamic-graph/debug.h"
using namespace std;
using namespace dynamicgraph;
/* --------------------------------------------------------------------- */
/* --- CLASS ----------------------------------------------------------- */
/* --------------------------------------------------------------------- */
namespace dynamicgraph {
FactoryStorage *FactoryStorage::getInstance() {
if (instance_ == 0) {
instance_ = new FactoryStorage;
}
return instance_;
}
FactoryStorage::
~FactoryStorage( void )
{
dgDEBUGINOUT(25);
void FactoryStorage::destroy() {
delete instance_;
instance_ = NULL;
}
FactoryStorage::FactoryStorage() : entityMap() {}
FactoryStorage::~FactoryStorage() {
instance_ = 0;
dgDEBUGINOUT(25);
}
/* --------------------------------------------------------------------- */
void FactoryStorage::
registerEntity( const std::string& entname,FactoryStorage::EntityConstructor_ptr ent )
{
void FactoryStorage::registerEntity(const std::string &entname,
FactoryStorage::EntityConstructor_ptr ent) {
dgDEBUGIN(25);
EntityMap::iterator entkey;
if( existEntity(entname,entkey) ) // key does exist
{
// DG_THROW ExceptionFactory( ExceptionFactory::OBJECT_CONFLICT,
// "Another entity class already defined with the same name. ",
// "(while adding entity class <%s> inside the g_factory).",
// entname.c_str() );
dgERRORF("Another entity class already defined with the same name. "
"(while adding entity class <%s> inside the factory).",
entname.c_str() );
}
else
{
dgDEBUG(30) << "Register entity <"<< entname
<< "> in the factory." <<std::endl;
entityMap[entname] = ent;
if (existEntity(entname)) {
DG_THROW ExceptionFactory(
ExceptionFactory::OBJECT_CONFLICT,
"Another entity class already defined with the same name. ",
"(while adding entity class <%s> inside the factory).",
entname.c_str());
dgERRORF(
"Another entity class already defined with the same name. "
"(while adding entity class <%s> inside the factory).",
entname.c_str());
} else {
if (!ent) {
// FIXME: we should have a better error code for that.
DG_THROW ExceptionFactory(ExceptionFactory::OBJECT_CONFLICT,
"Bad entity constructor.");
}
dgDEBUG(30) << "Register entity <" << entname << "> in the factory."
<< std::endl;
entityMap[entname] = ent;
}
dgDEBUGOUT(25);
}
void FactoryStorage::
deregisterEntity( const std::string& entname )
{
void FactoryStorage::deregisterEntity(const std::string &entname) {
dgDEBUGIN(25);
EntityMap::iterator entkey;
if(! existEntity(entname,entkey) ) // key does not exist
{
DG_THROW ExceptionFactory( ExceptionFactory::OBJECT_CONFLICT,
"Entity class not defined yet. ",
"(while removing entity class <%s>).",
entname.c_str() );
dgERRORF(ExceptionFactory::OBJECT_CONFLICT,
"Entity class not defined yet. "
"(while removing entity class <%s>).",
entname.c_str() );
}
else
{
dgDEBUG(30) << "Deregister entity <"<< entname
<< "> from the factory." <<std::endl;
entityMap.erase( entkey );
}
if (!existEntity(entname)) {
DG_THROW ExceptionFactory(
ExceptionFactory::OBJECT_CONFLICT, "Entity class not defined yet. ",
"(while removing entity class <%s>).", entname.c_str());
dgERRORF(ExceptionFactory::OBJECT_CONFLICT,
"Entity class not defined yet. "
"(while removing entity class <%s>).",
entname.c_str());
} else {
dgDEBUG(30) << "Deregister entity <" << entname << "> from the factory."
<< std::endl;
entityMap.erase(entname);
}
dgDEBUGOUT(25);
}
Entity* FactoryStorage::
newEntity( const std::string& classname,const std::string& objname )
{
dgDEBUG(15) << "New <"<<classname<<">Entity <"<<objname<<">"<<std::endl;
EntityMap::iterator entPtr;
if(! existEntity(classname,entPtr) )
{
DG_THROW ExceptionFactory( ExceptionFactory::UNREFERED_OBJECT,
"Unknown entity."," (while calling new_entity <%s>)",
classname.c_str() );
}
Entity *FactoryStorage::newEntity(const std::string &classname,
const std::string &objname) const {
dgDEBUG(15) << "New <" << classname << ">Entity <" << objname << ">"
<< std::endl;
EntityMap::const_iterator entPtr = entityMap.find(classname);
if (entPtr == entityMap.end()) {
DG_THROW ExceptionFactory(
ExceptionFactory::UNREFERED_OBJECT, "Unknown entity.",
" (while calling new_entity <%s>)", classname.c_str());
}
return entPtr->second(objname);
}
bool FactoryStorage::
existEntity( const std::string& name, EntityMap::iterator& entPtr )
{
entPtr = entityMap .find( name );
return ( entPtr != entityMap.end() );
}
bool FactoryStorage::
existEntity( const std::string& name )
{
EntityMap::iterator entPtr;return existEntity( name,entPtr );
// This checks efficiently if a key exists in an STL map using the
// approach suggested by Scott Meyer's Effective STL (item 24).
bool FactoryStorage::existEntity(const std::string &name) const {
EntityMap::const_iterator lb = entityMap.lower_bound(name);
return lb != entityMap.end() && !(entityMap.key_comp()(name, lb->first));
}
void FactoryStorage::
listEntities(std::vector <std::string>& outList)
{
for (EntityMap::iterator it = entityMap.begin();
it != entityMap.end(); it++) {
outList.push_back(it->first);
}
// FIXME: this should be removed at some point.
void FactoryStorage::listEntities(std::vector<std::string> &outList) const {
typedef std::pair<std::string, EntityConstructor_ptr> iter_t;
BOOST_FOREACH (const iter_t &entity, entityMap)
outList.push_back(entity.first);
}
/* --------------------------------------------------------------------- */
/* --- REGISTERERS ----------------------------------------------------- */
/* --------------------------------------------------------------------- */
EntityRegisterer::
EntityRegisterer( const std::string& entityClassName,
FactoryStorage::EntityConstructor_ptr maker)
:entityName( entityClassName )
{
EntityRegisterer::EntityRegisterer(const std::string &entityClassName,
FactoryStorage::EntityConstructor_ptr maker)
: entityName(entityClassName) {
dgDEBUGIN(15);
g_factory.registerEntity(entityClassName,maker);
FactoryStorage::getInstance()->registerEntity(entityClassName, maker);
dgDEBUGOUT(15);
}
EntityRegisterer::
~EntityRegisterer( void )
{
EntityRegisterer::~EntityRegisterer() {
dgDEBUGIN(15);
g_factory.deregisterEntity(entityName);
FactoryStorage::getInstance()->deregisterEntity(entityName);
dgDEBUGOUT(15);
}
/* --------------------------------------------------------------------- */
/* --- COMMAND LINE ---------------------------------------------------- */
/* --------------------------------------------------------------------- */
void FactoryStorage::
commandLine( const std::string& cmdLine,std::istringstream& cmdArgs,
std::ostream& os )
{
if( cmdLine == "help" )
{
os<< "factory ";
string cmd2; cmdArgs >> cmd2;
if(! cmdArgs.good())
os << " <arg>\t\t\t\taccess to the factory (help <arg> for more detail)" <<endl;
else if( cmd2 == "list" )
os << "list\t\t:List all available entities." << endl;
else if( cmd2 == "listEntities" )
os <<"listEntities\t:List available entities." << endl;
}
else if( cmdLine == "list" )
{
commandLine("listEntities",cmdArgs,os);
}
else if( cmdLine == "listEntities" )
{
os <<" List of available entities:" << endl;
for( EntityMap::iterator iter = entityMap.begin();iter!=entityMap.end();++iter )
{ os << " - " << iter->first << endl; }
}
return;
}
namespace dynamicgraph {
//! The global g_factory object.
FactoryStorage g_factory;
}
// The global factory.
FactoryStorage *FactoryStorage::instance_ = NULL;
} // end of namespace dynamicgraph.
/*
* Copyright 2010,
* François Bleibel,
* Olivier Stasse,
*
* CNRS/AIST
*
* This file is part of sot-core.
* sot-core 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.
* sot-core 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 Lesser General Public License for more details. You should
* have received a copy of the GNU Lesser General Public License along
* with sot-core. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cassert>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp>
#include <boost/foreach.hpp>
#include <boost/format.hpp>
#include <dynamic-graph/import-default-paths.h>
#include <dynamic-graph/import.h>
#include <dynamic-graph/debug.h>
#include <dynamic-graph/exception-abstract.h>
#include <dynamic-graph/exception-factory.h>
#include <dynamic-graph/interpreter.h>
// The default import paths has to be passed from the build system
// as a -D flag for instance.
// It contains the absolute path to the default directory where
// the scripts will be searched.
#ifndef DG_IMPORT_DEFAULT_PATHS
# error "Default import path is not defined."
#endif //! DG_IMPORT_DEFAULT_PATHS
static const char* ENV_DG_PATH = "DG_PATH";
namespace dynamicgraph
{
namespace command
{
namespace
{
/// Initialize import paths list (called during static initialization).
std::vector<std::string> initializePaths ();
/// \brief Import paths list.
///
/// This vector of string is similar to Unix variables such as
/// PATH. It contains all paths that are used to search when
/// importing a script.
///
/// The look-up is made from right to left:
///
/// importPaths = A:B:C
///
/// When typing ``import foo'', C will be searched first then B
/// and A. The search stops when the file is found.
std::vector<std::string> importPaths = initializePaths ();
/// Search for a module.
///
/// Returns the module full absolute path or an empty string if
/// it cannot be found.
std::string searchModule (const std::string& module);
/// \brief Remove quotes form a string.
///
/// Transform strings such as "foo" or 'foo' into foo.
void removeQuotes (std::string& msg);
std::vector<std::string> initializePaths ()
{
std::vector<std::string> importPaths;
importPaths.push_back (DG_IMPORT_DEFAULT_PATHS);
// Search for the environment variable value.
char* ScriptPath = getenv (ENV_DG_PATH);
if (!ScriptPath)
return importPaths;
// Split the environment variable.
std::string environmentSeparator;
// On Microsoft Windows, environment variables are splitted along
// the ``;'' character. It is ``:'' on *NIX systems.
#if defined _WIN32 || defined __CYGWIN__
environmentSeparator = ";";
#else
environmentSeparator = ":";
#endif // defined _WIN32 || defined __CYGWIN__
std::vector<std::string> splittedEnvironmentVariable;
boost::split (splittedEnvironmentVariable, ScriptPath,
boost::is_any_of (environmentSeparator));
// Insert it back.
std::back_insert_iterator<std::vector<std::string> > bi (importPaths);
std::copy (splittedEnvironmentVariable.begin (),
splittedEnvironmentVariable.end (), bi);
return importPaths;
}
std::string searchModule (const std::string& module)
{
// Make sure the traversal is right to left to enforce
// correct priorities.
typedef std::vector<std::string>::const_reverse_iterator citer_t;
for (citer_t it = importPaths.rbegin ();
it != importPaths.rend (); ++it)
{
const std::string& path = *it;
assert (!path.empty ());
std::string filename (path);
if (filename[filename.length () - 1] != '/')
filename += "/";
filename += module;
std::ifstream file (filename.c_str ());
if (file.is_open () && file.good ())
return filename;
}
return std::string ();
}
void removeQuotes (std::string& msg)
{
if ((msg[0] == '"' && msg[msg.length () - 1] == '"')
|| (msg[0] == '\'' && msg[msg.length () - 1] == '\''))
msg = msg.substr (1, msg.length () - 2);
}
} // end of anonymous namespace.
void import (dynamicgraph::Interpreter& interpreter,
const std::string& cmdLine,
std::istringstream& cmdArg,
std::ostream& os)
{
if (cmdLine == "help")
{
os << " - import <script.txt>\t\t\t\tImport the script."
<< std::endl
<< "\t\t\t\tBehaves like run but searches for files"
<< " in default script directories."
<< std::endl;
return;
}
dgDEBUGIN(15);
std::string module;
cmdArg >> module;
// Get rid of quotes.
removeQuotes (module);
std::string filename = searchModule (module);
std::ifstream file (filename.c_str ());
if (filename.empty () || !file.is_open () || !file.good ())
{
std::string scriptDirectories;
if (importPaths.empty ())
scriptDirectories = "empty";
else
{
BOOST_FOREACH (const std::string& path, importPaths)
{
scriptDirectories += path;
scriptDirectories += ", ";
}
scriptDirectories = scriptDirectories.substr
(0, scriptDirectories.length () - 2);
}
boost::format fmt
("failed to import module ``%1%'' (import paths: %2%).");
fmt % module;
fmt % scriptDirectories;
std::cout << fmt.str () << std::endl;
DG_THROW ExceptionFactory
(ExceptionFactory::READ_FILE, fmt.str ());
return;
}
int lineIdx = 0;
try
{
while (file.good ())
{
++lineIdx;
dgDEBUGIN (15);
std::string line;
std::getline (file, line);
if (line.empty ())
continue;
std::istringstream iss (line);
std::string currentCmdName;
std::string currentCmdArgs;
if (iss >> currentCmdName)
{
std::getline (iss, currentCmdArgs);
boost::format fmt ("Run ``%1%'' with args ``%2%''");
fmt % currentCmdName % currentCmdArgs;
dgDEBUG(25) << fmt.str () << std::endl;
std::istringstream issArgs (currentCmdArgs);
interpreter.cmd (currentCmdName, issArgs, os);
}
dgDEBUGOUT (15);
}
}
catch (ExceptionAbstract& exc)
{
// FIXME: come on...
std::string& msg = const_cast<std::string&> (exc.getStringMessage ());
boost::format fmt (" (in line %1% of file ``%2%'')");
fmt % lineIdx % filename;
msg = msg + fmt.str();
std::cout << msg << std::endl;
throw;
}
dgDEBUGOUT(15);
}
void pushImportPaths (dynamicgraph::Interpreter& interpreter,
const std::string& cmdLine,
std::istringstream& cmdArg,
std::ostream& os)
{
if (cmdLine == "help")
{
os << " - pushImportPaths <path>\t\t\t\tAdd path to default directories."
<< std::endl;
return;
}
std::string path;
cmdArg >> path;
removeQuotes (path);
importPaths.push_back (path);
}
void popImportPaths (dynamicgraph::Interpreter& interpreter,
const std::string& cmdLine,
std::istringstream& cmdArg,
std::ostream& os)
{
if (cmdLine == "help")
{
os << " - popImportPaths <path>\t\t\t\tDrop path from default directories."
<< std::endl;
return;
}
if (!importPaths.empty ())
importPaths.pop_back ();
}
} // end of namespace command.
} // end of namespace sot.
/*
* Copyright 2010,
* François Bleibel,
* Olivier Stasse,
*
* CNRS/AIST
*
* This file is part of dynamic-graph.
* dynamic-graph 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.
* dynamic-graph 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 Lesser General Public License for more details. You should
* have received a copy of the GNU Lesser General Public License along
* with dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
*/
/* --------------------------------------------------------------------- */
/* --- INCLUDE --------------------------------------------------------- */
/* --------------------------------------------------------------------- */
/* DYNAMIC-GRAPH */
#include <dynamic-graph/interpreter-helper.h>
#include <dynamic-graph/plugin-loader.h>
#include <dynamic-graph/debug.h>
/* --- STD --- */
using namespace std;
using namespace dynamicgraph;
/* --------------------------------------------------------------------- */
/* --- CLASS ----------------------------------------------------------- */
/* --------------------------------------------------------------------- */
InterpreterHelper::InterpreterHelper()
{
}
/* --------------------------------------------------------------------- */
/* --- NEW ------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
#include <dynamic-graph/factory.h>
using namespace std;
#include <dynamic-graph/entity.h>
#include <dynamic-graph/signal-base.h>
void InterpreterHelper::
cmdPlug( const std::string& obj1, const std::string & signame1,
const std::string& obj2, const std::string & signame2,
std::ostream& os )
{
dgDEBUG(20) << "Get Ent1 <"<<obj1<<"> ."<<endl;
Entity& ent1 = g_pool.getEntity(obj1);
dgDEBUG(20) << "Get Sig1 <"<<signame1<<"> ."<<endl;
SignalBase<int> &sig1 = ent1.getSignal(signame1);
dgDEBUG(20) << "Get Ent2 <"<<obj2<<"> ."<<endl;
Entity& ent2 = g_pool.getEntity(obj2);
dgDEBUG(20) << "Get Sig2 <"<<signame2<<"> ."<<endl;
SignalBase<int> &sig2 = ent2.getSignal(signame2);
dgDEBUG(25) << "Plug..."<<endl;
sig2.plug(&sig1);
}
void InterpreterHelper::
cmdNew( const std::string& className,
const std::string& objName,
std::ostream& os )
{
dgDEBUG(15) << "New <" << className<<"> requested."<<endl;
if( g_factory.existEntity( className ) )
{
dgDEBUG(15) << "New entity<"<<className<<"> " <<objName<<std::endl;
g_factory.newEntity(className,objName);
}
else os << " !! Class <" << className << "> does not exist."<<endl;
}
void InterpreterHelper::
cmdDestroy( const std::string& objName,
std::ostream& os)
{
dgDEBUG(15) << "Destroy <" << objName <<"> requested."<<endl;
delete &( g_pool.getEntity( objName ) );
}
void InterpreterHelper::
cmdLoadPlugin( const std::string& directory,
const std::string& pluginName,
std::ostream& os )
{
if( directory.length() != 0 ) dlPtr.setDirectory( directory );
dlPtr.addPlugin( pluginName );
try{
dgDEBUG(15) << "Try to load " << pluginName<< endl;
dlPtr.loadPlugins();
}catch( ExceptionAbstract& e ) { dgDEBUG(5) << e << endl; throw e; }
}
void InterpreterHelper::
cmdUnloadPlugin( const std::string& pluginName,
std::ostream& os )
{
dgDEBUGIN(15);
try{
dgDEBUG(25) << "Try short name " << pluginName << endl;
const std::string& fullname = dlPtr.searchPlugin(pluginName);
dgDEBUG(25) << "Full name " << fullname << endl;
dlPtr.unloadPlugin(fullname);
}
catch(...)
{
dgDEBUG(25) << "Full name " << pluginName << endl;
dlPtr.unloadPlugin(pluginName);
}
dgDEBUGOUT(15);
}
void InterpreterHelper::
cmdSetSignal( const std::string& objname,
const std::string& signame,
const std::string& value,
std::ostream& os )
{
dgDEBUGIN(15);
Entity& obj = g_pool.getEntity(objname);
SignalBase<int>& sig = obj.getSignal( signame );
istringstream cmdArg(value);
sig.set( cmdArg );
dgDEBUGOUT(15);
}
void InterpreterHelper::
cmdGetSignal( const std::string& objname,
const std::string& signame,
std::ostream& os )
{
dgDEBUGIN(15);
Entity& obj = g_pool.getEntity(objname);
SignalBase<int>& sig = obj.getSignal( signame );
os << signame << " = "; sig.get( os );
dgDEBUGOUT(15);
}
void InterpreterHelper::
cmdComputeSignal( const std::string& objname,
const std::string& signame,
const int &time,
std::ostream& os )
{
dgDEBUGIN(15);
Entity& obj = g_pool.getEntity(objname);
SignalBase<int>& sig = obj.getSignal( signame );
sig.recompute( time );
dgDEBUGOUT(15);
}
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
/*
* Copyright 2010,
* François Bleibel,
* Olivier Stasse,
*
* CNRS/AIST
*
* This file is part of dynamic-graph.
* dynamic-graph 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.
* dynamic-graph 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 Lesser General Public License for more details. You should
* have received a copy of the GNU Lesser General Public License along
* with dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
*/
/* --------------------------------------------------------------------- */
/* --- INCLUDE --------------------------------------------------------- */
/* --------------------------------------------------------------------- */
/* DYNAMIC-GRAPH */
#include <dynamic-graph/interpreter.h>
#include <dynamic-graph/plugin-loader.h>
#include <dynamic-graph/debug.h>
#include <dynamic-graph/import.h>
/* --- STD --- */
using namespace std;
using namespace dynamicgraph;
/* --------------------------------------------------------------------- */
/* --- CLASS ----------------------------------------------------------- */
/* --------------------------------------------------------------------- */
const std::string Interpreter::PROMPT_DEFAULT = "> ";
Interpreter::
Interpreter( PluginLoader* dl__ )
: dlPtr(dl__),initDone(false)//,prompt( PROMPT_DEFAULT )
{
registerFunction("plug",boost::bind(&Interpreter::cmdPlug,this,_1,_2,_3));
registerFunction("new",boost::bind(&Interpreter::cmdNew,this,_1,_2,_3));
registerFunction("destroy",boost::bind(&Interpreter::cmdDestroy,this,_1,_2,_3));
registerFunction("run",boost::bind(&Interpreter::cmdRun,this,_1,_2,_3));
registerFunction("loadPlugin",boost::bind(&Interpreter::cmdLoadPlugin,this,_1,_2,_3));
registerFunction("unloadPlugin",boost::bind(&Interpreter::cmdUnloadPlugin,this,_1,_2,_3));
registerFunction("help",boost::bind(&Interpreter::cmdHelp,this,_1,_2,_3));
registerFunction("set",boost::bind(&Interpreter::cmdSetSignal,this,_1,_2,_3));
registerFunction("get",boost::bind(&Interpreter::cmdGetSignal,this,_1,_2,_3));
registerFunction("compute",boost::bind(&Interpreter::cmdComputeSignal,this,_1,_2,_3));
registerFunction("import",boost::bind(&dynamicgraph::command::import,boost::ref(*this),_1,_2,_3));
registerFunction("pushImportPaths",boost::bind(&dynamicgraph::command::pushImportPaths,boost::ref(*this),_1,_2,_3));
registerFunction("popImportPaths",boost::bind(&dynamicgraph::command::popImportPaths,boost::ref(*this),_1,_2,_3));
prompt = PROMPT_DEFAULT;
initDone = true;
}
void Interpreter::
registerFunction( const string& funname,
const Interpreter::ShellBasicFunction& fun )
{
if( initDone ) {dgDEBUG(15) << "Register " << funname << std::endl;}
FunctionMap::iterator funkey = functionMap.find(funname);
if( funkey != functionMap.end() ) // key does exist
{
if( initDone )
{
dgDEBUG(15) << "!! Another function already defined with the same name. Overloading "
<< "Funname is" <<funname.c_str() << endl;
throw ExceptionFactory( ExceptionFactory::FUNCTION_CONFLICT,
"Another function already defined with the same name. ",
"Funname is <%s>.",funname.c_str() );
}
}
else
{
// dgDEBUG(10) << "Register function <"<< funname
// << "> in the shell interpretor." <<endl;
functionMap[funname] = fun;
}
}
bool Interpreter::
deregisterFunction( const std::string& funname )
{
return ( 0!=functionMap.erase(funname) );
}
/* --------------------------------------------------------------------- */
/* --- NEW ------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
#include <dynamic-graph/factory.h>
using namespace std;
#include <dynamic-graph/entity.h>
#include <dynamic-graph/signal-base.h>
void Interpreter::
cmdPlug( const std::string& cmdLine, istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - plug <obj1.sig1> <obj2.sig2>"
<< "\t\tPlug sig1 (producer) on sig2 (consumer)." <<endl;
return;
}
string ssig1,ssig2;
cmdArg>>ssig1>>ssig2;
string obj1,fun1;
string obj2,fun2;
istringstream str1(ssig1),str2(ssig2);
if( (!objectNameParser(str1,obj1,fun1))||(!objectNameParser(str2,obj2,fun2)) )
{
DG_THROW ExceptionFactory( ExceptionFactory::SYNTAX_ERROR,
"Plug function: syntax is plug OBJ1.SIG1 OBJ2.SIG2.",
"(while calling plug %s %s).",ssig1.c_str(),
ssig2.c_str() );
}
dgDEBUG(20) << "Get Ent1 <"<<obj1<<"> ."<<endl;
Entity& ent1 = g_pool.getEntity(obj1);
dgDEBUG(20) << "Get Sig1 <"<<fun1<<"> ."<<endl;
SignalBase<int> &sig1 = ent1.getSignal(fun1);
dgDEBUG(20) << "Get Ent2 <"<<obj2<<"> ."<<endl;
Entity& ent2 = g_pool.getEntity(obj2);
dgDEBUG(20) << "Get Sig2 <"<<fun2<<"> ."<<endl;
SignalBase<int> &sig2 = ent2.getSignal(fun2);
dgDEBUG(25) << "Plug..."<<endl;
sig2.plug(&sig1);
}
void Interpreter::
cmdNew( const std::string& cmdLine, istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - new <class> <object>"
<< "\t\t\tCreate a new entity." <<endl;
return;
}
string className;
string objName;
cmdArg >> className >>objName;
dgDEBUG(15) << "New <" << className<<"> requested."<<endl;
if( g_factory.existEntity( className ) )
{
dgDEBUG(15) << "New entity<"<<className<<"> " <<objName<<std::endl;
g_factory.newEntity(className,objName);
}
else os << " !! Class <" << className << "> does not exist."<<endl;
}
void Interpreter::
cmdDestroy( const std::string& cmdLine, istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - destroy <object>"
<< "\t\t\tDestroy an object." <<endl;
return;
}
string objName; cmdArg >> objName;
dgDEBUG(15) << "Destroy <" << objName <<"> requested."<<endl;
delete &( g_pool.getEntity( objName ) );
}
void Interpreter::
cmdLoadPlugin( const std::string& cmdLine, std::istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - loadPlugin <file.so> <directory>"
<< "\t\tLoad the plugin from the specified directory." <<endl;
return;
}
if( NULL!=dlPtr )
{
string pluginName,directory;
cmdArg >> pluginName;
cmdArg >> directory;
if( directory.length() != 0 ) dlPtr->setDirectory( directory );
dlPtr ->addPlugin( pluginName );
try{
dgDEBUG(15) << "Try to load " << pluginName<< endl;
dgDEBUG(25)<<"sotShell.dlPtr ="<< this->dlPtr <<endl;
dlPtr->loadPlugins();
dgDEBUG(25)<<"sotShell.dlPtr ="<< this->dlPtr <<endl;
}catch( ExceptionAbstract& e ) { dgDEBUG(5) << "ExceptionAbstract " << e << endl; throw e; }
}
else { os << "!! Dynamic loading functionalities not accessible through the shell." <<endl; }
}
void Interpreter::
cmdUnloadPlugin( const std::string& cmdLine, std::istringstream& cmdArg,
std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - unloadPlugin <path/file.so>"
<< "\t\tUnload the plugin." <<endl;
return;
}
dgDEBUGIN(15);
if( NULL!=dlPtr )
{
string pluginName;
cmdArg >> pluginName;
try{
dgDEBUG(25) << "Try short name " << pluginName << endl;
const std::string& fullname = dlPtr->searchPlugin(pluginName);
dgDEBUG(25) << "Full name " << fullname << endl;
dlPtr->unloadPlugin(fullname);
}
catch(...)
{
dgDEBUG(25) << "Full name " << pluginName << endl;
dlPtr->unloadPlugin(pluginName);
}
}
else { os << "!! Dynamic loading functionalities not accessible through the shell." <<endl; }
dgDEBUGOUT(15);
}
void Interpreter::
cmdHelp( const std::string& cmdLine, std::istringstream& cmdArg, std::ostream& os )
{
dgDEBUGIN(25);
os << "Help" <<endl;
std::string procname; bool personalizedHelp = false;
cmdArg >> ws;
if( cmdArg.good() )
{
const unsigned int gc = cmdArg.tellg();
cmdArg >> procname;
cmdArg.seekg(gc); cmdArg.clear();
personalizedHelp = true;
dgDEBUG(15)<< "Personalized help on <"<< procname<<">"<<gc<<endl;
}
bool procfund = !personalizedHelp;
for( FunctionMap::const_iterator iter=functionMap.begin();
iter!=functionMap.end();++iter )
{
if( iter->first!="help" )
if( (! personalizedHelp) || ( procname==iter->first ) )
{ iter->second(cmdLine,cmdArg,os); procfund=true; }
}
if(! procfund ) { os<<" *** Procedure <"<<procname<<"> not found."<<endl; }
dgDEBUGOUT(25);
}
void Interpreter::
cmdRun( const std::string& cmdLine, std::istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - run <script.txt>\t\t\t\tRun the script." <<endl;
return;
}
dgDEBUGIN(15);
string filename; cmdArg>>filename;
dgDEBUG(25) << "Script <" <<filename<<">"<<endl;
ifstream script( filename.c_str(),ios::in );
if(! script.is_open() )
{
DG_THROW ExceptionFactory( ExceptionFactory::READ_FILE,
"File is not open."," (while reading <%s>).",
filename.c_str() );
}
const int SIZE = 16384;
char line[SIZE];int lineIdx;
string name;
try{
for( lineIdx=1;;lineIdx++ )
{
dgDEBUGIN(15);
script.getline(line,SIZE);
if(! script.good() ) break;
istringstream issTmp(line);
if( issTmp >> name )
{
issTmp.getline(line,SIZE);
istringstream iss(line);
dgDEBUG(25) << "Run <"<<name<<"> with args <"<<line<<">"<<endl;
cmd( name,iss,os );
dgDEBUGOUT(15);
}
}
} catch( ExceptionAbstract& exc ) {
std::string& msg = (std::string&)exc.getStringMessage();
std::ostringstream oss;
oss <<" (in line " << lineIdx <<" of file <" << filename << ">)";
msg = msg + oss.str();
throw exc;
}
dgDEBUGOUT(15);
}
void Interpreter::
cmdSetSignal( const std::string& cmdLine, std::istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - set <obj.signal> <value>\t\t\tSet the signal to the constant value." <<endl;
return;
}
dgDEBUGIN(15);
string objname,signame;
objectNameParser(cmdArg,objname,signame);
Entity& obj = g_pool.getEntity(objname);
SignalBase<int>& sig = obj.getSignal( signame );
cmdArg >> ws;
sig.set( cmdArg );
dgDEBUGOUT(15);
}
void Interpreter::
cmdGetSignal( const std::string& cmdLine, std::istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - get <obj.signal> <value>\t\t\tGet the signal to the constant value." <<endl;
return;
}
dgDEBUGIN(15);
string objname,signame;
objectNameParser(cmdArg,objname,signame);
Entity& obj = g_pool.getEntity(objname);
SignalBase<int>& sig = obj.getSignal( signame );
os << signame << " = "; sig.get( os );
dgDEBUGOUT(15);
}
void Interpreter::
cmdComputeSignal( const std::string& cmdLine, std::istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - compute <obj.sig> <time>\t\t\tRecompute <sig> at time <time> " <<endl;
return;
}
dgDEBUGIN(15);
string objname,signame;
objectNameParser(cmdArg,objname,signame);
Entity& obj = g_pool.getEntity(objname);
SignalBase<int>& sig = obj.getSignal( signame );
int time; cmdArg >> std::ws;
if( cmdArg.good() )
{cmdArg >> time;} else {time=0;}
sig.recompute( time );
dgDEBUGOUT(15);
}
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
void Interpreter::
cmd( const std::string& cmdLine, istringstream& cmdArg, std::ostream& os )
{
istringstream cmdparse(cmdLine.c_str());
string obj,fun;
if (cmdLine.find_first_not_of(" ") == string::npos)
{}
else if( objectNameParser( cmdparse,obj,fun ) )
{
dgDEBUG(15) << "Object <" << obj<< "> function <"<<fun<<">"<<endl;
g_pool.commandLine( obj,fun,cmdArg,os );
}
else
{
dgDEBUG(15) << "Function <" << cmdLine <<">"<< endl;
FunctionMap::iterator funPtr = functionMap .find( cmdLine );
if( funPtr == functionMap.end() )
{
DG_THROW ExceptionFactory( ExceptionFactory::UNREFERED_FUNCTION,
"Unknown function."," (while calling <%s>)",
cmdLine.c_str() );
}
funPtr->second(cmdLine,cmdArg,os);
}
}
bool Interpreter::
objectNameParser( istringstream& cmdparse,std::string& objName,std::string& funName )
{
const int SIZE=128;
char buffer[SIZE];
cmdparse >> ws;
cmdparse.getline( buffer,SIZE,'.' );
if(! cmdparse.good() ) // The callback is not an object method
return false;
objName = buffer;
//cmdparse.getline( buffer,SIZE );
//funName = buffer;
cmdparse >> funName;
return true;
}
void Interpreter::
shell( std::istream& sin, std::ostream& sout, const std::string& promptUser )
{
while( 1 )
{
if( promptUser.length() ) sout << promptUser; else sout << prompt;
string cmdLine;
const int SIZE = 16384;
char cmdArgs[SIZE];
sin >>skipws>> cmdLine ;
dgDEBUG(15) << "Cmd <" <<cmdLine<<">"<<endl;
if( cmdLine == "exit" ) break;
if( sin.eof() ) break;
sin.getline( cmdArgs,SIZE-1 );
if( sin.gcount() >= SIZE-2 )
{
sout << "!! Line size exceeded" << endl;
do{ sin.getline( cmdArgs,SIZE-1 ); } while ( sin.gcount() >= SIZE-2 );
sout << cmdArgs << endl;
}
else
{
dgDEBUG(15) << "Args <" <<cmdArgs<<">"<<endl;
istringstream args (cmdArgs);
try{ cmd(cmdLine,args,sout); }
catch( exception& e ) { dgDEBUG(1) << e.what(); throw; }
catch(...) { dgDEBUG(1) << "!! unknow!." <<endl; throw; }
}
}
}
ShellFunctionRegisterer::
ShellFunctionRegisterer( const std::string& funName,
const Interpreter::ShellBasicFunction& f)
{
dgDEBUGIN(25);
g_shell.registerFunction(funName,f);
dgDEBUGOUT(25);
}
void Interpreter::writeCompletionList(std::ostream& os)
{
for( FunctionMap::iterator iter=functionMap.begin();
iter!=functionMap.end();iter++ )
{
const std::string & name = iter->first;
os << name << std::endl;
}
}
namespace dynamicgraph {
//! The global g_shell object.
Interpreter g_shell;
}
/*
* Copyright 2010,
* François Bleibel,
* Olivier Stasse,
*
* CNRS/AIST
*
* This file is part of dynamic-graph.
* dynamic-graph 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.
* dynamic-graph 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 Lesser General Public License for more details. You should
* have received a copy of the GNU Lesser General Public License along
* with dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
*/
/* --------------------------------------------------------------------- */
/* --- INCLUDE --------------------------------------------------------- */
/* --------------------------------------------------------------------- */
#ifdef WIN32
#include <Windows.h>
#else
#include <dlfcn.h>
#endif
/* --- DYNAMIC-GRAPH --- */
#include <dynamic-graph/plugin-loader.h>
#include <dynamic-graph/debug.h>
/* --- STD --- */
#include <fstream>
#include <sstream>
using namespace std;
using namespace dynamicgraph;
/* --------------------------------------------------------------------- */
/* --- CLASS ----------------------------------------------------------- */
/* --------------------------------------------------------------------- */
class dynamicgraph::PluginRefMap
{
public:
#ifdef WIN32
typedef HMODULE plugin_key_type;
#else
typedef void* plugin_key_type;
#endif
typedef std::map< std::string,plugin_key_type> KeyMap;
KeyMap keyMap;
};
PluginLoader::
PluginLoader( void )
{
pluginRefs = new PluginRefMap();
}
PluginLoader::
~PluginLoader( void )
{
delete pluginRefs;
}
const std::string& PluginLoader::
setDirectory( const std::string& n )
{
return pluginDirectory = n;
}
const std::string& PluginLoader::
getDirectory( void )
{
return pluginDirectory;
}
void PluginLoader::
loadPluginList( const std::string& configFileName, const std::string& dir )
{
dgDEBUGIN(15);
string name;
//while (1)
ifstream configFile( configFileName.c_str(),ios::in );
if(! configFile.is_open())
{
DG_THROW ExceptionFactory( ExceptionFactory::READ_FILE,
"File is not open."," (while reading <%s>).",
configFileName.c_str() );
}
for(;;)
{
configFile>>name;
if(configFile.eof()) break;
dgDEBUG(9)<<"Add <"<< name << "> to the list"<<endl;
addPlugin( name,dir);
}
dgDEBUGOUT(15);
}
void PluginLoader::
addPlugin( const std::string& name, const std::string& dir )
{
dgDEBUGIN(15);
if( dir.length() )
pluginNames.push_back( dir +"/"+name );
else
{ pluginNames.push_back( pluginDirectory +"/"+name ); }
dgDEBUGOUT(15);
}
/* -------------------------------------------------------------------------- */
/* --- DYNAMIC LOADER LIB --------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void PluginLoader::
loadPlugins( void )
{
dgDEBUGIN(15);
for( list<string>::iterator iter = pluginNames.begin();
iter!=pluginNames.end();++iter )
{
dgDEBUG(9)<<"Load <"<< *iter << "> plugin"<<endl;
#ifndef WIN32
PluginRefMap::plugin_key_type dlib = dlopen( iter->c_str(),RTLD_NOW|RTLD_GLOBAL);
#else
PluginRefMap::plugin_key_type dlib = LoadLibrary ( iter->c_str());
#endif
dgDEBUG(19)<<"Plugin <"<< *iter << "> loaded "<<endl;
if( NULL==dlib )
{
std::string wrongLib = *iter;
pluginNames.erase(iter);
#ifndef WIN32
dgDEBUG(5) << "Failure while loading: " <<dlerror() <<endl;
DG_THROW ExceptionFactory( ExceptionFactory::DYNAMIC_LOADING,
"Error while dlopen. ","<%s>",dlerror() );
#else
// Retrieve the system error message for the last-error code
LPTSTR pszMessage;
DWORD dwLastError = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwLastError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&pszMessage,
0, NULL );
dgDEBUG(5) << "Failure while loading: " << pszMessage <<endl;
std::ostringstream error_of;
error_of << "Error while LoadLibrary (" << wrongLib << ") ";
DG_THROW ExceptionFactory( ExceptionFactory::DYNAMIC_LOADING,
error_of.str().c_str(), pszMessage );
LocalFree(pszMessage);
#endif
}
loadedPluginNames[*iter] = (*iter);
pluginRefs->keyMap[*iter] = dlib;
}
pluginNames.clear();
dgDEBUGOUT(15);
}
/* -------------------------------------------------------------------------- */
/* --- DYNAMIC UN-LOADER LIB ------------------------------------------------ */
/* -------------------------------------------------------------------------- */
void PluginLoader::
unloadPlugin( const std::string& plugname )
{
dgDEBUGIN( 15 );
PluginRefMap::KeyMap::iterator plugkey = pluginRefs->keyMap.find(plugname);
if( plugkey==pluginRefs->keyMap.end() ) // key does exist
{
throw ExceptionFactory( ExceptionFactory::OBJECT_CONFLICT,
"Plugin not loaded",": <%s>.",plugname.c_str() );
}
#ifndef WIN32
const int errCode = dlclose(plugkey->second);
#else
const int errCode = FreeLibrary(plugkey->second);
#endif
if( errCode )
{
#ifndef WIN32
dgDEBUG(1) << "Error while unloading <" << plugname <<"> : "
<< dlerror() <<endl;
#else
// Retrieve the system error message for the last-error code
LPTSTR pszMessage;
DWORD dwLastError = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwLastError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&pszMessage,
0, NULL );
dgDEBUG(1) << "Error while unloading <" << plugname <<"> : "
<< pszMessage <<endl;
LocalFree(pszMessage);
#endif
}
//pluginRefs.erase( plugkey );
dgDEBUGOUT( 15 );
}
const std::string& PluginLoader::
searchPlugin( const std::string& plugname )
{
unsigned int refFound = 0;
const std::string *plugFullName =0;
for( PluginRefMap::KeyMap::iterator iter = pluginRefs->keyMap.begin();
iter!=pluginRefs->keyMap.end();++iter )
{
const std::string &str = iter->first;
size_t found = str.find_last_of("/\\");
dgDEBUG(15) << " folder: " << str.substr(0,found) << std::endl;
dgDEBUG(15) << " file: " << str.substr(found+1) << std::endl;
if( str.substr(found+1)==plugname )
{
refFound++;
plugFullName=&iter->first;
}
}
if( 1!=refFound ) throw 1;
return *plugFullName;
}
void PluginLoader::
unloadAllPlugins()
{
dgDEBUGIN( 15 );
PluginRefMap::KeyMap::iterator plugkey = pluginRefs->keyMap.begin();
while( plugkey!=pluginRefs->keyMap.end() )
{
#ifndef WIN32
const int errCode = dlclose(plugkey->second);
#else
const int errCode = FreeLibrary(plugkey->second);
#endif
if( errCode )
{
#ifndef WIN32
dgDEBUG(1) << "Error while unloading <" << plugkey->first <<"> : "
<< dlerror() <<endl;
#else
// Retrieve the system error message for the last-error code
LPTSTR pszMessage;
DWORD dwLastError = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwLastError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&pszMessage,
0, NULL );
dgDEBUG(1) << "Error while unloading <" << plugkey->first <<"> : "
<< pszMessage <<endl;
LocalFree(pszMessage);
#endif
}
plugkey++;
}
//pluginRefs.erase( plugkey );
dgDEBUGOUT( 15 );
}
......@@ -5,17 +5,6 @@
*
* CNRS/AIST
*
* This file is part of dynamic-graph.
* dynamic-graph 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.
* dynamic-graph 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 Lesser General Public License for more details. You should
* have received a copy of the GNU Lesser General Public License along
* with dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
*/
/* --------------------------------------------------------------------- */
......@@ -23,109 +12,127 @@
/* --------------------------------------------------------------------- */
/* --- DYNAMIC-GRAPH --- */
#include <dynamic-graph/pool.h>
#include <dynamic-graph/debug.h>
#include <dynamic-graph/entity.h>
#include "dynamic-graph/pool.h"
#include <list>
#include <sstream>
#include <string>
#include <typeinfo>
#include "dynamic-graph/debug.h"
#include "dynamic-graph/entity.h"
using namespace dynamicgraph;
/* --------------------------------------------------------------------- */
/* --- CLASS ----------------------------------------------------------- */
/* --------------------------------------------------------------------- */
PoolStorage::
~PoolStorage( void )
{
dgDEBUGIN(15);
PoolStorage *PoolStorage::getInstance() {
if (instance_ == 0) {
instance_ = new PoolStorage;
}
return instance_;
}
for( Entities::iterator iter=entity.begin();iter!=entity.end(); iter=entity.begin())
{
dgDEBUG(15) << "Delete \""
<< (iter->first) <<"\""<<std::endl;
delete ((Entity*)iter->second);
}
void PoolStorage::destroy() {
delete instance_;
instance_ = NULL;
}
PoolStorage::~PoolStorage() {
dgDEBUGIN(15);
for (Entities::iterator iter = entityMap.begin(); iter != entityMap.end();
// Here, this is normal that the next iteration is at the beginning
// of the map as deregisterEntity remove the element iter from the map.
iter = entityMap.begin()) {
dgDEBUG(15) << "Delete \"" << (iter->first) << "\"" << std::endl;
Entity *entity = iter->second;
deregisterEntity(iter);
delete (entity);
}
instance_ = 0;
dgDEBUGOUT(15);
return;
}
/* --------------------------------------------------------------------- */
void PoolStorage::registerEntity(const std::string &entname, Entity *ent) {
Entities::iterator entkey = entityMap.find(entname);
if (entkey != entityMap.end()) // key does exist
{
throw ExceptionFactory(
ExceptionFactory::OBJECT_CONFLICT,
"Another entity already defined with the same name. ",
"Entity name is <%s>.", entname.c_str());
} else {
dgDEBUG(10) << "Register entity <" << entname << "> in the pool."
<< std::endl;
entityMap[entname] = ent;
}
}
void PoolStorage::deregisterEntity(const std::string &entname) {
Entities::iterator entkey = entityMap.find(entname);
if (entkey == entityMap.end()) // key doesnot exist
{
throw ExceptionFactory(ExceptionFactory::OBJECT_CONFLICT,
"Entity not defined yet. ", "Entity name is <%s>.",
entname.c_str());
} else {
dgDEBUG(10) << "Deregister entity <" << entname << "> from the pool."
<< std::endl;
deregisterEntity(entkey);
}
}
/* --------------------------------------------------------------------- */
void PoolStorage::
registerEntity( const std::string& entname,Entity* ent )
{
Entities::iterator entkey = entity.find(entname);
if( entkey != entity.end() ) // key does exist
{
throw ExceptionFactory( ExceptionFactory::OBJECT_CONFLICT,
"Another entity already defined with the same name. ",
"Entity name is <%s>.",entname.c_str() );
}
else
{
dgDEBUG(10) << "Register entity <"<< entname
<< "> in the pool." <<std::endl;
entity[entname] = ent;
}
void PoolStorage::deregisterEntity(const Entities::iterator &entity) {
entityMap.erase(entity);
}
void PoolStorage::
deregisterEntity( const std::string& entname )
{
Entities::iterator entkey = entity.find(entname);
if( entkey == entity.end() ) // key doesnot exist
{
throw ExceptionFactory( ExceptionFactory::OBJECT_CONFLICT,
"Entity not defined yet. ",
"Entity name is <%s>.",entname.c_str() );
}
else
{
dgDEBUG(10) << "Deregister entity <"<< entname
<< "> from the pool." <<std::endl;
entity.erase( entkey );
}
Entity &PoolStorage::getEntity(const std::string &name) {
dgDEBUG(25) << "Get <" << name << ">" << std::endl;
Entities::iterator entPtr = entityMap.find(name);
if (entPtr == entityMap.end()) {
DG_THROW ExceptionFactory(ExceptionFactory::UNREFERED_OBJECT,
"Unknown entity.", " (while calling <%s>)",
name.c_str());
} else
return *entPtr->second;
}
Entity& PoolStorage::
getEntity( const std::string& name )
{
dgDEBUG(25) << "Get <" << name << ">"<<std::endl;
Entities::iterator entPtr = entity .find( name );
if( entPtr == entity.end() )
{
DG_THROW ExceptionFactory( ExceptionFactory::UNREFERED_OBJECT,
"Unknown entity."," (while calling <%s>)",
name.c_str() );
}
else return *entPtr->second;
const PoolStorage::Entities &PoolStorage::getEntityMap() const {
return entityMap;
}
void PoolStorage::
clearPlugin( const std::string& name )
{
dgDEBUGIN(5);
std::list< Entity* > toDelete;
for( Entities::iterator entPtr=entity.begin(); entPtr!=entity.end(); entPtr++ )
{
if( entPtr->second->getClassName() == name )
{ toDelete.push_back( entPtr->second ); }
}
for( std::list< Entity* >::iterator iter=toDelete.begin();
iter!=toDelete.end(); ++iter )
{
delete (Entity*) *iter;
}
dgDEBUGOUT(5);
return;
bool PoolStorage::existEntity(const std::string &name) {
return entityMap.find(name) != entityMap.end();
}
bool PoolStorage::existEntity(const std::string &name, Entity *&ptr) {
Entities::iterator entPtr = entityMap.find(name);
if (entPtr == entityMap.end())
return false;
else {
ptr = entPtr->second;
return true;
}
}
void PoolStorage::clearPlugin(const std::string &name) {
dgDEBUGIN(5);
std::list<Entity *> toDelete;
for (Entities::iterator entPtr = entityMap.begin(); entPtr != entityMap.end();
++entPtr)
if (entPtr->second->getClassName() == name)
toDelete.push_back(entPtr->second);
for (std::list<Entity *>::iterator iter = toDelete.begin();
iter != toDelete.end(); ++iter)
delete (Entity *)*iter;
dgDEBUGOUT(5);
}
/* --------------------------------------------------------------------- */
......@@ -135,15 +142,13 @@ clearPlugin( const std::string& name )
#include <time.h>
#endif /*WIN32*/
void PoolStorage::
writeGraph(const std::string &aFileName)
{
void PoolStorage::writeGraph(const std::string &aFileName) {
size_t IdxPointFound = aFileName.rfind(".");
std::string tmp1 = aFileName.substr(0,IdxPointFound);
std::string tmp1 = aFileName.substr(0, IdxPointFound);
size_t IdxSeparatorFound = aFileName.rfind("/");
std::string GenericName;
if (IdxSeparatorFound!=std::string::npos)
GenericName = tmp1.substr(IdxSeparatorFound,tmp1.length());
if (IdxSeparatorFound != std::string::npos)
GenericName = tmp1.substr(IdxSeparatorFound, tmp1.length());
else
GenericName = tmp1;
......@@ -152,120 +157,77 @@ writeGraph(const std::string &aFileName)
ltime = time(NULL);
struct tm ltimeformatted;
#ifdef WIN32
localtime_s(&ltimeformatted,&ltime);
localtime_s(&ltimeformatted, &ltime);
#else
localtime_r(&ltime,&ltimeformatted);
localtime_r(&ltime, &ltimeformatted);
#endif /*WIN32*/
/* Opening the file and writing the first comment. */
std::ofstream GraphFile;
GraphFile.open((char *)aFileName.c_str(),std::ofstream::out);
std::ofstream GraphFile(aFileName.c_str(), std::ofstream::out);
GraphFile << "/* This graph has been automatically generated. " << std::endl;
GraphFile << " " << 1900+ltimeformatted.tm_year
<< " Month: " << 1+ltimeformatted.tm_mon
<< " Day: " << ltimeformatted.tm_mday
<< " Time: " << ltimeformatted.tm_hour
<< ":" << ltimeformatted.tm_min;
GraphFile << " " << 1900 + ltimeformatted.tm_year
<< " Month: " << 1 + ltimeformatted.tm_mon
<< " Day: " << ltimeformatted.tm_mday
<< " Time: " << ltimeformatted.tm_hour << ":"
<< ltimeformatted.tm_min;
GraphFile << " */" << std::endl;
GraphFile << "digraph " << GenericName << " { ";
GraphFile << "\t graph [ label=\"" << GenericName << "\" bgcolor = white rankdir=LR ]" << std::endl
<< "\t node [ fontcolor = black, color = black, fillcolor = gold1, style=filled, shape=box ] ; " << std::endl;
GraphFile << "digraph \"" << GenericName << "\" { ";
GraphFile << "\t graph [ label=\"" << GenericName
<< "\" bgcolor = white rankdir=LR ]" << std::endl
<< "\t node [ fontcolor = black, color = black,"
<< "fillcolor = gold1, style=filled, shape=box ] ; " << std::endl;
GraphFile << "\tsubgraph cluster_Entities { " << std::endl;
GraphFile << "\t} " << std::endl;
for( Entities::iterator iter=entity.begin();
iter!=entity.end();iter++ )
{
Entity* ent = iter->second;
GraphFile << ent->getName()
<<" [ label = \"" << ent->getName() << "\" ," << std::endl
<<" fontcolor = black, color = black, fillcolor=cyan, style=filled, shape=box ]" << std::endl;
ent->writeGraph(GraphFile);
}
for (Entities::iterator iter = entityMap.begin(); iter != entityMap.end();
++iter) {
Entity *ent = iter->second;
GraphFile << "\"" << ent->getName() << "\""
<< " [ label = \"" << ent->getName() << "\" ," << std::endl
<< " fontcolor = black, color = black, fillcolor=cyan,"
<< " style=filled, shape=box ]" << std::endl;
ent->writeGraph(GraphFile);
}
GraphFile << "}"<< std::endl;
GraphFile << "}" << std::endl;
GraphFile.close();
}
void PoolStorage::
writeCompletionList(std::ostream& os)
{
for( Entities::iterator iter=entity.begin();
iter!=entity.end();iter++ )
{
Entity* ent = iter->second;
ent->writeCompletionList(os);
}
void PoolStorage::writeCompletionList(std::ostream &os) {
for (Entities::iterator iter = entityMap.begin(); iter != entityMap.end();
++iter) {
Entity *ent = iter->second;
ent->writeCompletionList(os);
}
}
void PoolStorage::
commandLine( const std::string& objectName,const std::string& functionName,
std::istringstream& cmdArg, std::ostream& os )
{
dgDEBUG(15) << "Object <" << objectName<< "> function <"
<<functionName<<">"<<std::endl;
if( objectName=="pool" )
{
if( functionName=="help" )
{
os <<"Pool: " << std::endl
<<" - list" << std::endl
<< " - writegraph FileName" << std::endl;
}
else if( functionName=="list" )
{
for( Entities::iterator iter=entity.begin();
iter!=entity.end();iter++ )
{
Entity* ent = iter->second;
os << ent->getName()
<<" (" << ent->getClassName() << ")" << std::endl;
}
}
else if (functionName=="writegraph")
{
std::string aFileName;
cmdArg >> aFileName;
writeGraph(aFileName);
}
}
else
{
Entity& ent = getEntity(objectName);
ent.commandLine(functionName,cmdArg,os);
}
}
#include <dynamic-graph/interpreter.h>
SignalBase<int>&
PoolStorage::
getSignal( std::istringstream& sigpath )
{
std::string objname,signame;
if(! Interpreter::objectNameParser( sigpath,objname,signame ) )
{ DG_THROW ExceptionFactory( ExceptionFactory::UNREFERED_SIGNAL,
"Parse error in signal name" ); }
Entity& ent = getEntity( objname );
return ent.getSignal( signame );
static bool objectNameParser(std::istringstream &cmdparse, std::string &objName,
std::string &funName) {
const int SIZE = 128;
char buffer[SIZE];
cmdparse >> std::ws;
cmdparse.getline(buffer, SIZE, '.');
if (!cmdparse.good()) // The callback is not an object method
return false;
objName = buffer;
// cmdparse.getline( buffer,SIZE );
// funName = buffer;
cmdparse >> funName;
return true;
}
SignalBase<int> &PoolStorage::getSignal(std::istringstream &sigpath) {
std::string objname, signame;
if (!objectNameParser(sigpath, objname, signame)) {
DG_THROW ExceptionFactory(ExceptionFactory::UNREFERED_SIGNAL,
"Parse error in signal name");
}
namespace dynamicgraph {
//! The global g_pool object.
PoolStorage g_pool;
Entity &ent = getEntity(objname);
return ent.getSignal(signame);
}
PoolStorage *PoolStorage::instance_ = 0;
/*
* Copyright 2010,
* François Bleibel,
* Olivier Stasse,
*
* CNRS/AIST
*
* This file is part of dynamic-graph.
* dynamic-graph 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.
* dynamic-graph 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 Lesser General Public License for more details. You should
* have received a copy of the GNU Lesser General Public License along
* with dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
*/
// Copyright 2010, François Bleibel, Thomas Moulard, Olivier Stasse,
// JRL, CNRS/AIST.
//
#include <dynamic-graph/exception-abstract.h>
#include <dynamic-graph/debug.h>
#include <dynamic-graph/exception-abstract.h>
using namespace std;
using namespace dynamicgraph;
/* ------------------------------------------------------------------------- */
/* --- CONSTRUCTORS -------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
#include <cstring>
namespace dynamicgraph {
const std::string ExceptionAbstract::EXCEPTION_NAME = "Abstract";
ExceptionAbstract::ExceptionAbstract(const int &_code, const std::string &_msg)
: code(_code), message(_msg) {}
ExceptionAbstract::
ExceptionAbstract (const int& _code,
const string & _msg)
:
code (_code),
message (_msg)
{
return ;
const char *ExceptionAbstract::getMessage() const {
return (this->message).c_str();
}
/* ------------------------------------------------------------------------ */
/* --- ACCESSORS ---------------------------------------------------------- */
/* ------------------------------------------------------------------------ */
const char *ExceptionAbstract::
getMessage (void)
{
return (this->message) .c_str();
const std::string &ExceptionAbstract::getStringMessage() const {
return this->message;
}
const string& ExceptionAbstract::
getStringMessage (void) const
{
return this->message;
int ExceptionAbstract::getCode() const { return this->code; }
ExceptionAbstract::Param &ExceptionAbstract::Param::initCopy(const Param &p) {
if (&p == this) return *this;
dgDEBUGIN(25);
if (p.pointersSet) {
strncpy(function, p.functionPTR, BUFFER_SIZE);
strncpy(file, p.filePTR, BUFFER_SIZE);
line = p.line;
pointersSet = false;
set = true;
} else
set = false;
dgDEBUGOUT(25);
return *this;
}
int ExceptionAbstract::
getCode (void)
{
return this->code;
ExceptionAbstract::Param::Param(const int &_line, const char *_function,
const char *_file)
: functionPTR(_function), line(_line), filePTR(_file), pointersSet(true) {
dgDEBUGINOUT(25);
}
std::ostream &operator<<(std::ostream &os, const ExceptionAbstract &error) {
os << error.getExceptionName() << "Error [#" << error.code
<< "]: " << error.message << std::endl;
/* ------------------------------------------------------------------------- */
/* --- MODIFIORS ----------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
#ifdef DYNAMICGRAPH_EXCEPTION_PASSING_PARAM
if (error.p.set)
os << "Thrown from " << error.p.file << ": " << error.p.function << " (#"
<< error.p.line << ")" << std::endl;
#endif // DYNAMICGRAPH_EXCEPTION_PASSING_PARAM
ExceptionAbstract::Param& ExceptionAbstract::Param::
initCopy( const Param& p )
{
dgDEBUGIN(25);
if( p.pointersSet )
{
strncpy( function,p.functionPTR,BUFFER_SIZE);
strncpy( file,p.filePTR,BUFFER_SIZE);
line = p.line;
pointersSet=false;
set=true;
} else set=false;
dgDEBUGOUT(25);
return *this;
}
ExceptionAbstract::Param::
Param( const int& _line, const char * _function, const char * _file )
: functionPTR(_function),line(_line),filePTR(_file),pointersSet(true)
{
dgDEBUGINOUT(25);
return os;
}
#endif //#ifdef DYNAMICGRAPH_EXCEPTION_PASSING_PARAM
/* ------------------------------------------------------------------------- */
/* --- OP << --------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
namespace dynamicgraph {
ostream &
operator << (ostream & os,
const ExceptionAbstract & error)
{
os << error.getExceptionName()<<"Error [#" << error.code << "]: " << error.message << endl;
#ifdef DYNAMICGRAPH_EXCEPTION_PASSING_PARAM
if( error.p.set )
os << "Thrown from "<<error.p.file << ": "<<error.p.function
<<" (#"<<error.p.line << ")"<<endl;
#endif //#ifdef DYNAMICGRAPH_EXCEPTION_PASSING_PARAM
return os;
}
} // namespace dynamicgraph
/** \file $Source$
*/
/*
* Local variables:
* c-basic-offset: 4
* End:
*/
} // end of namespace dynamicgraph.
......@@ -5,22 +5,12 @@
*
* CNRS/AIST
*
* This file is part of dynamic-graph.
* dynamic-graph 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.
* dynamic-graph 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 Lesser General Public License for more details. You should
* have received a copy of the GNU Lesser General Public License along
* with dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
*/
#include <dynamic-graph/exception-factory.h>
#include <dynamic-graph/debug.h>
#include <dynamic-graph/exception-factory.h>
#include <stdarg.h>
#include <cstdio>
using namespace dynamicgraph;
......@@ -31,40 +21,36 @@ using namespace dynamicgraph;
const std::string ExceptionFactory::EXCEPTION_NAME = "Factory";
ExceptionFactory::
ExceptionFactory ( const ExceptionFactory::ErrorCodeEnum& errcode,
const std::string & msg )
:ExceptionAbstract(errcode,msg)
{
dgDEBUGF( 15,"Created with message <%s>.",msg.c_str());
dgDEBUG( 1) <<"Created with message <%s>."<<msg<<std::endl;
ExceptionFactory::ExceptionFactory(
const ExceptionFactory::ErrorCodeEnum &errcode, const std::string &msg)
: ExceptionAbstract(errcode, msg) {
dgDEBUGF(15, "Created with message <%s>.", msg.c_str());
dgDEBUG(1) << "Created with message <%s>." << msg << std::endl;
}
ExceptionFactory::
ExceptionFactory ( const ExceptionFactory::ErrorCodeEnum& errcode,
const std::string & msg,const char* format, ... )
:ExceptionAbstract(errcode,msg)
{
ExceptionFactory::ExceptionFactory(
const ExceptionFactory::ErrorCodeEnum &errcode, const std::string &msg,
const char *format, ...)
: ExceptionAbstract(errcode, msg) {
va_list args;
va_start(args,format);
va_start(args, format);
const unsigned int SIZE = 256;
char buffer[SIZE];
vsnprintf(buffer,SIZE,format,args);
char buffer[SIZE];
vsnprintf(buffer, SIZE, format, args);
dgDEBUG(15) <<"Created "<<" with message <"
<<msg<<"> and buffer <"<<buffer<<">. "<<std::endl;
dgDEBUG(15) << "Created "
<< " with message <" << msg << "> and buffer <" << buffer << ">. "
<< std::endl;
message += buffer;
va_end(args);
dgDEBUG(1) << "Throw exception " << EXCEPTION_NAME << "[#" << errcode<<"]: "
<<"<"<< message << ">."<<std::endl;
dgDEBUG(1) << "Throw exception " << EXCEPTION_NAME << "[#" << errcode << "]: "
<< "<" << message << ">." << std::endl;
}
/*
* Local variables:
* c-basic-offset: 2
......
......@@ -5,21 +5,11 @@
*
* CNRS/AIST
*
* This file is part of dynamic-graph.
* dynamic-graph 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.
* dynamic-graph 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 Lesser General Public License for more details. You should
* have received a copy of the GNU Lesser General Public License along
* with dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
*/
#include <dynamic-graph/exception-signal.h>
#include <stdarg.h>
#include <cstdio>
using namespace dynamicgraph;
......@@ -30,32 +20,26 @@ using namespace dynamicgraph;
const std::string ExceptionSignal::EXCEPTION_NAME = "Signal";
ExceptionSignal::
ExceptionSignal ( const ExceptionSignal::ErrorCodeEnum& errcode,
const std::string & msg )
:ExceptionAbstract(errcode,msg)
{
}
ExceptionSignal::ExceptionSignal(const ExceptionSignal::ErrorCodeEnum &errcode,
const std::string &msg)
: ExceptionAbstract(errcode, msg) {}
ExceptionSignal::
ExceptionSignal ( const ExceptionSignal::ErrorCodeEnum& errcode,
const std::string & msg,const char* format, ... )
:ExceptionAbstract(errcode,msg)
{
ExceptionSignal::ExceptionSignal(const ExceptionSignal::ErrorCodeEnum &errcode,
const std::string &msg, const char *format,
...)
: ExceptionAbstract(errcode, msg) {
va_list args;
va_start(args,format);
va_start(args, format);
const unsigned int SIZE = 256;
char buffer[SIZE];
vsnprintf(buffer,SIZE,format,args);
char buffer[SIZE];
vsnprintf(buffer, SIZE, format, args);
message += buffer;
va_end(args);
}
/*
* Local variables:
* c-basic-offset: 2
......
......@@ -5,23 +5,12 @@
*
* CNRS/AIST
*
* This file is part of dynamic-graph.
* dynamic-graph 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.
* dynamic-graph 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 Lesser General Public License for more details. You should
* have received a copy of the GNU Lesser General Public License along
* with dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
*/
#include <dynamic-graph/exception-traces.h>
#include <stdarg.h>
#include <cstdio>
#include <cstdio>
using namespace dynamicgraph;
/* --------------------------------------------------------------------- */
......@@ -30,32 +19,26 @@ using namespace dynamicgraph;
const std::string ExceptionTraces::EXCEPTION_NAME = "Traces";
ExceptionTraces::
ExceptionTraces ( const ExceptionTraces::ErrorCodeEnum& errcode,
const std::string & msg )
:ExceptionAbstract(errcode,msg)
{
}
ExceptionTraces::ExceptionTraces(const ExceptionTraces::ErrorCodeEnum &errcode,
const std::string &msg)
: ExceptionAbstract(errcode, msg) {}
ExceptionTraces::
ExceptionTraces ( const ExceptionTraces::ErrorCodeEnum& errcode,
const std::string & msg,const char* format, ... )
:ExceptionAbstract(errcode,msg)
{
ExceptionTraces::ExceptionTraces(const ExceptionTraces::ErrorCodeEnum &errcode,
const std::string &msg, const char *format,
...)
: ExceptionAbstract(errcode, msg) {
va_list args;
va_start(args,format);
va_start(args, format);
const unsigned int SIZE = 256;
char buffer[SIZE];
vsnprintf(buffer,SIZE,format,args);
char buffer[SIZE];
vsnprintf(buffer, SIZE, format, args);
message += buffer;
va_end(args);
}
/*
* Local variables:
* c-basic-offset: 2
......
/* Copyright LAAS, CNRS
* Author: O. Stasse, 2019
* See LICENSE file in the root directory of this repository.
*/
#include <dynamic-graph/process-list.hh>
#include <fstream>
#include <sstream>
#include <string>
using namespace dynamicgraph::CPU;
CPUData::CPUData()
: user_mode_time_(0),
nice_time_(0),
system_time_(0),
idle_time_(0),
iowait_time_(0),
irq_time_(0),
softirq_time_(0),
steal_time_(0),
guest_time_(0),
guest_nice_time_(0),
percent_(0.0) {}
void CPUData::ProcessLine(std::istringstream &aCPULine) {
unsigned long long int luser_mode_time = 0, lnice_time = 0, lsystem_time = 0,
lidle_time = 0, liowait_time = 0, lirq_time = 0,
lsoftirq_time = 0, lsteal_time = 0, lguest_time = 0,
lguest_nice_time;
aCPULine >> luser_mode_time;
aCPULine >> lnice_time;
aCPULine >> lsystem_time;
aCPULine >> lidle_time;
aCPULine >> liowait_time;
aCPULine >> lirq_time;
aCPULine >> lsoftirq_time;
aCPULine >> lsteal_time;
aCPULine >> lguest_time;
aCPULine >> lguest_nice_time;
// Remove guest time already in user_time:
luser_mode_time -= lguest_time;
lnice_time -= lguest_nice_time;
// Compute cumulative time
unsigned long long int lidle_all_time = 0, lsystem_all_time = 0,
lguest_all_time = 0, ltotal_time = 0;
lidle_all_time = lidle_time + liowait_time;
lsystem_all_time = lsystem_time + lirq_time + lsoftirq_time;
lguest_all_time = lguest_time + lguest_nice_time;
ltotal_time = luser_mode_time + lnice_time + lsystem_all_time +
lidle_all_time + lsteal_time + lguest_all_time;
// Update periodic computation.
user_mode_period_ = computePeriod(luser_mode_time, user_mode_time_);
nice_period_ = computePeriod(lnice_time, nice_time_);
system_period_ = computePeriod(lsystem_time, system_time_);
system_all_period_ = computePeriod(lsystem_all_time, system_all_time_);
idle_period_ = computePeriod(lidle_time, idle_time_);
idle_all_period_ = computePeriod(lidle_all_time, idle_all_time_);
iowait_period_ = computePeriod(liowait_time, idle_time_);
irq_period_ = computePeriod(lirq_time, irq_time_);
softirq_period_ = computePeriod(lsoftirq_time, softirq_time_);
steal_period_ = computePeriod(lsteal_time, steal_time_);
guest_period_ = computePeriod(lguest_all_time, guest_time_);
total_period_ = computePeriod(ltotal_time, total_time_);
/// Update time.
user_mode_time_ = luser_mode_time;
nice_time_ = lnice_time;
system_time_ = lsystem_time;
system_all_time_ = lsystem_all_time;
idle_time_ = lidle_time;
idle_all_time_ = lidle_all_time;
iowait_time_ = liowait_time;
irq_time_ = lirq_time;
softirq_time_ = lsoftirq_time;
steal_time_ = lsteal_time;
guest_time_ = lguest_all_time;
total_time_ = ltotal_time;
if (total_period_ != 0) {
percent_ = (double)(user_mode_period_) / (double)(total_period_)*100.0;
percent_ += (double)(nice_period_) / (double)(total_period_)*100.0;
percent_ += (double)(system_period_) / (double)(total_period_)*100.0;
percent_ += (double)(irq_period_) / (double)(total_period_)*100.0;
percent_ += (double)(softirq_period_) / (double)(total_period_)*100.0;
percent_ += (double)(steal_period_) / (double)(total_period_)*100.0;
percent_ += (double)(iowait_period_) / (double)(total_period_)*100.0;
}
}
System::System() {
vCPUData_.clear();
init();
}
void System::init() {
init_ = false;
readProcStat();
init_ = true;
}
void System::ProcessCPULine(unsigned int cpunb, std::istringstream &aCPULine) {
vCPUData_[cpunb].ProcessLine(aCPULine);
}
void System::readProcStat() {
std::ifstream aif;
cpuNb_ = 1;
aif.open("/proc/stat", std::ifstream::in);
std::string aline;
aline.clear();
while (std::getline(aif, aline)) {
// Read on line of the file
std::istringstream anISSLine(aline);
std::string line_hdr;
anISSLine >> line_hdr;
// Check if the line start with cpu
std::size_t pos = line_hdr.find("cpu");
std::string str_cpunbr = line_hdr.substr(pos + 3);
// Check if this is the first line
if (pos == 0 and str_cpunbr.empty()) {
gCPUData_.ProcessLine(anISSLine);
gCPUData_.cpu_id_ = -1;
} else {
// If not then check if there is a CPU number
if (pos == 0) {
std::istringstream iss(str_cpunbr);
unsigned int lcpunb;
iss >> lcpunb;
// If we did not initialize
if (!init_) {
// Count the number of CPU.
if (lcpunb > cpuNb_) cpuNb_ = lcpunb;
} else
// Otherwise process the line.
ProcessCPULine(lcpunb, anISSLine);
}
}
}
if (!init_) {
/// The number of CPU has been detected by going through /proc/stat.
vCPUData_.resize(cpuNb_ + 1);
for (unsigned long i = 0; i < (unsigned long)cpuNb_; i++)
vCPUData_[i].cpu_id_ = (int)i;
}
aif.close();
}
/*
* Copyright 2010,
* François Bleibel,
* Olivier Stasse,
*
* CNRS/AIST
*
* This file is part of dynamic-graph.
* dynamic-graph 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.
* dynamic-graph 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 Lesser General Public License for more details. You should
* have received a copy of the GNU Lesser General Public License along
* with dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
*/
#include <dynamic-graph/functions.h>
#include <dynamic-graph/plugin-loader.h>
#include <dynamic-graph/factory.h>
#include <dynamic-graph/debug.h>
#include <dynamic-graph/entity.h>
#include <dynamic-graph/signal.h>
#include <dynamic-graph/exception-signal.h>
#ifdef WIN32
#include <Windows.h>
#endif
#include <fstream>
using namespace std;
using namespace dynamicgraph;
extern "C" {
ShellFunctionRegisterer regFun1
( "try",boost::bind(ShellFunctions::cmdTry,_1,_2,_3) );
ShellFunctionRegisterer regFun2
( "loadPlugins",boost::bind(ShellFunctions::cmdLoadPlugins,_1,_2,_3) );
ShellFunctionRegisterer regFun3
( "displayPlugins",boost::bind(ShellFunctions::cmdDisplayPlugins,_1,_2,_3) );
ShellFunctionRegisterer regFun4
( "factory",boost::bind(ShellFunctions::cmdDisplayFactory,_1,_2,_3) );
ShellFunctionRegisterer regFun5
( "#",boost::bind(ShellFunctions::cmdCommentary,_1,_2,_3) );
ShellFunctionRegisterer regFun7
( "unplug",boost::bind(ShellFunctions::cmdUnplug,_1,_2,_3) );
ShellFunctionRegisterer regFun8
( "clearPlugin",boost::bind(ShellFunctions::cmdClearPlugin,_1,_2,_3) );
ShellFunctionRegisterer regFun9
( "signalTime",boost::bind(ShellFunctions::cmdSignalTime,_1,_2,_3) );
ShellFunctionRegisterer regFun10
( "synchro",boost::bind(ShellFunctions::cmdSynchroSignal,_1,_2,_3) );
ShellFunctionRegisterer regFun11
( "echo",boost::bind(ShellFunctions::cmdEcho,_1,_2,_3) );
ShellFunctionRegisterer regFun12
( "copy",boost::bind(ShellFunctions::cmdCopy,_1,_2,_3) );
ShellFunctionRegisterer regFun13
( "freeze",boost::bind(ShellFunctions::cmdFreeze,_1,_2,_3) );
ShellFunctionRegisterer regFun13b
( "squeeze",boost::bind(ShellFunctions::cmdSqueeze,_1,_2,_3) );
ShellFunctionRegisterer regFun14
( "debugtrace",boost::bind(ShellFunctions::cmdEnableTrace,_1,_2,_3) );
ShellFunctionRegisterer regFun15
( "prompt",boost::bind(ShellFunctions::cmdSetPrompt,_1,_2,_3) );
ShellFunctionRegisterer regFun16
( "sleep",boost::bind(ShellFunctions::cmdSleep,_1,_2,_3) );
ShellFunctionRegisterer regFun17
( "beep",boost::bind(ShellFunctions::cmdBeep,_1,_2,_3) );
ShellFunctionRegisterer regFun19
( "completion",boost::bind(ShellFunctions::cmdCompletionList,_1,_2,_3) );
}
/*
* Copyright 2010,
* François Bleibel,
* Olivier Stasse,
*
* CNRS/AIST
*
* This file is part of dynamic-graph.
* dynamic-graph 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.
* dynamic-graph 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 Lesser General Public License for more details. You should
* have received a copy of the GNU Lesser General Public License along
* with dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
*/
#include <dynamic-graph/shell-procedure.h>
#include <dynamic-graph/plugin-loader.h>
#include <dynamic-graph/factory.h>
#include <dynamic-graph/debug.h>
#include <dynamic-graph/entity.h>
#include <fstream>
using namespace std;
using namespace dynamicgraph;
void ShellProcedure::
cmdStartProcedure( const std::string& cmdLine,std::istringstream& args,std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - proc <name>"
<< "\t\t\t\tProcedure header." <<endl;
return;
}
args>>procName;
dgDEBUG(5)<<"Proc <" <<procName<<">"<<endl;
currentProc.clear();
args >> ws;
while( args.good() )
{
std::string next;
args>>next>>ws;
currentProc.params.push_back(next);
}
}
void ShellProcedure::
cmdContinueProcedure( const std::string& cmdLine,std::istringstream& args,std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - -> cmd args..."
<< "\t\t\t\tProcedure body." <<endl;
return;
}
std::string cmd2;
args>>ws>>cmd2;
dgDEBUG(5)<<"Proc <" <<procName<<">: "<<cmd2<<endl;
Instruction ins; ins.cmd=cmd2;
args >> ws;
while( args.good() )
{
std::string next; int param=-1;
args>>next>>ws;
for( unsigned int i=0;i<currentProc.params.size();++i )
{ if( next==currentProc.params[i] ) { param=i; break; } }
ins.args.push_back(next);
ins.params.push_back( param );
}
currentProc.instructions.push_back( ins );
}
void ShellProcedure::
cmdEndProcedure( const std::string& cmdLine,std::istringstream& args,std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - endproc..."
<< "\t\t\t\tProcedure end." <<endl;
return;
}
dgDEBUG(5)<<"Proc <" <<procName<<">: endproc"<<endl;
procedureList[ procName ] = currentProc;
// std::string toto="toto";
// for( Procedure::iterator ins=procedureList[ toto ].begin();
// ins!=procedureList[ toto ].end(); ++ins )
// {
// dgDEBUG(15) << "Proc <" << procName << "> : "
// << ins->cmd << " -> " << ins->args <<endl;
// }
currentProc.clear();
if( g_shell.deregisterFunction( procName ))
{ os<< "Redefining proc <"<<procName<<">: procedure already defined. "
<< "Overwrite it."<<endl; }
ShellFunctionRegisterer registration
( procName.c_str(),boost::bind(&ShellProcedure::cmdProcedure,
this,procName,_1,_2,_3) );
}
void ShellProcedure::
cmdProcedure( const std::string& procname,
const std::string& cmdLine,std::istringstream& args,std::ostream& os )
{
if( cmdLine == "help" )
{
os<<" - "<<procname<<"\t\t\t\t\tUser-defined procedure"<<endl;
args >> ws;
if( args.good() )
{
std::string argname;
const unsigned int gc = args.tellg();
args >> argname;
args.seekg(gc); args.clear();
if( procname==argname )
{
/* if cmdline = "Help <procname>", then display
* the proc instruction. */
ProcedureList::iterator pair = procedureList.find( argname );
if( pair==procedureList.end() )
{
DG_THROW ExceptionFactory( ExceptionFactory::UNREFERED_FUNCTION,
"Undefined procedure",
": procedure <%s> not defined.",
argname.c_str() );
}
Procedure & proc = pair->second;
unsigned int cmdnum=1;
for( std::list<Instruction>::iterator ins=proc.instructions.begin();
ins!=proc.instructions.end(); ++ins )
{
os<<"\t#" <<cmdnum++<<" "<<ins->cmd; // <<" "<<ins->args <<endl;
for( unsigned int i=0;i<ins->args.size();++i )
{ os << " " << ins->args[i]; }
os << endl;
}
}
}
return;
}
dgDEBUG(15) << " Calling procedure <" <<cmdLine<<"> " <<endl;
ProcedureList::iterator pair = procedureList.find( cmdLine );
if( pair==procedureList.end() )
{
DG_THROW ExceptionFactory( ExceptionFactory::UNREFERED_FUNCTION,
"Undefined procedure",
": procedure <%s> not defined.",cmdLine.c_str() );
}
/* You need a copy here, in case the proc is removed from the
* list by itself:
* % proc next
* % -> proc next
* % -> -> echo TOTO
* % -> endproc
* % endproc
*/
Procedure proc = pair->second;
std::vector< std::string > paramValue;
for( unsigned int i=0;i<proc.params.size();++i )
{
args>>ws;
if( args.good() )
{
std::string next; args>>next>>ws;
paramValue.push_back( next );
dgDEBUG(25) << "Args : <" << next << ">"<<endl;
}
else { paramValue.push_back(""); }
}
istringstream iss; ostringstream oss;
for( std::list<Instruction>::iterator ins=proc.instructions.begin();
ins!=proc.instructions.end(); ++ins )
{
dgDEBUG(15) << "Proc <" << cmdLine << "> : " << ins->cmd << endl;
oss.clear(); oss.str("");
for( unsigned int i=0;i<ins->params.size();++i )
{
int paramArg = ins->params[i];
if( paramArg==-1 ) oss << ins->args[i] << " ";
else oss << paramValue[paramArg] << " ";
}
dgDEBUG(15) << " Args = " << oss.str() << endl;
iss.str(oss.str()); iss.clear();
g_shell.cmd(ins->cmd,iss,os);
}
}
void ShellProcedure::
cmdFor( const std::string& cmdLine,std::istringstream& args,std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - for 1 5 instruction "<<endl;
return;
}
std::string cmd2,idx;
int istart,iend;
{
stringstream oss;
args >> cmd2; oss.str( cmd2 );
const unsigned int SIZE = 32; char b1[SIZE],b2[SIZE],b3[SIZE];
oss.getline( b1,SIZE,'=' );
oss.getline( b2,SIZE,':' );
oss.getline( b3,SIZE );
dgDEBUG(15) << b1 << "/" << b2 << "/" << b3 << endl;
idx = b1; istart = atoi(b2); iend = atoi(b3);
args >> cmd2;
dgDEBUG(15) << "FOR <" << idx << "> = " << istart << " TO " << iend
<< " DO " << cmd2 <<endl;
}
string argsstr;
{
const unsigned int SIZE = 1024; char buffer[SIZE];
args.getline( buffer,SIZE );
argsstr = buffer;
}
for( int i=istart;i<=iend;++i )
{
istringstream iss; stringstream oss;
std::string insp;
istringstream issargs( argsstr );
while( issargs.good() )
{
issargs >> insp;
if( insp == idx ) { oss << i << " "; } else { oss<<insp<< " "; }
}
iss.str( oss.str() );
g_shell.cmd(cmd2,iss,os);
}
}
ShellProcedure sotShellProceduror;
extern "C" {
ShellFunctionRegisterer regFun1
( "proc",boost::bind(&ShellProcedure::cmdStartProcedure,
&sotShellProceduror,_1,_2,_3) );
ShellFunctionRegisterer regFun2
( "->",boost::bind(&ShellProcedure::cmdContinueProcedure,
&sotShellProceduror,_1,_2,_3) );
ShellFunctionRegisterer regFun3
( "endproc",boost::bind(&ShellProcedure::cmdEndProcedure,
&sotShellProceduror,_1,_2,_3) );
ShellFunctionRegisterer regFun4
( "for",boost::bind(&ShellProcedure::cmdFor,
_1,_2,_3) );
}
/*
* Copyright 2010,
* François Bleibel,
* Olivier Stasse,
*
* CNRS/AIST
*
* This file is part of dynamic-graph.
* dynamic-graph 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.
* dynamic-graph 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 Lesser General Public License for more details. You should
* have received a copy of the GNU Lesser General Public License along
* with dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
*/
#include <dynamic-graph/functions.h>
#include <dynamic-graph/plugin-loader.h>
#include <dynamic-graph/factory.h>
#include <dynamic-graph/debug.h>
#include <dynamic-graph/entity.h>
#include <dynamic-graph/signal.h>
#include <dynamic-graph/exception-signal.h>
#ifdef WIN32
#include <Windows.h>
#endif
#include <fstream>
using namespace std;
using namespace dynamicgraph;
void ShellFunctions::
cmdTry( const std::string cmdLine, istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - try <cmd...>"
<< "\t\t\t\tTry <cmd> and display the caught exception." <<endl;
return;
}
string cmdLine2;
cmdArg>>cmdLine2;
dgDEBUG(5)<<"Try <" <<cmdLine2<<">"<<endl;
try{
g_shell.cmd(cmdLine2,cmdArg,os);
} catch ( const ExceptionAbstract& e ) { os << "dgERROR catch: " <<endl<< e<<endl; }
catch( ... ) { os<<"Unknown error catch." <<endl; }
}
void ShellFunctions::
cmdLoadPlugins( const std::string cmdLine, std::istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - loadPlugins <file.txt> <directory>"
<< "\t\tLoad the plugins listed in the file." <<endl;
return;
}
if( NULL!=g_shell.dlPtr )
{
string pluginName,directory;
cmdArg >> pluginName;
cmdArg >> directory;
dgDEBUG(15) << "Load plugin list <" <<pluginName<<"> from dir <" << directory<<">."<<endl;
if( directory.length() != 0 ) g_shell.dlPtr->setDirectory( directory );
g_shell.dlPtr ->loadPluginList( pluginName );
g_shell.dlPtr->loadPlugins();
}
else { os << "!! Dynamic loading functionalities not accessible through the shell." <<endl; }
}
void ShellFunctions::
cmdClearPlugin( const std::string cmdLine, std::istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - clearPlugin <className>"
<< "\t\tDestroy all the objects of type <className>." <<endl;
return;
}
string pluginName;
cmdArg >> pluginName;
g_pool.clearPlugin( pluginName );
}
void ShellFunctions::
cmdDisplayPlugins( const std::string cmdLine, std::istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - displayPlugins "
<< "\t\t\t\tDisplay the name of the loaded plugins." <<endl;
return;
}
if( NULL!=g_shell.dlPtr )
{
std::map< std::string,std::string > m = g_shell.dlPtr->getLoadedPluginNames();
for( std::map< std::string,std::string >::const_iterator iter = m.begin();
iter!=m.end(); ++iter )
{
os << " - <"<<iter->first<<">:\t"<<iter->second<<endl;
}
}
else { os << "!! Dynamic loading functionalities not accessible through the shell." <<endl; }
}
void ShellFunctions::
cmdDisplayFactory( const std::string cmdLine, std::istringstream& cmdArg,
std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - "; g_factory.commandLine(cmdLine,cmdArg,os);
return;
}
string cmd2; cmdArg >> cmd2;
g_factory.commandLine( cmd2,cmdArg,os );
}
void ShellFunctions::
cmdCommentary( const std::string cmdLine, std::istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{ os << " # comment with '#': ignore the end of the line." <<endl; }
return;
}
void ShellFunctions::
cmdUnplug( const std::string cmdLine, istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - unplug <obj2.sig2>"
<< "\t\t\t\tPlug on sig2 (consumer) in sig1->sig2." <<endl;
return;
}
string ssig;
cmdArg>>ssig;
string obj2,fun2;
istringstream str2(ssig);
if( !Interpreter::objectNameParser(str2,obj2,fun2) )
{
DG_THROW ExceptionFactory( ExceptionFactory::SYNTAX_ERROR,
"Plug function: syntax is unplug OBJ2.SIG2.",
"(while calling plug %s %s).",ssig.c_str() );
}
dgDEBUG(20) << "Get Ent2 <"<<obj2<<"> ."<<endl;
Entity& ent2 = g_pool.getEntity(obj2);
dgDEBUG(20) << "Get Sig2 <"<<fun2<<"> ."<<endl;
SignalBase<int> &sig2 = ent2.getSignal(fun2);
dgDEBUG(25) << "Unplug..."<<endl;
sig2.unplug();
}
void ShellFunctions::
cmdSignalTime( const std::string cmdLine, istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - signalTime <obj.sig>"
<< "\t\t\t\tDisplay the time of sig." <<endl;
return;
}
string ssig;
cmdArg>>ssig;
string obj2,fun2;
istringstream str2(ssig);
if( !Interpreter::objectNameParser(str2,obj2,fun2) )
{
DG_THROW ExceptionFactory( ExceptionFactory::SYNTAX_ERROR,
"signalTime function: syntax is signalTime OBJ2.SIG2.",
"(while calling signalTime %s).",ssig.c_str() );
}
dgDEBUG(20) << "Get Ent2 <"<<obj2<<"> ."<<endl;
Entity& ent2 = g_pool.getEntity(obj2);
dgDEBUG(20) << "Get Sig2 <"<<fun2<<"> ."<<endl;
SignalBase<int> &sig2 = ent2.getSignal(fun2);
os << sig2.getTime() << endl;
}
void ShellFunctions::
cmdSynchroSignal( const std::string cmdLine, std::istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - synchro <obj.sig> [<period>]"
<< "\t\t\t\tGet/Set the syncho of a signal <sig>." <<endl;
return;
}
SignalBase<int> & sig = g_pool.getSignal( cmdArg );
cmdArg >>ws;
if( cmdArg.good() )
{
int period; cmdArg>>period;
sig.setPeriodTime( period );
cmdArg >>ws;
if( cmdArg.good() )
{
/* The sig is recomputed at the given period from the
* current time, at any time T so that T%p==0, p the period.
* By modifying the current time, the sig reomputation is done
* at T s.t. T%p=d, d the desynchro. */
int currTime = sig.getTime ();
int desynchro; cmdArg>>desynchro;
sig.setTime( currTime+desynchro );
}
}
else
{
os << "period = " << sig.getPeriodTime() << std::endl;
}
}
void ShellFunctions::
cmdEcho( const std::string cmdLine, std::istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - echo <string>"
<< "\t\t\t\tPrint <string. on the standard output." <<endl;
return;
}
cmdArg >>ws;
while( cmdArg.good() )
{
std::string toPrint; cmdArg>>toPrint;
os<<toPrint<<" ";
}
os<<std::endl;
}
void ShellFunctions::
cmdCopy( const std::string cmdLine, istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - copy <obj1.sig1> <obj2.sig2>"
<< "\t\tCopy the value of sig1 to constant value in sig2." <<endl;
return;
}
string ssig1,ssig2;
cmdArg>>ssig1>>ssig2;
istringstream str1(ssig1),str2(ssig2);
try {
SignalBase<int> &sig1 = g_pool.getSignal( str1 );
SignalBase<int> &sig2 = g_pool.getSignal( str2 );
dgDEBUG(25) << "Copy..."<<endl;
sig2.plug(&sig1);
sig2.setConstantDefault();
sig2.plug(&sig2);
} catch( ExceptionAbstract & err ) { throw; }
catch( ... ) {
DG_THROW ExceptionFactory( ExceptionFactory::SYNTAX_ERROR,
"Copy: syntax is copy OBJ1.SIG1 OBJ2.SIG2.",
"(while calling copy %s %s).",ssig1.c_str(),
ssig2.c_str() );
}
}
void ShellFunctions::
cmdFreeze( const std::string cmdLine, istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - freeze <obj.sig> "
<< "\t\tOn a ptr-sig: save the current value from the source "
<< "and unplug the signal." <<endl;
return;
}
string ssig1;
cmdArg>>ssig1;
istringstream str1(ssig1);
try {
SignalBase<int> &sig1 = g_pool.getSignal( str1 );
dgDEBUG(25) << "Unplug..."<<endl;
sig1.setConstantDefault();
sig1.plug(&sig1);
} catch( ... ) {
DG_THROW ExceptionFactory( ExceptionFactory::SYNTAX_ERROR,
"Freeze: syntax is freeze OBJ.SIG.",
"(while calling freeze %s ).",ssig1.c_str());
}
}
void ShellFunctions::
cmdSqueeze( const std::string cmdLine, istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - squeeze <mainObj.mainSig> <squeezeObj.sigIn> <squeezeObj.sigOut>"
<< "\t\tIntercalate squeezeObj between mainObj and its source." <<endl;
return;
}
string ssigMain,ssigIn,ssigOut;
cmdArg>>ssigMain>>ssigIn>>ssigOut;
istringstream strMain(ssigMain);
istringstream strIn(ssigIn);
istringstream strOut(ssigOut);
try {
SignalBase<int> &sigMain = g_pool.getSignal(strMain );
SignalBase<int> &sigIn = g_pool.getSignal( strIn );
SignalBase<int> &sigOut = g_pool.getSignal( strOut );
SignalBase<int> *sigMainSource = sigMain.getPluged();
if( sigMainSource==&sigMain )
{
DG_THROW ExceptionFactory( ExceptionFactory::SYNTAX_ERROR,
"The main signal is autopluged (or set constant). ",
"(while calling freeze %s ).",ssigMain.c_str());
}
sigMain.plug( &sigOut );
sigIn.plug( sigMainSource );
}
catch( ExceptionFactory& exc ) {
switch( exc.getCode() )
{
case ExceptionFactory::UNREFERED_SIGNAL:
DG_THROW ExceptionFactory( ExceptionFactory::SYNTAX_ERROR,
"Sqeeze: unknown signal. ",
"(error while searching signal: %s ).",
exc.getMessage() );
break;
default:
throw;
}
}
}
void ShellFunctions::
cmdEnableTrace( const std::string cmdLine, istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - debugtrace [{true|false}] [<filename>="
<< DebugTrace::DEBUG_FILENAME_DEFAULT <<"]"
<< "\t\tOpen/close the file <filename> for debug tracing." <<endl;
return;
}
string opt,filename;
cmdArg >> ws;
if( cmdArg.good() )
{
cmdArg>>opt>>ws;
if( opt=="true" )
if( cmdArg.good() )
{
cmdArg>>filename;
DebugTrace::openFile( filename.c_str() );
}
else { DebugTrace::openFile(); }
else DebugTrace::closeFile();
}
else
{
if( dgDEBUGFLOW.outputbuffer.good() ) os << "true" <<endl;
else os << "false" <<endl;
}
}
void ShellFunctions::
cmdSetPrompt( const std::string cmdLine, istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - prompt [<string>] "
<< "\t\tSet/get the default prompt." <<endl;
return;
}
string opt;
cmdArg >> ws;
if( cmdArg. good() )
{
char buffer [80]; cmdArg .getline(buffer,80);
g_shell .prompt = buffer;
} else { os << "Current prompt is <" << g_shell. prompt << ">." << endl; }
}
void ShellFunctions::
cmdSleep( const std::string cmdLine, istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - sleep [<float> secs] "
<< "\t\tSleep (time in secs)." <<endl;
return;
}
string opt;
cmdArg >> ws;
if( cmdArg. good() )
{
double secs; cmdArg >> secs;
if( secs > 0 )
#ifndef WIN32
usleep( int(secs*1000000) );
#else
Sleep( int(secs*1000) );
#endif
} else { /* TODO ERROR */ }
}
void ShellFunctions::
cmdBeep( const std::string cmdLine, istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - beep [<float> secs] "
<< "\t\tSend a bip to the std::cout." <<endl;
return;
}
os << char(7) << "Beep!" << std::endl;
}
void ShellFunctions::
cmdCompletionList( const std::string cmdLine, istringstream& cmdArg, std::ostream& os )
{
if( cmdLine == "help" )
{
os << " - completion <filename>"
<< "\t\tGenerate the completion list for current graph." <<endl;
return;
}
try {
std::string aFileName; cmdArg >> aFileName;
std::ofstream completionFile((char *)aFileName.c_str());
g_pool.writeCompletionList( completionFile );
} catch( ExceptionAbstract & err ) { throw; }
catch( ... ) {
DG_THROW ExceptionFactory( ExceptionFactory::SYNTAX_ERROR,
"setflag: sig should be of flag type. ",
"(while calling setflag).");
}
}
......@@ -5,21 +5,10 @@
*
* CNRS/AIST
*
* This file is part of dynamic-graph.
* dynamic-graph 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.
* dynamic-graph 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 Lesser General Public License for more details. You should
* have received a copy of the GNU Lesser General Public License along
* with dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
*/
#include <dynamic-graph/signal-array.h>
namespace dynamicgraph {
SignalArray<int> sotNOSIGNAL(0);
SignalArray<int> sotNOSIGNAL(0);
}
/*
* Copyright 2010,
* François Bleibel,
* Olivier Stasse,
*
* CNRS/AIST
*
* This file is part of dynamic-graph.
* dynamic-graph 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.
* dynamic-graph 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 Lesser General Public License for more details. You should
* have received a copy of the GNU Lesser General Public License along
* with dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
*/
#include <dynamic-graph/signal-caster.h>
#include <dynamic-graph/dynamic-graph-api.h>
#include <exception>
#include <boost/lambda/bind.hpp>
#include <string>
#include <sstream>
#include <algorithm>
#include <dynamic-graph/exception-signal.h>
using namespace std;
using namespace boost;
namespace dynamicgraph {
SignalCaster::SignalCaster() {
// nothing to initialize
}
SignalCaster::~SignalCaster() {
// no special cleanup to do
}
void SignalCaster::registerCast(const type_info& type, SignalCaster::displayer_type displayer,
SignalCaster::caster_type caster, SignalCaster::tracer_type tracer) {
if ( existsCast(type) ) {
std::string typeName(type.name());
std::ostringstream os;
os << "cast already registered for type " << typeName << ".";
throw ExceptionSignal(ExceptionSignal::GENERIC,
os.str()); //TODO: throw "cast already registered for type" exception
}
functions_[type.name()] = cast_functions_type(displayer,caster, tracer);
}
void SignalCaster::unregisterCast(const std::type_info& type) {
size_t n = functions_.erase(type.name());
if ( 0 == n ) // erase did not find element
throw ExceptionSignal(ExceptionSignal::GENERIC); // TODO: throw Cast not registered exception
}
bool SignalCaster::existsCast(const type_info& type) {
return functions_.find(type.name()) != functions_.end();
}
void SignalCaster::disp(const any& object, ostream& os) {
const char* type_name = object.type().name();
map<string, cast_functions_type>::iterator it =
functions_.find(type_name);
if ( it == functions_.end() )
throw ExceptionSignal(ExceptionSignal::BAD_CAST, "bad cast");
//TODO: throw "cast not registered" exception
(*it).second.get<0>()(object, os); // call display function (tuple index 0)
}
void SignalCaster::trace(const any& object, ostream& os) {
const char* type_name = object.type().name();
map<string, cast_functions_type>::iterator it =
functions_.find(type_name);
if ( it == functions_.end() )
throw ExceptionSignal(ExceptionSignal::BAD_CAST, "bad cast");;
//TODO: throw "cast not registered" exception
(*it).second.get<2>()(object, os); // call trace function (tuple index 2)
}
any SignalCaster::cast(const type_info& type, istringstream& iss) {
const char* type_name = type.name();
map<string, cast_functions_type>::iterator it = functions_.find(type_name);
if ( it == functions_.end() )
{
throw ExceptionSignal(ExceptionSignal::BAD_CAST,
"caster not in functions_ map.");
}
//TODO: throw "cast not registered" exception
return (*it).second.get<1>()(iss); // call cast function (tuple index 1)
}
/// The global instance of the caster class.
SignalCaster g_caster;
/// Registers useful casts
namespace {
DefaultCastRegisterer<double> double_reg;
DefaultCastRegisterer<int> int_reg;
DefaultCastRegisterer<unsigned int> uint_reg;
}
} // namespace dynamicgraph
......@@ -5,17 +5,6 @@
*
* CNRS/AIST
*
* This file is part of dynamic-graph.
* dynamic-graph 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.
* dynamic-graph 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 Lesser General Public License for more details. You should
* have received a copy of the GNU Lesser General Public License along
* with dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
*/
/* --------------------------------------------------------------------- */
......@@ -23,149 +12,159 @@
/* --------------------------------------------------------------------- */
/* DG */
#include <iomanip>
#include <boost/bind.hpp>
#include <dynamic-graph/tracer-real-time.h>
#include <dynamic-graph/all-commands.h>
#include <dynamic-graph/debug.h>
#include <dynamic-graph/pool.h>
#include <dynamic-graph/factory.h>
#include <dynamic-graph/pool.h>
#include <dynamic-graph/tracer-real-time.h>
#include <boost/bind.hpp>
#include <iomanip>
using namespace std;
using namespace dynamicgraph;
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(TracerRealTime,"TracerRealTime");
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(TracerRealTime, "TracerRealTime");
/* --------------------------------------------------------------------- */
/* --- DGOUTSTRINGSTREAM ---------------------------------------------- */
/* --------------------------------------------------------------------- */
OutStringStream::
OutStringStream( void )
: std::ostringstream()
,buffer( 0 ),index(0),bufferSize(0),full(false)
{
OutStringStream::OutStringStream()
: std::ostringstream(), buffer(0), index(0), bufferSize(0), full(false) {
dgDEBUGINOUT(15);
}
OutStringStream::
~OutStringStream( void )
{
OutStringStream::~OutStringStream() {
dgDEBUGIN(15);
if( buffer ) delete [] buffer ;
delete[] buffer;
dgDEBUGOUT(15);
}
void OutStringStream::
resize( const unsigned int & size )
{
void OutStringStream::resize(const std::streamsize &size) {
dgDEBUGIN(15);
index=0; bufferSize = size; full=false;
if( 0!=buffer ) delete [] buffer;
buffer = new char [size];
index = 0;
bufferSize = size;
full = false;
delete[] buffer;
buffer = new char[static_cast<size_t>(size)];
dgDEBUGOUT(15);
}
bool OutStringStream::
addData( const char * data, const unsigned int & size )
{
bool OutStringStream::addData(const char *data, const std::streamoff &size) {
dgDEBUGIN(15);
unsigned int towrite = size;
if( index+towrite>bufferSize )
{
dgDEBUGOUT(15);
full=true;
return false;
}//towrite=bufferSize-index;
memcpy( buffer+index,data,towrite );
index+=towrite;
std::streamsize towrite = static_cast<std::streamsize>(size);
if (index + towrite > bufferSize) {
dgDEBUGOUT(15);
full = true;
return false;
}
memcpy(buffer + index, data, static_cast<size_t>(towrite));
index += towrite;
dgDEBUGOUT(15);
return true;
}
}
void OutStringStream::
dump( std::ostream& os )
{
void OutStringStream::dump(std::ostream &os) {
dgDEBUGIN(15);
os.write( buffer,index );
os.write(buffer, index);
dgDEBUGOUT(15);
}
void OutStringStream::
empty( void )
{
void OutStringStream::empty() {
dgDEBUGIN(15);
index=0; full=false;
index = 0;
full = false;
dgDEBUGOUT(15);
}
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
TracerRealTime::TracerRealTime( const std::string n )
:Tracer(n)
,bufferSize( BUFFER_SIZE_DEFAULT )
{
TracerRealTime::TracerRealTime(const std::string &n)
: Tracer(n), bufferSize(BUFFER_SIZE_DEFAULT) {
dgDEBUGINOUT(15);
/* --- Commands --- */
{
using namespace dynamicgraph::command;
std::string doc = docCommandVoid0(
"Trash the current content of the buffers, without saving it.");
addCommand("empty",
makeCommandVoid0(*this, &TracerRealTime::emptyBuffers, doc));
addCommand("getBufferSize",
makeDirectGetter(*this, &bufferSize,
docDirectGetter("bufferSize", "int")));
addCommand("setBufferSize",
makeDirectSetter(*this, &bufferSize,
docDirectSetter("bufferSize", "int")));
} // using namespace command
dgDEBUGOUT(15);
}
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
void TracerRealTime::
openFile( const SignalBase<int> & sig,
const std::string& givenname )
{
void TracerRealTime::openFile(const SignalBase<int> &sig,
const std::string &givenname) {
dgDEBUGIN(15);
string signame;
if( givenname.length() )
{ signame = givenname; } else { signame = sig.shortName(); }
if (givenname.length()) {
signame = givenname;
} else {
signame = sig.shortName();
}
string filename = rootdir + basename + signame + suffix;
dgDEBUG(5) << "Sig <"<<sig.getName()
<< ">: new file "<< filename << endl;
std::ofstream * newfile = new std::ofstream( filename.c_str() );
dgDEBUG(5) << "Newfile:" << (void*) newfile << endl;
hardFiles.push_back( newfile );
dgDEBUG(5) << "Sig <" << sig.getName() << ">: new file " << filename << endl;
std::ofstream *newfile = new std::ofstream(filename.c_str());
if (!newfile->good()) {
delete newfile;
DG_THROW ExceptionTraces(
ExceptionTraces::NOT_OPEN,
"Could not open file " + filename + " for signal " + signame, "");
}
dgDEBUG(5) << "Newfile:" << (void *)newfile << endl;
hardFiles.push_back(newfile);
dgDEBUG(5) << "Creating Outstringstream" << endl;
//std::stringstream * newbuffer = new std::stringstream();
OutStringStream * newbuffer = new OutStringStream(); // std::stringstream();
newbuffer->resize( bufferSize );
// std::stringstream * newbuffer = new std::stringstream ();
OutStringStream *newbuffer = new OutStringStream(); // std::stringstream ();
newbuffer->resize(bufferSize);
newbuffer->givenname = givenname;
files.push_back( newbuffer );
files.push_back(newbuffer);
dgDEBUGOUT(15);
}
void TracerRealTime::
closeFiles( void )
{
void TracerRealTime::closeFiles() {
dgDEBUGIN(15);
std::lock_guard<std::mutex> files_lock(files_mtx);
FileList::iterator iter = files.begin();
HardFileList::iterator hardIter = hardFiles.begin();
while( files.end()!=iter )
{
dgDEBUG(25) << "Close the files." << endl;
while (files.end() != iter) {
dgDEBUG(25) << "Close the files." << endl;
std::stringstream * file = dynamic_cast< stringstream* >(*iter);
std::ofstream * hardFile = *hardIter;
std::stringstream *file = dynamic_cast<stringstream *>(*iter);
std::ofstream *hardFile = *hardIter;
(*hardFile) <<flush; hardFile->close();
delete file;
delete hardFile;
(*hardFile) << flush;
hardFile->close();
delete file;
delete hardFile;
++iter; ++hardIter;
}
++iter;
++hardIter;
}
dgDEBUG(25) << "Clear the lists." << endl;
files.clear();
......@@ -174,207 +173,133 @@ closeFiles( void )
dgDEBUGOUT(15);
}
void TracerRealTime::
trace( void )
{
void TracerRealTime::trace() {
dgDEBUGIN(15);
FileList::iterator iter = files.begin();
HardFileList::iterator hardIter = hardFiles.begin();
while( files.end()!=iter )
{
dgDEBUG(35) << "Next" << endl;
std::ostream * os = *iter;
if( NULL==os )
{ DG_THROW ExceptionTraces( ExceptionTraces::NOT_OPEN,
"The buffer is null",""); }
//std::stringstream & file = * dynamic_cast< stringstream* >(os);
OutStringStream * file = dynamic_cast< OutStringStream* >(os); // segfault
if( NULL==file )
{ DG_THROW ExceptionTraces( ExceptionTraces::NOT_OPEN,
"The buffer is not open",""); }
std::ofstream & hardFile = **hardIter;
if(! hardFile.good() )
{ DG_THROW ExceptionTraces( ExceptionTraces::NOT_OPEN,
"The file is not open",""); }
if( (hardFile.good())&&(NULL!=file) )
{
// const unsigned int SIZE = 1024*8;
// char buffer[SIZE];
// streambuf * pbuf = file.rdbuf();
// pbuf->pubseekpos(0);
// const unsigned int NB_BYTE = pbuf->in_avail();
// dgDEBUG(35) << "Bytes in buffer: " << NB_BYTE << endl;
// //dgDEBUG(35) << "Copie" <<endl<<file.str()<< endl;
// for( unsigned int index=0;index<NB_BYTE;index+=SIZE )
// {
// pbuf->pubseekpos( index );
// int nget = pbuf->sgetn( buffer,SIZE );
// dgDEBUG(35) << "Copie ["<<nget<<"] " <<buffer<<endl;
// hardFile.write( buffer,nget );
// }
//hardFile << file.str() << flush;
//file.seekp(0);
file->dump( hardFile );
file->empty();
hardFile.flush();
//file.str("");
}
++iter; ++hardIter;
while (files.end() != iter) {
dgDEBUG(35) << "Next" << endl;
std::ostream *os = *iter;
if (NULL == os) {
DG_THROW ExceptionTraces(ExceptionTraces::NOT_OPEN, "The buffer is null",
"");
}
// std::stringstream & file = * dynamic_cast< stringstream* >(os);
OutStringStream *file = dynamic_cast<OutStringStream *>(os); // segfault
if (NULL == file) {
DG_THROW ExceptionTraces(ExceptionTraces::NOT_OPEN,
"The buffer is not open", "");
}
std::ofstream &hardFile = **hardIter;
if (!hardFile.good()) {
DG_THROW ExceptionTraces(ExceptionTraces::NOT_OPEN,
"The file is not open", "");
}
if ((hardFile.good()) && (NULL != file)) {
file->dump(hardFile);
file->empty();
hardFile.flush();
}
++iter;
++hardIter;
}
dgDEBUGOUT(15);
}
void TracerRealTime::
emptyBuffers( void )
{
void TracerRealTime::emptyBuffers() {
dgDEBUGIN(15);
for( FileList::iterator iter = files.begin();files.end()!=iter;++iter )
{
//std::stringstream & file = * dynamic_cast< stringstream* >(*iter);
try {
OutStringStream & file = * dynamic_cast< OutStringStream* >(*iter);
file.empty();
//file.str("");
}
catch( ... ) { DG_THROW ExceptionTraces( ExceptionTraces::NOT_OPEN,
"The buffer is not open",""); }
for (FileList::iterator iter = files.begin(); files.end() != iter; ++iter) {
// std::stringstream & file = * dynamic_cast< stringstream* >(*iter);
try {
OutStringStream &file = *dynamic_cast<OutStringStream *>(*iter);
file.empty();
// file.str("");
} catch (...) {
DG_THROW ExceptionTraces(ExceptionTraces::NOT_OPEN,
"The buffer is not open", "");
}
}
dgDEBUGOUT(15);
}
// void TracerRealTime::
// emptyBuffer( std::stringstream & file )
// {
// streambuf * pbuf = file.rdbuf();
// pbuf->file.rdbuf() ->pubsetbuf( fileBuffer,10 );
// }
void TracerRealTime::
recordSignal( std::ostream& os,
const SignalBase<int>& sig )
{
void TracerRealTime::recordSignal(std::ostream &os,
const SignalBase<int> &sig) {
dgDEBUGIN(15);
try {
OutStringStream & file = dynamic_cast< OutStringStream& >(os);
OutStringStream &file = dynamic_cast<OutStringStream &>(os);
file.str("");
dgDEBUG(45) << "Empty file [" << file.tellp()
<< "] <" << file.str().c_str() <<"> " <<endl;
Tracer::recordSignal( file,sig );
file.addData( file.str().c_str(),file.tellp() );
dgDEBUG(35) << "Write data [" << file.tellp()
<< "] <" << file.str().c_str() <<"> " <<endl;
} catch( ExceptionAbstract & exc ) { throw exc; }
catch( ... ) {
DG_THROW ExceptionTraces( ExceptionTraces::NOT_OPEN,
"The buffer is not open","");
dgDEBUG(45) << "Empty file [" << file.tellp() << "] <" << file.str().c_str()
<< "> " << endl;
Tracer::recordSignal(file, sig);
file.addData(file.str().c_str(), file.tellp());
dgDEBUG(35) << "Write data [" << file.tellp() << "] <" << file.str().c_str()
<< "> " << endl;
} catch (ExceptionAbstract &exc) {
throw;
} catch (...) {
DG_THROW ExceptionTraces(ExceptionTraces::NOT_OPEN,
"The buffer is not open", "");
}
dgDEBUGOUT(15);
return ;
return;
}
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
void TracerRealTime::
display( std::ostream& os ) const
{
os << CLASS_NAME << " " << name << " [mode=" << (play?"play":"pause")
<< "] : "<< endl
<< " - Dep list: "<<endl;
void TracerRealTime::display(std::ostream &os) const {
os << CLASS_NAME << " " << name << " [mode=" << (play ? "play" : "pause")
<< "] : " << endl
<< " - Dep list: " << endl;
FileList::const_iterator iterFile = files.begin();
for( SignalList::const_iterator iter = toTraceSignals.begin();
toTraceSignals.end()!=iter;++iter )
{
dgDEBUG(35) << "Next" << endl;
const OutStringStream * file = dynamic_cast< OutStringStream* >(*iterFile);
os << " -> "<<(*iter)->getName();
if( file->givenname.length() ) os << " (in " << file->givenname << ")" ;
os << "\t";
if( file )
{
const std::streamsize PRECISION = os.precision();
const unsigned int SIZE = file->index;
const unsigned int MSIZE = file->bufferSize;
unsigned int dec=0; std::string unit ="";
if( (SIZE>>30)||(MSIZE>>30) ) { dec = 30; unit="Go"; }
else if( (SIZE>>20)||(MSIZE>>20) ) { dec = 20; unit="Mo"; }
else if( (SIZE>>10)||(MSIZE>>10) ) { dec = 10; unit="Ko"; }
os << "[" << std::setw(1)<<std::setprecision(1)
<< ((SIZE+0.0)/(1<<dec)) << unit << "/"
<< std::setprecision(2)<<((MSIZE+0.0)/(1<<dec))
<< unit << "]\t";
if( file->full ) os << "(FULL)";
os.precision(PRECISION);
}
os<<endl;
iterFile++;
for (SignalList::const_iterator iter = toTraceSignals.begin();
toTraceSignals.end() != iter; ++iter) {
dgDEBUG(35) << "Next" << endl;
const OutStringStream *file = dynamic_cast<OutStringStream *>(*iterFile);
os << " -> " << (*iter)->getName();
if (file->givenname.length()) os << " (in " << file->givenname << ")";
os << "\t";
if (file) {
const std::streamsize PRECISION = os.precision();
const std::streamsize SIZE = file->index;
const std::streamsize MSIZE = file->bufferSize;
unsigned int dec = 0;
std::string unit = "";
if ((SIZE >> 30) || (MSIZE >> 30)) {
dec = 30;
unit = "Go";
} else if ((SIZE >> 20) || (MSIZE >> 20)) {
dec = 20;
unit = "Mo";
} else if ((SIZE >> 10) || (MSIZE >> 10)) {
dec = 10;
unit = "Ko";
}
os << "[" << std::setw(1) << std::setprecision(1)
<< (((double)SIZE + 0.0) / (1 << dec)) << unit << "/"
<< std::setprecision(2) << (((double)MSIZE + 0.0) / (1 << dec)) << unit
<< "]\t";
if (file->full) os << "(FULL)";
os.precision(PRECISION);
}
os << endl;
++iterFile;
}
}
std::ostream& operator<< ( std::ostream& os,const TracerRealTime& t )
{
std::ostream &operator<<(std::ostream &os, const TracerRealTime &t) {
t.display(os);
return os;
}
void TracerRealTime::
commandLine( const std::string& cmdLine
,std::istringstream& cmdArgs
,std::ostream& os )
{
if( cmdLine=="help" )
{
os << "TracerRealTime: "<<endl
<< " - empty: trash the buffers." <<endl
<< " - bufferSize [<size>]: get/set the buffer size." <<endl;
Tracer::commandLine( cmdLine,cmdArgs,os );
}
else if( cmdLine=="empty" )
{
emptyBuffers();
}
else if( cmdLine=="bufferSize" )
{
cmdArgs>>ws;
if( cmdArgs.good() )
{
int s; cmdArgs>>s;
setBufferSize( s );
}
else os << getBufferSize() << endl;
}
else
Tracer::commandLine( cmdLine,cmdArgs,os );
}
......@@ -5,17 +5,6 @@
*
* CNRS/AIST
*
* This file is part of dynamic-graph.
* dynamic-graph 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.
* dynamic-graph 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 Lesser General Public License for more details. You should
* have received a copy of the GNU Lesser General Public License along
* with dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
*/
/* --------------------------------------------------------------------- */
......@@ -23,55 +12,108 @@
/* --------------------------------------------------------------------- */
/* DG */
#include <dynamic-graph/tracer.h>
#include <dynamic-graph/all-commands.h>
#include <dynamic-graph/debug.h>
#include <dynamic-graph/pool.h>
#include <dynamic-graph/factory.h>
#include <dynamic-graph/pool.h>
#include <dynamic-graph/tracer.h>
#include <dynamic-graph/value.h>
#include <boost/bind.hpp>
using namespace std;
using namespace dynamicgraph;
using namespace dynamicgraph::command;
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(Tracer,"Tracer");
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(Tracer, "Tracer");
/* --------------------------------------------------------------------- */
/* --- CLASS ----------------------------------------------------------- */
/* --------------------------------------------------------------------- */
Tracer::Tracer( const std::string n )
:Entity(n)
,toTraceSignals()
,traceStyle(TRACE_STYLE_DEFAULT)
,frequency(1)
,basename()
,suffix(".dat")
,rootdir()
,namesSet( false )
,files()
,names()
,play(false)
,timeStart(0)
,triger( boost::bind(&Tracer::recordTrigger,this,_1,_2),
sotNOSIGNAL,
"Tracer("+n+")::triger" )
{
signalRegistration( triger );
Tracer::Tracer(const std::string n)
: Entity(n),
toTraceSignals(),
traceStyle(TRACE_STYLE_DEFAULT),
frequency(1),
basename(),
suffix(".dat"),
rootdir(),
namesSet(false),
files(),
names(),
play(false),
timeStart(0),
triger(boost::bind(&Tracer::recordTrigger, this, _1, _2), sotNOSIGNAL,
"Tracer(" + n + ")::triger") {
signalRegistration(triger);
/* --- Commands --- */
{
using namespace dynamicgraph::command;
std::string doc;
doc = docCommandVoid2("Add a new signal to trace.", "string (signal name)",
"string (filename, empty for default");
addCommand("add",
makeCommandVoid2(*this, &Tracer::addSignalToTraceByName, doc));
doc =
docCommandVoid0("Remove all signals. If necessary, close open files.");
addCommand("clear",
makeCommandVoid0(*this, &Tracer::clearSignalToTrace, doc));
doc = docCommandVoid3(
"Gives the args for file opening, and "
"if signals have been set, open the corresponding files.",
"string (dirname)", "string (prefix)", "string (suffix)");
addCommand("open", makeCommandVoid3(*this, &Tracer::openFiles, doc));
doc = docCommandVoid0("Close all the open files.");
addCommand("close", makeCommandVoid0(*this, &Tracer::closeFiles, doc));
doc = docCommandVoid0(
"If necessary, dump "
"(can be done automatically for some traces type).");
addCommand("dump", makeCommandVoid0(*this, &Tracer::trace, doc));
doc = docCommandVoid0("Start the tracing process.");
addCommand("start", makeCommandVoid0(*this, &Tracer::start, doc));
doc = docCommandVoid0("Stop temporarily the tracing process.");
addCommand("stop", makeCommandVoid0(*this, &Tracer::stop, doc));
addCommand("getTimeStart",
makeDirectGetter(*this, &timeStart,
docDirectGetter("timeStart", "int")));
addCommand("setTimeStart",
makeDirectSetter(*this, &timeStart,
docDirectSetter("timeStart", "int")));
} // using namespace command
}
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
void Tracer::addSignalToTrace(const SignalBase<int> &sig,
const string &filename) {
dgDEBUGIN(15);
// openFile may throw so it should be called first.
if (namesSet) openFile(sig, filename);
toTraceSignals.push_back(&sig);
dgDEBUGF(15, "%p", &sig);
names.push_back(filename);
triger.addDependency(sig);
dgDEBUGOUT(15);
}
void Tracer::
addSignalToTrace( const SignalBase<int>& sig,
const string& filename )
{
void Tracer::addSignalToTraceByName(const string &signame,
const string &filename) {
dgDEBUGIN(15);
toTraceSignals.push_back( &sig ); dgDEBUGF(15,"%p",&sig);
names.push_back( filename );
if( namesSet ) openFile( sig,filename );
triger.addDependency( sig );
istringstream iss(signame);
SignalBase<int> &sig = PoolStorage::getInstance()->getSignal(iss);
addSignalToTrace(sig, filename);
dgDEBUGOUT(15);
}
......@@ -79,76 +121,70 @@ addSignalToTrace( const SignalBase<int>& sig,
* does not modify the file list (it does not close
* the files in particular.
*/
void Tracer::
clearSignalToTrace( void )
{
void Tracer::clearSignalToTrace() {
closeFiles();
toTraceSignals.clear();
triger.clearDependencies();
}
// void Tracer::
// parasite( SignalBase<int>& sig )
// {
// triger.parasite(sig);
// }
void Tracer::
openFiles( const std::string& rootdir_, const std::string& basename_,
const std::string& suffix_ )
{
void Tracer::openFiles(const std::string &rootdir_,
const std::string &basename_,
const std::string &suffix_) {
dgDEBUGIN(15);
int n = rootdir_.length();
rootdir=rootdir_;
if( (0<n)&('/'!=rootdir[n-1]) ) rootdir+='/';
std::basic_string<char>::size_type n = rootdir_.length();
rootdir = rootdir_;
if ((0 < n) & ('/' != rootdir[n - 1])) rootdir += '/';
basename=basename_;
suffix=suffix_;
basename = basename_;
suffix = suffix_;
if( files.size() ) closeFiles();
if (files.size()) closeFiles();
SignalList::const_iterator iter = toTraceSignals.begin();
NameList::const_iterator iterName = names.begin();
while( toTraceSignals.end()!=iter )
{
dgDEBUG(15) << "Open <" << (*iter)->getName()
<< "> in <" << *iterName << ">." << std::endl;
openFile( **iter,*iterName );
++iter; ++iterName;
}
while (toTraceSignals.end() != iter) {
dgDEBUG(15) << "Open <" << (*iter)->getName() << "> in <" << *iterName
<< ">." << std::endl;
openFile(**iter, *iterName);
++iter;
++iterName;
}
namesSet = true;
dgDEBUGOUT(15);
}
void Tracer::
openFile( const SignalBase<int> & sig,
const string& givenname )
{
void Tracer::openFile(const SignalBase<int> &sig, const string &givenname) {
dgDEBUGIN(15);
string signame;
if( givenname.length() )
{ signame = givenname; } else { signame = sig.shortName(); }
if (givenname.length()) {
signame = givenname;
} else {
signame = sig.shortName();
}
string filename = rootdir + basename + signame + suffix;
dgDEBUG(5) << "Sig <"<< sig.getName() << ">: new file "<< filename << endl;
std::ofstream * newfile = new std::ofstream( filename.c_str() );
files.push_back( newfile );
dgDEBUG(5) << "Sig <" << sig.getName() << ">: new file " << filename << endl;
std::ofstream *newfile = new std::ofstream(filename.c_str());
files.push_back(newfile);
dgDEBUGOUT(15);
}
void Tracer::
closeFiles( void )
{
void Tracer::closeFiles() {
dgDEBUGIN(15);
std::lock_guard<std::mutex> files_lock(files_mtx);
for( FileList::iterator iter = files.begin();files.end()!=iter;++iter )
{
std::ostream * filePtr = *iter;
delete filePtr;
}
for (FileList::iterator iter = files.begin(); files.end() != iter; ++iter) {
std::ostream *filePtr = *iter;
delete filePtr;
}
files.clear();
dgDEBUGOUT(15);
......@@ -158,164 +194,83 @@ closeFiles( void )
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
void Tracer::
record( void )
{
if(! play) { dgDEBUGINOUT(15); return;}
void Tracer::record() {
if (!play) {
dgDEBUGINOUT(15);
return;
}
dgDEBUGIN(15);
if( files.size()!=toTraceSignals.size() )
{ DG_THROW ExceptionTraces( ExceptionTraces::NOT_OPEN,
"No files open for tracing"," (file=%d != %d=sig).",
files.size(),toTraceSignals.size()); }
// Ensure record() never hangs. If the attempt to acquire the lock fails,
// then closeFiles() is active and we shouldn't write to files anyways.
std::unique_lock<std::mutex> files_lock(files_mtx, std::try_to_lock);
if (!files_lock.owns_lock()) {
dgDEBUGOUT(15);
return;
}
if (files.size() != toTraceSignals.size()) {
DG_THROW
ExceptionTraces(ExceptionTraces::NOT_OPEN, "No files open for tracing",
" (file=%d != %d=sig).", files.size(),
toTraceSignals.size());
}
FileList::iterator iterFile = files.begin();
SignalList::iterator iterSig = toTraceSignals.begin();
while( toTraceSignals.end()!=iterSig )
{
dgDEBUG(45) << "Try..." <<endl;
recordSignal( **iterFile,**iterSig );
++iterSig; ++iterFile;
}
while (toTraceSignals.end() != iterSig) {
dgDEBUG(45) << "Try..." << endl;
recordSignal(**iterFile, **iterSig);
++iterSig;
++iterFile;
}
dgDEBUGOUT(15);
}
void Tracer::
recordSignal( std::ostream& os,
const SignalBase<int>& sig )
{
void Tracer::recordSignal(std::ostream &os, const SignalBase<int> &sig) {
dgDEBUGIN(15);
try {
if( sig.getTime()>timeStart )
{
os<< sig.getTime() << "\t";
sig.trace(os); os<<endl;
}
if (sig.getTime() > timeStart) {
os << sig.getTime() << "\t";
sig.trace(os);
os << endl;
}
} catch (ExceptionAbstract &exc) {
os << exc << std::endl;
} catch (...) {
os << "Unknown error occurred while reading signal." << std::endl;
}
catch( ExceptionAbstract& exc ) { os << exc << std::endl; }
catch( ... ) { os << "Unknown error occured while reading signal." << std::endl; }
dgDEBUGOUT(15);
dgDEBUGOUT(15);
}
int& Tracer::
recordTrigger( int& dummy, const int& time )
{
dgDEBUGIN(15) << " time="<<time <<endl;
int &Tracer::recordTrigger(int &dummy, const int &time) {
dgDEBUGIN(15) << " time=" << time << endl;
record();
dgDEBUGOUT(15);
return dummy;
}
void Tracer::
trace( void )
{
}
void Tracer::trace() {}
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */
void Tracer::
display( std::ostream& os ) const
{
os << CLASS_NAME << " " << name << " [mode=" << (play?"play":"pause")
<< "] : "<< endl
<< " - Dep list: "<<endl;
for( SignalList::const_iterator iter = toTraceSignals.begin();
toTraceSignals.end()!=iter;++iter )
{ os << " -> "<<(*iter)->getName()<<endl; }
void Tracer::display(std::ostream &os) const {
os << CLASS_NAME << " " << name << " [mode=" << (play ? "play" : "pause")
<< "] : " << endl
<< " - Dep list: " << endl;
for (SignalList::const_iterator iter = toTraceSignals.begin();
toTraceSignals.end() != iter; ++iter) {
os << " -> " << (*iter)->getName() << endl;
}
}
std::ostream& operator<< ( std::ostream& os,const Tracer& t )
{
std::ostream &operator<<(std::ostream &os, const Tracer &t) {
t.display(os);
return os;
}
void Tracer::
commandLine( const std::string& cmdLine
,std::istringstream& cmdArgs
,std::ostream& os )
{
if( cmdLine=="help" )
{
os << "Tracer: "<<endl
<< " - add <obj.signal> [<filename>=<signal>] "<<endl
<< " - clear"<<endl
<< " - open [<root> [<file> [<suffix>=.dat]]]"<<endl
<< " - close "<<endl
<< " - record "<<endl
<< " - trace "<<endl
<< " - start/stop" << endl;
// << " - parasite <obj.signal> "<<endl;
Entity::commandLine( cmdLine,cmdArgs,os );
}
else if( cmdLine=="add" )
{
SignalBase<int> &sig = g_pool.getSignal(cmdArgs);
string r; cmdArgs>>ws>>r;
addSignalToTrace(sig,r);
dgDEBUG(14)<<"Add <" <<sig.getName()<<"> with nick \""<<r<<"\""<<endl;
}
else if( cmdLine=="clear" )
{ closeFiles(); toTraceSignals.clear(); }
else if( cmdLine=="open" )
{
string n,r="",s=".dat";
cmdArgs>>ws>>r;
if( cmdArgs.good() )
{
cmdArgs>>ws>>n;
if( cmdArgs.good() )
{
cmdArgs>>ws>>s;
}
}
//>>r>>s;
dgDEBUGF( 15,"Close files.");
closeFiles();
dgDEBUGF( 15,"Open files \"%s\" \"%s\" \"%s\".",
r.c_str(),n.c_str(),s.c_str());
openFiles(r,n,s);
}
else if( cmdLine=="close" ) { closeFiles(); }
else if( cmdLine=="trace" ) { trace(); }
else if( cmdLine=="record" )
{
//unsigned int t;
//cmdArgs >> ws>>t; if(! cmdArgs.good() ) t=0;
record();
}
// else if( cmdLine=="parasite" )
// {
// SignalBase<int> &sig = g_pool.getSignal( cmdArgs );
// parasite(sig);
// }
else if( cmdLine == "start" ) { play=true; }
else if( cmdLine == "stop" ) { play=false; }
else if( cmdLine == "timeStart" )
{
cmdArgs >> std::ws; if(! cmdArgs.good() )
{ os << "timeStart = " << timeStart << std::endl; }
else { cmdArgs >> timeStart; }
}
else //sotTaskAbstract::
Entity::commandLine( cmdLine,cmdArgs,os );
}
# Copyright 2010-2020, Olivier Stasse, Guilhem Saurel, JRL, CNRS/AIST, LAAS-CNRS
add_definitions(-DBOOST_TEST_DYN_LINK -DBOOST_TEST_MAIN)
add_definitions(-DTESTS_DATADIR="${CMAKE_CURRENT_SOURCE_DIR}/data")
add_definitions(-DTESTS_PLUGINDIR="${LIBRARY_OUTPUT_PATH}")
add_definitions(-DTESTS_DYNLIBSUFFIX="${CMAKE_SHARED_LIBRARY_SUFFIX}")
macro(DYNAMIC_GRAPH_TEST NAME)
add_unit_test(${NAME} "${NAME}.cpp")
target_link_libraries(${NAME} PRIVATE ${PROJECT_NAME}
Boost::unit_test_framework)
endmacro(DYNAMIC_GRAPH_TEST)
# Signal cast test.
set(signalcast_libs signal-cast-registerer-libA signal-cast-registerer-libB)
foreach(lib ${signalcast_libs})
add_library(${lib} SHARED "${lib}.cpp")
target_link_libraries(${lib} PRIVATE ${PROJECT_NAME})
endforeach()
dynamic_graph_test(signal-cast-registerer)
# Unit testing.
if(NOT APPLE)
dynamic_graph_test(entity)
endif(NOT APPLE)
dynamic_graph_test(custom-entity)
dynamic_graph_test(factory)
dynamic_graph_test(pool)
dynamic_graph_test(signal-time-dependent)
dynamic_graph_test(value)
dynamic_graph_test(signal-ptr)
dynamic_graph_test(real-time-logger)
dynamic_graph_test(debug-trace)
dynamic_graph_test(debug-tracer)
target_link_libraries(debug-tracer PRIVATE tracer)
dynamic_graph_test(debug-real-time-tracer)
target_link_libraries(debug-real-time-tracer PRIVATE tracer-real-time tracer)
dynamic_graph_test(debug-logger)
dynamic_graph_test(debug-logger-winit)
dynamic_graph_test(signal-all)
dynamic_graph_test(command-test)
dynamic_graph_test(test-mt)
target_link_libraries(test-mt PRIVATE tracer)
dynamic_graph_test(exceptions)