Skip to content

Commit

Permalink
CPPSDK: provider support added
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseenaSainul committed Nov 30, 2023
1 parent e679004 commit 2c73f35
Show file tree
Hide file tree
Showing 48 changed files with 418 additions and 114 deletions.
1 change: 1 addition & 0 deletions languages/cpp/language.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"unwrapResultObjects": false,
"createPolymorphicMethods": true,
"excludeDeclarations": true,
"extractProviderSchema": true,
"aggregateFiles": [
"/include/firebolt.h",
"/src/firebolt.cpp"
Expand Down
1 change: 1 addition & 0 deletions languages/cpp/src/shared/include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
#include <unordered_map>
#include <vector>
#include <optional>
#include <memory>

Original file line number Diff line number Diff line change
@@ -1 +1 @@
${shape} ${if.non.const}${if.non.anyOf}${if.non.array}${if.non.object}${base.title}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = response.${Property.dependency}${Property};${end.if.non.object}${end.if.non.array}${end.if.non.anyOf}${end.if.non.const}
${shape} ${if.non.const}${if.non.anyOf}${if.non.array}${if.non.object}${base.title}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = response.${Property.dependency}${Property};${end.if.non.object}${end.if.non.array}${end.if.non.anyOf}${end.if.non.const}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
${if.namespace.notsame}${info.Title}::${end.if.namespace.notsame}${title} response = proxyResponse->Value();
${property} = proxyResponse->Value();
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
${if.impl.array.optional}${base.title}.${property} = std::make_optional<${type}>();${end.if.impl.array.optional}
${if.impl.array.optional}${base.title}.${property} = std::make_optional<${type}>();${end.if.impl.array.optional}
auto index(proxyResponse->${Property}.Elements());
while (index.Next() == true) {
${if.object}${items.with.indent}${end.if.object}${if.non.object} ${base.title}.${property}${if.impl.array.optional}.value()${end.if.impl.array.optional}.push_back(index.Current().Value());${end.if.non.object}
Expand Down
47 changes: 47 additions & 0 deletions languages/cpp/templates/codeblocks/interface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
static void ProviderInvokeSession(std::string& methodName, JsonObject& jsonParameters, Firebolt::Error *err = nullptr)
{
Firebolt::Error status = Firebolt::Error::NotConnected;
FireboltSDK::Transport<WPEFramework::Core::JSON::IElement>* transport = FireboltSDK::Accessor::Instance().GetTransport();
if (transport != nullptr) {

JsonObject jsonResult;
status = transport->Invoke(methodName, jsonParameters, jsonResult);
if (status == Firebolt::Error::None) {
FIREBOLT_LOG_INFO(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module<FireboltSDK::Accessor>(), "%s is successfully invoked", methodName.c_str());
}

} else {
FIREBOLT_LOG_ERROR(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module<FireboltSDK::Accessor>(), "Error in getting Transport err = %d", status);
}
if (err != nullptr) {
*err = status;
}
}
static void ProviderFocusSession(std::string methodName, std::string& correlationId, Firebolt::Error *err = nullptr)
{
JsonObject jsonParameters;
WPEFramework::Core::JSON::Variant CorrelationId(correlationId);
jsonParameters.Set(_T("correlationId"), CorrelationId);

ProviderInvokeSession(methodName, jsonParameters, err);
}
static void ProviderResultSession(std::string methodName, std::string& correlationId, ${provider.xresponse.name} result, Firebolt::Error *err = nullptr)
{
JsonObject jsonParameters;
WPEFramework::Core::JSON::Variant CorrelationId(correlationId);
jsonParameters.Set(_T("correlationId"), CorrelationId);

${provider.xresponse.serialization}
ProviderInvokeSession(methodName, jsonParameters, err);
}
static void ProviderErrorSession(std::string methodName, std::string& correlationId, ${provider.xerror.name} result, Firebolt::Error *err = nullptr)
{
JsonObject jsonParameters;
WPEFramework::Core::JSON::Variant CorrelationId(correlationId);
jsonParameters.Set(_T("correlationId"), CorrelationId);

${provider.xerror.serialization}
ProviderInvokeSession(methodName, jsonParameters, err);
}

${methods}
12 changes: 12 additions & 0 deletions languages/cpp/templates/codeblocks/interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
struct I${info.Title}Session : virtual public IFocussableProviderSession {
virtual ~I${info.Title}Session() override = default;

virtual void error( ${provider.xerror.name} error, Firebolt::Error *err = nullptr ) = 0;
virtual void result( ${provider.xresponse.name} result, Firebolt::Error *err = nullptr ) = 0;
};

struct I${info.Title}Provider {
virtual ~I${info.Title}Provider() = default;

${methods}
};
1 change: 1 addition & 0 deletions languages/cpp/templates/codeblocks/provider-subscribe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${subscribe}
1 change: 1 addition & 0 deletions languages/cpp/templates/codeblocks/provider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${interface}
1 change: 1 addition & 0 deletions languages/cpp/templates/codeblocks/provider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${interface}
5 changes: 5 additions & 0 deletions languages/cpp/templates/codeblocks/subscribe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
eventName = _T("${info.title.lowercase}.onRequest${method.Name}");
status = FireboltSDK::Event::Instance().Subscribe<${event.result.json.type}>(eventName, jsonParameters, ${info.Title}${method.Name}SessionInnerCallback, reinterpret_cast<void*>(&provider), nullptr);
if (status != Firebolt::Error::None) {
FIREBOLT_LOG_ERROR(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module<FireboltSDK::Accessor>(), "Error in %s subscribe = %d", eventName.c_str(), status);
}
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

1 change: 1 addition & 0 deletions languages/cpp/templates/declarations-override/provide.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void provide( I${info.Title}Provider& provider ) override;
1 change: 0 additions & 1 deletion languages/cpp/templates/declarations/calls-metrics.h
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

1 change: 1 addition & 0 deletions languages/cpp/templates/declarations/provide.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
virtual void provide( I${info.Title}Provider& provider ) = 0;
17 changes: 17 additions & 0 deletions languages/cpp/templates/interfaces/default.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class ${info.Title}${method.Name}Session : virtual public IProviderSession {
public:
std::string correlationId () const override
{
return _correlationId;
}

public:
std::string _correlationId;
};
static void ${info.Title}${method.Name}SessionInnerCallback( void* provider, const void* userData, void* jsonResponse )
{
//TODO: code to convert jsonResponse to ${method.name} session
I${info.Title}Provider& ${info.title.lowercase}Provider = *(reinterpret_cast<I${info.Title}Provider*>(provider));
${info.title.lowercase}Provider.${method.name}( parameters, session );
}

1 change: 1 addition & 0 deletions languages/cpp/templates/interfaces/default.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
virtual void ${method.name}( ${method.signature.params}, IProviderSession& session ) = 0;
44 changes: 44 additions & 0 deletions languages/cpp/templates/interfaces/focusable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class ${info.Title}${method.Name}Session : virtual public I${info.Title}Session {
public:
${info.Title}${method.Name}Session( const std::string& correlationId )
: _correlationId(correlationId)
{
}

std::string correlationId() const override
{
return _correlationId;
}
void focus( Firebolt::Error *err = nullptr ) override
{
ProviderFocusSession("${info.title.lowercase}.${method.name}Focus", _correlationId, err);
}
void result( ${provider.xresponse.name} response, Firebolt::Error *err = nullptr ) override
{
ProviderResultSession("${info.title.lowercase}.${method.name}Response", _correlationId, response, err);
}
void error( ${provider.xerror.name} error, Firebolt::Error *err = nullptr ) override
{
ProviderErrorSession("${info.title.lowercase}.${method.name}Error", _correlationId, error, err);
}

public:
std::string _correlationId;
};
static void ${info.Title}${method.Name}SessionInnerCallback( void* provider, const void* userData, void* jsonResponse )
{
${event.callback.serialization}
ASSERT(proxyResponse.IsValid() == true);

if (proxyResponse.IsValid() == true) {
${event.callback.initialization}

${event.callback.instantiation}
proxyResponse.Release();

std::unique_ptr<I${info.Title}Session> ${info.title.lowercase}${method.Name}Session = std::make_unique<${info.Title}${method.Name}Session>(${method.result.name}.correlationId);
I${info.Title}Provider& ${info.title.lowercase}Provider = *(reinterpret_cast<I${info.Title}Provider*>(provider));
${info.title.lowercase}Provider.${method.name}(${method.result.name}.parameters, std::move(${info.title.lowercase}${method.Name}Session));
}
}

1 change: 1 addition & 0 deletions languages/cpp/templates/interfaces/focusable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
virtual void ${method.name}( ${method.signature.params}, std::unique_ptr<I${info.Title}Session> session ) = 0;
2 changes: 1 addition & 1 deletion languages/cpp/templates/json-types/integer.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
WPEFramework::Core::JSON::DecSInt32
WPEFramework::Core::JSON::DecSInt32
1 change: 0 additions & 1 deletion languages/cpp/templates/methods/calls-metrics.cpp
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

3 changes: 3 additions & 0 deletions languages/cpp/templates/methods/default.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
} else {
FIREBOLT_LOG_ERROR(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module<FireboltSDK::Accessor>(), "Error in getting Transport err = %d", status);
}
if (err != nullptr) {
*err = status;
}

return${if.result.nonvoid} ${method.result.name}${end.if.result.nonvoid};
}
3 changes: 2 additions & 1 deletion languages/cpp/templates/methods/polymorphic-pull-event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
WPEFramework::Core::JSON::Variant CorrelationId = proxyResponse->CorrelationId.Value();
jsonParameters.Set(_T("correlationId"), CorrelationId);
${method.pulls.json.type} ${method.pulls.result.title}Container;

