Skip navigation links
IT services and product development
Menu
TwoLogs
IT services and product development

Source code for C_Connector

Download

The source files for this module are listed below.  You can also download the module C_Connector as a zip archive; this archive contains all the source files and documentation.

Description

Connectors allow bi-directional linked list-like connections between an arbitrary amount of objects.

Information

The CConnector class is a base class that allows your classes to be linked together in a linked list-like manner.  All the links are bi-directional.  You can link as many objects together as you want; the linking structure is not limited to a flat (two-dimensional) list.

To connect two objects together, just call Connect on one of them and pass it the object to connect to.  The reverse connection will also automatically be added.  To disconnect two objects, just call the Disconnect method and pass it the object to disconnect from.  You can also call the DisconnectAll method to disconnect the object from all it's connected objects.  The IsConnected and ConnectionCount methods allow you to query the connection status of an object.

The CConnector object also implements STL-like begin() and end() methods to access all connected objects via a standard STL iterator.

If a CConnector object goes out of scope, it will also automatically break all it's connections.

Files

Each file belonging to this source code module is listed below.

Connector.h

/*******************************************************************************

  Version: 3
  Author:  Carl Colijn, TwoLogs
  Contact: c.colijn@twologs.com
  Source:  https://www.twologs.com/sourcecode

  This code is freely distributable, as long as this comment remains intact.
  If you find this source useful, you may use this code in your own projects
  free of charge, but some acknowledgement to the author of this code is always
  appreciated :)
  The source is however distributed 'as is' without waranty and/or support, and
  may not be fit for each and every application.  Use it at your own discretion
  and at your own risk.
  The source already has undergone testing.  This doesn't mean however that all
  bugs are removed from this piece of code.  If you find one of them, please
  contact me about it.  I can however not guarantee when and if the bug will be
  fixed.

  More information about this module can be found in the accompanying HTML file.

*******************************************************************************/

#ifndef INCLUDE_TWOLOGS_COMMON_CONNECTOR_H
#define INCLUDE_TWOLOGS_COMMON_CONNECTOR_H

#include <set>

class CConnector {
public:
  // Con- & destructor
  CConnector();
  virtual ~CConnector();

  // Makes a new connection with the given connector
  bool Connect(CConnector* poOther);

  // Disconnects the connection from the given connector
  bool Disconnect(CConnector* poOther);

  // Disconnects all connectors
  bool DisconnectAll();

  // The range of connected connectors
  typedef std::multiset<CConnector*> CConnectors;
  CConnectors::iterator begin();
  CConnectors::iterator end();

  // Returns whether we are connected to other connectors
  bool IsConnected() const;

  // Returns the number of connected connectors
  unsigned long ConnectionCount() const;

private:
  // The connections connected to this connector
  CConnectors m_apoConnectors;
};

#endif // INCLUDE_TWOLOGS_COMMON_CONNECTOR_H

Connector.cpp

/*******************************************************************************

  Version: 3
  Author:  Carl Colijn, TwoLogs
  Contact: c.colijn@twologs.com
  Source:  https://www.twologs.com/sourcecode

  This code is freely distributable, as long as this comment remains intact.
  If you find this source useful, you may use this code in your own projects
  free of charge, but some acknowledgement to the author of this code is always
  appreciated :)
  The source is however distributed 'as is' without waranty and/or support, and
  may not be fit for each and every application.  Use it at your own discretion
  and at your own risk.
  The source already has undergone testing.  This doesn't mean however that all
  bugs are removed from this piece of code.  If you find one of them, please
  contact me about it.  I can however not guarantee when and if the bug will be
  fixed.

  More information about this module can be found in the accompanying HTML file.

*******************************************************************************/

#include "Connector.h"

// Con- & destructor
CConnector::CConnector() {
}
CConnector::~CConnector() {
  // Disconnect all our connections
  while (m_apoConnectors.size() > 0) {
    Disconnect(*m_apoConnectors.begin());
  }
}

// Makes a new connection with the given connector
bool CConnector::Connect(CConnector* poOtherConnector) {
  // Add us to the list of the other connector
  auto ppoOtherNewConnector = poOtherConnector->m_apoConnectors.insert(this);
  bool bSuccess = ppoOtherNewConnector != poOtherConnector->m_apoConnectors.end();
  if (bSuccess) {
    // Done -> also add the other connector to our list
    auto ppoOurNewConnector = m_apoConnectors.insert(poOtherConnector);
    bSuccess = ppoOurNewConnector != m_apoConnectors.end();
    if (!bSuccess) {
      // Couldn't -> remove us from the list of the other connector again
      poOtherConnector->m_apoConnectors.erase(ppoOtherNewConnector);
    }
  }

  // And return if a new connection could be established
  return bSuccess;
}

// Disconnects the connection from the given connector
bool CConnector::Disconnect(CConnector* poOtherConnector) {
  // Disconnect us from the other connector
  return 1 == m_apoConnectors.erase(poOtherConnector) &&
  // ... and the other way around as well
         1 == poOtherConnector->m_apoConnectors.erase(this);
}

// Disconnects all connectors
bool CConnector::DisconnectAll() {
  bool bSuccess = true;
  for(CConnector* poOtherConnector: m_apoConnectors) {
    bSuccess = bSuccess && (1 == poOtherConnector->m_apoConnectors.erase(this));
  }
  m_apoConnectors.clear();
  return bSuccess;
}

// The range of connected connectors
CConnector::CConnectors::iterator CConnector::begin() {
  return m_apoConnectors.begin();
}
CConnector::CConnectors::iterator CConnector::end() {
  return m_apoConnectors.end();
}

// Returns whether we are connected to other connectors
bool CConnector::IsConnected() const {
  return m_apoConnectors.begin() != m_apoConnectors.end();
}

// Returns the number of connected connectors
unsigned long CConnector::ConnectionCount() const {
  return m_apoConnectors.size();
}