Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ jobs:
uses: actions/setup-python@65d7f2d534ac1bc67fcd62888c5f4f3d2cb2b236 # v4.7.1
with:
python-version: "3.11"
- uses: psf/black@8a737e727ac5ab2f1d4cf5876720ed276dc8dc4b # 25.1.0
- uses: psf/black@6305bf1ae645ab7541be4f5028a86239316178eb # 26.1.0
- uses: py-actions/flake8@84ec6726560b6d5bd68f2a5bed83d62b52bb50ba # v2.3.0
24 changes: 6 additions & 18 deletions src/shacl2code/lang/cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,10 @@ def suffix(s):
for s in self.HEADERS:
guard = f"_{self.macro_prefix}_{id_str(s).upper()}"
yield self.basename.parent / s, t / (s + ".j2"), {
"guard_begin": comment_wrap(
textwrap.dedent(
f"""\
"guard_begin": comment_wrap(textwrap.dedent(f"""\
#ifndef {guard}
#define {guard}
"""
)
),
""")),
"guard_end": comment_wrap(f"#endif // {guard}"),
}

Expand Down Expand Up @@ -188,26 +184,18 @@ def get_extra_env(self):
"prop_is_list": prop_is_list,
"parent_cpp_classes": parent_cpp_classes,
"macro_prefix": self.macro_prefix,
"api_def_begin": comment_wrap(
textwrap.dedent(
f"""\
"api_def_begin": comment_wrap(textwrap.dedent(f"""\
#ifndef DOXYGEN_SKIP
#include "api.hpp"
// These are so that we don't have to use Jinja templates below since that messes up the formatting
#define EXPORT {self.macro_prefix}_API
#define LOCAL {self.macro_prefix}_LOCAL
#endif // DOXYGEN_SKIP
"""
)
),
"api_def_end": comment_wrap(
textwrap.dedent(
"""\
""")),
"api_def_end": comment_wrap(textwrap.dedent("""\
#undef EXPORT
#undef LOCAL
"""
)
),
""")),
"ns_begin": comment_wrap(
"\n".join(f"namespace {n} {{" for n in self.namespace.split("::"))
),
Expand Down
15 changes: 15 additions & 0 deletions src/shacl2code/lang/templates/cpp/decode.cpp.j2
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ vim: ft=cpp
#include <string>

#include "decode.hpp"
#include "objectset.hpp"

/* {{ "*" + "/" }}
{%- if namespace %}
Expand All @@ -28,6 +29,20 @@ using std::string_literals::operator""s;
DecoderState::DecoderState(SHACLObjectSet* objectSet) : mObjectSet(objectSet) {}
DecoderState::~DecoderState() {}

std::string DecoderState::expandIRI(std::string const& iri) const {
if (!mObjectSet) {
return iri;
}
return mObjectSet->expandIRI(iri);
}

std::string DecoderState::compactIRI(std::string const& iri) const {
if (!mObjectSet) {
return iri;
}
return mObjectSet->compactIRI(iri);
}

// ElementDecoder
ElementDecoder::ElementDecoder(DecoderState& state) : mState(state) {}
ElementDecoder::~ElementDecoder() {}
Expand Down
26 changes: 25 additions & 1 deletion src/shacl2code/lang/templates/cpp/decode.hpp.j2
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,34 @@ class EXPORT DecoderState {
/**
* @brief Get object set
*
* @returns The SHACLObjectSet being encoded
* @returns The SHACLObjectSet being decoded
*/
SHACLObjectSet* getObjectSet() const { return mObjectSet; }

/**
* @brief Expand IRI
*
* Expands an IRI if there is a SHACLObjectSet attached to the state,
* otherwise returns the original IRI.
*
* @param[in] iri The IRI to expand
*
* @returns the expanded IRI
*/
std::string expandIRI(std::string const& iri) const;

