Line data Source code
1 : //---------------------------------------------------------------- 2 : // 3 : // File: EnumChannelDirection.cpp 4 : // 5 : //---------------------------------------------------------------- 6 : 7 : #include "codec/EnumChannelDirection.h" 8 : #include "gen/StringUtils.h" 9 : 10 : using namespace EnumChannelDirection; 11 : using namespace Gen; 12 : 13 : constexpr const char* InvalidStr = "Invalid"; 14 : constexpr const char* NotApplicableStr = "NotApplicable"; 15 : constexpr const char* ForwardStr = "Forward"; 16 : constexpr const char* ReturnStr = "Return"; 17 : 18 : /*-----------------------------------------------------------*//** 19 : 20 : Convert enum to string. 21 : 22 : @param[in] d 23 : The enum of interest. 24 : 25 : @return 26 : Enum as a string. 27 : */ 28 : std::string 29 28 : EnumChannelDirection::toString(ChannelDirection d) noexcept 30 : { 31 28 : switch (d) 32 : { 33 0 : case ChannelDirection::Invalid: return InvalidStr; 34 33 : case ChannelDirection::NotApplicable: return NotApplicableStr; 35 30 : case ChannelDirection::Forward: return ForwardStr; 36 21 : case ChannelDirection::Return: return ReturnStr; 37 : } 38 0 : return InvalidStr; 39 : } 40 : 41 : /*-----------------------------------------------------------*//** 42 : 43 : Convert ChannelDirection string to enum. 44 : 45 : @param[in] s 46 : The channel's direction as a string. Matches against these strings 47 : "Forward", "Return", or "notapplicable". Matching is 48 : not sensitive to case. 49 : 50 : @return 51 : Enum value. 52 : 53 : @throws std::invalid_argument 54 : Throws for unrecognized strings. isConvertibleFrom() 55 : can be used if throw is not needed. 56 : */ 57 : ChannelDirection 58 4 : EnumChannelDirection::fromString(const std::string& s) 59 : { 60 10 : for (auto val : enumerators) 61 : { 62 9 : if (StringUtils::insensitiveEquals(s, toString(val))) 63 : { 64 3 : return val; 65 : } 66 : } 67 : 68 : throw std::invalid_argument( 69 : "EnumChannelDirection: Conversion error: " 70 1 : "Invalid conversion to enum: " + 71 2 : s + 72 2 : ", does not match any of " + 73 3 : listAsString()); 74 : } 75 : 76 : /*-----------------------------------------------------------*//** 77 : 78 : Returns a std::string of the enums separated by a space. 79 : 80 : @return 81 : Returns a std::string of the enums separated by a space. 82 : */ 83 : std::string 84 2 : EnumChannelDirection::listAsString() 85 : { 86 2 : std::string s; 87 8 : for (auto val : enumerators) 88 : { 89 6 : s += toString(val); 90 6 : s += " "; 91 : } 92 2 : StringUtils::trimEnd(s); 93 2 : return s; 94 0 : } 95 : 96 : /*-----------------------------------------------------------*//** 97 : 98 : Determines if given string can convert to enum. 99 : 100 : @param[in] s 101 : The string of interest. 102 : 103 : @return 104 : True if convertible; false otherwise. 105 : */ 106 : bool 107 4 : EnumChannelDirection::isConvertibleFrom(const std::string& s) noexcept 108 : { 109 10 : for (auto val : enumerators) 110 : { 111 9 : if (StringUtils::insensitiveEquals(s, toString(val))) 112 : { 113 3 : return true; 114 : } 115 : } 116 1 : return false; 117 : } 118 : 119 : /*-----------------------------------------------------------*//** 120 : 121 : Convert to enum from unsigned integer. 122 : 123 : @param[in] v 124 : The enum as an unsigned integer. 125 : 126 : @return 127 : Enum value. Returns Invalid for an unrecognized conversion. 128 : */ 129 : ChannelDirection 130 4 : EnumChannelDirection::fromUnsigned(unsigned v) noexcept 131 : { 132 4 : if (v == static_cast<unsigned>(ChannelDirection::Forward)) 133 : { 134 1 : return ChannelDirection::Forward; 135 : } 136 3 : else if (v == static_cast<unsigned>(ChannelDirection::Return)) 137 : { 138 1 : return ChannelDirection::Return; 139 : } 140 2 : else if (v == static_cast<unsigned>(ChannelDirection::NotApplicable)) 141 : { 142 1 : return ChannelDirection::NotApplicable; 143 : } 144 : else 145 : { 146 1 : return ChannelDirection::Invalid; 147 : } 148 : } 149 : 150 : /*-----------------------------------------------------------*//** 151 : 152 : Stream output operator for ::ChannelDirection. 153 : 154 : @param[in,out] out 155 : The output stream. 156 : 157 : @param[in] d 158 : The channel direction. 159 : 160 : @return 161 : The output stream. 162 : 163 : @ingroup free 164 : */ 165 : std::ostream& 166 1 : operator<< (std::ostream& out, const ChannelDirection& d) 167 : { 168 1 : out << EnumChannelDirection::toString(d); 169 1 : return out; 170 : } 171 : 172 : //-----------------------------------------------------------------