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

Enroll ntsa::Uri, ntsa::Host, and ntsa::Transport into the bdlat introspection framework #223

Merged
merged 1 commit into from
Sep 30, 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
45 changes: 45 additions & 0 deletions groups/nts/ntsa/ntsa_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,5 +340,50 @@ bsl::ostream& Host::print(bsl::ostream& stream,
return stream;
}

const bdlat_SelectionInfo* Host::lookupSelectionInfo(int id)
{
const int numSelections =
sizeof(SELECTION_INFO_ARRAY) / sizeof(SELECTION_INFO_ARRAY[0]);

if (id < 0 || id >= numSelections) {
return 0;
}

return &SELECTION_INFO_ARRAY[id];
}

const bdlat_SelectionInfo* Host::lookupSelectionInfo(
const char* name,
int nameLength)
{
const bsl::size_t numSelections =
sizeof(SELECTION_INFO_ARRAY) / sizeof(SELECTION_INFO_ARRAY[0]);

for (bsl::size_t i = 0; i < numSelections; ++i) {
const bdlat_SelectionInfo& selectionInfo = SELECTION_INFO_ARRAY[i];
if (selectionInfo.d_nameLength == nameLength) {
const int compare =
bsl::memcmp(selectionInfo.d_name_p, name, nameLength);
if (compare == 0) {
return &selectionInfo;
}
}
}

return 0;
}

const char Host::CLASS_NAME[11] = "ntsa::Host";

// clang-format off
const bdlat_SelectionInfo Host::SELECTION_INFO_ARRAY[4] =
{
{ ntsa::HostType::e_UNDEFINED, "undefined", 9, "", 0 },
{ ntsa::HostType::e_DOMAIN_NAME, "domain", 6, "", 0 },
{ ntsa::HostType::e_IP, "ip", 2, "", 0 },
{ ntsa::HostType::e_LOCAL_NAME, "local", 5, "", 0 }
};
// clang-format on

} // close package namespace
} // close enterprise namespace
176 changes: 176 additions & 0 deletions groups/nts/ntsa/ntsa_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ BSLS_IDENT("$Id: $")
#include <ntsa_ipaddress.h>
#include <ntscfg_platform.h>
#include <ntsscm_version.h>
#include <bdlat_typetraits.h>
#include <bdlat_selectioninfo.h>
#include <bdlat_choicefunctions.h>
#include <bslh_hash.h>
#include <bsls_assert.h>
#include <bsls_objectbuffer.h>
Expand Down Expand Up @@ -238,6 +241,51 @@ class Host
int level = 0,
int spacesPerLevel = 4) const;

/// Set the value of this object to be the default for the selection
/// indicated by the specified 'id'. Return 0 on success, and non-zero
/// value otherwise (i.e., the selection is not found).
int makeSelection(int id);

/// Set the value of this object to be the default for the selection
/// indicated by the specified 'name' of the specified 'nameLength'.
/// Return 0 on success, and non-zero value otherwise (i.e., the selection
/// is not found).
int makeSelection(const char* name, int nameLength);

/// Return the selection ID of the current selection in the choice.
int selectionId() const;

/// Invoke the specified 'manipulator' on the address of the modifiable
/// selection, supplying 'manipulator' with the corresponding selection
/// information structure. Return the value returned from the invocation
/// of 'manipulator' if this object has a defined selection, and -1
/// otherwise.
template <typename MANIPULATOR>
int manipulateSelection(MANIPULATOR& manipulator);

/// Invoke the specified 'accessor' on the non-modifiable selection,
/// supplying 'accessor' with the corresponding selection information
/// structure. Return the value returned from the invocation of 'accessor'
/// if this object has a defined selection, and -1 otherwise.
template <typename ACCESSOR>
int accessSelection(ACCESSOR& accessor) const;

/// Return the compiler-independant name for this class.
static const char CLASS_NAME[11];

/// The selection info array, indexed by selection index.
static const bdlat_SelectionInfo SELECTION_INFO_ARRAY[4];

/// Return selection information for the selection indicated by the
/// specified 'id' if the selection exists, and 0 otherwise.
static const bdlat_SelectionInfo* lookupSelectionInfo(int id);

