.. _program_listing_file_include_flamegpu_runtime_agent_AgentInstance.h: Program Listing for File AgentInstance.h ======================================== |exhale_lsh| :ref:`Return to documentation for file ` (``include/flamegpu/runtime/agent/AgentInstance.h``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #ifndef INCLUDE_FLAMEGPU_RUNTIME_AGENT_AGENTINSTANCE_H_ #define INCLUDE_FLAMEGPU_RUNTIME_AGENT_AGENTINSTANCE_H_ #include #include #include #include #include "flamegpu/model/AgentData.h" #include "flamegpu/simulation/AgentVector.h" #include "flamegpu/detail/Any.h" namespace flamegpu { class AgentDescription; class AgentInstance { friend AgentVector::iterator AgentVector::insert(AgentVector::const_iterator pos, flamegpu::size_type count, const AgentInstance& value); friend AgentVector::iterator AgentVector::insert(flamegpu::size_type pos, flamegpu::size_type count, const AgentInstance& value); public: explicit AgentInstance(const CAgentDescription &agent_desc); AgentInstance(const AgentInstance& other); explicit AgentInstance(const AgentVector::CAgent& other); AgentInstance(AgentInstance&& other) noexcept; AgentInstance& operator=(const AgentInstance& other); AgentInstance& operator=(const AgentVector::CAgent& other); AgentInstance& operator=(AgentInstance&& other) noexcept; template T getVariable(const std::string& variable_name) const; template std::array getVariable(const std::string& variable_name) const; template T getVariable(const std::string& variable_name, unsigned int index) const; #ifdef SWIG template std::vector getVariableArray(const std::string& variable_name) const; #endif template void setVariable(const std::string& variable_name, T value); template void setVariable(const std::string& variable_name, const std::array& value); template void setVariable(const std::string& variable_name, unsigned int index, T value); #ifdef SWIG template void setVariableArray(const std::string& variable_name, const std::vector& value); #endif private: std::map _data; std::shared_ptr _agent; }; template T AgentInstance::getVariable(const std::string& variable_name) const { const auto v_it = _data.find(variable_name); if (v_it == _data.end()) { THROW exception::InvalidAgentVar("Variable with name '%s' was not found in agent, " "in AgentInstance::getVariable().", variable_name.c_str()); } const auto& v_buff = v_it->second; if (v_buff.elements != detail::type_decode::len_t) { THROW exception::InvalidVarType("Variable '%s' is an array variable, use the array method instead, " "in AgentInstance::getVariable().", variable_name.c_str()); } if (v_buff.type != std::type_index(typeid(typename detail::type_decode::type_t))) { THROW exception::InvalidVarType("Variable '%s' is of a different type. " "'%s' was expected, but '%s' was requested," "in AgentInstance::getVariable().", variable_name.c_str(), v_buff.type.name(), typeid(typename detail::type_decode::type_t).name()); } return *static_cast(v_buff.ptr); } template std::array AgentInstance::getVariable(const std::string& variable_name) const { const auto v_it = _data.find(variable_name); if (v_it == _data.end()) { THROW exception::InvalidAgentVar("Variable with name '%s' was not found in agent, " "in AgentInstance::getVariable().", variable_name.c_str()); } const auto& v_buff = v_it->second; if (v_buff.elements != N * detail::type_decode::len_t) { THROW exception::InvalidVarType("Variable '%s' has '%u' elements, but an array of length %u was passed, " "in AgentInstance::getVariable().", variable_name.c_str(), v_buff.elements / detail::type_decode::len_t, N); } if (v_buff.type != std::type_index(typeid(typename detail::type_decode::type_t))) { THROW exception::InvalidVarType("Variable '%s' is of a different type. " "'%s' was expected, but '%s' was requested," "in AgentInstance::getVariable().", variable_name.c_str(), v_buff.type.name(), typeid(typename detail::type_decode::type_t).name()); } std::array rtn; memcpy(rtn.data(), v_buff.ptr, sizeof(T) * N); return rtn; } template T AgentInstance::getVariable(const std::string& variable_name, const unsigned int index) const { const auto v_it = _data.find(variable_name); if (v_it == _data.end()) { THROW exception::InvalidAgentVar("Variable with name '%s' was not found in agent, " "in AgentInstance::getVariable().", variable_name.c_str()); } const auto& v_buff = v_it->second; if (N && N != v_buff.elements) { THROW exception::OutOfBoundsException("Variable array '%s' length mismatch '%u' != '%u', " "in AgentInstance::getVariable()\n", variable_name.c_str(), N, v_buff.elements); } if (v_buff.elements % detail::type_decode::len_t != 0) { THROW exception::InvalidVarType("Variable array length (%u) is not visible by vector length (%u), " "in AgentInstance::getVariable().", v_buff.elements, detail::type_decode::len_t, variable_name.c_str()); } const unsigned int t_index = detail::type_decode::len_t * index + detail::type_decode::len_t; if (t_index > v_buff.elements || t_index < index) { THROW exception::OutOfBoundsException("Index '%u' exceeds array bounds [0-%u) of variable '%s', " "in AgentInstance::getVariable().", index, v_buff.elements, variable_name.c_str()); } if (v_buff.type != std::type_index(typeid(typename detail::type_decode::type_t))) { THROW exception::InvalidVarType("Variable '%s' is of a different type. " "'%s' was expected, but '%s' was requested," "in AgentInstance::getVariable().", variable_name.c_str(), v_buff.type.name(), typeid(typename detail::type_decode::type_t).name()); } return static_cast(v_buff.ptr)[index]; } #ifdef SWIG template std::vector AgentInstance::getVariableArray(const std::string& variable_name) const { const auto v_it = _data.find(variable_name); if (v_it == _data.end()) { THROW exception::InvalidAgentVar("Variable with name '%s' was not found in agent, " "in AgentInstance::getVariableArray().", variable_name.c_str()); } const auto& v_buff = v_it->second; if (v_buff.elements % detail::type_decode::len_t != 0) { THROW exception::InvalidVarType("Variable array length (%u) is not visible by vector length (%u), " "in AgentInstance::getVariableArray().", v_buff.elements, detail::type_decode::len_t, variable_name.c_str()); } if (v_buff.type != std::type_index(typeid(typename detail::type_decode::type_t))) { THROW exception::InvalidVarType("Variable '%s' is of a different type. " "'%s' was expected, but '%s' was requested," "in AgentInstance::getVariableArray().", variable_name.c_str(), v_buff.type.name(), typeid(typename detail::type_decode::type_t).name()); } std::vector rtn(static_cast(v_buff.elements / detail::type_decode::len_t)); memcpy(rtn.data(), static_cast(v_buff.ptr), sizeof(T) * v_buff.elements); return rtn; } #endif template void AgentInstance::setVariable(const std::string& variable_name, T value) { const auto v_it = _data.find(variable_name); if (v_it == _data.end()) { THROW exception::InvalidAgentVar("Variable with name '%s' was not found in agent, " "in AgentInstance::setVariable().", variable_name.c_str()); } auto& v_buff = v_it->second; if (v_buff.elements != detail::type_decode::len_t) { THROW exception::InvalidVarType("Variable '%s' is an array variable, use the array method instead, " "in AgentInstance::setVariable().", variable_name.c_str()); } if (v_buff.type != std::type_index(typeid(typename detail::type_decode::type_t))) { THROW exception::InvalidVarType("Variable '%s' is of a different type. " "'%s' was expected, but '%s' was requested," "in AgentInstance::setVariable().", variable_name.c_str(), v_buff.type.name(), typeid(typename detail::type_decode::type_t).name()); } // do the replace *static_cast(v_buff.ptr) = value; } template void AgentInstance::setVariable(const std::string& variable_name, const std::array& value) { const auto v_it = _data.find(variable_name); if (v_it == _data.end()) { THROW exception::InvalidAgentVar("Variable with name '%s' was not found in agent, " "in AgentInstance::setVariable().", variable_name.c_str()); } auto& v_buff = v_it->second; if (v_buff.elements != N * detail::type_decode::len_t) { THROW exception::InvalidVarType("Variable '%s' has '%u' elements, but an array of length %u was passed, " "in AgentInstance::setVariable().", variable_name.c_str(), v_buff.elements, N); } if (v_buff.type != std::type_index(typeid(typename detail::type_decode::type_t))) { THROW exception::InvalidVarType("Variable '%s' is of a different type. " "'%s' was expected, but '%s' was requested," "in AgentInstance::setVariable().", variable_name.c_str(), v_buff.type.name(), typeid(typename detail::type_decode::type_t).name()); } memcpy(static_cast(v_buff.ptr), value.data(), sizeof(T) * N); } template void AgentInstance::setVariable(const std::string& variable_name, const unsigned int index, T value) { const auto v_it = _data.find(variable_name); if (v_it == _data.end()) { THROW exception::InvalidAgentVar("Variable with name '%s' was not found in agent, " "in AgentInstance::setVariable().", variable_name.c_str()); } auto& v_buff = v_it->second; if (N && N != v_buff.elements) { THROW exception::OutOfBoundsException("Variable array '%s' length mismatch '%u' != '%u', " "in AgentInstance::setVariable()\n", variable_name.c_str(), N, v_buff.elements); } if (v_buff.elements % detail::type_decode::len_t != 0) { THROW exception::InvalidVarType("Variable array length (%u) is not visible by vector length (%u), " "in AgentInstance::setVariable().", v_buff.elements, detail::type_decode::len_t, variable_name.c_str()); } const unsigned int t_index = detail::type_decode::len_t * index + detail::type_decode::len_t; if (t_index > v_buff.elements || t_index < index) { THROW exception::OutOfBoundsException("Index '%u' exceeds array bounds [0-%u) of variable '%s', " "in AgentInstance::setVariable().", index, v_buff.elements, variable_name.c_str()); } if (v_buff.type != std::type_index(typeid(typename detail::type_decode::type_t))) { THROW exception::InvalidVarType("Variable '%s' is of a different type. " "'%s' was expected, but '%s' was requested," "in AgentInstance::setVariable().", variable_name.c_str(), v_buff.type.name(), typeid(typename detail::type_decode::type_t).name()); } static_cast(v_buff.ptr)[index] = value; } #ifdef SWIG template void AgentInstance::setVariableArray(const std::string& variable_name, const std::vector& value) { const auto v_it = _data.find(variable_name); if (v_it == _data.end()) { THROW exception::InvalidAgentVar("Variable with name '%s' was not found in agent, " "in AgentInstance::setVariableArray().", variable_name.c_str()); } auto& v_buff = v_it->second; if (v_buff.elements != value.size() * detail::type_decode::len_t) { THROW exception::InvalidVarType("Variable '%s' has '%u' elements, but an array of length %u was passed, " "in AgentInstance::setVariableArray().", variable_name.c_str(), v_buff.elements, value.size() * detail::type_decode::len_t); } if (v_buff.type != std::type_index(typeid(typename detail::type_decode::type_t))) { THROW exception::InvalidVarType("Variable '%s' is of a different type. " "'%s' was expected, but '%s' was requested," "in AgentInstance::setVariableArray().", variable_name.c_str(), v_buff.type.name(), typeid(typename detail::type_decode::type_t).name()); } memcpy(static_cast(v_buff.ptr), value.data(), sizeof(T) * v_buff.elements); } #endif // SWIG } // namespace flamegpu #endif // INCLUDE_FLAMEGPU_RUNTIME_AGENT_AGENTINSTANCE_H_