Here is a sample of a collection template implementing ICapeIdentification and ICapeCollection which similar to the sdk documentation:
- Code: Select all
#pragma once
#include <COBIA.h>
using namespace COBIA;
template <typename MyCollectionItem> class MyCollection :
public CapeOpenObject<MyCollection<MyCollectionItem>>,
public CAPEOPEN_1_2::CapeIdentificationAdapter<MyCollection<MyCollectionItem>>,
public CAPEOPEN_1_2::CapeCollectionAdapter<MyCollectionItem,MyCollection<MyCollectionItem>> {
// Members
CapeStringImpl& unitName;
std::vector<MyCollectionItem> items;
public:
const CapeStringImpl getDescriptionForErrorSource() {
return COBIATEXT("Collection of ") + unitName;
}
MyCollection(CapeStringImpl& _unitName) : unitName(_unitName) {
}
~MyCollection() {
}
void addItem(MyCollectionItem item) {
items.emplace_back(item);
}
// CAPEOPEN_1_2::ICapeIdentification
void getComponentName(/*out*/ CapeString name) {
name = COBIATEXT("foo");
}
void putComponentName(/*in*/ CapeString name) {
throw cape_open_error(COBIAERR_Denied);
}
void getComponentDescription(/*out*/ CapeString desc) {
desc = COBIATEXT("bar");
}
void putComponentDescription(/*in*/ CapeString desc) {
throw cape_open_error(COBIAERR_Denied);
}
// CAPEOPEN_1_2::ICapeCollection<CAPEOPEN_1_2::ICapeUnitPort>
// Lookup by index
MyCollectionItem Item(/*in*/ CapeInteger index) {
if ((index < 0) || (index >= (CapeInteger)items.size())) {
throw cape_open_error(COBIAERR_NoSuchItem);
}
return items[index];
}
// Lookup by name
MyCollectionItem Item(/*in*/ CapeString name) {
CapeStringImpl itemName;
for (MyCollectionItem& item : items) {
CAPEOPEN_1_2::CapeIdentification identification(item);
identification.getComponentName(itemName);
if (itemName == name) {
return item;
}
}
throw cape_open_error(COBIAERR_NoSuchItem);
}
CapeInteger getCount() {
return (CapeInteger)items.size();
}
};
template <typename MyCollectionItem> using MyCollectionPtr = CapeOpenObjectSmartPointer<MyCollection<MyCollectionItem>>;
And here is a sample of the port implementation also similar to the documentation.
- Code: Select all
#pragma once
#include <COBIA.h>
using namespace COBIA;
class MyMaterialPort :
public CapeOpenObject<MyMaterialPort>,
public CAPEOPEN_1_2::CapeIdentificationAdapter<MyMaterialPort>,
public CAPEOPEN_1_2::CapeUnitPortAdapter<MyMaterialPort> {
// Members
CapeStringImpl &unitName;
CAPEOPEN_1_2::CapeValidationStatus &unitValidationStatus;
CapeStringImpl portName;
CAPEOPEN_1_2::CapePortDirection direction;
CAPEOPEN_1_2::CapeThermoMaterial connectedMaterial;
CapeBoolean _primary;
public:
const CapeStringImpl getDescriptionForErrorSource() {
return portName + COBIATEXT(" port of ") + unitName;
}
MyMaterialPort(CapeStringImpl& _unitName, CAPEOPEN_1_2::CapeValidationStatus& _unitValidationStatus,
const COBIACHAR* _portName, CAPEOPEN_1_2::CapePortDirection _direction, CapeBoolean _primary) :
unitName(_unitName), unitValidationStatus(_unitValidationStatus),
portName(_portName), direction(_direction), primary(_primary) {
}
~MyMaterialPort() {
}
CapeBoolean isPrimary() {
return primary;
}
CAPEOPEN_1_2::CapeThermoMaterial getMaterial() {
return connectedMaterial;
}
// CAPEOPEN_1_2::ICapeIdentification
void getComponentName(/*out*/ CapeString name) {
name = this->portName;
}
void putComponentName(/*in*/ CapeString name) {
throw cape_open_error(COBIAERR_Denied);
}
void getComponentDescription(/*out*/ CapeString desc) {
desc = COBIATEXT("Material Port");
}
void putComponentDescription(/*in*/ CapeString desc) {
throw cape_open_error(COBIAERR_Denied);
}
// CAPEOPEN_1_2::ICapeUnitPort
CAPEOPEN_1_2::CapePortType getPortType() {
return CAPEOPEN_1_2::CAPE_MATERIAL;
}
CAPEOPEN_1_2::CapePortDirection getDirection() {
return direction;
}
CapeInterface getConnectedObject() {
return connectedMaterial;
}
void Connect(/*in*/ CapeInterface objectToConnect) {
CAPEOPEN_1_2::CapeThermoMaterial newConnectedMaterial = objectToConnect;
if (!newConnectedMaterial) {
//expected a material object
throw cape_open_error(COBIAERR_NoSuchInterface);
}
unitValidationStatus = CAPEOPEN_1_2::CAPE_NOT_VALIDATED;
connectedMaterial = newConnectedMaterial;
}
void Disconnect() {
unitValidationStatus = CAPEOPEN_1_2::CAPE_NOT_VALIDATED;
connectedMaterial.clear();
}
};
using MyMaterialPortPtr = CapeOpenObjectSmartPointer<MyMaterialPort>;
In my UnitOperation class I can define and initialise the collection as follows:
MyCollectionPtr<CAPEOPEN_1_2::CapeUnitPort> portCollection;
but the build error happens if I define
MyCollectionPtr<MyMaterialPortPtr> portCollection;
The same applies to other parameter implementations too if used as template argument instead of CAPEOPEN_1_2::CapeParameter.