src/model/ParentObject.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 MODEL_PARENTOBJECT_HPP | ||
| 7 | #define MODEL_PARENTOBJECT_HPP | ||
| 8 | |||
| 9 | #include "ModelAPI.hpp" | ||
| 10 | #include "ModelObject.hpp" | ||
| 11 | |||
| 12 | namespace openstudio { | ||
| 13 | namespace model { | ||
| 14 | |||
| 15 | namespace detail { | ||
| 16 | class ParentObject_Impl; | ||
| 17 | } | ||
| 18 | |||
| 19 | /** ParentObject is an abstract class derived from ModelObject. | ||
| 20 | * | ||
| 21 | * \todo It is an open question if the functionality of ParentObject should be merged | ||
| 22 | * with ModelObject and ParentObject deprecated. | ||
| 23 | */ | ||
| 24 | class MODEL_API ParentObject : public ModelObject | ||
| 25 | { | ||
| 26 | public: | ||
| 27 | 15917680 | virtual ~ParentObject() override = default; | |
| 28 | // Default the copy and move operators because the virtual dtor is explicit | ||
| 29 | 1408473 | ParentObject(const ParentObject& other) = default; | |
| 30 | 7232413 | ParentObject(ParentObject&& other) = default; | |
| 31 | 1124 | ParentObject& operator=(const ParentObject&) = default; | |
| 32 | 917504 | ParentObject& operator=(ParentObject&&) = default; | |
| 33 | |||
| 34 | /** @name Constructors and Destructors */ | ||
| 35 | //@{ | ||
| 36 | |||
| 37 | //@} | ||
| 38 | /** @name Getters */ | ||
| 39 | //@{ | ||
| 40 | |||
| 41 | //@} | ||
| 42 | /** @name Setters */ | ||
| 43 | //@{ | ||
| 44 | |||
| 45 | //@} | ||
| 46 | |||
| 47 | /// Return direct child objects in the Model. | ||
| 48 | std::vector<ModelObject> children() const; | ||
| 49 | |||
| 50 | // DLM@20110614: this should be static on the wrapper, abstract for the impl. | ||
| 51 | /// Returns a vector of allowable children types. | ||
| 52 | std::vector<IddObjectType> allowableChildTypes() const; | ||
| 53 | |||
| 54 | protected: | ||
| 55 | /** @name Constructors and Destructors */ | ||
| 56 | //@{ | ||
| 57 | |||
| 58 | /// Constructs a new ParentObject object in the model. | ||
| 59 | ParentObject(IddObjectType type, const Model& model); | ||
| 60 | |||
| 61 | //@} | ||
| 62 | |||
| 63 | /// @cond | ||
| 64 | |||
| 65 | using ImplType = detail::ParentObject_Impl; | ||
| 66 | |||
| 67 | friend class Model; | ||
| 68 | friend class openstudio::IdfObject; | ||
| 69 | friend class openstudio::detail::IdfObject_Impl; | ||
| 70 | friend class detail::ParentObject_Impl; | ||
| 71 | |||
| 72 | explicit ParentObject(std::shared_ptr<detail::ParentObject_Impl> impl); | ||
| 73 | |||
| 74 | private: | ||
| 75 | REGISTER_LOGGER("openstudio.model.ParentObject"); | ||
| 76 | |||
| 77 | /// @endcond | ||
| 78 | }; | ||
| 79 | |||
| 80 | // optional ParentObject | ||
| 81 | using OptionalParentObject = boost::optional<ParentObject>; | ||
| 82 | |||
| 83 | // vector of ParentObject | ||
| 84 | using ParentObjectVector = std::vector<ParentObject>; | ||
| 85 | |||
| 86 | /** Returns object and all other objects accessible by calling children() on encountered | ||
| 87 | * \link ParentObject ParentObjects \endlink. LifeCycleCost and AdditionalProperties objects | ||
| 88 | * are not technically children but may be included by setting the optional second argument to true. \relates ParentObject | ||
| 89 | * An additional switch can exclude ResourceObject which have a directUseCount > 1, which is especially useful for remove() so you don't remove | ||
| 90 | * ResourceObjects (such as Curve) if they are actually used by another object than the one you're deleting */ | ||
| 91 | MODEL_API std::vector<ModelObject> getRecursiveChildren(const ParentObject& object, bool includeLifeCycleCostsAndAdditionalProperties = false, | ||
| 92 | bool includeUsedResources = true); | ||
| 93 | |||
| 94 | /** Returns all objects in getRecursiveChildren(object) that can be cast to type T. */ | ||
| 95 | template <typename T> | ||
| 96 | std::vector<T> getSubsetOfRecursiveChildren(const ParentObject& object) { | ||
| 97 | std::vector<ModelObject> allChildren = getRecursiveChildren(object); | ||
| 98 | std::vector<T> result; | ||
| 99 | for (const ModelObject& child : allChildren) { | ||
| 100 | boost::optional<T> candidate = child.optionalCast<T>(); | ||
| 101 | if (candidate) { | ||
| 102 | result.push_back(*candidate); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | return result; | ||
| 106 | } | ||
| 107 | |||
| 108 | /** Returns object and all other objects accessible by calling children() and resources() on | ||
| 109 | * encountered \link ModelObject ModelObjects \endlink. \relates ParentObject */ | ||
| 110 | MODEL_API std::vector<ModelObject> getRecursiveChildrenAndResources(const ModelObject& object); | ||
| 111 | |||
| 112 | /** Returns all objects in getRecursiveChildrenAndResources(object) that can be cast to type T. */ | ||
| 113 | template <typename T> | ||
| 114 | std::vector<T> getSubsetOfRecursiveChildrenAndResources(const ModelObject& object) { | ||
| 115 | std::vector<ModelObject> allChildrenAndResources = getRecursiveChildrenAndResources(object); | ||
| 116 | std::vector<T> result; | ||
| 117 | for (const ModelObject& childOrResource : allChildrenAndResources) { | ||
| 118 | boost::optional<T> candidate = childOrResource.optionalCast<T>(); | ||
| 119 | if (candidate) { | ||
| 120 | result.push_back(*candidate); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | return result; | ||
| 124 | } | ||
| 125 | |||
| 126 | } // namespace model | ||
| 127 | } // namespace openstudio | ||
| 128 | |||
| 129 | #endif // MODEL_PARENTOBJECT_HPP | ||
| 130 |