nixiz / cpp-ports

Ports for C++ is a header-only library for creating relations between classes without sharing their instances

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ports for C++

Ports for C++ is a header-only library for creating relations (associations) between classes without sharing their instances.

Port Structure and Usage

For detailed examples please look Demos.

Port structure is a simple template class that holds the pointers of given types. After an object registers itself with the Port class, it will become accessible over the target classes. This allows classes to access each others instance indirectly without sharing the instance.

template <class In, class Out>
class Port
{
public:
  typedef In  InType;
  typedef Out OutType;
  ...
private:
  static InPtrType  in_ptr;
  static OutPtrType out_ptr;
};

Declare Ports Definition in Class

Class declares a Port class with itself(to make itself accessible to target class) and target class so Port establishes a two-way relation with given classes.

// ClassA.h
class ClassB; // associated classes can be forward declared.
class ClassA
{
  typedef Port<ClassA, ClassB> portB;
public:
  ClassA() = default;
  ...
};

To create this relation successfully Class B also needs to declare the same port with opposite order with template types.

// ClassB.h
class ClassA;
class ClassB
{
  typedef Port<ClassB, ClassA> portA;
public:
  ClassB() = default;
  ...
};

Register Port with given InType

All classes that specify In type in the port class must register the object of that type.

// ClassA.cpp
ClassA::ClassA()
{
  PortBinder<portB::InType, portB::OutType>::Register(this);
  // or register with macro
  REGISTER_PORT(portB);
}
// ClassB.cpp
ClassB::ClassB()
{
  REGISTER_PORT(portA);
}

Port Usage With Given Association

After these operations, the relations defined with the Port class can be used as followed

// From ClassA instance to ClassB function call
ClassA::foo()
{
  portB::OutPtr()->bar();
  // or use port with macro
  PORT(portB)->bar();
}

Limitations

Will be updated.

License

This library is licensed under the MIT License - see the LICENSE file for details.

About

Ports for C++ is a header-only library for creating relations between classes without sharing their instances

License:MIT License


Languages

Language:C++ 100.0%