1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
|
/**
* @file component.h
* @brief Components' related stuff (interfaces and adapters)
* @author Cesar Mauri Loba (cesar at crea-si dot com)
*
* -------------------------------------------------------------------------
*
* Copyright: (C) 2010-12 Cesar Mauri Loba - CREA Software Systems
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SPCORE_COMPONENT_H
#define SPCORE_COMPONENT_H
#include "spcore/baseobj.h"
#include "spcore/coreruntime.h"
#include <vector>
#include <string>
#include "spcore/pin.h"
#include "spcore/iterator.h"
#include "spcore/configuration.h"
#include <algorithm>
#include <string.h>
#include <boost/type_traits.hpp>
#include <boost/static_assert.hpp>
/**
@todo typedef as a neutral window type
*/
class wxWindow;
namespace spcore {
// Forward declarations
template<class T> class IIterator;
class IInputPin;
class IOutputPin;
/**
@brief Interface for all components.
*/
class IComponent : public IBaseObject {
protected:
virtual ~IComponent() {};
public:
/**
@brief Return the name of the component.
@return Pointer to an internally managed string with the name of the component.
*/
virtual const char* GetName() const = 0;
/**
@brief Return the name of the type of the component.
@return Pointer to an internally managed string with the name.
*/
virtual const char* GetTypeName() const = 0;
/**
@brief Opens a window for this component (if it has one).
@param parent Pointer to the parent window.
@return Pointer to the window or NULL.
The lifetime of this instance belongs to the component and should never be deleted.
*/
virtual wxWindow* GetGUI(wxWindow * parent) = 0;
/**
@brief Add a child component.
@param component Smart pointer to the child component.
@return 0 when OK
Note not all components support composition.
*/
virtual int AddChild(SmartPtr<IComponent> component) = 0;
/**
@brief Get the list of children compoents.
@return Smart pointer to a component iterator. Can be NULL.
Note not all components support composition.
*/
virtual SmartPtr<IIterator<IComponent*> > QueryComponents() = 0;
/**
@brief Get the input pins of a component (if any).
@return Smart pointer to an iterator of input pins.
*/
virtual SmartPtr<IIterator<IInputPin*> > GetInputPins() = 0;
/**
@brief Get the output pins of a component (if any).
@return Smart pointer to an iterator of output pins.
*/
virtual SmartPtr<IIterator<IOutputPin*> > GetOutputPins() = 0;
/**
@brief Returns whether this component provides a thread of execution.
@return true or false.
Use Start() and Stop() methods to control thread state.
*/
virtual bool ProvidesExecThread() const = 0;
/**
@brief Starts the component (and children if any).
@return 0 if successfully started, -1 otherwise.
This call is used to start/stop the component which can perform specific
actions to enable/disable it and, when ProvidesExecThread is true then
also runs the thread.
*/
virtual int Start() = 0;
/**
@brief Stops the component.
See comment for Start() method.
*/
virtual void Stop() = 0;
/**
@brief Initializes the component.
@return 0 if successfully initialized, -1 otherwise.
*/
virtual int Initialize() = 0;
/**
@brief Finishes the component.
*/
virtual void Finish() = 0;
/**
@brief Store internal settings.
@param cfg Configuration object
*/
virtual void SaveSettings(IConfiguration& cfg)= 0;
/**
@brief Store internal settings.
@param cfg Configuration object.
*/
virtual void LoadSettings(IConfiguration& cfg)= 0;
/**
@brief Static method which searches an input pin given its name.
@param component Component
@param name Name of the input pin to look for.
@return Pointer to the pin or NULL if not found.
*/
static inline
IInputPin* FindInputPin(IComponent & component, const char * name) {
assert (name);
if (!name) return NULL;
SmartPtr<IIterator<IInputPin*> > ipit= component.GetInputPins();
for (; !ipit->IsDone(); ipit->Next()) {
if (strcmp(ipit->CurrentItem()->GetName(), name)== 0) return ipit->CurrentItem();
}
return NULL;
}
/**
@brief Static method which searches an output pin given its name.
@param component Component
@param name Name of the output pin to look for.
@return Pointer to the pin or NULL if not found.
*/
static inline
IOutputPin* FindOutputPin(IComponent & component, const char * name) {
assert (name);
if (!name) return NULL;
SmartPtr<IIterator<IOutputPin*> > opit= component.GetOutputPins();
for (; !opit->IsDone(); opit->Next()) {
if (strcmp(opit->CurrentItem()->GetName(), name)== 0) return opit->CurrentItem();
}
return NULL;
}
};
/**
@brief Adapter class to implement IComponent conforming classes.
This class is intended to be derived to implement leaf type components
(i.e. without child components). If you need to implement a component
that is composed of children use class CCompositeComponentAdapter instead.
*/
class CComponentAdapter : public IComponent {
public:
/**
@brief Constructor
@param name Name given to the component.
@param argc Number of arguments passed to the parameter argv.
@param argv Array of parameters (can be NULL if argc==0).
argc, argv are not used actually but these parameters are needed
to provide an uniform construction mechanism.
*/
CComponentAdapter(const char * name, int argc, const char * argv[])
: m_initialized(false)
, m_name(name)
{
// Unused parameters
(void) argc;
(void) argv;
}
virtual ~CComponentAdapter() {
std::vector<IInputPin*>::iterator iti;
for (iti= m_inputPins.begin(); iti!= m_inputPins.end(); ++iti)
(*iti)->Release();
m_inputPins.clear();
std::vector<IOutputPin*>::iterator ito;
for (ito= m_outputPins.begin(); ito!= m_outputPins.end(); ++ito)
(*ito)->Release();
m_outputPins.clear();
}
virtual const char* GetName() const { return m_name.c_str(); }
virtual wxWindow* GetGUI(wxWindow * parent) {
(void) parent;
return NULL;
}
virtual int AddChild(SmartPtr<IComponent> component) { return -1; }
virtual SmartPtr<IIterator<IComponent*> > QueryComponents() {
return SmartPtr<IIterator<IComponent*> >(NULL, false);
}
virtual SmartPtr<IIterator<IInputPin*> > GetInputPins() {
return SmartPtr<IIterator<IInputPin*> >(new CIteratorVector<IInputPin *>(m_inputPins), false);
}
virtual SmartPtr<IIterator<IOutputPin*> > GetOutputPins() {
return SmartPtr<IIterator<IOutputPin*> >(new CIteratorVector<IOutputPin *>(m_outputPins), false);
}
virtual bool ProvidesExecThread() const { return false; }
virtual int Start() {
int retval= Initialize();
if (retval) return retval;
return DoStart();
}
virtual void Stop() { DoStop(); }
virtual int Initialize() {
if (m_initialized) return 0;
int retval= DoInitialize();
if (retval) return retval;
m_initialized= true;
return 0;
}
virtual void Finish() {
if (m_initialized) {
DoFinish();
m_initialized= false;
}
}
virtual void SaveSettings(IConfiguration&) {}
virtual void LoadSettings(IConfiguration&) {}
protected:
/**
@brief Perform the actual component initialization.
@return 0 if successfully initialized, -1 otherwise.
*/
virtual int DoInitialize() { return 0; }
/**
@brief Perform the actual component finalization.
*/
virtual void DoFinish() { }
/**
@brief Return whether the component is initialized.
*/
virtual bool IsInitialized() const { return m_initialized; }
/**
@brief Actual component start.
*/
virtual int DoStart() { return 0; }
/**
@brief Actual component stop.
*/
virtual void DoStop() {}
/**
@brief Registers a new input pin.
@return -1 when error (pin already registered).
*/
int RegisterInputPin(IInputPin & pin) {
std::vector<IInputPin*>::iterator it= find (m_inputPins.begin(), m_inputPins.end(), &pin);
if (it!= m_inputPins.end()) return -1; // Already registered
pin.AddRef();
m_inputPins.push_back (&pin);
return 0;
}
/**
@brief Registers a new output pin.
@return -1 when error (pin already registered).
*/
int RegisterOutputPin(IOutputPin & pin) {
std::vector<IOutputPin*>::iterator it= find (m_outputPins.begin(), m_outputPins.end(), &pin);
if (it!= m_outputPins.end()) return -1; // Already registered
pin.AddRef();
m_outputPins.push_back (&pin);
return 0;
}
private:
bool m_initialized;
std::vector<IInputPin *> m_inputPins;
std::vector<IOutputPin *> m_outputPins;
std::string m_name;
};
/**
@brief Adapter class to implement IComponent conforming classes which,
in turn, need composition support.
*/
class CCompositeComponentAdapter : public CComponentAdapter {
public:
CCompositeComponentAdapter(const char * name, int argc, const char * argv[])
: CComponentAdapter(name, argc, argv) { }
virtual ~CCompositeComponentAdapter() {
Stop();
Finish();
// Delete components
for (std::vector<IComponent*>::iterator it= m_components.begin(); it!= m_components.end(); ++it)
(*it)->Release();
}
virtual int AddChild(SmartPtr<IComponent> component) {
std::vector<IComponent*>::iterator it= m_components.begin();
for(; it!= m_components.end(); ++it) {
// Check if the component (by pointer) has been already registered.
// TODO: check that component is not registered (comparing pointers)
// anywhere in this component composite
if ((*it)== component.get()) break;
// Check also by name
if (strcmp((*it)->GetName(), component->GetName())== 0) break;
}
if (it!= m_components.end()) {
// Component already registered
return -1;
}
// Finaly add component to vector
component->AddRef();
m_components.push_back(component.get());
return 0;
}
virtual SmartPtr<IIterator<IComponent*> > QueryComponents() {
return SmartPtr<IIterator<IComponent*> >(new CIteratorVector<IComponent*>(m_components), false);
}
virtual int Start() {
int retval= Initialize();
if (retval!= 0) return retval;
std::vector<IComponent*>::iterator it;
for (it= m_components.begin(); retval== 0 && it!= m_components.end(); ++it)
retval= (*it)->Start();
if (retval!= 0) Stop();
return retval;
}
virtual void Stop() {
std::vector<IComponent*>::iterator it;
for (it= m_components.begin(); it!= m_components.end(); ++it)
(*it)->Stop();
}
virtual int Initialize() {
int retval= 0;
retval= DoInitialize();
if (retval) return retval;
std::vector<IComponent*>::iterator it;
for (it= m_components.begin(); retval== 0 && it!= m_components.end(); ++it)
retval= (*it)->Initialize();
if (retval!= 0) Finish();
return retval;
}
virtual void Finish() {
Stop();
DoFinish();
std::vector<IComponent*>::iterator it;
for (it= m_components.begin(); it!= m_components.end(); ++it)
(*it)->Finish();
}
private:
std::vector<IComponent *> m_components;
};
/**
@brief Interface class for factories of components.
*/
class IComponentFactory : public IBaseObject {
protected:
virtual ~IComponentFactory() {}
public:
/**
@brief Return the name of the type of components this factory creates.
@return Pointer to an internally managed string.
*/
virtual const char* GetName() const = 0;
/**
@brief Create a new component instance.
@param name Name the component will be given.
@param argc Number of arguments passed to the parameter argv.
@param argv Array of parameters (can be NULL if argc==0).
@return Smart pointer to the newly created component which can contain NULL if an error ocurred.
*/
virtual SmartPtr<IComponent> CreateInstance(const char * name, int argc, const char * argv[]) = 0;
};
/**
@brief Connects an output pin of a component with an input of another component.
@param srcComponent Source component.
@param srcPin Number of order of the output of the source component.
@param dstComponent Destination component.
@param dstPin Number of order of the input of the destination component.
@return 0 when success
@return -1 invalid component
@return -2 trying to connect the component with itself
@return -3 invalid pin number
@return -4 pin type mismatch
Uses pin number to refer to a specific pin
*/
inline int Connect(IComponent * srcComponent, unsigned int srcPin, IComponent * dstComponent, unsigned int dstPin) {
if (srcComponent== NULL || dstComponent== NULL) return -1; // Invalid component
if (srcComponent== dstComponent) return -2; // Forbids connecting pins of the same component
unsigned int i;
SmartPtr<IIterator<IOutputPin*> > itop= srcComponent->GetOutputPins();
itop->First();
i= 0;
while (i!= srcPin && !itop->IsDone()) {
itop->Next();
++i;
}
if (itop->IsDone()) return -3; // Invalid pin number
SmartPtr<IIterator<IInputPin*> > itip= dstComponent->GetInputPins();
itip->First();
i= 0;
while (i!=dstPin && !itip->IsDone()) {
itip->Next();
++i;
}
if (itip->IsDone()) return -3; // Invalid pin number
if (itop->CurrentItem()->Connect (*(itip->CurrentItem()))!= 0) return -4; // Pin type mismatch
return 0;
}
/**
@brief Connects an output pin of a component with an input of another component.
@param srcComponent Source component.
@param srcPin Namne of the output of the source component.
@param dstComponent Destination component.
@param dstPin Name of the input of the destination component.
@return 0 when success
@return -1 invalid component
@return -2 trying to connect the component with itself
@return -3 invalid pin number
@return -4 pin type mismatch
*/
inline int Connect(IComponent * srcComponent, const char * srcPin, IComponent * dstComponent, const char * dstPin) {
if (srcComponent== NULL || dstComponent== NULL) return -1; // Invalid component
if (srcComponent== dstComponent) return -2; // Forbids connecting pins of the same component
if (srcPin== NULL || dstPin== NULL) return -3; // Invalid pin name
SmartPtr<IIterator<IOutputPin*> > itop= srcComponent->GetOutputPins();
for (itop->First(); !itop->IsDone(); itop->Next()) {
if (strcmp(srcPin, itop->CurrentItem()->GetName())== 0) break;
}
if (itop->IsDone()) return -3; // Invalid pin name
SmartPtr<IIterator<IInputPin*> > itip= dstComponent->GetInputPins();
for (itip->First(); !itip->IsDone(); itip->Next()) {
if (strcmp(dstPin, itip->CurrentItem()->GetName())== 0) break;
}
if (itip->IsDone()) return -3; // Invalid pin name
if (itop->CurrentItem()->Connect (*(itip->CurrentItem()))!= 0) return -4; // Pin type mismatch
return 0;
}
/**
@brief Helper template class to create simple component factories.
@tparam COTYPE Type of the component.
*/
template <typename COTYPE>
class ComponentFactory : public spcore::IComponentFactory {
BOOST_STATIC_ASSERT( (boost::is_base_of<IComponent,COTYPE>::value) );
public:
virtual const char* GetName() const { return COTYPE::getTypeName(); }
virtual SmartPtr<IComponent> CreateInstance(const char * name, int argc, const char * argv[]) {
std::string exceptionMessage;
try {
return SmartPtr<IComponent>(new COTYPE(name, argc, argv), false);
}
catch(std::exception& e) {
exceptionMessage= e.what();
}
catch(...) {
exceptionMessage= "unexpected error creating component: " + std::string(name);
}
// If code reaches this point means that an exception has been raised
// signal this error condition by adding a log entry and retuning a
// null instance
std::string msg("error creating instance:");
msg.append(name);
if (exceptionMessage.size()> 0) {
msg.append (":");
msg.append (exceptionMessage);
}
getSpCoreRuntime()->LogMessage(ICoreRuntime::LOG_ERROR, msg.c_str(), "spcore");
return SmartPtr<IComponent>(NULL);
}
};
/**
@brief Helper template to create simple singleton component factories.
@tparam COTYPE Type of the component.
*/
template <typename COTYPE>
class SingletonComponentFactory : public spcore::IComponentFactory {
BOOST_STATIC_ASSERT( (boost::is_base_of<IComponent,COTYPE>::value) );
public:
virtual const char* GetName() const { return COTYPE::getTypeName(); }
virtual SmartPtr<IComponent> CreateInstance(const char * name, int argc, const char * argv[]) {
if (m_instance.get()) return m_instance;
// Crete instance for the first time
std::string exceptionMessage;
try {
m_instance= SmartPtr<COTYPE>(new COTYPE(name, argc, argv), false);
return m_instance;
}
catch(std::exception& e) {
exceptionMessage= e.what();
}
catch(...) {
exceptionMessage= "unexpected error creating component: " + std::string(name);
}
// If code reaches this point means that an exception has been raised
// signal this error condition by adding a log entry and retuning a
// null instance
assert (m_instance.get()== NULL);
std::string msg("error creating instance:");
msg.append(name);
if (exceptionMessage.size()> 0) {
msg.append (":");
msg.append (exceptionMessage);
}
getSpCoreRuntime()->LogMessage(ICoreRuntime::LOG_ERROR, msg.c_str(), "spcore");
return SmartPtr<IComponent>(NULL);
}
private:
SmartPtr<COTYPE> m_instance;
};
} // namespace spcore
#endif
|