stringtemplate4 icon indicating copy to clipboard operation
stringtemplate4 copied to clipboard

How to add string condition matching judgment to the. stg template? If I add this syntax, it will report an error; Without this condition, the template can be generated normally. I am not very familiar with ST syntax. Can you help me solve this problem

Open Eternity1987 opened this issue 8 months ago • 1 comments

$if(struct.scopedname.first()=="shm_msgs")$`
// Skip NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT for shm_msgs namespace
$else$
#ifndef SWIG
#include <nlohmann/json.hpp>
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT($struct.name$,$\n$$struct.members:{m |$m.name$}; separator=", "$)
#endif
$endif$

How to add string condition matching judgment to the. stg template? If I add this syntax, it will report an error; Without this condition, the template can be generated normally. I am not very familiar with ST syntax. Can you help me solve this problem

Eternity1987 avatar Jun 04 '25 11:06 Eternity1987

struct_type(ctx, parent, struct, extensions, member_list) ::= <<
$member_list$
/*!
 * @brief This class represents the structure $struct.name$ defined by the user in the IDL file.
 * @ingroup $ctx.trimfilename$
 */
class $struct.name$$if(struct.inheritance)$ : public $public_struct_inheritances(struct.inheritance)$$endif$
{
public:

    using PubSubType = $struct.name$PubSubType;
    using TypeHelper = $struct.name$TypeHelper;

    /*!
     * @brief Default constructor.
     */
    eProsima_user_DllExport $struct.name$();

    /*!
     * @brief Default destructor.
     */
    eProsima_user_DllExport ~$struct.name$();

    /*!
     * @brief Copy constructor.
     * @param x Reference to the object $struct.scopedname$ that will be copied.
     */
    eProsima_user_DllExport $struct.name$(
            const $struct.name$& x);

    /*!
     * @brief Move constructor.
     * @param x Reference to the object $struct.scopedname$ that will be copied.
     */
    eProsima_user_DllExport $struct.name$(
            $struct.name$&& x) noexcept;

    /*!
     * @brief Copy assignment.
     * @param x Reference to the object $struct.scopedname$ that will be copied.
     */
    eProsima_user_DllExport $struct.name$& operator =(
            const $struct.name$& x);

    /*!
     * @brief Move assignment.
     * @param x Reference to the object $struct.scopedname$ that will be copied.
     */
    eProsima_user_DllExport $struct.name$& operator =(
            $struct.name$&& x) noexcept;

    /*!
     * @brief Comparison operator.
     * @param x $struct.scopedname$ object to compare.
     */
    eProsima_user_DllExport bool operator ==(
            const $struct.name$& x) const;

    /*!
     * @brief Comparison operator.
     * @param x $struct.scopedname$ object to compare.
     */
    eProsima_user_DllExport bool operator !=(
            const $struct.name$& x) const;

    $struct.members:{ member | $public_member_declaration(member)$}; separator="\n"$
    $serialization_functions(struct)$
    $extensions : { extension | $extension$}; separator="\n"$
public:

    $struct.members:{ member | $private_member_declaration(member=member)$}; separator="\n"$

    using SharedPtr = std::shared_ptr<$struct.name$>;
    using ConstSharedPtr = std::shared_ptr<$struct.name$ const>;

    using UniquePtr = std::unique_ptr<$struct.name$>;
    using ConstUniquePtr = std::unique_ptr<$struct.name$ const>;

    using WeakPtr = std::weak_ptr<$struct.name$>;
    using ConstWeakPtr = std::weak_ptr<$struct.name$ const>;

};

**$if(struct.scopedname.first()=="shm_msgs")$
// Skip NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT for shm_msgs namespace
$else$
#ifndef SWIG
#include <nlohmann/json.hpp>
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT($struct.name$,$\n$$struct.members:{m |$m.name$}; separator=", "$)
#endif
$endif$**
>>

Full Code

Eternity1987 avatar Jun 04 '25 11:06 Eternity1987