Loading...
Searching...
No Matches
yaml_parser.h
Go to the documentation of this file.
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2022, Google Inc.
*
* libcamera YAML parsing helper
*/
#pragma once
#include <iterator>
#include <map>
#include <optional>
#include <stdint.h>
#include <string>
#include <string_view>
#include <vector>
#include <libcamera/base/class.h>
#include <libcamera/geometry.h>
namespace libcamera {
class File;
class YamlParserContext;
class YamlObject
{
private:
struct Value {
Value(std::string &&k, std::unique_ptr<YamlObject> &&v)
: key(std::move(k)), value(std::move(v))
{
}
std::string key;
std::unique_ptr<YamlObject> value;
};
using Container = std::vector<Value>;
using ListContainer = std::vector<std::unique_ptr<YamlObject>>;
public:
#ifndef __DOXYGEN__
template<typename Derived>
class Iterator
{
public:
using difference_type = std::ptrdiff_t;
using iterator_category = std::forward_iterator_tag;
Iterator(typename Container::const_iterator it)
: it_(it)
{
}
Derived &operator++()
{
++it_;
return *static_cast<Derived *>(this);
}
Derived operator++(int)
{
Derived it = *static_cast<Derived *>(this);
it_++;
return it;
}
friend bool operator==(const Iterator &a, const Iterator &b)
{
return a.it_ == b.it_;
}
friend bool operator!=(const Iterator &a, const Iterator &b)
{
return a.it_ != b.it_;
}
protected:
Container::const_iterator it_;
};
template<typename Iterator>
class Adapter
{
public:
Adapter(const Container &container)
: container_(container)
{
}
Iterator begin() const
{
return Iterator{ container_.begin() };
}
Iterator end() const
{
return Iterator{ container_.end() };
}
protected:
const Container &container_;
};
class ListIterator : public Iterator<ListIterator>
{
public:
using value_type = const YamlObject &;
using pointer = const YamlObject *;
using reference = value_type;
value_type operator*() const
{
return *it_->value.get();
}
pointer operator->() const
{
return it_->value.get();
}
};
class DictIterator : public Iterator<DictIterator>
{
public:
using value_type = std::pair<const std::string &, const YamlObject &>;
using pointer = value_type *;
using reference = value_type &;
value_type operator*() const
{
return { it_->key, *it_->value.get() };
}
};
class DictAdapter : public Adapter<DictIterator>
{
public:
using key_type = std::string;
};
class ListAdapter : public Adapter<ListIterator>
{
};
#endif /* __DOXYGEN__ */
YamlObject();
~YamlObject();
bool isValue() const
{
return type_ == Type::Value;
}
bool isList() const
{
return type_ == Type::List;
}
bool isDictionary() const
{
return type_ == Type::Dictionary;
}
bool isEmpty() const
{
return type_ == Type::Empty;
}
explicit operator bool() const
{
return type_ != Type::Empty;
}
std::size_t size() const;
template<typename T>
std::optional<T> get() const
{
return Getter<T>{}.get(*this);
}
template<typename T, typename U>
T get(U &&defaultValue) const
{
return get<T>().value_or(std::forward<U>(defaultValue));
}
#ifndef __DOXYGEN__
template<typename T,
std::enable_if_t<
std::is_same_v<bool, T> ||
std::is_same_v<float, T> ||
std::is_same_v<double, T> ||
std::is_same_v<int8_t, T> ||
std::is_same_v<uint8_t, T> ||
std::is_same_v<int16_t, T> ||
std::is_same_v<uint16_t, T> ||
std::is_same_v<int32_t, T> ||
std::is_same_v<uint32_t, T> ||
std::is_same_v<std::string, T> ||
std::is_same_v<Size, T>> * = nullptr>
#else
template<typename T>
#endif
std::optional<std::vector<T>> getList() const;
DictAdapter asDict() const { return DictAdapter{ list_ }; }
ListAdapter asList() const { return ListAdapter{ list_ }; }
const YamlObject &operator[](std::size_t index) const;
bool contains(std::string_view key) const;
const YamlObject &operator[](std::string_view key) const;
private:
LIBCAMERA_DISABLE_COPY_AND_MOVE(YamlObject)
template<typename T>
friend struct Getter;
friend class YamlParserContext;
enum class Type {
Dictionary,
List,
Value,
Empty,
};
template<typename T, typename Enable = void>
struct Getter {
std::optional<T> get(const YamlObject &obj) const;
};
Type type_;
std::string value_;
Container list_;
std::map<std::string, YamlObject *, std::less<>> dictionary_;
};
class YamlParser final
{
public:
static std::unique_ptr<YamlObject> parse(File &file);
};
} /* namespace libcamera */
class.h
Utilities to help constructing class interfaces.
LIBCAMERA_DISABLE_COPY_AND_MOVE
#define LIBCAMERA_DISABLE_COPY_AND_MOVE(klass)
Disable copy and move construction and assignment of the klass.
Definition class.h:29
libcamera::File
Interface for I/O operations on files.
Definition file.h:25
libcamera::YamlObject
A class representing the tree structure of the YAML content.
Definition yaml_parser.h:28
libcamera::YamlObject::size
std::size_t size() const
Retrieve the number of elements in a dictionary or list YamlObject.
Definition yaml_parser.cpp:99
libcamera::YamlObject::get
std::optional< T > get() const
Parse the YamlObject as a T value.
Definition yaml_parser.h:175
libcamera::YamlObject::get
T get(U &&defaultValue) const
Parse the YamlObject as a T value.
Definition yaml_parser.h:181
libcamera::YamlObject::isValue
bool isValue() const
Return whether the YamlObject is a value.
Definition yaml_parser.h:151
libcamera::YamlObject::contains
bool contains(std::string_view key) const
Check if an element of a dictionary exists.
Definition yaml_parser.cpp:372
libcamera::YamlObject::isDictionary
bool isDictionary() const
Return whether the YamlObject is a dictionary.
Definition yaml_parser.h:159
libcamera::YamlObject::operator[]
const YamlObject & operator[](std::size_t index) const
Retrieve the element from list YamlObject by index.
Definition yaml_parser.cpp:353
libcamera::YamlObject::asDict
DictAdapter asDict() const
Wrap a dictionary YamlObject in an adapter that exposes iterators.
Definition yaml_parser.h:205
libcamera::YamlObject::isEmpty
bool isEmpty() const
Return whether the YamlObject is an empty.
Definition yaml_parser.h:163
libcamera::YamlObject::getList
std::optional< std::vector< T > > getList() const
Parse the YamlObject as a list of T.
libcamera::YamlObject::asList
ListAdapter asList() const
Wrap a list YamlObject in an adapter that exposes iterators.
Definition yaml_parser.h:206
libcamera::YamlObject::isList
bool isList() const
Return whether the YamlObject is a list.
Definition yaml_parser.h:155
libcamera::YamlParser
A helper class for parsing a YAML file.
Definition yaml_parser.h:240
libcamera::YamlParser::parse
static std::unique_ptr< YamlObject > parse(File &file)
Parse a YAML file as a YamlObject.
Definition yaml_parser.cpp:765
geometry.h
Data structures related to geometric objects.
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17
libcamera::operator*
Matrix< U, Rows, Cols > operator*(T d, const Matrix< U, Rows, Cols > &m)
Multiply the matrix by a scalar.
Definition matrix.h:133