Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions src/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ target_sources(
TimeUtils.cpp
TxUtils.cpp
LedgerUtils.cpp
newconfig/ConfigDefinition.cpp
newconfig/ObjectView.cpp
newconfig/ArrayView.cpp
newconfig/ValueView.cpp
)

target_link_libraries(
Expand Down
191 changes: 191 additions & 0 deletions src/util/newconfig/Array.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2024, the clio developers.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

#pragma once

#include "util/Assert.hpp"
#include "util/newconfig/ConfigValue.hpp"
#include "util/newconfig/ValueView.hpp"

#include <cstddef>
#include <iterator>
#include <vector>

namespace util::config {

/**
* @brief Array definition for Json/Yaml config
*
* Used in ClioConfigDefinition to represent multiple potential values (like whitelist)
*/
class Array {
public:
/** @brief Custom iterator class which returns ValueView of what's underneath the Array
*/
struct ArrayIterator {
using iterator_category = std::forward_iterator_tag;
using pointer = ConfigValue const*;
using reference = ConfigValue const&;
using valueType = ConfigValue;

/**
* @brief Constructs an ArrayIterator with a pointer to the ConfigValue
*
* @param ptr Pointer to the ConfigValue
*/
ArrayIterator(pointer ptr) : m_ptr(ptr)
{
}

/**
* @brief Prefix increment operator
*
* @return Reference to the incremented ArrayIterator
*/
ArrayIterator&
operator++()
{
m_ptr++;
return *this;
}

/**
* @brief Postfix increment operator
*
* @return Copy of the ArrayIterator before increment
*/
ArrayIterator
operator++(int)
{
ArrayIterator temp = *this;
m_ptr++;
return temp;
}

/**
* @brief Dereference operator to get a ValueView of the ConfigValue
*
* @return ValueView of the ConfigValue
*/
ValueView
operator*()
{
return ValueView(*m_ptr);
}

/**
* @brief Equality operator
*
* @param other Another ArrayIterator to compare
* @return true if iterators are equal, otherwise false
*/
bool
operator==(ArrayIterator const& other) const
{
return m_ptr == other.m_ptr;
}

/**
* @brief Inequality operator
*
* @param other Another ArrayIterator to compare
* @return true if iterators are not equal, otherwise false
*/
bool
operator!=(ArrayIterator const& other) const
{
return m_ptr != other.m_ptr;
}

private:
pointer m_ptr;
};

/**
* @brief Constructs an Array with the provided arguments
*
* @tparam Args Types of the arguments
* @param args Arguments to initialize the elements of the Array
*/
template <typename... Args>
constexpr Array(Args... args) : elements_{args...}
{
}

/**
* @brief Returns an iterator to the beginning of the Array
*
* @return Iterator to the beginning of the Array
*/
auto
begin() const
{
return ArrayIterator{elements_.data()};
}

/**
* @brief Returns an iterator to the end of the Array
*
* @return Iterator to the end of the Array
*/
auto
end() const
{
return ArrayIterator{elements_.data() + elements_.size()};
}

/** @brief Add ConfigValues to Array class
*
* @param value The ConfigValue to add
*/
void
emplace_back(ConfigValue value)
{
elements_.emplace_back(value);
}

/**
* @brief Returns the number of values stored in the Array
*
* @return Number of values stored in the Array
*/
size_t
size() const
{
return elements_.size();
}

/**
* @brief Returns the ConfigValue at the specified index
*
* @param idx Index of the ConfigValue to retrieve
* @return ConfigValue at the specified index
*/
ConfigValue const&
at(std::size_t idx) const
{
ASSERT(idx < elements_.size(), "index is out of scope");
return elements_[idx];
}

private:
std::vector<ConfigValue> elements_;
};

} // namespace util::config
72 changes: 72 additions & 0 deletions src/util/newconfig/ArrayView.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2024, the clio developers.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

#include "util/newconfig/ArrayView.hpp"

#include "util/Assert.hpp"
#include "util/newconfig/Array.hpp"
#include "util/newconfig/ConfigDefinition.hpp"
#include "util/newconfig/ConfigValue.hpp"
#include "util/newconfig/ObjectView.hpp"
#include "util/newconfig/ValueView.hpp"

#include <algorithm>
#include <cstddef>
#include <stdexcept>
#include <string_view>

namespace util::config {

ArrayView::ArrayView(std::string_view prefix, ClioConfigDefinition const& configDef)
: prefix_{prefix}, clioConfig_{configDef}
{
auto it = std::find_if(configDef.map_.begin(), configDef.map_.end(), [this](auto const& pair) {
return pair.first.starts_with(prefix_);
});
ASSERT(it != clioConfig_.map_.end(), "prefix does not exist in config definition.");
ASSERT(prefix_.contains(".[]"), "Not an array");
}

ValueView
ArrayView::valueAt(std::size_t idx) const
{
ASSERT(clioConfig_.map_.contains(prefix_), "Current string is prefix, not full key");
ConfigValue const& val = std::get<Array>(clioConfig_.map_.at(prefix_)).at(idx);
return ValueView{val};
}

size_t
ArrayView::size() const
{
for (auto const& pair : clioConfig_.map_) {
if (pair.first.starts_with(prefix_)) {
return std::get<Array>(pair.second).size();
}
}
throw std::logic_error("Arrayview is initialized with incorrect prefix.");
}

ObjectView
ArrayView::objectAt(std::size_t idx) const
{
ASSERT(idx < this->size(), "Object index is out of scope");
return ObjectView{prefix_, idx, clioConfig_};
}

} // namespace util::config
110 changes: 110 additions & 0 deletions src/util/newconfig/ArrayView.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2024, the clio developers.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

#pragma once

#include "util/Assert.hpp"
#include "util/newconfig/Array.hpp"
#include "util/newconfig/ConfigDefinition.hpp"
#include "util/newconfig/ConfigValue.hpp"
#include "util/newconfig/ObjectView.hpp"
#include "util/newconfig/ValueView.hpp"

#include <cstddef>
#include <string>
#include <string_view>

namespace util::config {

/**
* @brief View for array structure for config.
*
* This class provides a view into an array structure within ClioConfigDefinition.
* It allows accessing individual elements of the array as either values or objects, and
* is used within the ClioConfigDefinition to represent multiple potential values.
*/
class ArrayView {
public:
ArrayView() = delete;

/**
* @brief Constructs an ArrayView with the given prefix and config definition.
*
* @param prefix The prefix for the array view.
* @param configDef The ClioConfigDefinition instance.
*/
ArrayView(std::string_view prefix, ClioConfigDefinition const& configDef);

/**
* @brief Returns an ObjectView at the specified index.
*
* @param idx Index of the object to retrieve.
* @return ObjectView at the specified index.
*/
ObjectView
objectAt(std::size_t idx) const;

/**
* @brief Returns a ValueView at the specified index.
*
* @param idx Index of the value to retrieve.
* @return ValueView at the specified index.
*/
ValueView
valueAt(std::size_t idx) const;

/**
* @brief Returns the number of elements in the array.
*
* @return Number of elements in the array.
* @throw std::logic_error if somehow ArrayView doesn't exist
*/
size_t
size() const;

/**
* @brief Returns an iterator to the beginning of the values.
*
* @return Iterator to the beginning of the values.
*/
auto
beginValues() const
{
ASSERT(clioConfig_.map_.contains(prefix_), "Current string is prefix, not full key");
return std::get<Array>(clioConfig_.map_.at(prefix_)).begin();
}

/**
* @brief Returns an iterator to the end of the values.
*
* @return Iterator to the end of the values.
*/
auto
endValues() const
{
ASSERT(clioConfig_.map_.contains(prefix_), "Current string is prefix, not full key");
return std::get<Array>(clioConfig_.map_.at(prefix_)).end();
}

private:
std::string const prefix_;
ClioConfigDefinition const& clioConfig_;
};

} // namespace util::config
Loading