{
${method.pulls.result.serialization.with.indent}
}
string resultStr;
${method.pulls.result.title}Container.ToString(resultStr);
WPEFramework::Core::JSON::VariantContainer resultContainer(resultStr);
Expand Down
3 changes: 3 additions & 0 deletions languages/cpp/templates/methods/polymorphic-pull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
} else {
FIREBOLT_LOG_ERROR(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module<FireboltSDK::Accessor>(), "Error in getting Transport err = %d", status);
}
if (err != nullptr) {
*err = status;
}

return${if.result.nonvoid} ${method.result.name}${end.if.result.nonvoid};
}
8 changes: 8 additions & 0 deletions languages/cpp/templates/methods/provide.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
void ${info.Title}Impl::provide( I${info.Title}Provider& provider )
{
string eventName;
Firebolt::Error status = Firebolt::Error::None;
JsonObject jsonParameters;

/* ${PROVIDERS_SUBSCRIBE} */
}
5 changes: 2 additions & 3 deletions languages/cpp/templates/modules/include/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ namespace ${info.Title} {
/* ${ENUMS} */${end.if.enums}
${if.types}
// Types
/* ${TYPES} */
${end.if.types}
/* ${TYPES} */${end.if.types}
${if.providers}/* ${PROVIDERS} */${end.if.providers}
${if.methods}struct I${info.Title} {

virtual ~I${info.Title}() = default;

// Methods & Events
/* ${METHODS:declarations} */

};${end.if.methods}

} //namespace ${info.Title}
Expand Down
3 changes: 2 additions & 1 deletion languages/cpp/templates/modules/src/module_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
${if.implementations}
namespace Firebolt {
namespace ${info.Title} {

${if.providers}
/* ${PROVIDERS} */${end.if.providers}
// Methods
/* ${METHODS} */

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}${title} map = ${property}${if.impl.optional}.value()${end.if.impl.optional};
${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}${title} map;
${if.impl.optional}if (${property}.has_value()) {
map = ${property}.value();
}${end.if.impl.optional}${if.impl.non.optional}map = ${property};${end.if.impl.non.optional}
WPEFramework::Core::JSON::Variant ${property}Variant;
for (auto element: map) {
WPEFramework::Core::JSON::Variant jsonElement = element.second;
WPEFramework::Core::JSON::VariantContainer jsonContainer;
jsonContainer.Set(element.first.c_str(), jsonElement);
${property}Variant = jsonContainer;
}
jsonParameters.Set(_T("${property}"), ${property}Variant);
jsonParameters.Set(_T("${property}"), ${property}Variant);
10 changes: 7 additions & 3 deletions languages/cpp/templates/parameter-serialization/array.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
WPEFramework::Core::JSON::ArrayType<WPEFramework::Core::JSON::Variant> ${property}Array;
for (auto& element : ${property}) {
${if.object}${items.with.indent}${end.if.object}${if.non.object} ${property}Array.Add() = element;${end.if.non.object}
}
${if.impl.array.optional}if (${property}.has_value()) {
for (auto& element : ${property}.value()) {
${if.object}${items.with.indent}${end.if.object}${if.non.object} ${property}Array.Add() = element;${end.if.non.object}
}
}${end.if.impl.array.optional}${if.impl.array.non.optional}for (auto& element : ${property}) {
${if.object}${items.with.indent}${end.if.object}${if.non.object} ${property}Array.Add() = element;${end.if.non.object}
}${end.if.impl.array.non.optional}
WPEFramework::Core::JSON::Variant ${property}Variant;
${property}Variant.Array(${property}Array);
jsonParameters.Set(_T("${property}"), ${property}Variant);
8 changes: 6 additions & 2 deletions languages/cpp/templates/parameter-serialization/enum.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}JsonData_${title} jsonValue = ${property}${if.optional}.value()${end.if.optional};
${if.optional}if (${property}.has_value()) {
${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}JsonData_${title} jsonValue = ${property}.value();
WPEFramework::Core::JSON::Variant ${property}Variant(jsonValue.Data());
jsonParameters.Set(_T("${property}"), ${property}Variant);
}${end.if.optional}${if.non.optional}${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}JsonData_${title} jsonValue = ${property};
WPEFramework::Core::JSON::Variant ${property}Variant(jsonValue.Data());
jsonParameters.Set(_T("${property}"), ${property}Variant);
jsonParameters.Set(_T("${property}"), ${property}Variant);${end.if.non.optional}
7 changes: 5 additions & 2 deletions languages/cpp/templates/parameter-serialization/generic.cpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
WPEFramework::Core::JSON::Variant ${property}Variant(${property}${if.optional}.value()${end.if.optional});
jsonParameters.Set(_T("${property}"), ${property}Variant);
${if.optional}if (${property}.has_value()) {
WPEFramework::Core::JSON::Variant ${property}Variant(${property}.value());
jsonParameters.Set(_T("${property}"), ${property}Variant);
}${end.if.optional}${if.non.optional}WPEFramework::Core::JSON::Variant ${property}Variant(${property});
jsonParameters.Set(_T("${property}"), ${property}Variant);${end.if.non.optional}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}JsonData_${title} ${property}Container;
{
${properties}
}
string ${property}Str;
${property}Container.ToString(${property}Str);
WPEFramework::Core::JSON::VariantContainer ${property}VariantContainer(${property}Str);
WPEFramework::Core::JSON::Variant ${property}Variant = ${property}VariantContainer;
${property}Array.Add() = ${property}Variant;
${property}Array.Add() = ${property}Variant;
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
WPEFramework::Core::JSON::Variant ${property}Variant(${property}${if.optional}.value()${end.if.optional});
jsonParameters.Set(_T("${property}"), ${property}Variant);
${if.optional}if (${property}.has_value()) {
WPEFramework::Core::JSON::Variant ${property}Variant(${property}.value());
jsonParameters.Set(_T("${property}"), ${property}Variant);
}${end.if.optional}${if.non.optional}WPEFramework::Core::JSON::Variant ${property}Variant(${property});
jsonParameters.Set(_T("${property}"), ${property}Variant);${end.if.non.optional}
15 changes: 13 additions & 2 deletions languages/cpp/templates/parameter-serialization/object.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
auto element = ${property}${if.impl.optional}.value()${end.if.impl.optional};
${if.impl.optional}if (${property}.has_value()) {
auto element = ${property}.value();
${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}JsonData_${title} ${property}Container;
${properties}
string ${property}Str;
${property}Container.ToString(${property}Str);
WPEFramework::Core::JSON::VariantContainer ${property}VariantContainer(${property}Str);
WPEFramework::Core::JSON::Variant ${property}Variant = ${property}VariantContainer;
jsonParameters.Set(_T("${property}"), ${property}Variant);
}${end.if.impl.optional}${if.impl.non.optional}auto element = ${property};
${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}JsonData_${title} ${property}Container;
{
${properties}
}
string ${property}Str;
${property}Container.ToString(${property}Str);
WPEFramework::Core::JSON::VariantContainer ${property}VariantContainer(${property}Str);
WPEFramework::Core::JSON::Variant ${property}Variant = ${property}VariantContainer;
jsonParameters.Set(_T("${property}"), ${property}Variant);
jsonParameters.Set(_T("${property}"), ${property}Variant);${end.if.impl.non.optional}
4 changes: 3 additions & 1 deletion languages/cpp/templates/parameter-serialization/property.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
${shape} ${if.non.const}${if.non.anyOf}${if.non.array}${if.non.object}${base.title}Container.${Property} = element.${property}${if.optional}.value()${end.if.optional};${end.if.non.object}${end.if.non.array}${end.if.non.anyOf}${end.if.non.const}
${shape} ${if.non.const}${if.non.anyOf}${if.non.array}${if.non.object}${if.optional}if (element.${property}.has_value()) {
${base.title}Container.${Property} = element.${property}.value();
}${end.if.optional}${if.non.optional}${base.title}Container.${Property} = element.${property};${end.if.non.optional}${end.if.non.object}${end.if.non.array}${end.if.non.anyOf}${end.if.non.const}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
${base.title}Container.${Property.dependency}${Property} = element${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property}${if.optional}.value()${end.if.optional};
${if.optional}if (element${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property}.has_value()) {
${base.title}Container.${Property.dependency}${Property} = element${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property}.value();
}${end.if.optional}${if.non.optional}${base.title}Container.${Property.dependency}${Property} = element${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property};${end.if.non.optional}
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
WPEFramework::Core::JSON::ArrayType<${json.type}> ${property}Array;
${type} ${property} = element${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property}${if.impl.array.optional}.value()${end.if.impl.array.optional};
for (auto& element : ${property}) {
${if.object}${items.with.indent}${end.if.object}${if.non.object} ${property}Array.Add() = element;${end.if.non.object}
}
${base.title}Container.${Property.dependency}Add(_T("${property}"), &${property}Array);
${if.impl.array.optional}if (element${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property}.has_value()) {
WPEFramework::Core::JSON::ArrayType<${json.type}> ${property}Array;
${type} ${property} = element${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property}.value();
for (auto& element : ${property}) {
${if.object}${items.with.indent}${end.if.object}${if.non.object} ${property}Array.Add() = element;${end.if.non.object}
}
${base.title}Container.${Property.dependency}Add(_T("${property}"), &${property}Array);
}${end.if.impl.array.optional}${if.impl.array.non.optional}WPEFramework::Core::JSON::ArrayType<${json.type}> ${property}Array;
${type} ${property} = element${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property};
for (auto& element : ${property}) {
${if.object}${items.with.indent}${end.if.object}${if.non.object} ${property}Array.Add() = element;${end.if.non.object}
}
${base.title}Container.${Property.dependency}Add(_T("${property}"), &${property}Array);${end.if.impl.array.non.optional}
Loading

0 comments on commit 2c73f35

Please sign in to comment.