Skip to content

Commit

Permalink
Add the extension in read method and not by read caller (#132)
Browse files Browse the repository at this point in the history
* Add the extension in read method and not by read caller
* Remove covariance

Signed-off-by: Sébastien LAIGRE <[email protected]>
Signed-off-by: Mathieu BAGUE <[email protected]>
  • Loading branch information
sebalaig authored Jun 22, 2020
1 parent 1edf7cb commit ceefe5d
Show file tree
Hide file tree
Showing 26 changed files with 57 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace entsoe {

class EntsoeAreaXmlSerializer : public converter::xml::AbstractExtensionXmlSerializer {
public: // ExtensionXmlSerializer
std::unique_ptr<Extension> read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const override;
Extension& read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const override;

void write(const Extension& extension, converter::xml::NetworkXmlWriterContext& context) const override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace entsoe {

class MergedXnodeXmlSerializer : public converter::xml::AbstractExtensionXmlSerializer {
public: // ExtensionXmlSerializer
std::unique_ptr<Extension> read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const override;
Extension& read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const override;

void write(const Extension& extension, converter::xml::NetworkXmlWriterContext& context) const override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define POWSYBL_IIDM_EXTENSIONS_ENTSOE_XNODEXMLSERIALIZER_HPP

#include <powsybl/iidm/converter/xml/AbstractExtensionXmlSerializer.hpp>
#include <powsybl/iidm/extensions/entsoe/Xnode.hpp>

namespace powsybl {

Expand All @@ -20,7 +21,7 @@ namespace entsoe {

class XnodeXmlSerializer : public converter::xml::AbstractExtensionXmlSerializer {
public: // ExtensionXmlSerializer
std::unique_ptr<Extension> read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const override;
Extension& read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const override;

void write(const Extension& extension, converter::xml::NetworkXmlWriterContext& context) const override;

Expand Down
5 changes: 3 additions & 2 deletions extensions/entsoe/src/EntsoeAreaXmlSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ EntsoeAreaXmlSerializer::EntsoeAreaXmlSerializer() :
AbstractExtensionXmlSerializer("entsoeArea", "network", "ea", "http://www.itesla_project.eu/schema/iidm/ext/entsoe_area/1_0") {
}

std::unique_ptr<Extension> EntsoeAreaXmlSerializer::read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const {
Extension& EntsoeAreaXmlSerializer::read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const {
if (!stdcxx::isInstanceOf<Substation>(extendable)) {
throw AssertionError(stdcxx::format("Unexpected extendable type: %1% (%2% expected)", stdcxx::demangle(extendable), stdcxx::demangle<Substation>()));
}
auto& substation = dynamic_cast<Substation&>(extendable);

const auto& code = Enum::fromString<EntsoeGeographicalCode>(context.getReader().readUntilEndElement(getExtensionName()));

return stdcxx::make_unique<EntsoeArea>(substation, code);
extendable.addExtension(stdcxx::make_unique<EntsoeArea>(substation, code));
return extendable.getExtension<EntsoeArea>();
}

void EntsoeAreaXmlSerializer::write(const Extension& extension, converter::xml::NetworkXmlWriterContext& context) const {
Expand Down
5 changes: 3 additions & 2 deletions extensions/entsoe/src/MergedXnodeXmlSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ MergedXnodeXmlSerializer::MergedXnodeXmlSerializer() :
AbstractExtensionXmlSerializer("mergedXnode", "network", "mxn", "http://www.itesla_project.eu/schema/iidm/ext/merged_xnode/1_0") {
}

std::unique_ptr<Extension> MergedXnodeXmlSerializer::read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const {
Extension& MergedXnodeXmlSerializer::read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const {
if (!stdcxx::isInstanceOf<Line>(extendable)) {
throw AssertionError(stdcxx::format("Unexpected extendable type: %1% (%2% expected)", stdcxx::demangle(extendable), stdcxx::demangle<Line>()));
}
Expand All @@ -43,7 +43,8 @@ std::unique_ptr<Extension> MergedXnodeXmlSerializer::read(Extendable& extendable
const auto& xnodeQ2 = context.getReader().getAttributeValue<double>("xnodeQ2");
const auto& code = context.getReader().getAttributeValue("code");

return stdcxx::make_unique<Extension, MergedXnode>(line, rdp, xdp, xnodeP1, xnodeQ1, xnodeP2, xnodeQ2, "", "", code);
extendable.addExtension(stdcxx::make_unique<MergedXnode>(line, rdp, xdp, xnodeP1, xnodeQ1, xnodeP2, xnodeQ2, "", "", code));
return extendable.getExtension<MergedXnode>();
}

void MergedXnodeXmlSerializer::write(const Extension& extension, converter::xml::NetworkXmlWriterContext& context) const {
Expand Down
4 changes: 2 additions & 2 deletions extensions/entsoe/src/XnodeXmlSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ XnodeXmlSerializer::XnodeXmlSerializer() :
AbstractExtensionXmlSerializer("xnode", "network", "xn", "http://www.itesla_project.eu/schema/iidm/ext/xnode/1_0") {
}

std::unique_ptr<Extension> XnodeXmlSerializer::read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const {
Extension& XnodeXmlSerializer::read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const {
const auto& code = context.getReader().getAttributeValue("code");
extendable.newExtension<XnodeAdder>().withCode(code).add();
return stdcxx::make_unique<Xnode>(extendable.getExtension<Xnode>());
return extendable.getExtension<Xnode>();
}

void XnodeXmlSerializer::write(const Extension& extension, converter::xml::NetworkXmlWriterContext& context) const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace iidm {

class ActivePowerControlXmlSerializer : public converter::xml::AbstractExtensionXmlSerializer {
public: // ExtensionXmlSerializer
std::unique_ptr<Extension> read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const override;
Extension& read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const override;

void write(const Extension& extension, converter::xml::NetworkXmlWriterContext& context) const override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace iidm {

class CoordinatedReactiveControlXmlSerializer : public converter::xml::AbstractExtensionXmlSerializer {
public: // ExtensionXmlSerializer
std::unique_ptr<Extension> read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const override;
Extension& read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const override;

void write(const Extension& extension, converter::xml::NetworkXmlWriterContext& context) const override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace iidm {

class ThreeWindingsTransformerPhaseAngleClockXmlSerializer : public converter::xml::AbstractExtensionXmlSerializer {
public: // ExtensionXmlSerializer
std::unique_ptr<Extension> read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const override;
Extension& read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const override;

void write(const Extension& extension, converter::xml::NetworkXmlWriterContext& context) const override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace iidm {

class TwoWindingsTransformerPhaseAngleClockXmlSerializer : public converter::xml::AbstractExtensionXmlSerializer {
public: // ExtensionXmlSerializer
std::unique_ptr<Extension> read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const override;
Extension& read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const override;

void write(const Extension& extension, converter::xml::NetworkXmlWriterContext& context) const override;

Expand Down
6 changes: 2 additions & 4 deletions extensions/iidm/src/ActivePowerControlXmlSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
#include <powsybl/iidm/extensions/iidm/ActivePowerControl.hpp>
#include <powsybl/iidm/extensions/iidm/ActivePowerControlAdder.hpp>

#include <powsybl/stdcxx/make_unique.hpp>

#include <powsybl/xml/XmlStreamReader.hpp>
#include <powsybl/xml/XmlStreamWriter.hpp>

Expand All @@ -36,12 +34,12 @@ ActivePowerControlXmlSerializer::ActivePowerControlXmlSerializer() :
AbstractExtensionXmlSerializer("activePowerControl", "network", "apc", "http://www.itesla_project.eu/schema/iidm/ext/active_power_control/1_0") {
}

std::unique_ptr<Extension> ActivePowerControlXmlSerializer::read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const {
Extension& ActivePowerControlXmlSerializer::read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const {
const auto& participate = context.getReader().getAttributeValue<bool>("participate");
const auto& droop = context.getReader().getAttributeValue<double>("droop");

extendable.newExtension<ActivePowerControlAdder>().withParticipate(participate).withDroop(droop).add();
return stdcxx::make_unique<ActivePowerControl>(extendable.getExtension<ActivePowerControl>());
return extendable.getExtension<ActivePowerControl>();
}

void ActivePowerControlXmlSerializer::write(const Extension& extension, converter::xml::NetworkXmlWriterContext& context) const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ CoordinatedReactiveControlXmlSerializer::CoordinatedReactiveControlXmlSerializer
AbstractExtensionXmlSerializer("coordinatedReactiveControl", "network", "crc", "http://www.powsybl.org/schema/iidm/ext/coordinated_reactive_control/1_0") {
}

std::unique_ptr<Extension> CoordinatedReactiveControlXmlSerializer::read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const {
Extension& CoordinatedReactiveControlXmlSerializer::read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const {
if (!stdcxx::isInstanceOf<Generator>(extendable)) {
throw AssertionError(stdcxx::format("Unexpected extendable type: %1% (%2% expected)", stdcxx::demangle(extendable), stdcxx::demangle<Generator>()));
}
auto& generator = dynamic_cast<Generator&>(extendable);

const auto& qPercent = context.getReader().getAttributeValue<double>("qPercent");

return stdcxx::make_unique<Extension, CoordinatedReactiveControl>(generator, qPercent);
extendable.addExtension(stdcxx::make_unique<CoordinatedReactiveControl>(generator, qPercent));
return extendable.getExtension<CoordinatedReactiveControl>();
}

void CoordinatedReactiveControlXmlSerializer::write(const Extension& extension, converter::xml::NetworkXmlWriterContext& context) const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ThreeWindingsTransformerPhaseAngleClockXmlSerializer::ThreeWindingsTransformerPh
AbstractExtensionXmlSerializer("threeWindingsTransformerPhaseAngleClock", "network", "threewtpac", "http://www.powsybl.org/schema/iidm/ext/three_windings_transformer_phase_angle_clock/1_0") {
}

std::unique_ptr<Extension> ThreeWindingsTransformerPhaseAngleClockXmlSerializer::read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const {
Extension& ThreeWindingsTransformerPhaseAngleClockXmlSerializer::read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const {
if (!stdcxx::isInstanceOf<ThreeWindingsTransformer>(extendable)) {
throw AssertionError(stdcxx::format("Unexpected extendable type: %1% (%2% expected)", stdcxx::demangle(extendable), stdcxx::demangle<ThreeWindingsTransformer>()));
}
Expand All @@ -42,15 +42,16 @@ std::unique_ptr<Extension> ThreeWindingsTransformerPhaseAngleClockXmlSerializer:
const auto& phaseAngleClockLeg2 = context.getReader().getOptionalAttributeValue("phaseAngleClockLeg2", 0UL);
const auto& phaseAngleClockLeg3 = context.getReader().getOptionalAttributeValue("phaseAngleClockLeg3", 0UL);

return stdcxx::make_unique<Extension, ThreeWindingsTransformerPhaseAngleClock>(transformer, phaseAngleClockLeg2, phaseAngleClockLeg3);
extendable.addExtension(stdcxx::make_unique<ThreeWindingsTransformerPhaseAngleClock>(transformer, phaseAngleClockLeg2, phaseAngleClockLeg3));
return extendable.getExtension<ThreeWindingsTransformerPhaseAngleClock>();
}

void ThreeWindingsTransformerPhaseAngleClockXmlSerializer::write(const Extension& extension, converter::xml::NetworkXmlWriterContext& context) const {
const auto& ext = safeCast<ThreeWindingsTransformerPhaseAngleClock>(extension);
void ThreeWindingsTransformerPhaseAngleClockXmlSerializer::write(const Extension& extension, converter::xml::NetworkXmlWriterContext& context) const {
const auto& ext = safeCast<ThreeWindingsTransformerPhaseAngleClock>(extension);

context.getExtensionsWriter().writeOptionalAttribute("phaseAngleClockLeg2", ext.getPhaseAngleClockLeg2(), 0UL);
context.getExtensionsWriter().writeOptionalAttribute("phaseAngleClockLeg3", ext.getPhaseAngleClockLeg3(), 0UL);
}
context.getExtensionsWriter().writeOptionalAttribute("phaseAngleClockLeg2", ext.getPhaseAngleClockLeg2(), 0UL);
context.getExtensionsWriter().writeOptionalAttribute("phaseAngleClockLeg3", ext.getPhaseAngleClockLeg3(), 0UL);
}

} // namespace iidm

Expand All @@ -59,3 +60,4 @@ void ThreeWindingsTransformerPhaseAngleClockXmlSerializer::write(const Extension
} // namespace iidm

} // namespace powsybl

Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ TwoWindingsTransformerPhaseAngleClockXmlSerializer::TwoWindingsTransformerPhaseA
AbstractExtensionXmlSerializer("twoWindingsTransformerPhaseAngleClock", "network", "twowtpac", "http://www.powsybl.org/schema/iidm/ext/two_windings_transformer_phase_angle_clock/1_0") {
}

std::unique_ptr<Extension> TwoWindingsTransformerPhaseAngleClockXmlSerializer::read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const {
Extension& TwoWindingsTransformerPhaseAngleClockXmlSerializer::read(Extendable& extendable, converter::xml::NetworkXmlReaderContext& context) const {
if (!stdcxx::isInstanceOf<TwoWindingsTransformer>(extendable)) {
throw AssertionError(stdcxx::format("Unexpected extendable type: %1% (%2% expected)", stdcxx::demangle(extendable), stdcxx::demangle<TwoWindingsTransformer>()));
}
auto& transformer = dynamic_cast<TwoWindingsTransformer&>(extendable);

const auto& phaseAngleClock = context.getReader().getOptionalAttributeValue("phaseAngleClock", 0UL);

return stdcxx::make_unique<Extension, TwoWindingsTransformerPhaseAngleClock>(transformer, phaseAngleClock);
extendable.addExtension(stdcxx::make_unique<TwoWindingsTransformerPhaseAngleClock>(transformer, phaseAngleClock));
return extendable.getExtension<TwoWindingsTransformerPhaseAngleClock>();
}

void TwoWindingsTransformerPhaseAngleClockXmlSerializer::write(const Extension& extension, converter::xml::NetworkXmlWriterContext& context) const {
Expand All @@ -55,3 +56,4 @@ void TwoWindingsTransformerPhaseAngleClockXmlSerializer::write(const Extension&
} // namespace iidm

} // namespace powsybl

4 changes: 2 additions & 2 deletions include/powsybl/iidm/converter/xml/ExtensionXmlSerializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ class ExtensionXmlSerializer : public ExtensionProvider {
* @param extendable The holder of the new extension
* @param context The current XML context
*
* @return An extension
* @return The added extension
*
* @throw An {@link AssertionError} if the extension is not compatible with the extendable type
*/
virtual std::unique_ptr<Extension> read(Extendable& extendable, NetworkXmlReaderContext& context) const = 0;
virtual Extension& read(Extendable& extendable, NetworkXmlReaderContext& context) const = 0;

/**
* Write an extension in XML
Expand Down
3 changes: 1 addition & 2 deletions src/iidm/converter/xml/NetworkXml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ void readExtensions(Identifiable& identifiable, NetworkXmlReaderContext& context

stdcxx::CReference<ExtensionXmlSerializer> serializer = extensionProviders.findProvider(extensionName);
if (serializer) {
std::unique_ptr<Extension> extension = serializer.get().read(identifiable, context);
identifiable.addExtension(std::move(extension));
serializer.get().read(identifiable, context);
} else {
extensionsNotFound.insert(extensionName);
context.getReader().readUntilEndElement(extensionName, []() {});
Expand Down
5 changes: 3 additions & 2 deletions test/iidm/converter/xml/extensions/LoadBarXmlSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ LoadBarXmlSerializer::LoadBarXmlSerializer() :
AbstractExtensionXmlSerializer("loadBar", "network", "bar", "http://www.itesla_project.eu/schema/iidm/ext/loadbar/1_0") {
}

std::unique_ptr<Extension> LoadBarXmlSerializer::read(Extendable& extendable, NetworkXmlReaderContext& /*context*/) const {
Extension& LoadBarXmlSerializer::read(Extendable& extendable, NetworkXmlReaderContext& /*context*/) const {
if (!stdcxx::isInstanceOf<Load>(extendable)) {
throw AssertionError(stdcxx::format("Unexpected extendable type: %1% (%2% expected)", stdcxx::demangle(extendable), stdcxx::demangle<Load>()));
}
auto& load = dynamic_cast<Load&>(extendable);

return stdcxx::make_unique<network::LoadBarExt>(load);
extendable.addExtension(stdcxx::make_unique<network::LoadBarExt>(load));
return extendable.getExtension<network::LoadBarExt>();
}

void LoadBarXmlSerializer::write(const Extension& /*extension*/, NetworkXmlWriterContext& /*context*/) const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace extensions {

class LoadBarXmlSerializer : public AbstractExtensionXmlSerializer {
public: // ExtensionXmlSerializer
std::unique_ptr<Extension> read(Extendable& extendable, NetworkXmlReaderContext& context) const override;
Extension& read(Extendable& extendable, NetworkXmlReaderContext& context) const override;

void write(const Extension& extension, NetworkXmlWriterContext& context) const override;

Expand Down
5 changes: 3 additions & 2 deletions test/iidm/converter/xml/extensions/LoadFooXmlSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ LoadFooXmlSerializer::LoadFooXmlSerializer() :
AbstractExtensionXmlSerializer("loadFoo", "network", "foo", "http://www.itesla_project.eu/schema/iidm/ext/loadfoo/1_0") {
}

std::unique_ptr<Extension> LoadFooXmlSerializer::read(Extendable& extendable, NetworkXmlReaderContext& /*context*/) const {
Extension& LoadFooXmlSerializer::read(Extendable& extendable, NetworkXmlReaderContext& /*context*/) const {
if (!stdcxx::isInstanceOf<Load>(extendable)) {
throw AssertionError(stdcxx::format("Unexpected extendable type: %1% (%2% expected)", stdcxx::demangle(extendable), stdcxx::demangle<Load>()));
}
auto& load = dynamic_cast<Load&>(extendable);

return stdcxx::make_unique<network::LoadFooExt>(load);
extendable.addExtension(stdcxx::make_unique<network::LoadFooExt>(load));
return extendable.getExtension<network::LoadFooExt>();
}

void LoadFooXmlSerializer::write(const Extension& /*extension*/, NetworkXmlWriterContext& /*context*/) const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace extensions {

class LoadFooXmlSerializer : public AbstractExtensionXmlSerializer {
public: // ExtensionXmlSerializer
std::unique_ptr<Extension> read(Extendable& extendable, NetworkXmlReaderContext& context) const override;
Extension& read(Extendable& extendable, NetworkXmlReaderContext& context) const override;

void write(const Extension& extension, NetworkXmlWriterContext& context) const override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@ LoadMockExtXmlSerializer::LoadMockExtXmlSerializer() :
.build()) {
}

std::unique_ptr<Extension> LoadMockExtXmlSerializer::read(Extendable& extendable, NetworkXmlReaderContext& context) const {
Extension& LoadMockExtXmlSerializer::read(Extendable& extendable, NetworkXmlReaderContext& context) const {
checkReadingCompatibility(context);

if (!stdcxx::isInstanceOf<Load>(extendable)) {
throw AssertionError(stdcxx::format("Unexpected extendable type: %1% (%2% expected)", stdcxx::demangle(extendable), stdcxx::demangle<Load>()));
}
auto& load = dynamic_cast<Load&>(extendable);

return stdcxx::make_unique<LoadMockExt>(load);
extendable.addExtension(stdcxx::make_unique<LoadMockExt>(load));
return extendable.getExtension<LoadMockExt>();
}

void LoadMockExtXmlSerializer::write(const Extension& /*extension*/, NetworkXmlWriterContext& /*context*/) const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace extensions {

class LoadMockExtXmlSerializer : public AbstractVersionableExtensionXmlSerializer {
public: // ExtensionXmlSerializer
std::unique_ptr<Extension> read(Extendable& extendable, NetworkXmlReaderContext& context) const override;
Extension& read(Extendable& extendable, NetworkXmlReaderContext& context) const override;

void write(const Extension& extension, NetworkXmlWriterContext& context) const override;

Expand Down
Loading

0 comments on commit ceefe5d

Please sign in to comment.