/**
* @brief Compact IRI
*
* Compacts an IRI if there is a SHACLOBjectSet attached to the state,
* otherwise returns the original IRI.
*
* @param[in] iri The IRI to compact
*
* @returns the compacted IRI
*/
std::string compactIRI(std::string const& iri) const;

private:
SHACLObjectSet* mObjectSet;
};
Expand Down
17 changes: 16 additions & 1 deletion src/shacl2code/lang/templates/cpp/encode.cpp.j2
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ vim: ft=cpp

#include "encode.hpp"
#include "object.hpp"
#include "objectset.hpp"

/* {{ "*" + "/" }}
{%- if namespace %}
Expand All @@ -31,7 +32,7 @@ namespace {{ n }} {
using std::string_literals::operator""s;

// EncoderState
EncoderState::EncoderState() {}
EncoderState::EncoderState(SHACLObjectSet* objectSet) : mObjectSet(objectSet) {}
EncoderState::~EncoderState() {}

std::string EncoderState::getObjectId(SHACLObject const& obj) {
Expand Down Expand Up @@ -78,6 +79,20 @@ EncoderState::objectKey EncoderState::getObjectKey(
return reinterpret_cast<uintptr_t>(&obj);
}

std::string EncoderState::expandIRI(std::string const& iri) const {
if (!mObjectSet) {
return iri;
}
return mObjectSet->expandIRI(iri);
}

std::string EncoderState::compactIRI(std::string const& iri) const {
if (!mObjectSet) {
return iri;
}
return mObjectSet->compactIRI(iri);
}

// ElementEncoder
ElementEncoder::ElementEncoder(EncoderState& state) : mState(state) {}
ElementEncoder::~ElementEncoder() {}
Expand Down
53 changes: 50 additions & 3 deletions src/shacl2code/lang/templates/cpp/encode.hpp.j2
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ vim: ft=cpp
/* {{ ns_begin }} */

