src/utilities/idf/WorkspaceObject_Impl.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 UTILITIES_IDF_WORKSPACEOBJECT_IMPL_HPP | ||
| 7 | #define UTILITIES_IDF_WORKSPACEOBJECT_IMPL_HPP | ||
| 8 | |||
| 9 | #include <utilities/UtilitiesAPI.hpp> | ||
| 10 | #include <nano/nano_signal_slot.hpp> // Signal-Slot replacement | ||
| 11 | |||
| 12 | #include <utilities/idf/IdfObject_Impl.hpp> | ||
| 13 | #include <utilities/idf/ObjectPointer.hpp> | ||
| 14 | |||
| 15 | namespace openstudio { | ||
| 16 | |||
| 17 | // forward declarations | ||
| 18 | class WorkspaceObject; | ||
| 19 | class Workspace; | ||
| 20 | |||
| 21 | namespace detail { | ||
| 22 | |||
| 23 | class Workspace_Impl; // forward declaration | ||
| 24 | |||
| 25 | struct UTILITIES_API ForwardPointer | ||
| 26 | { | ||
| 27 | unsigned fieldIndex; | ||
| 28 | Handle targetHandle; | ||
| 29 | |||
| 30 | /// \todo Default constructor needed to iterate over Source Map, but setting fieldIndex to 0 | ||
| 31 | /// seems sub-optimal. | ||
| 32 | ForwardPointer() : fieldIndex(0) {} | ||
| 33 | 1233959 | ForwardPointer(unsigned i, const Handle& h) : fieldIndex(i), targetHandle(h) {} | |
| 34 | }; | ||
| 35 | using ForwardPointerSet = std::set<ForwardPointer, FieldIndexLess<ForwardPointer>>; | ||
| 36 | |||
| 37 | struct UTILITIES_API SourceData | ||
| 38 | { | ||
| 39 | using pointer_type = ForwardPointer; | ||
| 40 | using pointer_set = ForwardPointerSet; | ||
| 41 | |||
| 42 | pointer_set pointers; | ||
| 43 | |||
| 44 | 301981 | SourceData() = default; | |
| 45 | }; | ||
| 46 | using OptionalSourceData = boost::optional<SourceData>; | ||
| 47 | |||
| 48 | struct UTILITIES_API ReversePointer | ||
| 49 | { | ||
| 50 | Handle sourceHandle; | ||
| 51 | unsigned fieldIndex; | ||
| 52 | |||
| 53 | ReversePointer() : fieldIndex(0) {} | ||
| 54 | 601211 | ReversePointer(const Handle& h, unsigned i) : sourceHandle(h), fieldIndex(i) {} | |
| 55 | }; | ||
| 56 | struct UTILITIES_API ReversePointerLess | ||
| 57 | { | ||
| 58 | 1482706 | bool operator()(const ReversePointer& left, const ReversePointer& right) const { | |
| 59 |
2/2✓ Branch 1 taken 140920 times.
✓ Branch 2 taken 1341786 times.
|
1482706 | if (left.sourceHandle == right.sourceHandle) { |
| 60 | 140920 | return (left.fieldIndex < right.fieldIndex); | |
| 61 | } else { | ||
| 62 | 1341786 | return (left.sourceHandle < right.sourceHandle); | |
| 63 | } | ||
| 64 | } | ||
| 65 | }; | ||
| 66 | using ReversePointerSet = std::set<ReversePointer, ReversePointerLess>; | ||
| 67 | |||
| 68 | struct UTILITIES_API TargetData | ||
| 69 | { | ||
| 70 | using pointer_type = ReversePointer; | ||
| 71 | using pointer_set = ReversePointerSet; | ||
| 72 | |||
| 73 | pointer_set reversePointers; | ||
| 74 | }; | ||
| 75 | using OptionalTargetData = boost::optional<TargetData>; | ||
| 76 | |||
| 77 | template <class T> | ||
| 78 | 2344213 | typename T::pointer_set::iterator getIteratorAtFieldIndex(typename T::pointer_set& pointerSet, unsigned fieldIndex) { | |
| 79 | 2344213 | return std::find_if(pointerSet.begin(), pointerSet.end(), | |
| 80 | 7178559 | [fieldIndex](const auto& ptr_type) { return fieldIndexEqualTo<typename T::pointer_type>(ptr_type, fieldIndex); }); | |
| 81 | } | ||
| 82 | |||
| 83 | template <class T> | ||
| 84 | 8571897 | typename T::pointer_set::const_iterator getConstIteratorAtFieldIndex(const typename T::pointer_set& pointerSet, unsigned fieldIndex) { | |
| 85 | 8571897 | return std::find_if(pointerSet.begin(), pointerSet.end(), | |
| 86 | 33199240 | [fieldIndex](const auto& ptr_type) { return fieldIndexEqualTo<typename T::pointer_type>(ptr_type, fieldIndex); }); | |
| 87 | } | ||
| 88 | |||
| 89 | class UTILITIES_API WorkspaceObject_Impl : public IdfObject_Impl | ||
| 90 | { | ||
| 91 | public: | ||
| 92 | /** @name Constructors */ | ||
| 93 | //@{ | ||
| 94 | |||
| 95 | /** Construct from IdfObject_Impl (via idfObject). */ | ||
| 96 | WorkspaceObject_Impl(const IdfObject& idfObject, Workspace_Impl* workspace, bool keepHandle = false); | ||
| 97 | |||
| 98 | /** Copy constructor. Assigns new handle, keeps same workspace pointer. Workspace should know | ||
| 99 | * whether copy is to live in original workspace, or in cloned workspace. If the latter, | ||
| 100 | * Workspace will need to update the workspace pointer, and any source and target pointer | ||
| 101 | * handles. */ | ||
| 102 | WorkspaceObject_Impl(const WorkspaceObject_Impl& other, Workspace_Impl* workspace, bool keepHandle = false); | ||
| 103 | |||
| 104 | /** Complete construction process by pointing to workspace and replacing name pointers. */ | ||
| 105 | virtual void initializeOnAdd(bool expectToLosePointers = false); | ||
| 106 | |||
| 107 | /** Complete copy construction process by updating pointer handles. */ | ||
| 108 | virtual void initializeOnClone(const HandleMap& oldNewHandleMap); | ||
| 109 | |||
| 110 | 728781 | virtual ~WorkspaceObject_Impl() = default; | |
| 111 | |||
| 112 | /// remove the object from the workspace | ||
| 113 | /// return std::vector<IdfObject> containing any removed object(s) | ||
| 114 | // TODO: replace with undo struct including connections | ||
| 115 | virtual std::vector<IdfObject> remove(); | ||
| 116 | |||
| 117 | //@} | ||
| 118 | /** @name Getters */ | ||
| 119 | //@{ | ||
| 120 | |||
| 121 | /** Returns pointer to this object's workspace. */ | ||
| 122 | Workspace_Impl* workspaceImpl() const; | ||
| 123 | |||
| 124 | /** Returns this object's workspace. */ | ||
| 125 | Workspace workspace() const; | ||
| 126 | |||
| 127 | /** Get the value of field index, if index < numFields(). Optionally, if returnDefault is | ||
| 128 | * passed in as true, getString will return the default value for non-existent | ||
| 129 | * (non-extensible) fields and fields with empty data, if a default exists. */ | ||
| 130 | virtual boost::optional<std::string> getString(unsigned index, bool returnDefault = false, bool returnUninitializedEmpty = false) const override; | ||
| 131 | |||
| 132 | virtual boost::optional<std::string> getField(unsigned index, bool returnDefault = false) const override; | ||
| 133 | |||
| 134 | /** Returns the object pointed to by the field at index, if it exists. */ | ||
| 135 | boost::optional<WorkspaceObject> getTarget(unsigned index) const; | ||
| 136 | |||
| 137 | /** Returns the objects pointed to by this object. */ | ||
| 138 | std::vector<WorkspaceObject> targets() const; | ||
| 139 | |||
| 140 | /** Returns the index of the field in this object that points to targetHandle, if possible. */ | ||
| 141 | std::vector<unsigned> getSourceIndices(const Handle& targetHandle) const; | ||
| 142 | |||
| 143 | /** Returns the objects that point to this object. */ | ||
| 144 | std::vector<WorkspaceObject> sources() const; | ||
| 145 | |||
| 146 | /** Returns the objects of type that point to this object. */ | ||
| 147 | std::vector<WorkspaceObject> getSources(IddObjectType type) const; | ||
| 148 | |||
| 149 | /** Provided for Workspace_Impl to get easy access to targetData. */ | ||
| 150 | ReversePointerSet getReversePointers() const; | ||
| 151 | |||
| 152 | //@} | ||
| 153 | /** @name Setters */ | ||
| 154 | //@{ | ||
| 155 | |||
| 156 | /** Sets the name field if it exists, returning the actual name string set. Returns false | ||
| 157 | * otherwise. The return value and newName may differ (by an appended integer) if a | ||
| 158 | * conflict with newName was detected. Name conflicts will not be automatically avoided | ||
| 159 | * in IDF (text-only) mode. */ | ||
| 160 | virtual boost::optional<std::string> setName(const std::string& newName) override; | ||
| 161 | virtual boost::optional<std::string> setName(const std::string& newName, bool checkValidity) override; | ||
| 162 | |||
| 163 | /** Sets the name field to a new, unique name, if the name field exists and (is empty or | ||
| 164 | * overwrite == true). Returns false otherwise. Created name is returned. */ | ||
| 165 | virtual boost::optional<std::string> createName() override; | ||
| 166 | virtual boost::optional<std::string> createName(bool overwrite) override; | ||
| 167 | |||
| 168 | /** Sets the field at index to value, if possible. Returns false if the value cannot be set for any | ||
| 169 | * reason. (Perhaps index >= numFields(), or setting field index to value would invalidate the | ||
| 170 | * object.) */ | ||
| 171 | virtual bool setString(unsigned index, const std::string& value) override; | ||
| 172 | virtual bool setString(unsigned index, const std::string& value, bool checkValidity) override; | ||
| 173 | |||
| 174 | /** Sets the pointer at field index to targetHandle, if possible. The field must be of \object-list | ||
| 175 | * type, and targetHandle must be valid (null or in the containing Workspace, and if the strictness | ||
| 176 | * is greater than enums::None, must correspond to an object of a proper type). */ | ||
| 177 | virtual bool setPointer(unsigned index, const Handle& targetHandle); | ||
| 178 | virtual bool setPointer(unsigned index, const Handle& targetHandle, bool checkValidity); | ||
| 179 | |||
| 180 | /** Pushes a new field onto the object, if possible. Returns false if the operation cannot be completed | ||
| 181 | * for any reason. */ | ||
| 182 | virtual bool pushString() override; | ||
| 183 | virtual bool pushString(bool checkValidity) override; | ||
| 184 | virtual bool pushString(const std::string& value) override; | ||
| 185 | virtual bool pushString(const std::string& value, bool checkValidity) override; | ||
| 186 | |||
| 187 | /** Pushes a new pointer field onto the object, pointing to targetHandle, if possible. Returns | ||
| 188 | * false if operation cannot be completed for any reason. */ | ||
| 189 | bool pushPointer(const Handle& targetHandle); | ||
| 190 | bool pushPointer(const Handle& targetHandle, bool checkValidity); | ||
| 191 | |||
| 192 | /** Pops the final extensible group from the object, if possible. Returns the popped data if | ||
| 193 | * successful. Otherwise, the returned vector will be empty. */ | ||
| 194 | virtual std::vector<std::string> popExtensibleGroup() override; | ||
| 195 | virtual std::vector<std::string> popExtensibleGroup(bool checkValidity) override; | ||
| 196 | |||
| 197 | //@} | ||
| 198 | /** @name Queries */ | ||
| 199 | //@{ | ||
| 200 | |||
| 201 | /** Returns true if object is connected to a workspace and initialized. */ | ||
| 202 | bool initialized() const; | ||
| 203 | |||
| 204 | /** Returns the number of objects that point to this object. */ | ||
| 205 | unsigned numSources() const; | ||
| 206 | |||
| 207 | /** Returns true if this object points to another object. To qualify, there must be at least one | ||
| 208 | * pointer field holding a non-null pointer. */ | ||
| 209 | bool isSource() const; | ||
| 210 | |||
| 211 | bool isSource(unsigned index) const; | ||
| 212 | |||
| 213 | /** Returns true if this object can point to another object. To qualify, there must be at least | ||
| 214 | * one \object-list field. */ | ||
| 215 | bool canBeSource() const; | ||
| 216 | |||
| 217 | /** Returns true if field index can be a source field. If no refLists are specified, then | ||
| 218 | * this call is equivalent to iddObject().getField(index)->isObjectListField(). If a | ||
| 219 | * non-empty vector of refLists is specified, then true is only returned if there is a match | ||
| 220 | * between field index's object lists and the referencesLists argument. */ | ||
| 221 | bool canBeSource(unsigned index, const std::vector<std::string>& refLists = std::vector<std::string>()) const; | ||
| 222 | |||
| 223 | /** Returns true if another object points to this object. */ | ||
| 224 | bool isTarget() const; | ||
| 225 | |||
| 226 | /** Returns the reference lists of which this object is a member, if this object has a name. */ | ||
| 227 | std::vector<std::string> canBeTarget() const; | ||
| 228 | |||
| 229 | /** Checks for equality of objectListFields(). Prerequisite: iddObject()s must be | ||
| 230 | * equal. Looks for object equality of targets if same workspace(), otherwise, looks for data | ||
| 231 | * field equality of targets. */ | ||
| 232 | bool objectListFieldsEqual(const WorkspaceObject& other) const; | ||
| 233 | |||
| 234 | /** Returns true if for all objectListFields() that for both objects are non-empty, | ||
| 235 | * the values are equal (using the same criteria as objectListFieldsEqual). | ||
| 236 | * Prerequisite: iddObject()s must be equal. */ | ||
| 237 | bool objectListFieldsNonConflicting(const WorkspaceObject& other) const; | ||
| 238 | |||
| 239 | //@} | ||
| 240 | /** @name Serialization */ | ||
| 241 | //@{ | ||
| 242 | |||
| 243 | /** Returns equivalent IdfObject, naming targets if necessary. All data is cloned. */ | ||
| 244 | IdfObject_ImplPtr idfObjectImplPtr(); | ||
| 245 | |||
| 246 | /** Returns equivalent IdfObject, leaving unnamed target objects unnamed. All data is cloned. */ | ||
| 247 | IdfObject_ImplPtr idfObjectImplPtr() const; | ||
| 248 | |||
| 249 | /** Returns equivalent IdfObject, naming targets if necessary. All data is cloned. */ | ||
| 250 | IdfObject idfObject(); | ||
| 251 | |||
| 252 | /** Returns equivalent IdfObject, leaving unnamed target objects unnamed. All data is cloned. */ | ||
| 253 | IdfObject idfObject() const; | ||
| 254 | |||
| 255 | //@} | ||
| 256 | /** @name Signal Helpers */ | ||
| 257 | //@{ | ||
| 258 | |||
| 259 | /** Emits signals after batch update and error checking is complete, clears the diffs */ | ||
| 260 | virtual void emitChangeSignals() override; | ||
| 261 | |||
| 262 | //@} | ||
| 263 | |||
| 264 | //@} | ||
| 265 | /** @name Nano Signal */ | ||
| 266 | //@{ | ||
| 267 | |||
| 268 | /** Emitted when a pointer field is changed. */ | ||
| 269 | Nano::Signal<void(int, Handle, Handle)> onRelationshipChange; | ||
| 270 | |||
| 271 | /** Emitted when this object is disconnected from the workspace. Do not | ||
| 272 | * access any methods of this object as it is invalid. */ | ||
| 273 | Nano::Signal<void(const Handle&)> onRemoveFromWorkspace; | ||
| 274 | // Nano::Signal<void(Handle&)> onRemoveFromWorkspaceRef; | ||
| 275 | |||
| 276 | Nano::Signal<void(const openstudio::Handle&)> workspaceObjectRemovedSignal; | ||
| 277 | |||
| 278 | protected: | ||
| 279 | friend class Workspace_Impl; | ||
| 280 | |||
| 281 | /** Denotes that this object has been initialized by Workspace_Impl. */ | ||
| 282 | void setInitialized(); | ||
| 283 | |||
| 284 | /** Disconnects this object from its workspace. Nullifies m_workspace and m_handle. */ | ||
| 285 | void disconnect(); | ||
| 286 | |||
| 287 | /** Mechanics only exposed to Workspace_Impl for use in object removal. */ | ||
| 288 | void nullifyPointer(unsigned index); | ||
| 289 | |||
| 290 | void nullifyReversePointer(const Handle& sourceHandle, unsigned index); | ||
| 291 | |||
| 292 | void setReversePointer(const Handle& sourceHandle, unsigned index); | ||
| 293 | |||
| 294 | /** Called when restoring object because could not remove and retain validity. Double-checks | ||
| 295 | * that companion pointers are in place. May not be able to fix all if multiple objects are | ||
| 296 | * being restored. Does not throw or log because trusts Workspace to restore all relevant | ||
| 297 | * objects. */ | ||
| 298 | void restorePointers(); | ||
| 299 | |||
| 300 | // QUERY HELPERS | ||
| 301 | |||
| 302 | virtual void populateValidityReport(ValidityReport& report, bool checkNames) const override; | ||
| 303 | |||
| 304 | /** Returns true if the object is identifiable (within its collection) by IddObjectType and | ||
| 305 | * name. Also, if this object is in a reference list, its name must be unique within (single) | ||
| 306 | * overlapping references. */ | ||
| 307 | virtual bool uniquelyIdentifiableByName() const; | ||
| 308 | |||
| 309 | /** Checks ObjectList fields, and calls IdfObject_Impl version. */ | ||
| 310 | virtual bool fieldDataIsCorrectType(unsigned index) const override; | ||
| 311 | |||
| 312 | virtual bool fieldIsNonnullIfRequired(unsigned index) const override; | ||
| 313 | |||
| 314 | private: | ||
| 315 | bool m_initialized; | ||
| 316 | Workspace_Impl* m_workspace; | ||
| 317 | OptionalSourceData m_sourceData; | ||
| 318 | OptionalTargetData m_targetData; | ||
| 319 | |||
| 320 | // SETTER HELPERS | ||
| 321 | |||
| 322 | /** Sets pointer at field index to targetHandle, and returns old target. */ | ||
| 323 | Handle setPointerImpl(unsigned index, const Handle& targetHandle); | ||
| 324 | |||
| 325 | boost::optional<Handle> convertToTargetHandle(const std::string& name, const std::set<std::string>& referenceLists, bool checkValidity) const; | ||
| 326 | |||
| 327 | void restoreOriginalNumFields(unsigned n); | ||
| 328 | |||
| 329 | bool popField(); | ||
| 330 | |||
| 331 | // configure logging | ||
| 332 | − | REGISTER_LOGGER("utilities.idf.WorkspaceObject"); | |
| 333 | }; | ||
| 334 | |||
| 335 | /** \relates WorkspaceObject_Impl */ | ||
| 336 | using WorkspaceObject_ImplPtr = std::shared_ptr<WorkspaceObject_Impl>; | ||
| 337 | |||
| 338 | /** \relates WorkspaceObject_Impl */ | ||
| 339 | using WorkspaceObject_ImplPtrVector = std::vector<WorkspaceObject_ImplPtr>; | ||
| 340 | |||
| 341 | } // namespace detail | ||
| 342 | |||
| 343 | } // namespace openstudio | ||
| 344 | |||
| 345 | #endif // UTILITIES_IDF_WORKSPACEOBJECT_IMPL_HPP | ||
| 346 |