Skip to content

Commit

Permalink
Create enum mappers between LV enums and proto values (#240)
Browse files Browse the repository at this point in the history
Create enum mappers between LV enums and proto values
  • Loading branch information
pratheekshasn authored Apr 5, 2023
1 parent 434a181 commit 140f41c
Show file tree
Hide file tree
Showing 18 changed files with 82 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

</Property>
<Item Name="RPC Messages" Type="Folder">
<Item Name="package_templatemessage" Type="Folder">
<Item Name="Get package_template_enum for Value.vi" Type="VI" URL="../RPC Messages/Get package_template_enum for Value.vi"/>
<Item Name="Get Value for package_template_enum.vi" Type="VI" URL="../RPC Messages/Get Value for package_template_enum.vi"/>
</Item>
<Item Name="Register gRPC Messages.vi" Type="VI" URL="../RPC Messages/Register gRPC Messages.vi"/>
</Item>
<Item Name="RPC Service" Type="Folder">
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<Item Name="package_templatemessage.ctl" Type="VI" URL="../RPC Messages/package_templatemessage.ctl"/>
<Item Name="Get package_templatemessage.vi" Type="VI" URL="../RPC Messages/Get package_templatemessage.vi"/>
<Item Name="Set package_templatemessage.vi" Type="VI" URL="../RPC Messages/Set package_templatemessage.vi"/>
<Item Name="Get package_template_enum for Value.vi" Type="VI" URL="../RPC Messages/Get package_template_enum for Value.vi"/>
<Item Name="Get Value for package_template_enum.vi" Type="VI" URL="../RPC Messages/Get Value for package_template_enum.vi"/>
</Item>
<Item Name="Register gRPC Messages.vi" Type="VI" URL="../RPC Messages/Register gRPC Messages.vi"/>
</Item>
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@
<Item Name="Get Or Create Typedef.vi" Type="VI" URL="../Top Level API/Get Or Create Typedef.vi"/>
<Item Name="Create Enum Data Clusters.vi" Type="VI" URL="../Top Level API/Create Enum Data Clusters.vi"/>
<Item Name="Create Enum Data Clusters (simple).vi" Type="VI" URL="../Top Level API/Create Enum Data Clusters (simple).vi"/>
<Item Name="Create Enum Mapper.vi" Type="VI" URL="../Top Level API/Create Enum Mapper.vi"/>
<Item Name="Update Enum Mapper.vi" Type="VI" URL="../Top Level API/Update Enum Mapper.vi"/>
<Item Name="Create Mapper VIs for Enum.vi" Type="VI" URL="../Top Level API/Create Mapper VIs for Enum.vi"/>
</Item>
<Item Name="RPC Events" Type="Folder">
<Item Name="Populate gRPC Events.vi" Type="VI" URL="../Top Level API/Populate gRPC Events.vi"/>
Expand Down
38 changes: 4 additions & 34 deletions src/cluster_copier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -334,21 +334,6 @@ namespace grpc_labview {
}
}

//---------------------------------------------------------------------
//---------------------------------------------------------------------
int GetLVEnumValueFromProtoValue(int protoValue, std::shared_ptr<EnumMetadata> enumMetadata)
{
int value = 0;
auto lvValue = enumMetadata->ProtoEnumToLVEnum.find(protoValue);
if (lvValue != enumMetadata->ProtoEnumToLVEnum.end())
value = (lvValue->second).front(); // Since one proto value can be mapped to multiple LV enum values, always return the first element.
else
{
throw InvalidEnumValueException("Invalid enum value!");
}
return value;
}

