Cosmetic improvements for concrete_constraints.hpp and basic_constraint.hpp

This commit is contained in:
Tristan Penman 2020-07-05 22:50:34 +10:00
parent 7917b2f75f
commit e46af24588
2 changed files with 240 additions and 259 deletions

View File

@ -9,56 +9,46 @@ namespace valijson {
namespace constraints {
/**
* @brief Template class that implements the accept() and clone() functions of
* the Constraint interface.
* @brief Template class that implements the accept() and clone() functions of the Constraint interface.
*
* @tparam ConstraintType name of the concrete constraint type, which must
* provide a copy constructor.
* @tparam ConstraintType name of the concrete constraint type, which must provide a copy constructor.
*/
template<typename ConstraintType>
struct BasicConstraint: Constraint
{
typedef internal::CustomAllocator<void *> Allocator;
typedef std::basic_string<char, std::char_traits<char>,
internal::CustomAllocator<char> > String;
typedef std::basic_string<char, std::char_traits<char>, internal::CustomAllocator<char>> String;
BasicConstraint()
: allocator() { }
: m_allocator() { }
BasicConstraint(Allocator::CustomAlloc allocFn, Allocator::CustomFree freeFn)
: allocator(allocFn, freeFn) { }
: m_allocator(allocFn, freeFn) { }
BasicConstraint(const BasicConstraint &other)
: allocator(other.allocator) { }
: m_allocator(other.m_allocator) { }
virtual ~BasicConstraint<ConstraintType>() { }
~BasicConstraint<ConstraintType>() override = default;
virtual bool accept(ConstraintVisitor &visitor) const
bool accept(ConstraintVisitor &visitor) const override
{
return visitor.visit(*static_cast<const ConstraintType*>(this));
}
virtual Constraint * clone(CustomAlloc allocFn, CustomFree freeFn) const
Constraint * clone(CustomAlloc allocFn, CustomFree freeFn) const override
{
void *ptr = allocFn(sizeof(ConstraintType));
if (!ptr) {
throw std::runtime_error(
"Failed to allocate memory for cloned constraint");
throw std::runtime_error("Failed to allocate memory for cloned constraint");
}
try {
return new (ptr) ConstraintType(
*static_cast<const ConstraintType*>(this));
} catch (...) {
freeFn(ptr);
throw;
}
return new (ptr) ConstraintType(*static_cast<const ConstraintType*>(this));
}
protected:
Allocator allocator;
Allocator m_allocator;
};
} // namespace constraints

File diff suppressed because it is too large Load Diff