84 lines
2.0 KiB
C++
84 lines
2.0 KiB
C++
|
#include <optional>
|
||
|
#include <unordered_map>
|
||
|
#include <span>
|
||
|
#include <string>
|
||
|
#include <string_view>
|
||
|
#include <tuple>
|
||
|
#include <variant>
|
||
|
#include <vector>
|
||
|
|
||
|
namespace Lml
|
||
|
{
|
||
|
///
|
||
|
/// Tree leaf composed of zero or more values.
|
||
|
///
|
||
|
using Scalar = std::vector<std::string>;
|
||
|
|
||
|
///
|
||
|
/// Content body capable of holding Scalars as properties and other child Documents as elements.
|
||
|
///
|
||
|
struct Document final
|
||
|
{
|
||
|
///
|
||
|
/// Custom hashing mechanism used by properties to support lookups with string types other than std::string.
|
||
|
///
|
||
|
struct PropertyHash
|
||
|
{
|
||
|
using HashType = std::hash<std::string_view>;
|
||
|
|
||
|
using is_transparent = void;
|
||
|
|
||
|
size_t operator()(const char* str) const
|
||
|
{
|
||
|
return HashType{}(str);
|
||
|
}
|
||
|
|
||
|
size_t operator()(std::string_view str) const
|
||
|
{
|
||
|
return HashType{}(str);
|
||
|
}
|
||
|
|
||
|
size_t operator()(std::string const& str) const
|
||
|
{
|
||
|
return HashType{}(str);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
///
|
||
|
/// Map of property names and their associated Scalar.
|
||
|
///
|
||
|
using PropertyMap = std::unordered_map<std::string, Scalar, PropertyHash, std::equal_to<>>;
|
||
|
|
||
|
///
|
||
|
/// List of associated element names and Documents.
|
||
|
///
|
||
|
using ElementList = std::vector<std::tuple<std::string, Document>>;
|
||
|
|
||
|
///
|
||
|
/// All live property Scalars belonging to the Document.
|
||
|
///
|
||
|
PropertyMap properties {};
|
||
|
|
||
|
///
|
||
|
/// All live element Documents belonging to the Document.
|
||
|
///
|
||
|
ElementList elements {};
|
||
|
|
||
|
///
|
||
|
/// Attempts to parse the provided data into the live Document object model, returning true if it was
|
||
|
/// successful, otherwise false.
|
||
|
///
|
||
|
bool Parse(std::string_view const & data);
|
||
|
|
||
|
///
|
||
|
/// Attempts to retrieve a single string value from a Scalar defined under the provided property name,
|
||
|
/// returning it or null if no such property exists.
|
||
|
///
|
||
|
std::optional<std::string> Property(std::string_view const & property);
|
||
|
|
||
|
///
|
||
|
/// Serializes the live Document object model to the provided string with the specified indentation level.
|
||
|
///
|
||
|
void SerializeTo(std::string & string, size_t indentation) const;
|
||
|
};
|
||
|
}
|