15#include <unordered_map>
68 inline unsigned int id()
const {
return _id; }
76 virtual std::size_t
hash()
const = 0;
90 virtual std::unique_ptr<Attribute>
clone()
const = 0;
97 virtual std::unique_ptr<Attribute> clone() const override \
99 return std::unique_ptr<Attribute>(new T(*this)); \
102#define hashfunc(...) \
103 virtual std::size_t hash() const override \
106 Moose::hash_combine(h, __VA_ARGS__); \
145bool operator==(
const std::unique_ptr<Attribute> & lhs,
const std::unique_ptr<Attribute> & rhs);
156 std::size_t h = attrib.
hash();
164struct hash<
std::vector<std::unique_ptr<Attribute>>>
167 std::size_t
operator()(
const std::vector<std::unique_ptr<Attribute>> & attribs)
const
170 for (
auto & attrib : attribs)
189 template <
typename T>
191 template <
typename T>
208 template <
typename... Attribs>
225 template <
typename T>
231 for (
auto & attrib : q.attributes())
232 _attribs.push_back(attrib->clone());
249 for (std::size_t i = std::tuple_size<AttribTuple>::value; i < other.
_attribs.size(); i++)
255 template <
typename T>
264 for (
auto & attrib : q.attributes())
265 _attribs.push_back(attrib->clone());
276 for (std::size_t i = std::tuple_size<AttribTuple>::value; i < other.
_attribs.size(); i++)
284 template <
typename T,
typename... Args>
287 _attribs.emplace_back(
new T(*
_w, std::forward<Args>(args)...));
311 template <
typename T,
typename... Args>
312 std::vector<T *> &
queryInto(std::vector<T *> & results, Args &&... args)
324 template <
typename T,
typename... Args>
342 template <
typename T,
typename... Args>
343 std::vector<T *> &
queryIntoHelper(std::vector<T *> & results,
const bool sort, Args &&... args)
346 setKeysInner<0, KeyType<Attribs>...>(args...);
348 std::size_t query_id;
350 if (entry ==
_cache.end())
352 setAttribsInner<0, KeyType<Attribs>...>(args...);
362 query_id = entry->second;
367 template <
int Index,
typename A,
typename... As>
379 template <
int Index,
typename K,
typename... Args>
390 template <
int Index,
typename K,
typename... Args>
406 std::map<std::pair<bool, KeyTuple>, std::size_t>
_cache;
429 template <
typename T,
typename... Args>
437 _attrib_list.push_back(std::unique_ptr<Attribute>(
new T(*
this, dummy_args...)));
444 inline unsigned int attribID(
const std::string & name)
449 mooseError(
"no ID exists for unregistered attribute '", name,
"'");
454 void add(std::shared_ptr<MooseObject> obj);
471 std::size_t
count(
const std::vector<std::unique_ptr<Attribute>> & conds);
477 template <
typename T>
478 std::vector<T *> &
queryInto(
const std::vector<std::unique_ptr<Attribute>> & conds,
479 std::vector<T *> & results)
484 std::size_t
queryID(
const std::vector<std::unique_ptr<Attribute>> & conds);
486 template <
typename T>
487 std::vector<T *> &
queryInto(
int query_id, std::vector<T *> & results,
bool show_all =
false)
490 auto & objs =
query(query_id);
492 results.reserve(objs.size());
493 for (
auto & obj : objs)
495 mooseAssert(obj,
"Null object");
501 if constexpr (std::is_convertible_v<MooseObject *, std::remove_cv_t<T> *> ||
502 std::is_convertible_v<std::remove_cv_t<T> *,
MooseObject *>)
504 cast_obj = cast_ptr<T *>(obj);
510 cast_obj =
dynamic_cast<T *
>(obj);
511 mooseAssert(cast_obj,
512 "Queried object " + obj->typeAndName() +
" has incompatible c++ type with " +
513 MooseUtils::prettyCppType<T>());
516 mooseAssert(std::find(results.begin(), results.end(), cast_obj) == results.end(),
518 if (show_all || obj->enabled())
519 results.push_back(cast_obj);
526 int prepare(std::vector<std::unique_ptr<Attribute>> conds);
530 const std::vector<MooseObject *> &
query(
int query_id);
534 std::unique_ptr<WarehouseStorage>
_store;
535 std::vector<std::shared_ptr<MooseObject>>
_objects;
536 std::unordered_map<MooseObject *, std::size_t>
_obj_ids;
544 std::unordered_map<std::vector<std::unique_ptr<Attribute>>,
int>
_query_cache;
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
bool operator==(const std::unique_ptr< Attribute > &lhs, const std::unique_ptr< Attribute > &rhs)
TheWarehouse uses this operator function for indexing and caching queries.
This attribute describes sorting state.
AttribSorted & operator=(AttribSorted &&)=default
virtual bool isMatch(const Attribute &other) const override
isMatch returns true if the meta-data stored in this attribute is equivalent to that stored in other.
void setFrom(const Key &k)
AttribSorted(AttribSorted &&)=default
AttribSorted(const AttribSorted &)=default
AttribSorted & operator=(const AttribSorted &)=default
AttribSorted(TheWarehouse &w)
AttribSorted(TheWarehouse &w, bool is_sorted)
virtual bool isEqual(const Attribute &other) const override
isEqual returns true if the meta-data stored in this attribute is identical to that stored in other.
virtual void initFrom(const MooseObject *obj) override
initFrom reads and stores the desired meta-data from obj for later matching comparisons.
Attribute is an abstract class that can be implemented in order to track custom metadata about MooseO...
unsigned int id() const
returns the unique attribute ID associated with all attributes that have the same (mose derived) clas...
bool operator!=(const Attribute &other) const
virtual std::unique_ptr< Attribute > clone() const =0
clone creates and returns and identical (deep) copy of this attribute - i.e.
virtual std::size_t hash() const =0
This function must return a deterministic value that is uniquely determined by the data the attribute...
virtual bool isMatch(const Attribute &other) const =0
isMatch returns true if the meta-data stored in this attribute is equivalent to that stored in other.
virtual bool isEqual(const Attribute &other) const =0
isEqual returns true if the meta-data stored in this attribute is identical to that stored in other.
virtual void initFrom(const MooseObject *obj)=0
initFrom reads and stores the desired meta-data from obj for later matching comparisons.
bool operator==(const Attribute &other) const
Every object that can be built by the factory should be derived from this class.
QueryCache is a convenient way to construct and pass around (possible partially constructed) warehous...
std::vector< std::unique_ptr< Attribute > > _attribs
std::vector< std::unique_ptr< Attribute > > attributes() const
attribs returns a copy of the constructed Attribute list for the query in its current state.
std::size_t numAttribs() const
Gets the number of attributes associated with the cached query.
QueryCache(TheWarehouse &w)
Creates a new query operating on the given warehouse w.
std::size_t count()
count returns the number of results that match the query (this requires actually running the query).
QueryCache(const QueryCache &other)
QueryCache & operator=(const T &q)
Copy constructor from another Query.
QueryCache & condition(Args &&... args)
Adds a new condition to the query.
QueryCache clone() const
clone creates and returns an independent copy of the query in its current state.
std::tuple< AttribType< Attribs >... > AttribTuple
std::tuple< KeyType< Attribs >... > KeyTuple
std::vector< T * > & queryIntoHelper(std::vector< T * > &results, const bool sort, Args &&... args)
queryInto executes the query and stores the results in the given vector.
std::map< std::pair< bool, KeyTuple >, std::size_t > _cache
std::vector< T * > & queryInto(std::vector< T * > &results, Args &&... args)
queryInto executes the query and stores the results in the given vector.
TheWarehouse & warehouse() const
std::vector< T * > & queryIntoUnsorted(std::vector< T * > &results, Args &&... args)
queryInto executes the query and stores the results in the given vector.
QueryCache & operator=(const QueryCache &other)
void setKeysInner(K &k, Args &... args)
void setAttribsInner(K k, Args &... args)
TheWarehouse is a container for MooseObjects that allows querying/filtering over various customizeabl...
int prepare(std::vector< std::unique_ptr< Attribute > > conds)
prepares a query and returns an associated query_id (i.e. for use with the query function).
std::unordered_map< std::string, unsigned int > _attrib_ids
std::mutex _query_cache_mutex
Query query()
query creates and returns an initialized a query object for querying objects from the warehouse.
std::vector< std::shared_ptr< MooseObject > > _objects
unsigned int attribID(const std::string &name)
Returns a unique ID associated with the given attribute name - i.e.
void add(std::shared_ptr< MooseObject > obj)
add adds a new object to the warehouse and stores attributes/metadata about it for running queries/fi...
std::size_t count(const std::vector< std::unique_ptr< Attribute > > &conds)
count returns the number of objects that match the provided query conditions.
std::unordered_map< MooseObject *, std::size_t > _obj_ids
void readAttribs(const MooseObject *obj, std::vector< std::unique_ptr< Attribute > > &attribs)
void update(MooseObject *obj)
update updates the metadata/attribute-info stored for the given object obj that must already exists i...
std::vector< std::vector< MooseObject * > > _obj_cache
std::unique_ptr< WarehouseStorage > _store
std::mutex _obj_cache_mutex
unsigned int registerAttribute(const std::string &name, Args... dummy_args)
registers a new "tracked" attribute of type T for the warehouse.
std::unordered_map< std::vector< std::unique_ptr< Attribute > >, int > _query_cache
std::vector< T * > & queryInto(const std::vector< std::unique_ptr< Attribute > > &conds, std::vector< T * > &results)
queryInto takes the given conditions (i.e.
std::size_t queryID(const std::vector< std::unique_ptr< Attribute > > &conds)
std::vector< std::unique_ptr< Attribute > > _attrib_list
std::vector< T * > & queryInto(int query_id, std::vector< T * > &results, bool show_all=false)
void hash_combine(std::size_t &)
Used for hash function specialization for Attribute objects.
std::size_t operator()(const Attribute &attrib) const
std::size_t operator()(const std::vector< std::unique_ptr< Attribute > > &attribs) const