Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ntsa::TransportRole #239

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion groups/nts/ntsa/ntsa_transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ int TransportMode::fromString(TransportMode::Value* result,
*result = e_STREAM;
return 0;
}

if (bdlb::String::areEqualCaseless(string, "DATAGRAM")) {
*result = e_DATAGRAM;
return 0;
Expand Down Expand Up @@ -223,6 +222,67 @@ bsl::ostream& operator<<(bsl::ostream& stream, TransportMode::Value rhs)
return TransportMode::print(stream, rhs);
}

int TransportRole::fromInt(TransportRole::Value* result, int number)
{
switch (number) {
case TransportRole::e_UNDEFINED:
case TransportRole::e_CLIENT:
case TransportRole::e_SERVER:
*result = static_cast<TransportRole::Value>(number);
return 0;
default:
return -1;
}
}

int TransportRole::fromString(TransportRole::Value* result,
const bslstl::StringRef& string)
{
if (bdlb::String::areEqualCaseless(string, "UNDEFINED")) {
*result = e_UNDEFINED;
return 0;
}
if (bdlb::String::areEqualCaseless(string, "CLIENT")) {
*result = e_CLIENT;
return 0;
}
if (bdlb::String::areEqualCaseless(string, "SERVER")) {
*result = e_SERVER;
return 0;
}

return -1;
}

const char* TransportRole::toString(TransportRole::Value value)
{
switch (value) {
case e_UNDEFINED: {
return "UNDEFINED";
} break;
case e_CLIENT: {
return "CLIENT";
} break;
case e_SERVER: {
return "SERVER";
} break;
}

BSLS_ASSERT(!"invalid enumerator");
return 0;
}

bsl::ostream& TransportRole::print(bsl::ostream& stream,
TransportRole::Value value)
{
return stream << toString(value);
}

bsl::ostream& operator<<(bsl::ostream& stream, TransportRole::Value rhs)
{
return TransportRole::print(stream, rhs);
}

int Transport::fromInt(Transport::Value* result, int number)
{
switch (number) {
Expand Down
54 changes: 52 additions & 2 deletions groups/nts/ntsa/ntsa_transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,57 @@ struct TransportMode {
/// @related ntsa::TransportMode
bsl::ostream& operator<<(bsl::ostream& stream, TransportMode::Value rhs);

/// Provide an enumeration of the socket types.
/// Provide an enumeration of the socket transport roles.
///
/// @par Thread Safety
/// This struct is thread safe.
///
/// @ingroup module_ntsa_system
struct TransportRole {
public:
/// Provide an enumeration of the socket transport roles.
enum Value {
/// The socket transport role is undefined.
e_UNDEFINED = 0,

/// The socket is used to actively initiate the establishment the
/// transport.
e_CLIENT = 1,

/// The socket is used to passively wait for a peer to initiate the
/// establishment of the transport.
e_SERVER = 2
};

/// Return the string representation exactly matching the enumerator
/// name corresponding to the specified enumeration 'value'.
static const char* toString(Value value);

/// Load into the specified 'result' the enumerator matching the
/// specified 'string'. Return 0 on success, and a non-zero value with
/// no effect on 'result' otherwise (i.e., 'string' does not match any
/// enumerator).
static int fromString(Value* result, const bslstl::StringRef& string);

/// Load into the specified 'result' the enumerator matching the
/// specified 'number'. Return 0 on success, and a non-zero value with
/// no effect on 'result' otherwise (i.e., 'number' does not match any
/// enumerator).
static int fromInt(Value* result, int number);

/// Write to the specified 'stream' the string representation of the
/// specified enumeration 'value'. Return a reference to the modifiable
/// 'stream'.
static bsl::ostream& print(bsl::ostream& stream, Value value);
};

/// Format the specified 'rhs' to the specified output 'stream' and return a
/// reference to the modifiable 'stream'.
///
/// @related ntsa::TransportRole
bsl::ostream& operator<<(bsl::ostream& stream, TransportRole::Value rhs);

/// Provide an enumeration of the socket transport types.
///
/// @details
/// A socket type is defined by its address family, transport protocol, and
Expand All @@ -191,7 +241,7 @@ bsl::ostream& operator<<(bsl::ostream& stream, TransportMode::Value rhs);
/// @ingroup module_ntsa_system
struct Transport {
public:
/// Provide an enumeration of the socket types.
/// Provide an enumeration of the socket transport types.
enum Value {
/// The socket transport is undefined.
e_UNDEFINED = 0,
Expand Down
Loading