Skip to content

Commit

Permalink
Improvements to LayerDescription::addAgentFunction()
Browse files Browse the repository at this point in the history
These were missed during #952

Closes #1003
  • Loading branch information
Robadob committed Nov 30, 2022
1 parent 32addb1 commit f529111
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
2 changes: 1 addition & 1 deletion include/flamegpu/model/AgentFunctionDescription.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CAgentFunctionDescription : public DependencyNode {
/**
* Accesses internals to validate function description before adding to layer
*/
friend void LayerDescription::addAgentFunction(const AgentFunctionDescription&);
friend void LayerDescription::addAgentFunction(const CAgentFunctionDescription&);
/**
* Accesses internals to validate function belongs to the same model as the DependencyGraph
*/
Expand Down
25 changes: 19 additions & 6 deletions include/flamegpu/model/LayerDescription.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,16 @@ class LayerDescription : public CLayerDescription {
* Adds an agent function to this layer
* The agent function will be called during this stage of model execution
* @param a Agent function as defined using FLAMEGPU_AGENT_FUNCTION notation
* @tparam AgentFunction Struct containing agent function definition
* @tparam AgentFn Struct containing agent function definition
* @throw exception::InvalidAgentFunc If the agent function does not exist within the model hierarchy
* @throw exception::InvalidAgentFunc If the agent function has already been added to the layer
* @throw exception::InvalidLayerMember If the layer already contains a SubModel
* @throw exception::InvalidLayerMember If the agent function outputs to a message list output to by an existing agent function of the layer
* @note The agent function must first be added to an Agent
* @see AgentDescription::newFunction(const std::string &, AgentFunction)
*/
template<typename AgentFunction>
void addAgentFunction(AgentFunction a = AgentFunction());
template<typename AgentFn>
void addAgentFunction(AgentFn a = AgentFn());
/**
* Adds an agent function to this layer
* The agent function will be called during this stage of model execution
Expand All @@ -158,6 +158,11 @@ class LayerDescription : public CLayerDescription {
* @throw exception::InvalidLayerMember If the agent function outputs to a message list output to by an existing agent function of the layer
* @throw exception::InvalidLayerMember If the agent function outputs an agent in the same agent state as an existing agent function's input state (or vice versa)
*/
void addAgentFunction(const CAgentFunctionDescription& afd);
/**
* @copydoc LayerDescription::addAgentFunction(const CAgentFunctionDescription&)
* @note This additional version is required, to stop the template form of this method being used
*/
void addAgentFunction(const AgentFunctionDescription &afd);
/**
* Adds an agent function to this layer
Expand Down Expand Up @@ -232,11 +237,19 @@ class LayerDescription : public CLayerDescription {
*/
inline void addHostFunctionCallback(HostFunctionCallback *func_callback);
#endif
/**
* @param index Index of the function to return
* @return A mutable reference to the agent function at the provided index
* @throw exception::OutOfBoundsException When index exceeds number of agent functions in the layer
* @see LayerDescription::getAgentFunctionsCount()
* @note Functions are stored in a set, so order may change as new functions are added
*/
AgentFunctionDescription AgentFunction(unsigned int index);
};


template<typename AgentFunction>
void LayerDescription::addAgentFunction(AgentFunction /*af*/) {
template<typename AgentFn>
void LayerDescription::addAgentFunction(AgentFn /*af*/) {
if (layer->sub_model) {
THROW exception::InvalidLayerMember("A layer containing agent functions and/or host functions, may not also contain a submodel, "
"in LayerDescription::addAgentFunction()\n");
Expand All @@ -245,7 +258,7 @@ void LayerDescription::addAgentFunction(AgentFunction /*af*/) {
THROW exception::InvalidLayerMember("A layer containing host functions, may not also contain agent functions, "
"in LayerDescription::addAgentFunction()\n");
}
AgentFunctionWrapper * func_compare = AgentFunction::fnPtr();
AgentFunctionWrapper * func_compare = AgentFn::fnPtr();
// Find the matching agent function in model hierarchy
auto mdl = layer->model.lock();
if (!mdl) {
Expand Down
2 changes: 0 additions & 2 deletions src/flamegpu/model/AgentDescription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,4 @@ void AgentDescription::setSortPeriod(const unsigned int sortPeriod) {
agent->sortPeriod = sortPeriod;
}



} // namespace flamegpu
16 changes: 15 additions & 1 deletion src/flamegpu/model/LayerDescription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ LayerDescription::LayerDescription(std::shared_ptr<LayerData> data)
/**
* Accessors
*/
void LayerDescription::addAgentFunction(const AgentFunctionDescription &afd) {
void LayerDescription::addAgentFunction(const AgentFunctionDescription& afd) {
addAgentFunction(CAgentFunctionDescription(afd));
}
void LayerDescription::addAgentFunction(const CAgentFunctionDescription &afd) {
if (afd.function->model.lock() == layer->model.lock()) {
auto m = layer->model.lock();
// Find the same afd in the model hierarchy
Expand Down Expand Up @@ -253,5 +256,16 @@ void LayerDescription::_addHostFunctionCallback(HostFunctionCallback* func_callb
"in LayerDescription::addHostFunctionCallback()");
}
}
AgentFunctionDescription LayerDescription::AgentFunction(unsigned int index) {
if (index < layer->agent_functions.size()) {
auto it = layer->agent_functions.begin();
for (unsigned int i = 0; i < index; ++i)
++it;
return AgentFunctionDescription(*it);
}
THROW exception::OutOfBoundsException("Index %d is out of bounds (only %d items exist) "
"in LayerDescription.getAgentFunction().",
index, layer->agent_functions.size());
}

} // namespace flamegpu

0 comments on commit f529111

Please sign in to comment.