src/utilities/idd/IddFileAndFactoryWrapper.cpp
| 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 | #include "IddFileAndFactoryWrapper.hpp" | ||
| 7 | #include <utilities/idd/IddFactory.hxx> | ||
| 8 | #include <utilities/idd/IddEnums.hxx> | ||
| 9 | #include <utilities/embedded_files.hxx> | ||
| 10 | |||
| 11 | #include "../core/Assert.hpp" | ||
| 12 | #include "../core/Containers.hpp" | ||
| 13 | |||
| 14 | namespace openstudio { | ||
| 15 | |||
| 16 | // CONSTRUCTORS | ||
| 17 | |||
| 18 |
2/2✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
|
1 | IddFileAndFactoryWrapper::IddFileAndFactoryWrapper() : m_iddFileType(IddFileType(IddFileType::OpenStudio)) {} |
| 19 | |||
| 20 | 5453 | IddFileAndFactoryWrapper::IddFileAndFactoryWrapper(const IddFile& iddFile) : m_iddFile(iddFile) {} | |
| 21 | |||
| 22 |
1/1✓ Branch 2 taken 13553 times.
|
13553 | IddFileAndFactoryWrapper::IddFileAndFactoryWrapper(IddFileType iddFileType) : m_iddFileType(iddFileType) { |
| 23 |
3/3✓ Branch 1 taken 13553 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 13552 times.
|
13553 | if (iddFileType == IddFileType::UserCustom) { |
| 24 |
6/6✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 16 taken 1 time.
|
1 | LOG(Warn, "IddFileType set to UserCustom without an actual IddFile being specified. Please " |
| 25 | << "construct IddFileAndFactoryWrapper from an IddFile, or from a different IddFileType."); | ||
| 26 | } | ||
| 27 | 13553 | } | |
| 28 | |||
| 29 | // GETTERS | ||
| 30 | |||
| 31 | 11356 | std::string IddFileAndFactoryWrapper::version() const { | |
| 32 | 11356 | std::string result; | |
| 33 | |||
| 34 |
2/2✓ Branch 1 taken 5124 times.
✓ Branch 2 taken 6232 times.
|
22712 | if (m_iddFile) { |
| 35 |
2/2✓ Branch 1 taken 5124 times.
✓ Branch 4 taken 5124 times.
|
5124 | result = m_iddFile->version(); |
| 36 |
1/2✓ Branch 1 taken 6232 times.
✗ Branch 2 not taken.
|
12464 | } else if (m_iddFileType) { |
| 37 |
3/3✓ Branch 1 taken 6232 times.
✓ Branch 4 taken 6232 times.
✓ Branch 7 taken 6232 times.
|
6232 | result = IddFactory::instance().getVersion(*m_iddFileType); |
| 38 | } else { | ||
| 39 | ✗ | LOG_AND_THROW("Invalid IddFactoryWrapper has no IddFile set."); | |
| 40 | } | ||
| 41 | |||
| 42 | 11356 | return result; | |
| 43 | ✗ | } | |
| 44 | |||
| 45 | ✗ | std::string IddFileAndFactoryWrapper::header() const { | |
| 46 | ✗ | std::string result; | |
| 47 | |||
| 48 | ✗ | if (m_iddFile) { | |
| 49 | ✗ | result = m_iddFile->header(); | |
| 50 | ✗ | } else if (m_iddFileType) { | |
| 51 | ✗ | result = IddFactory::instance().getHeader(*m_iddFileType); | |
| 52 | } else { | ||
| 53 | ✗ | LOG_AND_THROW("Invalid IddFactoryWrapper has no IddFile set."); | |
| 54 | } | ||
| 55 | |||
| 56 | ✗ | return result; | |
| 57 | ✗ | } | |
| 58 | |||
| 59 | 358 | std::vector<IddObject> IddFileAndFactoryWrapper::objects() const { | |
| 60 | 358 | IddObjectVector result; | |
| 61 | |||
| 62 |
1/2✓ Branch 1 taken 358 times.
✗ Branch 2 not taken.
|
716 | if (m_iddFile) { |
| 63 |
2/2✓ Branch 1 taken 358 times.
✓ Branch 4 taken 358 times.
|
358 | result = m_iddFile->objects(); |
| 64 | ✗ | } else if (m_iddFileType) { | |
| 65 | ✗ | result = IddFactory::instance().getObjects(*m_iddFileType); | |
| 66 | } else { | ||
| 67 | ✗ | LOG_AND_THROW("Invalid IddFactoryWrapper has no IddFile set."); | |
| 68 | } | ||
| 69 | |||
| 70 | 358 | return result; | |
| 71 | ✗ | } | |
| 72 | |||
| 73 | ✗ | std::vector<std::string> IddFileAndFactoryWrapper::groups() const { | |
| 74 | ✗ | StringVector result; | |
| 75 | |||
| 76 | ✗ | if (m_iddFile) { | |
| 77 | ✗ | result = m_iddFile->groups(); | |
| 78 | ✗ | } else if (m_iddFileType) { | |
| 79 | ✗ | result = IddFactory::instance().getGroups(*m_iddFileType); | |
| 80 | } else { | ||
| 81 | ✗ | LOG_AND_THROW("Invalid IddFactoryWrapper has no IddFile set."); | |
| 82 | } | ||
| 83 | |||
| 84 | ✗ | return result; | |
| 85 | ✗ | } | |
| 86 | |||
| 87 | ✗ | std::vector<IddObject> IddFileAndFactoryWrapper::getObjectsInGroup(const std::string& group) const { | |
| 88 | ✗ | IddObjectVector result; | |
| 89 | |||
| 90 | ✗ | if (m_iddFile) { | |
| 91 | ✗ | result = m_iddFile->getObjectsInGroup(group); | |
| 92 | ✗ | } else if (m_iddFileType) { | |
| 93 | ✗ | result = IddFactory::instance().getObjectsInGroup(group, *m_iddFileType); | |
| 94 | } else { | ||
| 95 | ✗ | LOG_AND_THROW("Invalid IddFactoryWrapper has no IddFile set."); | |
| 96 | } | ||
| 97 | |||
| 98 | ✗ | return result; | |
| 99 | ✗ | } | |
| 100 | |||
| 101 | ✗ | std::vector<IddObject> IddFileAndFactoryWrapper::getObjects(const boost::regex& objectRegex) const { | |
| 102 | ✗ | IddObjectVector result; | |
| 103 | |||
| 104 | ✗ | if (m_iddFile) { | |
| 105 | ✗ | result = m_iddFile->getObjects(objectRegex); | |
| 106 | ✗ | } else if (m_iddFileType) { | |
| 107 | ✗ | result = IddFactory::instance().getObjects(objectRegex, *m_iddFileType); | |
| 108 | } else { | ||
| 109 | ✗ | LOG_AND_THROW("Invalid IddFactoryWrapper has no IddFile set."); | |
| 110 | } | ||
| 111 | |||
| 112 | ✗ | return result; | |
| 113 | ✗ | } | |
| 114 | |||
| 115 | 82786 | boost::optional<IddObject> IddFileAndFactoryWrapper::versionObject() const { | |
| 116 | 82786 | OptionalIddObject result; | |
| 117 | |||
| 118 |
2/2✓ Branch 1 taken 5460 times.
✓ Branch 2 taken 77326 times.
|
165572 | if (m_iddFile) { |
| 119 |
3/3✓ Branch 1 taken 5460 times.
✓ Branch 4 taken 5460 times.
✓ Branch 7 taken 5460 times.
|
5460 | result = m_iddFile->versionObject(); |
| 120 |
1/2✓ Branch 1 taken 77326 times.
✗ Branch 2 not taken.
|
154652 | } else if (m_iddFileType) { |
| 121 |
4/4✓ Branch 1 taken 77326 times.
✓ Branch 4 taken 77326 times.
✓ Branch 7 taken 77326 times.
✓ Branch 10 taken 77326 times.
|
77326 | result = IddFactory::instance().getVersionObject(*m_iddFileType); |
| 122 | } else { | ||
| 123 | ✗ | LOG_AND_THROW("Invalid IddFactoryWrapper has no IddFile set."); | |
| 124 | } | ||
| 125 | |||
| 126 | 82786 | return result; | |
| 127 | ✗ | } | |
| 128 | |||
| 129 | 1231155 | boost::optional<IddObject> IddFileAndFactoryWrapper::getObject(const std::string& objectName) const { | |
| 130 | 1231155 | OptionalIddObject result; | |
| 131 | |||
| 132 |
2/2✓ Branch 1 taken 873003 times.
✓ Branch 2 taken 358152 times.
|
2462310 | if (m_iddFile) { |
| 133 |
3/3✓ Branch 1 taken 873003 times.
✓ Branch 4 taken 873003 times.
✓ Branch 7 taken 873003 times.
|
873003 | result = m_iddFile->getObject(objectName); |
| 134 |
1/2✓ Branch 1 taken 358152 times.
✗ Branch 2 not taken.
|
716304 | } else if (m_iddFileType) { |
| 135 |
2/2✓ Branch 1 taken 358152 times.
✓ Branch 4 taken 358152 times.
|
358152 | OptionalIddObject candidate = IddFactory::instance().getObject(objectName); |
| 136 |
2/2✓ Branch 0 taken 358151 times.
✓ Branch 1 taken 1 time.
|
358152 | if (candidate) { |
| 137 |
5/5✓ Branch 1 taken 358151 times.
✓ Branch 4 taken 358151 times.
✓ Branch 7 taken 358151 times.
✓ Branch 9 taken 358147 times.
✓ Branch 10 taken 4 times.
|
358151 | if (isInFile(candidate->type())) { |
| 138 |
1/1✓ Branch 1 taken 358147 times.
|
358147 | result = candidate; |
| 139 | } | ||
| 140 | } | ||
| 141 | 358152 | } else { | |
| 142 | ✗ | LOG_AND_THROW("Invalid IddFactoryWrapper has no IddFile set."); | |
| 143 | } | ||
| 144 | |||
| 145 | 1231155 | return result; | |
| 146 | ✗ | } | |
| 147 | |||
| 148 | 116315 | boost::optional<IddObject> IddFileAndFactoryWrapper::getObject(IddObjectType objectType) const { | |
| 149 | |||
| 150 | 116315 | OptionalIddObject result; | |
| 151 | |||
| 152 |
3/3✓ Branch 1 taken 116315 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 116312 times.
|
116315 | if (objectType == IddObjectType::UserCustom) { |
| 153 |
8/8✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
✓ Branch 13 taken 3 times.
✓ Branch 16 taken 3 times.
✓ Branch 19 taken 3 times.
✓ Branch 22 taken 3 times.
|
3 | LOG(Info, "Asked to return IddObject of type IddObjectType::UserCustom. Since " |
| 154 | << "UserCustom object types are generally not unique, returning false rather than " | ||
| 155 | << "an IddObject. Please specify a different IddObjectType, or use " << "getObject(const std::string&)."); | ||
| 156 | 3 | return result; | |
| 157 | } | ||
| 158 | |||
| 159 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 116312 times.
|
232624 | if (m_iddFile) { |
| 160 | ✗ | result = m_iddFile->getObject(objectType); | |
| 161 |
1/2✓ Branch 1 taken 116312 times.
✗ Branch 2 not taken.
|
232624 | } else if (m_iddFileType) { |
| 162 |
3/3✓ Branch 1 taken 116312 times.
✓ Branch 3 taken 114850 times.
✓ Branch 4 taken 1462 times.
|
116312 | if (isInFile(objectType)) { |
| 163 |
3/3✓ Branch 1 taken 114850 times.
✓ Branch 4 taken 114850 times.
✓ Branch 7 taken 114850 times.
|
114850 | result = IddFactory::instance().getObject(objectType); |
| 164 | } | ||
| 165 | } else { | ||
| 166 | ✗ | LOG_AND_THROW("Invalid IddFactoryWrapper has no IddFile set."); | |
| 167 | } | ||
| 168 | |||
| 169 | 116312 | return result; | |
| 170 | ✗ | } | |
| 171 | |||
| 172 | 24 | std::vector<IddObject> IddFileAndFactoryWrapper::requiredObjects() const { | |
| 173 | |||
| 174 | 24 | IddObjectVector result; | |
| 175 | |||
| 176 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 21 times.
|
48 | if (m_iddFile) { |
| 177 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | result = m_iddFile->requiredObjects(); |
| 178 |
1/2✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
|
42 | } else if (m_iddFileType) { |
| 179 |
3/3✓ Branch 1 taken 21 times.
✓ Branch 4 taken 21 times.
✓ Branch 7 taken 21 times.
|
21 | result = IddFactory::instance().getRequiredObjects(*m_iddFileType); |
| 180 | } else { | ||
| 181 | ✗ | LOG_AND_THROW("Invalid IddFactoryWrapper has no IddFile set."); | |
| 182 | } | ||
| 183 | |||
| 184 | 24 | return result; | |
| 185 | ✗ | } | |
| 186 | |||
| 187 | 24 | std::vector<IddObject> IddFileAndFactoryWrapper::uniqueObjects() const { | |
| 188 | |||
| 189 | 24 | IddObjectVector result; | |
| 190 | |||
| 191 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 21 times.
|
48 | if (m_iddFile) { |
| 192 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | result = m_iddFile->uniqueObjects(); |
| 193 |
1/2✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
|
42 | } else if (m_iddFileType) { |
| 194 |
3/3✓ Branch 1 taken 21 times.
✓ Branch 4 taken 21 times.
✓ Branch 7 taken 21 times.
|
21 | result = IddFactory::instance().getUniqueObjects(*m_iddFileType); |
| 195 | } else { | ||
| 196 | ✗ | LOG_AND_THROW("Invalid IddFactoryWrapper has no IddFile set."); | |
| 197 | } | ||
| 198 | |||
| 199 | 24 | return result; | |
| 200 | ✗ | } | |
| 201 | |||
| 202 | 4233 | IddFile IddFileAndFactoryWrapper::iddFile() const { | |
| 203 |
2/2✓ Branch 1 taken 4078 times.
✓ Branch 2 taken 155 times.
|
8466 | if (m_iddFile) { |
| 204 | 4078 | return *m_iddFile; | |
| 205 |
1/2✓ Branch 1 taken 155 times.
✗ Branch 2 not taken.
|
310 | } else if (m_iddFileType) { |
| 206 | 155 | return IddFactory::instance().getIddFile(*m_iddFileType); | |
| 207 | } | ||
| 208 | |||
| 209 | ✗ | LOG_AND_THROW("Invalid IddFileAndFactoryWrapper has no IddFile set."); | |
| 210 | return {}; | ||
| 211 | } | ||
| 212 | |||
| 213 | 602208 | IddFileType IddFileAndFactoryWrapper::iddFileType() const { | |
| 214 |
2/2✓ Branch 1 taken 600444 times.
✓ Branch 2 taken 1764 times.
|
1204416 | if (m_iddFileType) { |
| 215 | 600444 | return *m_iddFileType; | |
| 216 | } | ||
| 217 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1764 times.
|
1764 | OS_ASSERT(m_iddFile); |
| 218 |
1/1✓ Branch 1 taken 1764 times.
|
1764 | return {IddFileType::UserCustom}; |
| 219 | } | ||
| 220 | |||
| 221 | // SETTERS | ||
| 222 | |||
| 223 | 1 | void IddFileAndFactoryWrapper::setIddFile(const IddFile& iddFile) { | |
| 224 | 1 | m_iddFile = iddFile; | |
| 225 | 1 | m_iddFileType = boost::none; | |
| 226 | 1 | } | |
| 227 | |||
| 228 | 117 | void IddFileAndFactoryWrapper::setIddFile(IddFileType iddFileType) { | |
| 229 |
3/3✓ Branch 1 taken 117 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 115 times.
|
117 | if (iddFileType == IddFileType::UserCustom) { |
| 230 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
|
4 | if (m_iddFile) { |
| 231 |
6/6✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 16 taken 1 time.
|
1 | LOG(Warn, "setIddFile(IddFileType::UserCustom) called when a custom IddFile has already been " |
| 232 | << "set. Taking no action (keeping the custom file already specified)."); | ||
| 233 | 1 | return; | |
| 234 | } | ||
| 235 | // m_iddFileType is set to something other than IddFileType::UserCustom. Obey the user, but log | ||
| 236 | // a warning. | ||
| 237 |
6/6✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 16 taken 1 time.
|
1 | LOG(Warn, "IddFileType set to UserCustom without an actual IddFile being specified. Please " |
| 238 | << "choose a different IddFileType, or setIddFile using an IddFile object."); | ||
| 239 | } | ||
| 240 | 116 | m_iddFile = boost::none; | |
| 241 | 116 | m_iddFileType = iddFileType; | |
| 242 | } | ||
| 243 | |||
| 244 | // QUERIES | ||
| 245 | |||
| 246 | 729338 | bool IddFileAndFactoryWrapper::isInFile(IddObjectType objectType) const { | |
| 247 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 729336 times.
|
1458676 | if (m_iddFile) { |
| 248 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
|
2 | OptionalIddObject candidate = m_iddFile->getObject(objectType); |
| 249 | 2 | return candidate.has_value(); | |
| 250 |
1/2✓ Branch 2 taken 729336 times.
✗ Branch 3 not taken.
|
1458674 | } else if (m_iddFileType) { |
| 251 | 729336 | return IddFactory::instance().isInFile(objectType, *m_iddFileType); | |
| 252 | } | ||
| 253 | |||
| 254 | ✗ | LOG_AND_THROW("Invalid IddFactoryWrapper has no IddFile set."); | |
| 255 | return false; | ||
| 256 | } | ||
| 257 | |||
| 258 | 4 | bool IddFileAndFactoryWrapper::isInFile(const std::string& objectName) const { | |
| 259 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
8 | if (m_iddFile) { |
| 260 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
|
4 | OptionalIddObject candidate = m_iddFile->getObject(objectName); |
| 261 | 4 | return candidate.has_value(); | |
| 262 |
0/2✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
4 | } else if (m_iddFileType) { |
| 263 | ✗ | OptionalIddObject candidate = IddFactory::instance().getObject(objectName); | |
| 264 | ✗ | if (candidate) { | |
| 265 | ✗ | return IddFactory::instance().isInFile(candidate->type(), *m_iddFileType); | |
| 266 | } else { | ||
| 267 | ✗ | return false; | |
| 268 | } | ||
| 269 | ✗ | } | |
| 270 | |||
| 271 | ✗ | LOG_AND_THROW("Invalid IddFactoryWrapper has no IddFile set."); | |
| 272 | return false; | ||
| 273 | } | ||
| 274 | |||
| 275 | ✗ | IddFile get_1_9_0_CBECC_IddFile() { | |
| 276 | ✗ | std::stringstream ss; | |
| 277 | ✗ | ss << ::openstudio::embedded_files::getFileAsString(":/idd/versions/1_9_0_CBECC/OpenStudio.idd"); | |
| 278 | ✗ | auto cbeccIddFile = IddFile::load(ss); | |
| 279 | ✗ | OS_ASSERT(cbeccIddFile); | |
| 280 | ✗ | return cbeccIddFile.get(); | |
| 281 | ✗ | } | |
| 282 | |||
| 283 | } // namespace openstudio | ||
| 284 |