src/generateiddfactory/WriteEnums.hpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #ifndef GENERATEIDDFACTORY_WRITEENUMS_HPP | ||
| 7 | #define GENERATEIDDFACTORY_WRITEENUMS_HPP | ||
| 8 | |||
| 9 | #include <iostream> | ||
| 10 | #include <iomanip> | ||
| 11 | #include <cmath> | ||
| 12 | #include <sstream> | ||
| 13 | #include <exception> | ||
| 14 | |||
| 15 | namespace openstudio { | ||
| 16 | |||
| 17 | template <typename Container> | ||
| 18 | ✗ | void writeDomain(std::ostream& t_os, const std::string& t_name, const Container& t_values, bool in_class) { | |
| 19 | ✗ | if (in_class) { | |
| 20 | ✗ | t_os << " enum domain " << '\n'; | |
| 21 | } else { | ||
| 22 | ✗ | t_os << " enum " << t_name << "::domain : int" << '\n'; | |
| 23 | } | ||
| 24 | |||
| 25 | ✗ | t_os << " {" << '\n'; | |
| 26 | |||
| 27 | ✗ | for (const auto& val : t_values) { | |
| 28 | ✗ | t_os << val.first << ", "; | |
| 29 | } | ||
| 30 | ✗ | t_os << " };" << '\n'; | |
| 31 | ✗ | } | |
| 32 | |||
| 33 | template <typename Container> | ||
| 34 | ✗ | void writeBuildStringVec(std::ostream& t_os, const std::string& t_name, const Container& t_values, bool in_class) { | |
| 35 | ✗ | if (in_class) { | |
| 36 | ✗ | t_os << " static VecType buildStringVec(bool isd)" << '\n'; | |
| 37 | } else { | ||
| 38 | ✗ | t_os << " " << t_name << "::VecType " << t_name << "::buildStringVec(bool isd)" << '\n'; | |
| 39 | } | ||
| 40 | |||
| 41 | t_os << " {" << '\n' | ||
| 42 | << " struct evalue" << '\n' | ||
| 43 | << " {" << '\n' | ||
| 44 | << " int value; const char *name; const char *description;" << '\n' | ||
| 45 | << " };" << '\n' | ||
| 46 | << " const evalue a[] =" << '\n' | ||
| 47 | ✗ | << " {" << '\n'; | |
| 48 | |||
| 49 | ✗ | for (const auto& val : t_values) { | |
| 50 | ✗ | t_os << "{ " << t_name << "::" << val.first << ", \"" << val.first << "\", \"" << val.second << "\"}," << '\n'; | |
| 51 | } | ||
| 52 | |||
| 53 | t_os << " { 0,0,0 }" << '\n' | ||
| 54 | << " };" << '\n' | ||
| 55 | << | ||
| 56 | |||
| 57 | " VecType v;" << '\n' | ||
| 58 | << " int i = 0;" << '\n' | ||
| 59 | << " while (!(a[i].value == 0 && a[i].name == 0 && a[i].description == 0))" << '\n' | ||
| 60 | << " {" << '\n' | ||
| 61 | << " if (isd)" << '\n' | ||
| 62 | << " {" << '\n' | ||
| 63 | << " std::string description = a[i].description; " << '\n' | ||
| 64 | << " if (!description.empty())" << '\n' | ||
| 65 | << " {" << '\n' | ||
| 66 | << " v.push_back(PT(description, a[i].value));" << '\n' | ||
| 67 | << " }" << '\n' | ||
| 68 | << " } else {" << '\n' | ||
| 69 | << " v.push_back(PT(a[i].name, a[i].value));" << '\n' | ||
| 70 | << " }" << '\n' | ||
| 71 | << " ++i;" << '\n' | ||
| 72 | << " }" << '\n' | ||
| 73 | << " return v;" << '\n' | ||
| 74 | ✗ | << " }" << '\n'; | |
| 75 | ✗ | } | |
| 76 | |||
| 77 | template <typename Container> | ||
| 78 | ✗ | void writeEnumFast(std::ostream& t_os, const std::string& t_name, const Container& t_values) { | |
| 79 | t_os << "#ifdef SWIG " << '\n' | ||
| 80 | << " OPENSTUDIO_ENUM(" << t_name << ", )" << '\n' | ||
| 81 | << "#else" << '\n' | ||
| 82 | << "class " << t_name << ": public ::EnumBase<" << t_name << "> {" << '\n' | ||
| 83 | ✗ | << " public: " << '\n'; | |
| 84 | |||
| 85 | ✗ | writeDomain(t_os, t_name, t_values, true); | |
| 86 | |||
| 87 | t_os << " " << t_name << "()" << '\n' | ||
| 88 | ✗ | << " : EnumBase<" << t_name << ">(" << t_values.front().first << ") {} " << '\n' | |
| 89 | << " " << t_name << "(const std::string &t_name) " << '\n' | ||
| 90 | << " : EnumBase<" << t_name << ">(t_name) {} " << '\n' | ||
| 91 | << " " << t_name << "(int t_value) " << '\n' | ||
| 92 | << " : EnumBase<" << t_name << ">(t_value) {} " << '\n' | ||
| 93 | << " static std::string enumName() " << '\n' | ||
| 94 | << " { return \"" << t_name << "\"; }" << '\n' | ||
| 95 | << " domain value() const { return static_cast<domain>(EnumBase<" << t_name << ">::integer_value()); }" << '\n' | ||
| 96 | << | ||
| 97 | |||
| 98 | " private:" << '\n' | ||
| 99 | << " friend class EnumBase<" << t_name << ">;" << '\n' | ||
| 100 | << " typedef std::pair<std::string, int> PT;" << '\n' | ||
| 101 | ✗ | << " typedef std::vector<PT> VecType;" << '\n'; | |
| 102 | |||
| 103 | ✗ | writeBuildStringVec(t_os, t_name, t_values, true); | |
| 104 | |||
| 105 | t_os << " " << '\n' | ||
| 106 | << | ||
| 107 | // " friend class boost::serialization::access;" << '\n' << | ||
| 108 | |||
| 109 | // " template<class Archive> void serialize(Archive & ar, const unsigned int version)" << '\n' << | ||
| 110 | // " {" << '\n' << | ||
| 111 | // " ar & boost::serialization::make_nvp(\"value\", m_value);" << '\n' << | ||
| 112 | // " }" << '\n' << | ||
| 113 | " };" << '\n' | ||
| 114 | << | ||
| 115 | |||
| 116 | " inline std::ostream &operator<<(std::ostream &os, const " << t_name << " &e)" << '\n' | ||
| 117 | << " {" << '\n' | ||
| 118 | << " return os << e.valueName() << \"(\" << e.value() << \")\";" << '\n' | ||
| 119 | << " }" << '\n' | ||
| 120 | << | ||
| 121 | |||
| 122 | " typedef boost::optional<" << t_name << "> Optional" << t_name << " ;" << '\n' | ||
| 123 | ✗ | << "#endif"; | |
| 124 | ✗ | } | |
| 125 | |||
| 126 | template <typename Container> | ||
| 127 | void writeEnum(std::ostream& t_os, const std::string& t_name, const Container& t_values) { | ||
| 128 | // write IddObjectType enum. is very large, so split into 7 groups. | ||
| 129 | auto groupSize = static_cast<unsigned>(std::ceil(static_cast<double>(t_values.size()) / 7.0)); | ||
| 130 | unsigned n = 0; // number of objects written so far--will start with Catchall and UserCustom | ||
| 131 | t_os << "OPENSTUDIO_ENUM7( " << t_name << " ," << '\n'; | ||
| 132 | |||
| 133 | for (const auto& val : t_values) { | ||
| 134 | if (n > 0 && (n % groupSize) == 0) { | ||
| 135 | t_os << " ," << '\n'; | ||
| 136 | } | ||
| 137 | |||
| 138 | // writes the enum value (name and description) | ||
| 139 | t_os << " ((" << val.first << ")"; | ||
| 140 | |||
| 141 | if (!val.second.empty()) { | ||
| 142 | t_os << "(" << val.second << ")"; | ||
| 143 | } | ||
| 144 | |||
| 145 | t_os << ")" << '\n'; | ||
| 146 | ++n; | ||
| 147 | } | ||
| 148 | |||
| 149 | t_os << ");" << '\n'; | ||
| 150 | } | ||
| 151 | |||
| 152 | } // namespace openstudio | ||
| 153 | |||
| 154 | #endif // GENERATEIDDFACTORY_WRITEENUMS_HPP | ||
| 155 |