/// Return selection information for the selection indicated by the
/// specified 'name' of the specified 'nameLength' if the selection
/// exists, and 0 otherwise.
static const bdlat_SelectionInfo* lookupSelectionInfo(
const char* name, int nameLength);

/// Defines the traits of this type. These traits can be used to select,
/// at compile-time, the most efficient algorithm to manipulate objects
/// of this type.
Expand Down Expand Up @@ -395,6 +443,134 @@ void hashAppend(HASH_ALGORITHM& algorithm, const Host& value)
}
}

NTSCFG_INLINE
int Host::makeSelection(int id)
{
switch (id) {
case ntsa::HostType::e_UNDEFINED:
this->reset();
break;
case ntsa::HostType::e_DOMAIN_NAME:
this->makeDomainName();
break;
case ntsa::HostType::e_IP:
this->makeIp();
break;
case ntsa::HostType::e_LOCAL_NAME:
this->makeLocalName();
break;
default:
return -1;
}

return 0;
}

NTSCFG_INLINE
int Host::makeSelection(const char* name, int nameLength)
{
const bdlat_SelectionInfo *selectionInfo =
ntsa::Host::lookupSelectionInfo(name, nameLength);
if (selectionInfo == 0) {
return -1;
}

return this->makeSelection(selectionInfo->d_id);
}

NTSCFG_INLINE
int Host::selectionId() const
{
return static_cast<int>(d_type);
}

template <typename MANIPULATOR>
int Host::manipulateSelection(MANIPULATOR& manipulator)
{
int rc;

if (d_type == ntsa::HostType::e_UNDEFINED) {
;
}
else if (d_type == ntsa::HostType::e_DOMAIN_NAME) {
bsl::string representation;
rc = manipulator(&representation, SELECTION_INFO_ARRAY[d_type]);
if (rc != 0) {
return rc;
}

if (!d_domainName.object().parse(representation)) {
return 1;
}
}
else if (d_type == ntsa::HostType::e_IP) {
bsl::string representation;
rc = manipulator(&representation, SELECTION_INFO_ARRAY[d_type]);
if (rc != 0) {
return rc;
}

if (!d_ip.object().parse(representation)) {
return 1;
}
}
else if (d_type == ntsa::HostType::e_LOCAL_NAME) {
rc = manipulator(&d_localName.object(), SELECTION_INFO_ARRAY[d_type]);
if (rc != 0) {
return rc;
}
}
else {
return -1;
}

return 0;
}

template <typename ACCESSOR>
int Host::accessSelection(ACCESSOR& accessor) const
{
int rc;

const bdlat_SelectionInfo *selectionInfo =
ntsa::Host::lookupSelectionInfo(d_type);
if (selectionInfo == 0) {
return -1;
}

if (d_type == ntsa::HostType::e_UNDEFINED) {
;
}
else if (d_type == ntsa::HostType::e_DOMAIN_NAME) {
bsl::string representation = d_domainName.object().text();
rc = accessor(representation, SELECTION_INFO_ARRAY[d_type]);
if (rc != 0) {
return rc;
}
}
else if (d_type == ntsa::HostType::e_IP) {
bsl::string representation = d_ip.object().text();
rc = accessor(representation, SELECTION_INFO_ARRAY[d_type]);
if (rc != 0) {
return rc;
}
}
else if (d_type == ntsa::HostType::e_LOCAL_NAME) {
rc = accessor(d_localName.object(), SELECTION_INFO_ARRAY[d_type]);
if (rc != 0) {
return rc;
}
}
else {
return -1;
}

return 0;
}

} // close package namespace

BDLAT_DECL_CHOICE_TRAITS(ntsa::Host)

} // close enterprise namespace
#endif
112 changes: 112 additions & 0 deletions groups/nts/ntsa/ntsa_host.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@

#include <ntsa_host.h>
#include <ntscfg_test.h>
#include <balber_berdecoder.h>
#include <balber_berencoder.h>
#include <baljsn_decoder.h>
#include <baljsn_encoder.h>
#include <bdlsb_memoutstreambuf.h>
#include <bdlsb_fixedmeminstreambuf.h>
#include <bslma_testallocator.h>
#include <bsl_sstream.h>
#include <bsl_string.h>
Expand Down Expand Up @@ -125,9 +131,115 @@ NTSCFG_TEST_CASE(2)
NTSCFG_TEST_ASSERT(ta.numBlocksInUse() == 0);
}

