Stiffstream / sobjectizer

An implementation of Actor, Publish-Subscribe, and CSP models in one rather small C++ framework. With performance, quality, and stability proved by years in the production.

Home Page:https://stiffstream.com/en/products/sobjectizer.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Optional name for an agent?

eao197 opened this issue · comments

For some case (like logging and run-time monitoring) may be useful to have a possibility to specify a name for an agent. That name has to be stored inside so_5::agent_t and should be available via a pointer/reference to so_5::agent_t.

I don't think it's a good idea to place another std::string into so_5::agent_t, because in the most cases it will be an empty value.

Maybe something like that:

namespace so_5
{

// Public stuff.

struct anonymous_agent_id_t { std::array<char, N> m_ptr_as_id; };
struct named_agent_id_t { std::string_view m_name; }
using agent_name_t = std::variant<anonymous_agent_id_t, named_agent_id_t>;

[[nodiscard]]
std::string_view to_string_view(const agent_name_t & name);

class agent_t {
public:
  [[nodiscard]]
  agent_name_t
  so_agent_name() const noexcept;

private:
  std::unique_ptr<char[]> m_agent_name;
};

} /* namespace so_5 */

If the name is not specified in the constructor (via modificators for context_t object) then agent_t::m_agent_name will be nullptr. And agent_t::so_agent_name() will return an instance of anonymous_agent_id_t where m_ptr_as_id will hold a string representation of agent's this pointer.

If the name is specified, then agent_t::m_agent_name will contain a copy of name inside, and agent_t::so_agent_name() will return named_agent_id_t that references this name.

So if the name is not used the space penalty will be just sizeof(std::unique_ptr<char[]>) (8 bytes for 64-bit platform).

It's a part of v.5.8.2 release.