-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The first draft of agent_identity_t class.
- Loading branch information
Showing
12 changed files
with
349 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* SObjectizer-5 | ||
*/ | ||
|
||
/*! | ||
* \file | ||
* \brief Types related to agents identity (name). | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <so_5/declspec.hpp> | ||
|
||
#include <so_5/fwd.hpp> | ||
|
||
#include <array> | ||
#include <iostream> | ||
#include <string> | ||
#include <string_view> | ||
#include <variant> | ||
|
||
namespace so_5 | ||
{ | ||
|
||
//FIXME: document this! | ||
class agent_identity_t | ||
{ | ||
friend class agent_t; | ||
|
||
//! Type for case when agent has no user-provided name. | ||
struct pointer_only_t | ||
{ | ||
//! Capacity of c-string for holding string representation. | ||
//! | ||
//! Size of hex representation of a pointer + "<noname:>".length() + 1. | ||
static constexpr std::size_t c_string_size = | ||
(sizeof(void*) * 2u) | ||
+ std::string_view{ "<noname:>" }.size() | ||
+ 1u /* terminating 0-symbol */; | ||
|
||
//! Value. | ||
const void * m_pointer_value; | ||
|
||
// NOTE: this method is implemented in agent.cpp source file. | ||
//! Make a c-string with text representation of a value. | ||
SO_5_FUNC [[nodiscard]] | ||
std::array<char, c_string_size> | ||
make_c_string() const noexcept; | ||
}; | ||
|
||
//! Type for case when agent has user-provided name. | ||
struct actual_name_t | ||
{ | ||
//! Value. | ||
std::string_view m_name; | ||
}; | ||
|
||
//FIXME: document this! | ||
struct to_string_visitor_t | ||
{ | ||
[[nodiscard]] | ||
std::string | ||
operator()( const pointer_only_t & v ) const | ||
{ | ||
const auto c_str = v.make_c_string(); | ||
return std::string( c_str.data(), pointer_only_t::c_string_size - 1u ); | ||
} | ||
|
||
[[nodiscard]] | ||
std::string | ||
operator()( const actual_name_t & v ) const | ||
{ | ||
return std::string( v.m_name ); | ||
} | ||
}; | ||
|
||
//FIXME: document this! | ||
struct to_ostream_visitor_t | ||
{ | ||
std::ostream & m_to; | ||
|
||
to_ostream_visitor_t( std::ostream & to ) : m_to{ to } {} | ||
|
||
void | ||
operator()( const pointer_only_t & v ) const | ||
{ | ||
const auto c_str = v.make_c_string(); | ||
m_to << c_str.data(); | ||
} | ||
|
||
void | ||
operator()( const actual_name_t & v ) const | ||
{ | ||
m_to << v.m_name; | ||
} | ||
}; | ||
|
||
//! Type of identity holder. | ||
using value_t = std::variant< pointer_only_t, actual_name_t >; | ||
|
||
//! Agent's identity. | ||
value_t m_value; | ||
|
||
//! Initializing constructor for case when agent has no user specified name. | ||
agent_identity_t( const void * pointer ) noexcept | ||
: m_value{ pointer_only_t{ pointer } } | ||
{} | ||
|
||
//! Initializing constructor for case when agent has a user specified name. | ||
agent_identity_t( std::string_view name ) noexcept | ||
: m_value{ actual_name_t{ name } } | ||
{} | ||
|
||
public: | ||
//! Does agent have an actual name? | ||
[[nodiscard]] | ||
bool | ||
has_actual_name() const noexcept | ||
{ | ||
return std::holds_alternative< actual_name_t >( m_value ); | ||
} | ||
|
||
//! Attempt to get an agent name. | ||
/*! | ||
* \return empty std::string_view if agent has no an actual name. | ||
*/ | ||
[[nodiscard]] | ||
std::string_view | ||
actual_name() const noexcept | ||
{ | ||
if( const auto * an = std::get_if< actual_name_t >( &m_value ) ) | ||
return an->m_name; | ||
else | ||
return std::string_view{}; | ||
} | ||
|
||
//! Transform identity into a string. | ||
[[nodiscard]] | ||
std::string | ||
to_string() const | ||
{ | ||
return std::visit( to_string_visitor_t{}, m_value ); | ||
} | ||
|
||
friend std::ostream & | ||
operator<<( std::ostream & to, const agent_identity_t & what ) | ||
{ | ||
std::visit( to_ostream_visitor_t{ to }, what.m_value ); | ||
return to; | ||
} | ||
}; | ||
|
||
} /* namespace so_5 */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_subdirectory(agent_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
set(UNITTEST _unit.test.agent.agent_name) | ||
include(${CMAKE_SOURCE_DIR}/cmake/unittest.cmake) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* A unit-test for agent_t::so_agent_name(). | ||
*/ | ||
|
||
#include <so_5/all.hpp> | ||
|
||
#include <test/3rd_party/various_helpers/ensure.hpp> | ||
#include <test/3rd_party/various_helpers/time_limited_execution.hpp> | ||
|
||
namespace test | ||
{ | ||
|
||
class anonymous_agent_t final : public so_5::agent_t | ||
{ | ||
public: | ||
using so_5::agent_t::agent_t; | ||
|
||
void | ||
so_evt_start() override | ||
{ | ||
const auto id = so_agent_name(); | ||
std::cout << "anonymous_agent_t: " << id << std::endl; | ||
|
||
ensure_or_die( !id.has_actual_name(), "agent should not have a name!" ); | ||
|
||
const auto str_id = id.to_string(); | ||
ensure_or_die( "<noname:" == str_id.substr( 0u, 8u ), | ||
"unexpected prefix!" ); | ||
|
||
so_deregister_agent_coop_normally(); | ||
} | ||
}; | ||
|
||
class named_agent_t final : public so_5::agent_t | ||
{ | ||
public: | ||
named_agent_t( context_t ctx ) | ||
: so_5::agent_t{ ctx + so_5::name_for_agent_t{ "Alice" } } | ||
{} | ||
|
||
void | ||
so_evt_start() override | ||
{ | ||
const auto id = so_agent_name(); | ||
std::cout << "named_agent_t: " << id << std::endl; | ||
|
||
ensure_or_die( id.has_actual_name(), "agent should have a name!" ); | ||
|
||
constexpr std::string_view expected_value{ "Alice" }; | ||
ensure_or_die( id.actual_name() == expected_value, | ||
"unexpected result of id.actual_name()!" ); | ||
|
||
const auto str_id = id.to_string(); | ||
ensure_or_die( str_id == expected_value, | ||
"unexpected result of id.to_string()!" ); | ||
|
||
so_deregister_agent_coop_normally(); | ||
} | ||
}; | ||
|
||
void | ||
init( so_5::environment_t & env ) | ||
{ | ||
env.register_agent_as_coop( env.make_agent< anonymous_agent_t >() ); | ||
env.register_agent_as_coop( env.make_agent< named_agent_t >() ); | ||
} | ||
|
||
} /* namespace test */ | ||
|
||
int | ||
main() | ||
{ | ||
try | ||
{ | ||
run_with_time_limit( []{ so_5::launch( test::init ); }, 5 ); | ||
|
||
return 0; | ||
} | ||
catch( const std::exception & x ) | ||
{ | ||
std::cerr << "Exception: " << x.what() << std::endl; | ||
} | ||
|
||
return 2; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require 'mxx_ru/cpp' | ||
|
||
MxxRu::Cpp::exe_target { | ||
|
||
required_prj 'so_5/prj.rb' | ||
|
||
target '_unit.test.agent.agent_name' | ||
|
||
cpp_source 'main.cpp' | ||
} | ||
|
Oops, something went wrong.