void ClusterDataCopier::CopyEnumToCluster(const std::shared_ptr<MessageElementMetadata> metadata, int8_t* start, const std::shared_ptr<LVMessageValue>& value)
{
std::shared_ptr<EnumMetadata> enumMetadata = metadata->_owner->FindEnumMetadata(metadata->embeddedMessageName);
Expand All @@ -363,7 +348,7 @@ namespace grpc_labview {
for (size_t i = 0; i < count; i++)
{
auto protoValue = (repeatedEnum->_value.data())[i];
mappedArray[i] = GetLVEnumValueFromProtoValue(protoValue, enumMetadata);
mappedArray[i] = enumMetadata->GetLVEnumValueFromProtoValue(protoValue);
}

if (count != 0)
Expand All @@ -380,7 +365,7 @@ namespace grpc_labview {
else
{
auto enumValueFromPrtobuf = ((LVEnumMessageValue*)value.get())->_value;
*(int*)start = GetLVEnumValueFromProtoValue(enumValueFromPrtobuf, enumMetadata);
*(int*)start = enumMetadata->GetLVEnumValueFromProtoValue(enumValueFromPrtobuf);
}
}

Expand Down Expand Up @@ -737,21 +722,6 @@ namespace grpc_labview {
}
}

//---------------------------------------------------------------------
//---------------------------------------------------------------------
int32_t GetProtoValueForLVValue(int32_t enumValueFromLV, std::shared_ptr<EnumMetadata> enumMetadata)
{
int value = 0;
// Find the equivalent proto value for enumValueFromLV
auto protoValue = enumMetadata->LVEnumToProtoEnum.find(enumValueFromLV);
if (protoValue != enumMetadata->LVEnumToProtoEnum.end())
value = protoValue->second;
else
{
throw InvalidEnumValueException("Invalid enum value!");
}
return value;
}
void ClusterDataCopier::CopyEnumFromCluster(const std::shared_ptr<MessageElementMetadata> metadata, int8_t* start, LVMessage& message)
{
std::shared_ptr<EnumMetadata> enumMetadata = metadata->_owner->FindEnumMetadata(metadata->embeddedMessageName);
Expand All @@ -774,7 +744,7 @@ namespace grpc_labview {
{
auto enumValueFromLV = data[i];
// Find the equivalent proto value for enumValueFromLV
mappedArray[i] = GetProtoValueForLVValue(enumValueFromLV, enumMetadata);
mappedArray[i] = enumMetadata->GetProtoValueFromLVEnumValue(enumValueFromLV);
}

repeatedValue->_value.Reserve(count);
Expand All @@ -787,7 +757,7 @@ namespace grpc_labview {
else
{
auto enumValueFromLV = *(int32_t*)start;
int protoValue = GetProtoValueForLVValue(enumValueFromLV, enumMetadata);
int protoValue = enumMetadata->GetProtoValueFromLVEnumValue(enumValueFromLV);
auto value = std::make_shared<LVEnumMessageValue>(metadata->protobufIndex, protoValue);
message._values.emplace(metadata->protobufIndex, value);
}
Expand Down
32 changes: 30 additions & 2 deletions src/enum_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#pragma once

#include <list>
#include <exceptions.h>

namespace grpc_labview
{
Expand All @@ -16,15 +17,42 @@ namespace grpc_labview
{
}

uint32_t GetLVEnumValueFromProtoValue(int32_t protoValue)
{
uint32_t value = 0;
auto lvValue = ProtoEnumToLVEnum.find(protoValue);
if (lvValue != ProtoEnumToLVEnum.end())
value = (lvValue->second).front(); // Since one proto value can be mapped to multiple LV enum values, always return the first element.
else
{
throw InvalidEnumValueException("Invalid enum value!");
}
return value;
}

int32_t GetProtoValueFromLVEnumValue(uint32_t enumValueFromLV)
{
int32_t value = 0;
// Find the equivalent proto value for enumValueFromLV
auto protoValue = LVEnumToProtoEnum.find(enumValueFromLV);
if (protoValue != LVEnumToProtoEnum.end())
value = protoValue->second;
else
{
throw InvalidEnumValueException("Invalid enum value!");
}
return value;
}

public:
std::string messageName;
std::string typeUrl;
std::string elements;
bool allowAlias;
int clusterSize;
int alignmentRequirement;
std::map<int, int32_t> LVEnumToProtoEnum;
std::map<int32_t, std::list<int>> ProtoEnumToLVEnum;
std::map<uint32_t, int32_t> LVEnumToProtoEnum;
std::map<int32_t, std::list<uint32_t>> ProtoEnumToLVEnum;
};

//---------------------------------------------------------------------
Expand Down
48 changes: 39 additions & 9 deletions src/grpc_interop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,45 +114,45 @@ namespace grpc_labview
return res;
}

std::map<int, int32_t> CreateMapBetweenLVEnumAndProtoEnumvalues(std::string enumValues)
std::map<uint32_t, int32_t> CreateMapBetweenLVEnumAndProtoEnumvalues(std::string enumValues)
{
std::map<int, int32_t> lvEnumToProtoEnum;
std::map<uint32_t, int32_t> lvEnumToProtoEnum;
int seqLVEnumIndex = 0;
for (std::string keyValuePair : SplitString(enumValues, ";"))
{
auto keyValue = SplitString(keyValuePair, "=");
assert(keyValue.size() == 2);

int protoEnumNumeric = std::stoi(keyValue[1]);
lvEnumToProtoEnum.insert(std::pair<int, int32_t>(seqLVEnumIndex, protoEnumNumeric));
lvEnumToProtoEnum.insert(std::pair<uint32_t, int32_t>(seqLVEnumIndex, protoEnumNumeric));
seqLVEnumIndex += 1;
}
return lvEnumToProtoEnum;
}

void MapInsertOrAssign(std::map<int32_t, std::list<int>> *protoEnumToLVEnum, int protoEnumNumeric, std::list<int> lvEnumNumericValues)
void MapInsertOrAssign(std::map<int32_t, std::list<uint32_t>>*protoEnumToLVEnum, int protoEnumNumeric, std::list<uint32_t> lvEnumNumericValues)
{
auto existingElement = protoEnumToLVEnum->find(protoEnumNumeric);
if (existingElement != protoEnumToLVEnum->end())
{
protoEnumToLVEnum->erase(protoEnumNumeric);
protoEnumToLVEnum->insert(std::pair<int, std::list<int>>(protoEnumNumeric, lvEnumNumericValues));
protoEnumToLVEnum->insert(std::pair<int32_t, std::list<uint32_t>>(protoEnumNumeric, lvEnumNumericValues));
}
else
protoEnumToLVEnum->insert(std::pair<int, std::list<int>>(protoEnumNumeric, lvEnumNumericValues));
protoEnumToLVEnum->insert(std::pair<int32_t, std::list<uint32_t>>(protoEnumNumeric, lvEnumNumericValues));
}

std::map<int32_t, std::list<int>> CreateMapBetweenProtoEnumAndLVEnumvalues(std::string enumValues)
std::map<int32_t, std::list<uint32_t>> CreateMapBetweenProtoEnumAndLVEnumvalues(std::string enumValues)
{
std::map<int32_t, std::list<int>> protoEnumToLVEnum;
std::map<int32_t, std::list<uint32_t>> protoEnumToLVEnum;
int seqLVEnumIndex = 0;
for (std::string keyValuePair : SplitString(enumValues, ";"))
{
auto keyValue = SplitString(keyValuePair, "=");
int protoEnumNumeric = std::stoi(keyValue[1]);
assert(keyValue.size() == 2);

std::list<int> lvEnumNumericValues;
std::list<uint32_t> lvEnumNumericValues;
auto existingElement = protoEnumToLVEnum.find(protoEnumNumeric);
if (existingElement != protoEnumToLVEnum.end())
lvEnumNumericValues = existingElement->second;
Expand Down Expand Up @@ -285,6 +285,36 @@ LIBRARY_EXPORT int32_t RegisterEnumMetadata2(grpc_labview::gRPCid** id, grpc_lab
return 0;
}

//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT uint32_t GetLVEnumValueFromProtoValue(grpc_labview::gRPCid** id, const char* enumName, int protoValue, uint32_t* lvEnumValue)
{
auto server = (*id)->CastTo<grpc_labview::MessageElementMetadataOwner>();
if (server == nullptr)
{
return -1;
}
auto metadata = (server.get())->FindEnumMetadata(std::string(enumName));
*(uint32_t*)lvEnumValue = metadata.get()->GetLVEnumValueFromProtoValue(protoValue);

return 0;
}

//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int32_t GetProtoValueFromLVEnumValue(grpc_labview::gRPCid** id, const char* enumName, int lvEnumValue, int32_t* protoValue)
{
auto server = (*id)->CastTo<grpc_labview::MessageElementMetadataOwner>();
if (server == nullptr)
{
return -1;
}
auto metadata = (server.get())->FindEnumMetadata(std::string(enumName));
*(int32_t*)protoValue = metadata.get()->GetProtoValueFromLVEnumValue(lvEnumValue);

return 0;
}

//---------------------------------------------------------------------
//---------------------------------------------------------------------
LIBRARY_EXPORT int32_t CompleteMetadataRegistration(grpc_labview::gRPCid** id)
Expand Down

0 comments on commit 140f41c

Please sign in to comment.