NTSCFG_TEST_CASE(3)
{
int rc;

ntscfg::TestAllocator ta;
{
ntsa::Host e1("10.26.55.100");
ntsa::Host e2;

bdlsb::MemOutStreamBuf osb(&ta);

balber::BerEncoder encoder(0, &ta);
rc = encoder.encode(&osb, e1);
if (rc != 0) {
NTSCFG_TEST_LOG_DEBUG << encoder.loggedMessages()
<< NTSCFG_TEST_LOG_END;

NTSCFG_TEST_EQ(rc, 0);
}

rc = osb.pubsync();
NTSCFG_TEST_EQ(rc, 0);

NTSCFG_TEST_GT(osb.length(), 0);
NTSCFG_TEST_NE(osb.data(), 0);

NTSCFG_TEST_LOG_DEBUG << "Encoded:\n"
<< bdlb::PrintStringHexDumper(
osb.data(),
static_cast<bsl::size_t>(osb.length()))
<< NTSCFG_TEST_LOG_END;

bdlsb::FixedMemInStreamBuf isb(osb.data(), osb.length());

balber::BerDecoder decoder(0, &ta);
rc = decoder.decode(&isb, &e2);
if (rc != 0) {
NTSCFG_TEST_LOG_DEBUG << encoder.loggedMessages()
<< NTSCFG_TEST_LOG_END;

NTSCFG_TEST_EQ(rc, 0);
}

NTSCFG_TEST_EQ(e2, e1);
}
NTSCFG_TEST_EQ(ta.numBlocksInUse(), 0);
}

NTSCFG_TEST_CASE(4)
{
int rc;

ntscfg::TestAllocator ta;
{
bsl::vector<ntsa::Host> e1(&ta);
bsl::vector<ntsa::Host> e2(&ta);

e1.push_back(ntsa::Host("ntf.example.com"));
e1.push_back(ntsa::Host("10.26.55.100"));
e1.push_back(ntsa::Host("/tmp/ntf/socket"));

bdlsb::MemOutStreamBuf osb;

baljsn::Encoder encoder(&ta);
rc = encoder.encode(&osb, e1);
if (rc != 0) {
NTSCFG_TEST_LOG_DEBUG << encoder.loggedMessages()
<< NTSCFG_TEST_LOG_END;

NTSCFG_TEST_EQ(rc, 0);
}

rc = osb.pubsync();
NTSCFG_TEST_EQ(rc, 0);

NTSCFG_TEST_GT(osb.length(), 0);
NTSCFG_TEST_NE(osb.data(), 0);

NTSCFG_TEST_LOG_DEBUG << "Encoded: "
<< bsl::string_view(
osb.data(),
static_cast<bsl::size_t>(osb.length()))
<< NTSCFG_TEST_LOG_END;

bdlsb::FixedMemInStreamBuf isb(osb.data(), osb.length());

baljsn::Decoder decoder(&ta);
rc = decoder.decode(&isb, &e2);
if (rc != 0) {
NTSCFG_TEST_LOG_DEBUG << encoder.loggedMessages()
<< NTSCFG_TEST_LOG_END;

NTSCFG_TEST_EQ(rc, 0);
}

NTSCFG_TEST_EQ(e2.size(), e1.size());

for (bsl::size_t i = 0; i < e1.size(); ++i) {
NTSCFG_TEST_EQ(e2[i], e1[i]);
}
}
NTSCFG_TEST_EQ(ta.numBlocksInUse(), 0);
}

NTSCFG_TEST_DRIVER
{
NTSCFG_TEST_REGISTER(1);
NTSCFG_TEST_REGISTER(2);
NTSCFG_TEST_REGISTER(3);
NTSCFG_TEST_REGISTER(4);
}
NTSCFG_TEST_DRIVER_END;
7 changes: 7 additions & 0 deletions groups/nts/ntsa/ntsa_transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,13 @@ int Transport::fromString(Transport::Value* result,
return -1;
}

int Transport::fromString(Value* result, const char* string, int stringLength)
{
return Transport::fromString(
result,
bslstl::StringRef(string, static_cast<bsl::size_t>(stringLength)));
}

const char* Transport::toString(Transport::Value value)
{
switch (value) {
Expand Down
Loading
Loading