Skip to content

Commit

Permalink
prmJointType uses string to serialize to/from JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
adeguet1 committed Apr 9, 2024
1 parent 28c5ba4 commit b444fab
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 32 deletions.
13 changes: 12 additions & 1 deletion cisstCommon/cmnDataFunctionsEnumMacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Author(s): Anton Deguet
Created on: 2011-06-27
(C) Copyright 2011-2021 Johns Hopkins University (JHU), All Rights Reserved.
(C) Copyright 2011-2024 Johns Hopkins University (JHU), All Rights Reserved.
--- begin cisst license - do not edit ---
Expand Down Expand Up @@ -115,4 +115,15 @@ public: \
data = static_cast<_enum>(dataPromoted); \
}

#define CMN_IMPLEMENT_DATA_FUNCTIONS_JSON_FOR_ENUM_AS_STRING(_enum, _to_string, _from_string) \
template <> void cmnDataJSON<_enum>::SerializeText(const _enum & data, Json::Value & jsonValue) { \
const std::string _s = _to_string(data); \
cmnDataJSON<std::string>::SerializeText(_s, jsonValue); \
} \
template <> void cmnDataJSON<_enum>::DeSerializeText(_enum & data, const Json::Value & jsonValue) CISST_THROW(std::runtime_error) { \
std::string _s; \
cmnDataJSON<std::string>::DeSerializeText(_s, jsonValue); \
data = _from_string(_s); \
}

#endif // _cmnDataFunctionsEnumMacros_h
60 changes: 58 additions & 2 deletions cisstParameterTypes/code/prmJointType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Author(s): Anton Deguet
Created on: 2010-10-20
(C) Copyright 2010-2018 Johns Hopkins University (JHU), All Rights Reserved.
(C) Copyright 2010-2024 Johns Hopkins University (JHU), All Rights Reserved.
--- begin cisst license - do not edit ---
Expand All @@ -17,9 +17,10 @@ no warranty. The complete license can be found in license.txt and
*/

#include <cisstParameterTypes/prmJointType.h>
#include <locale>

#if CISST_HAS_JSON
CMN_IMPLEMENT_DATA_FUNCTIONS_JSON_FOR_ENUM(prmJointType, int);
CMN_IMPLEMENT_DATA_FUNCTIONS_JSON_FOR_ENUM_AS_STRING(prmJointType, prmJointTypeToString, prmJointTypeFromString);
#endif

void prmJointTypeToFactor(const vctDynamicVector<prmJointType> & types,
Expand All @@ -42,3 +43,58 @@ void prmJointTypeToFactor(const vctDynamicVector<prmJointType> & types,
}
}
}

std::string prmJointTypeToString(const prmJointType & data)
{
switch (data) {
case PRM_JOINT_UNDEFINED:
return "UNDEFINED";
break;
case PRM_JOINT_PRISMATIC:
return "PRISMATIC";
break;
case PRM_JOINT_REVOLUTE:
return "REVOLUTE";
break;
case PRM_JOINT_INACTIVE:
return "INACTIVE";
break;
default:
break;
}
cmnThrow("prmJointTypeToString called with invalid enum");
return "";
}

prmJointType prmJointTypeFromString(const std::string & value)
{
if (value == "UNDEFINED") {
return PRM_JOINT_UNDEFINED;
};
if (value == "PRISMATIC") {
return PRM_JOINT_PRISMATIC;
};
if (value == "REVOLUTE") {
return PRM_JOINT_REVOLUTE;
};
if (value == "INACTIVE") {
return PRM_JOINT_INACTIVE;
};
std::string message = "prmJointTypeFromString can't find matching enum for " + value + ". Options are: ";
std::vector<std::string> options = prmJointTypeVectorString();
for (std::vector<std::string>::const_iterator i = options.begin(); i != options.end(); ++i) message += *i + " ";
cmnThrow(message);
return PRM_JOINT_UNDEFINED;
}

const std::vector<std::string> & prmJointTypeVectorString(void)
{
static std::vector<std::string> vectorString;
if (vectorString.empty()) {
vectorString.push_back("UNDEFINED");
vectorString.push_back("PRISMATIC");
vectorString.push_back("REVOLUTE");
vectorString.push_back("INACTIVE");
}
return vectorString;
}
41 changes: 12 additions & 29 deletions cisstParameterTypes/prmJointType.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Author(s): Anton Deguet
Created on: 2013-05-14
(C) Copyright 2013-2021 Johns Hopkins University (JHU), All Rights Reserved.
(C) Copyright 2013-2024 Johns Hopkins University (JHU), All Rights Reserved.
--- begin cisst license - do not edit ---
Expand Down Expand Up @@ -34,42 +34,25 @@ typedef enum JointType {
PRM_JOINT_INACTIVE
} prmJointType;

void CISST_EXPORT prmJointTypeToFactor(const vctDynamicVector<prmJointType> & types,
const double prismaticFactor,
const double revoluteFactor,
vctDynamicVector<double> & factors);

std::string CISST_EXPORT prmJointTypeToString(const prmJointType & data);

prmJointType CISST_EXPORT prmJointTypeFromString(const std::string & value);

const std::vector<std::string> & CISST_EXPORT prmJointTypeVectorString(void);

typedef vctDynamicVector<prmJointType> prmJointTypeVec;
typedef mtsGenericObjectProxy<prmJointTypeVec> prmJointTypeProxy;
CMN_DECLARE_SERVICES_INSTANTIATION(prmJointTypeProxy);

inline std::string cmnDataHumanReadable(const prmJointType & data)
{
switch (data) {
case PRM_JOINT_UNDEFINED:
return "undefined";
break;
case PRM_JOINT_PRISMATIC:
return "prismatic";
break;
case PRM_JOINT_REVOLUTE:
return "revolute";
break;
case PRM_JOINT_INACTIVE:
return "inactive";
break;
default:
return "unknown";
break;
}
return "unknown";
}

CMN_DATA_SPECIALIZATION_FOR_ENUM(prmJointType, int);

#if CISST_HAS_JSON
CMN_DECLARE_DATA_FUNCTIONS_JSON_FOR_ENUM_EXPORT(prmJointType);
#endif

void CISST_EXPORT prmJointTypeToFactor(const vctDynamicVector<prmJointType> & types,
const double prismaticFactor,
const double revoluteFactor,
vctDynamicVector<double> & factors);

#endif // _prmJointType_h

0 comments on commit b444fab

Please sign in to comment.