Skip to content

Commit

Permalink
+ariles2::Any2: replacement for ariles2::Any
Browse files Browse the repository at this point in the history
  • Loading branch information
asherikov committed Jul 25, 2024
1 parent addcde4 commit d1bb12c
Show file tree
Hide file tree
Showing 6 changed files with 339 additions and 46 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
CHANGELOG
=========

UNRELEASED
==========

Added
-----
* `ariles2::Any2`: a replacement for deprecated `ariles2::Any`.


2.3.2
=====

Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,10 @@ Related software

* A library with similar functionality in C++17
<https://github.com/injae/serdepp>.


Deprecated
==========

- `ariles2::namevalue`
- `ariles2::Any`
232 changes: 232 additions & 0 deletions include/ariles2/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,238 @@ namespace ariles2
} // namespace ariles2


namespace ariles2
{
template <template <class> class t_Pointer, class t_Base, class t_Instantiator>
class ARILES2_VISIBILITY_ATTRIBUTE Any2 : public ariles2::DefaultBase
{
#define ARILES2_ENTRIES(v) \
ARILES2_TYPED_ENTRY_(v, id, std::string) \
ARILES2_TYPED_ENTRY_(v, value, t_Pointer<t_Base>)
#include ARILES2_INITIALIZE

protected:
bool isConsistent() const
{
if (("" != id_) && (nullptr != value_.get()))
{
return (true);
}

if (("" == id_) && (nullptr == value_.get()))
{
return (true);
}

return (false);
}


public:
Any2()
{
ariles2::apply<ariles2::Defaults>(*this);
}


explicit Any2(const std::string &id)
{
build(id);
}


void build(const std::string &id)
{
id_ = id;
value_ = t_Instantiator::instantiate(id_);
CPPUT_ASSERT(nullptr != value_.get(), "Could not instantiate class.");
}


bool isInitialized() const
{
return ("" != id_ && nullptr != value_.get());
}


/// @{
/**
* @brief Cast methods are potentially dangerous, no id checks are
* performed. If value is not initialized the returned pointer may
* be nullptr.
*/
template <class t_Derived>
t_Derived *cast()
{
return (dynamic_cast<t_Derived *>(value_.get()));
}


template <class t_Derived>
const t_Derived *cast() const
{
return (dynamic_cast<const t_Derived *>(value_.get()));
}
/// @}


/// @{
/**
* @brief These casts succeed if the Ariles config section id
* matches the given string.
*/
template <class t_Derived>
t_Derived *cast(const std::string &config_section_id)
{
if (isInitialized())
{
if (config_section_id == value_->arilesDefaultID())
{
return (dynamic_cast<t_Derived *>(value_.get()));
}
}
return (nullptr);
}


template <class t_Derived>
const t_Derived *cast(const std::string &config_section_id) const
{
if (isInitialized())
{
if (config_section_id == value_->arilesDefaultID())
{
return (dynamic_cast<t_Derived *>(value_.get()));
}
}
return (nullptr);
}
/// @}


t_Base *operator->()
{
CPPUT_ASSERT(isInitialized(), "Not initialized");
return (value_.get());
}


const t_Base *operator->() const
{
CPPUT_ASSERT(isInitialized(), "Not initialized");
return (value_.get());
}


t_Base &operator*()
{
CPPUT_ASSERT(isInitialized(), "Not initialized");
return (*value_);
}


const t_Base &operator*() const
{
CPPUT_ASSERT(isInitialized(), "Not initialized");
return (*value_);
}


// Ariles methods

void arilesVisit(ariles2::Write &visitor, const ariles2::Write::Parameters &param) const
{
CPPUT_TRACE_FUNCTION;
CPPUT_ASSERT(
isConsistent(),
"Could not write config: entry is in an inconsistent (partially initialized) state.");

visitor.visitMapEntry(id_, "id", param);
if (isInitialized())
{
value_->arilesVirtualVisit(visitor, param);
}
}


void arilesVisit(ariles2::Read &visitor, const ariles2::Read::Parameters &param)
{
CPPUT_TRACE_FUNCTION;

if (visitor.visitMapEntry(id_, "id", param))
{
if ("" == id_)
{
CPPUT_ASSERT(param.allow_missing_entries_, "Id is empty, value cannot be read.");
}
else
{
build(id_);

try
{
value_->arilesVirtualVisit(visitor, param);
}
catch (const std::exception &e)
{
CPPUT_THROW("Failed to parse entry <", id_, "> || ", e.what());
}
}
}
}


void arilesVisit(const ariles2::Finalize &visitor, const ariles2::Finalize::Parameters &param)
{
CPPUT_TRACE_FUNCTION;
if (isInitialized())
{
value_->arilesVirtualVisit(visitor, param);
}
}


void arilesVisit(const ariles2::PreWrite &visitor, const ariles2::PreWrite::Parameters &param)
{
CPPUT_TRACE_FUNCTION;
if (isInitialized())
{
value_->arilesVirtualVisit(visitor, param);
}
}


void arilesVisit(const ariles2::Defaults &visitor, const ariles2::Defaults::Parameters &param)
{
CPPUT_TRACE_FUNCTION;
if (isInitialized())
{
value_->arilesVirtualVisit(visitor, param);
}
}


std::size_t arilesVisit(const ariles2::Count &visitor, const ariles2::Count::Parameters &param) const
{
if (isInitialized())
{
return (this->value_->arilesVirtualVisit(visitor, param) + 1);
}

return (1);
}


std::size_t arilesVisit(const ariles2::CountMissing &visitor, const ariles2::CountMissing::Parameters &param)
const
{
CPPUT_ASSERT(isInitialized(), "Not initialized");
return (this->value_->arilesVirtualVisit(visitor, param));
}
};
} // namespace ariles2


namespace ariles2
{
template <class t_Pointer>
Expand Down
5 changes: 4 additions & 1 deletion tests/api_v2/fixtures/002_comparison.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ namespace ariles_tests
// -------

t_Configurable configurable_in;
/*
BOOST_CHECK_NO_THROW(ariles2::apply<typename t_Visitor::Reader>(
getReaderInitializer("configurable_match_simple.cfg"), configurable_in););
getReaderInitializer("configurable_match_simple.cfg"), configurable_in););*/
ariles2::apply<typename t_Visitor::Reader>(
getReaderInitializer("configurable_match_simple.cfg"), configurable_in);

// -------

Expand Down
Loading

0 comments on commit d1bb12c

Please sign in to comment.