src/utilities/idf/ObjectOrderBase.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_OBJECTORDERBASE_HPP | ||
| 7 | #define UTILITIES_IDF_OBJECTORDERBASE_HPP | ||
| 8 | |||
| 9 | #include "../UtilitiesAPI.hpp" | ||
| 10 | |||
| 11 | #include "../idd/IddEnums.hpp" | ||
| 12 | |||
| 13 | #include "../core/Logger.hpp" | ||
| 14 | #include "../core/Optional.hpp" | ||
| 15 | |||
| 16 | /** \file ObjectOrderBase.hpp | ||
| 17 | * | ||
| 18 | * Handles ordering by IddObjectType. Does not explicitly sort any objects, but provides | ||
| 19 | * methods for user-manipulation of the order, and a predicate function, less, that compares | ||
| 20 | * IddObjectTypes. */ | ||
| 21 | |||
| 22 | namespace openstudio { | ||
| 23 | |||
| 24 | /** Base class for ordering objects. Provides ordering by IddObjectType enum (using the | ||
| 25 | * implicit conversion to int), and user-specified ordering of IddObjectTypes. */ | ||
| 26 | class UTILITIES_API ObjectOrderBase | ||
| 27 | { | ||
| 28 | public: | ||
| 29 | // CONSTRUCTORS | ||
| 30 | |||
| 31 | /** Default constructor establishes ordering by IddObjectType enum. */ | ||
| 32 | ObjectOrderBase(); | ||
| 33 | |||
| 34 | /** Constructor specifies that a derived class is in charge. */ | ||
| 35 | ObjectOrderBase(bool directOrder); | ||
| 36 | |||
| 37 | /** A user-specified order of IddObjectTypes is established on construction. */ | ||
| 38 | ObjectOrderBase(const IddObjectTypeVector& iddOrder); | ||
| 39 | |||
| 40 | 18083 | virtual ~ObjectOrderBase() = default; | |
| 41 | |||
| 42 | // GETTERS AND SETTERS | ||
| 43 | |||
| 44 | /** Returns true if ordering by IddObjectType enum. */ | ||
| 45 | bool orderByIddEnum() const; | ||
| 46 | /** Clears out the previous order, and establishes IddObjectType enum as the orderer. */ | ||
| 47 | virtual void setOrderByIddEnum(); | ||
| 48 | |||
| 49 | /** Returns the user-specified order by IddObjectType, if it exists. Otherwise return value | ||
| 50 | * evaluates to false. */ | ||
| 51 | OptionalIddObjectTypeVector iddOrder() const; | ||
| 52 | /** Clears out the previous order, and establishes order as the new basis for comparison. */ | ||
| 53 | virtual void setIddOrder(const IddObjectTypeVector& order); | ||
| 54 | /** Adds type to the user-specified IddObjectType order, if it exists. Returns false if such | ||
| 55 | * an order does not exist. */ | ||
| 56 | bool push_back(IddObjectType type); | ||
| 57 | /** Inserts type before insertBeforeType in the user-specified IddObjectType order, if it | ||
| 58 | * exists. Returns false if such an order does not exist. If insertBeforeType is not in the | ||
| 59 | * order, this method is equivalent to push_back(type). */ | ||
| 60 | bool insert(IddObjectType type, IddObjectType insertBeforeType); | ||
| 61 | /** Inserts type at index of the user-specified IddObjectType order, if it exists, and index | ||
| 62 | * is valid. Returns false if such an order does not exist. Is equivalent to push_back(type) | ||
| 63 | * if index is out of range. */ | ||
| 64 | bool insert(IddObjectType type, unsigned index); | ||
| 65 | /** Moves type from current location and places before insertBeforeType, if a user-specified | ||
| 66 | * IddObjectType order exists. Otherwise, returns false. Also returns false if type is not | ||
| 67 | * in the order. If insertBeforeType is not in the order, this method is equivalent to | ||
| 68 | * move(type,order.size()). */ | ||
| 69 | bool move(IddObjectType type, IddObjectType insertBeforeType); | ||
| 70 | /** Moves type from current location to index in user-specified IddObjectType order, if it | ||
| 71 | * exists. Returns false otherwise, or if type is not in the order. If index is invalid, | ||
| 72 | * this method is equivalent to move(type,order.size()). */ | ||
| 73 | bool move(IddObjectType type, unsigned index); | ||
| 74 | /** Swaps type1 and type2 in the user-specified IddObjectType order, if it exists and contains | ||
| 75 | * type1 and type2. Otherwise, returns false. */ | ||
| 76 | bool swap(IddObjectType type1, IddObjectType type2); | ||
| 77 | /** Deletes type from the user-specified IddObjectType order, if it exists and contains type. | ||
| 78 | * Otherwise, returns false. */ | ||
| 79 | bool erase(IddObjectType type); | ||
| 80 | |||
| 81 | /** Disables ordering by IddObjectType. Is called by derived classes to signal that they are | ||
| 82 | * setting the order. */ | ||
| 83 | void setDirectOrder(); | ||
| 84 | |||
| 85 | // SORTING | ||
| 86 | |||
| 87 | /** Predicate function used by derived classes to sort objects and handles. */ | ||
| 88 | virtual bool less(IddObjectType left, IddObjectType right) const; | ||
| 89 | |||
| 90 | /** Predicate function used by derived classes to sort objects and handles. Assumes | ||
| 91 | * uninitialized optionals are always greater than. */ | ||
| 92 | virtual bool less(boost::optional<IddObjectType> left, boost::optional<IddObjectType> right) const; | ||
| 93 | |||
| 94 | // STATE CHECKING | ||
| 95 | |||
| 96 | /** Returns true if ordering by IddObjectType enum, or if ordering by user-specified | ||
| 97 | * IddObjectType order, and type is explicitly listed. */ | ||
| 98 | bool inOrder(const IddObjectType& type) const; | ||
| 99 | |||
| 100 | /** Returns the index of type in IddObjectType order if ordering directly by enum, or by | ||
| 101 | * user-specified order. Otherwise, the return value evaluates to false. */ | ||
| 102 | OptionalUnsigned indexInOrder(const IddObjectType& type) const; | ||
| 103 | |||
| 104 | protected: | ||
| 105 | bool m_orderByIddEnum; | ||
| 106 | OptionalIddObjectTypeVector m_iddOrder; | ||
| 107 | |||
| 108 | // HELPER FUNCTIONS | ||
| 109 | IddObjectTypeVector::iterator getIterator(const IddObjectType& type); | ||
| 110 | IddObjectTypeVector::const_iterator getIterator(const IddObjectType& type) const; | ||
| 111 | |||
| 112 | private: | ||
| 113 | REGISTER_LOGGER("utilities.idf.ObjectOrderBase"); | ||
| 114 | }; | ||
| 115 | |||
| 116 | } // namespace openstudio | ||
| 117 | |||
| 118 | #endif // UTILITIES_IDF_OBJECTORDERBASE_HPP | ||
| 119 |