15 #include <unordered_map> 67 inline unsigned int id()
const {
return _id; }
75 virtual std::size_t
hash()
const = 0;
89 virtual std::unique_ptr<Attribute>
clone()
const = 0;
95 #define clonefunc(T) \ 96 virtual std::unique_ptr<Attribute> clone() const override \ 98 return std::unique_ptr<Attribute>(new T(*this)); \ 101 #define hashfunc(...) \ 102 virtual std::size_t hash() const override \ 105 Moose::hash_combine(h, __VA_ARGS__); \ 144 bool operator==(
const std::unique_ptr<Attribute> & lhs,
const std::unique_ptr<Attribute> & rhs);
155 std::size_t h = attrib.
hash();
163 struct hash<
std::vector<std::unique_ptr<Attribute>>>
166 std::size_t
operator()(
const std::vector<std::unique_ptr<Attribute>> & attribs)
const 169 for (
auto & attrib : attribs)
188 template <
typename T>
190 template <
typename T>
207 template <
typename... Attribs>
224 template <
typename T>
230 for (
auto & attrib : q.attributes())
231 _attribs.push_back(attrib->clone());
248 for (std::size_t i = std::tuple_size<AttribTuple>::value; i < other.
_attribs.size(); i++)
254 template <
typename T>
263 for (
auto & attrib : q.attributes())
264 _attribs.push_back(attrib->clone());
275 for (std::size_t i = std::tuple_size<AttribTuple>::value; i < other.
_attribs.size(); i++)
283 template <
typename T,
typename... Args>
286 _attribs.emplace_back(
new T(*
_w, std::forward<Args>(args)...));
310 template <
typename T,
typename... Args>
311 std::vector<T *> &
queryInto(std::vector<T *> & results, Args &&... args)
323 template <
typename T,
typename... Args>
341 template <
typename T,
typename... Args>
342 std::vector<T *> &
queryIntoHelper(std::vector<T *> & results,
const bool sort, Args &&... args)
345 setKeysInner<0, KeyType<Attribs>...>(args...);
347 std::size_t query_id;
349 if (entry ==
_cache.end())
351 setAttribsInner<0, KeyType<Attribs>...>(args...);
361 query_id = entry->second;
366 template <
int Index,
typename A,
typename... As>
378 template <
int Index,
typename K,
typename... Args>
389 template <
int Index,
typename K,
typename... Args>
405 std::map<std::pair<bool, KeyTuple>, std::size_t>
_cache;
428 template <
typename T,
typename... Args>
436 _attrib_list.push_back(std::unique_ptr<Attribute>(
new T(*
this, dummy_args...)));
443 inline unsigned int attribID(
const std::string & name)
448 mooseError(
"no ID exists for unregistered attribute '",
name,
"'");
453 void add(std::shared_ptr<MooseObject> obj);
470 std::size_t
count(
const std::vector<std::unique_ptr<Attribute>> & conds);
476 template <
typename T>
477 std::vector<T *> &
queryInto(
const std::vector<std::unique_ptr<Attribute>> & conds,
478 std::vector<T *> & results)
483 std::size_t
queryID(
const std::vector<std::unique_ptr<Attribute>> & conds);
485 template <
typename T>
486 std::vector<T *> &
queryInto(
int query_id, std::vector<T *> & results,
bool show_all =
false)
489 auto & objs =
query(query_id);
491 results.reserve(objs.size());
492 for (
auto & obj : objs)
494 mooseAssert(obj,
"Null object");
495 auto cast_obj =
dynamic_cast<T *
>(obj);
497 mooseAssert(cast_obj,
498 "Queried object " + obj->typeAndName() +
" has incompatible c++ type with " +
499 MooseUtils::prettyCppType<T>());
500 mooseAssert(std::find(results.begin(), results.end(), cast_obj) == results.end(),
502 if (show_all || obj->enabled())
503 results.push_back(cast_obj);
510 int prepare(std::vector<std::unique_ptr<Attribute>> conds);
514 const std::vector<MooseObject *> &
query(
int query_id);
518 std::unique_ptr<WarehouseStorage>
_store;
519 std::vector<std::shared_ptr<MooseObject>>
_objects;
520 std::unordered_map<MooseObject *, std::size_t>
_obj_ids;
528 std::unordered_map<std::vector<std::unique_ptr<Attribute>>,
int>
_query_cache;
std::string name(const ElemQuality q)
unsigned int id() const
returns the unique attribute ID associated with all attributes that have the same (mose derived) clas...
std::mutex _obj_cache_mutex
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::vector< T * > & queryIntoHelper(std::vector< T *> &results, const bool sort, Args &&... args)
queryInto executes the query and stores the results in the given vector.
QueryCache is a convenient way to construct and pass around (possible partially constructed) warehous...
bool operator!=(const Attribute &other) const
Attribute is an abstract class that can be implemented in order to track custom metadata about MooseO...
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
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::vector< T * > & queryInto(std::vector< T *> &results, Args &&... args)
queryInto executes the query and stores the results in the given vector.
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...
This attribute describes sorting state.
QueryCache(const QueryCache &other)
bool operator==(const Attribute &other) const
std::tuple< KeyType< Attribs >... > KeyTuple
Attribute(TheWarehouse &w, const std::string name)
Constructs/initializes a new attribute with the specified name for use in warehouse w...
void hash_combine(std::size_t &)
Used for hash function specialization for Attribute objects.
std::size_t count()
count returns the number of results that match the query (this requires actually running the query)...
QueryCache(TheWarehouse &w)
Creates a new query operating on the given warehouse w.
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 isEqual(const Attribute &other) const override
isEqual returns true if the meta-data stored in this attribute is identical to that stored in other...
std::size_t operator()(const Attribute &attrib) const
std::vector< std::unique_ptr< Attribute > > _attribs
std::unique_ptr< WarehouseStorage > _store
void setAttribsInner(K k, Args &... args)
std::unordered_map< std::string, unsigned int > _attrib_ids
QueryCache & operator=(const T &q)
Copy constructor from another Query.
std::mutex _query_cache_mutex
std::map< std::pair< bool, KeyTuple >, std::size_t > _cache
bool operator==(const std::unique_ptr< Attribute > &lhs, const std::unique_ptr< Attribute > &rhs)
TheWarehouse uses this operator function for indexing and caching queries.
std::vector< std::shared_ptr< MooseObject > > _objects
Every object that can be built by the factory should be derived from this class.
std::size_t numAttribs() const
Gets the number of attributes associated with the cached query.
QueryCache & operator=(const QueryCache &other)
void readAttribs(const MooseObject *obj, std::vector< std::unique_ptr< Attribute >> &attribs)
TheWarehouse & warehouse() const
std::size_t queryID(const std::vector< std::unique_ptr< Attribute >> &conds)
TheWarehouse is a container for MooseObjects that allows querying/filtering over various customizeabl...
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...
std::vector< T * > & queryInto(int query_id, std::vector< T *> &results, bool show_all=false)
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).
QueryCache clone() const
clone creates and returns an independent copy of the query in its current state.
constexpr bool is_sorted()
Check if the given index sequence is sorted ()internal function)
std::tuple< AttribType< Attribs >... > AttribTuple
std::unordered_map< std::vector< std::unique_ptr< Attribute > >, int > _query_cache
AttribSorted(TheWarehouse &w)
std::unordered_map< MooseObject *, std::size_t > _obj_ids
std::size_t operator()(const std::vector< std::unique_ptr< Attribute >> &attribs) const
void setKeysInner(K &k, Args &... args)
void update(MooseObject *obj)
update updates the metadata/attribute-info stored for the given object obj that must already exists i...
Query query()
query creates and returns an initialized a query object for querying objects from the warehouse...
void setFrom(const Key &k)
unsigned int registerAttribute(const std::string &name, Args... dummy_args)
registers a new "tracked" attribute of type T for the warehouse.
std::vector< std::unique_ptr< Attribute > > attributes() const
attribs returns a copy of the constructed Attribute list for the query in its current state...
virtual void initFrom(const MooseObject *obj) override
initFrom reads and stores the desired meta-data from obj for later matching comparisons.
std::vector< T * > & queryInto(const std::vector< std::unique_ptr< Attribute >> &conds, std::vector< T *> &results)
queryInto takes the given conditions (i.e.
QueryCache & condition(Args &&... args)
Adds a new condition to the query.
unsigned int attribID(const std::string &name)
Returns a unique ID associated with the given attribute name - i.e.
AttribSorted & operator=(const AttribSorted &)=default
std::vector< T * > & queryIntoUnsorted(std::vector< T *> &results, Args &&... args)
queryInto executes the query and stores the results in the given vector.
AttribSorted(TheWarehouse &w, bool is_sorted)
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 void initFrom(const MooseObject *obj)=0
initFrom reads and stores the desired meta-data from obj for later matching comparisons.
std::vector< std::unique_ptr< Attribute > > _attrib_list
std::vector< std::vector< MooseObject * > > _obj_cache
virtual std::unique_ptr< Attribute > clone() const =0
clone creates and returns and identical (deep) copy of this attribute - i.e.