class SHACLObject;
class SHACLObjectSet;
class ObjectEncoder;
class ListEncoder;
class DateTime;
Expand All @@ -43,7 +44,7 @@ class EXPORT EncoderState {
*
* Creates a new EncoderState
*/
EncoderState();
EncoderState(SHACLObjectSet* objectSet = nullptr);

/**
* @brief Destructor
Expand Down Expand Up @@ -123,6 +124,37 @@ class EXPORT EncoderState {
*/
void removeWritten(SHACLObject const& obj);

/**
* @brief Get object set
*
* @returns The SHACLObjectSet being encoded
*/
SHACLObjectSet* getObjectSet() const { return mObjectSet; }

/**
* @brief Expand IRI
*
* Expands an IRI if there is a SHACLObjectSet attached to the state,
* otherwise returns the original IRI.
*
* @param[in] iri The IRI to expand
*
* @returns the expanded IRI
*/
std::string expandIRI(std::string const& iri) const;

/**
* @brief Compact IRI
*
* Compacts an IRI if there is a SHACLOBjectSet attached to the state,
* otherwise returns the original IRI.
*
* @param[in] iri The IRI to compact
*
* @returns the compacted IRI
*/
std::string compactIRI(std::string const& iri) const;

private:
using objectKey = uintptr_t;

Expand All @@ -140,6 +172,7 @@ class EXPORT EncoderState {
std::set<std::shared_ptr<SHACLObject>> mRefed;
std::unordered_set<objectKey> mIsRefed;
std::unordered_set<objectKey> mWritten;
SHACLObjectSet* mObjectSet;
};

/**
Expand Down Expand Up @@ -252,13 +285,27 @@ class EXPORT ValueEncoder : public ElementEncoder {
*/
virtual void writeFloat(double value) = 0;

/**
* @brief Write dictionary
*
* Writes a dictionary in the output. The function @p f will be invoked
* with a new ObjectEncoder to encoder the contest pf the dictionary
*
* @param[in] f Function called by the encoder to encoder the
* content of the dictionary
*
* @see ObjectEncoder
*/
virtual void writeDict(std::function<void(ObjectEncoder&)> const& f) = 0;

/**
* @brief Write object
*
* Writes an object in the output. The function @p f will be invoked
* with a new ObjectEncoder to encode the contents of the object
*
* @param[in] o The object to encode
* @param[in] type The type IRI to use for the object
* @param[in] id The ID to use when encoding the object. This will
* always be a valid ID (either an IRI or a blank
* node), and should be used by the encoder instead of querying the
Expand All @@ -271,8 +318,8 @@ class EXPORT ValueEncoder : public ElementEncoder {
*
* @see ObjectEncoder
*/
virtual void writeObject(SHACLObject const& o, std::string const& id,
bool needs_id,
virtual void writeObject(SHACLObject const& o, std::string const& type,
std::string const& id, bool needs_id,
std::function<void(ObjectEncoder&)> const& f) = 0;

/**
Expand Down
6 changes: 4 additions & 2 deletions src/shacl2code/lang/templates/cpp/extensible.cpp.j2
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ std::vector<Variant>& SHACLExtensibleObjectBase::getExtProperty(
void SHACLExtensibleObjectBase::encodeExtensibleProperties(
ObjectEncoder& encoder, ErrorHandler& errorHandler,
ObjectPath& path) const {
auto& state = encoder.getState();
for (auto const& p : mExtendedProperties) {
auto const& v = p.second;
if (!v.size()) {
Expand Down Expand Up @@ -80,8 +81,9 @@ void SHACLExtensibleObjectBase::encodeExtensibleProperties(
errorHandler,
path);
} else {
item_encoder.writeIRI(value.iri(),
{});
item_encoder.writeIRI(
state.compactIRI(value.iri()),
{});
}

} else {
Expand Down
16 changes: 12 additions & 4 deletions src/shacl2code/lang/templates/cpp/jsonld-header.j2
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ namespace {{ n }} {
#endif
// clang-format on

class SHACLObjectSet;

// Encode
/**
* @brief JSONLD Encoder State
Expand All @@ -45,7 +47,8 @@ namespace {{ n }} {
class EXPORT JSONLDEncoderState : public EncoderState {
public:
/// Constructor
JSONLDEncoderState(std::ostream& output);
JSONLDEncoderState(std::ostream& output,
SHACLObjectSet* objectSet = nullptr);

/// Destructor
virtual ~JSONLDEncoderState();
Expand Down Expand Up @@ -112,8 +115,12 @@ class EXPORT JSONLDValueEncoder : public ValueEncoder {
/// @copydoc {{ nsprefix }}::ValueEncoder::writeFloat()
void writeFloat(double value) override;

/// @copydoc {{ nsprefix }}::ValueEncoder::writeDict()
void writeDict(std::function<void(ObjectEncoder&)> const& f) override;

/// @copydoc {{ nsprefix }}::ValueEncoder::writeObject()
void writeObject(SHACLObject const& o, std::string const& id, bool needs_id,
void writeObject(SHACLObject const& o, std::string const& type,
std::string const& id, bool needs_id,
std::function<void(ObjectEncoder&)> const& f) override;

/// @copydoc {{ nsprefix }}::ValueEncoder::writeList()
Expand Down Expand Up @@ -256,8 +263,9 @@ class EXPORT JSONLDValueDecoder : public ValueDecoder {
class EXPORT JSONLDObjectDecoder : public ObjectDecoder {
public:
/// Constructor
JSONLDObjectDecoder(DecoderState& state, JSONData const& data,
std::unordered_set<std::string> const& ignoreProperties = {});
JSONLDObjectDecoder(
DecoderState& state, JSONData const& data,
std::unordered_set<std::string> const& ignoreProperties = {});

/// Destructor
virtual ~JSONLDObjectDecoder();
Expand Down
Loading
Loading