utility.h File Reference

This file contains various functions and classes to work with layers like getting or resolving using a path. More...

#include "garlic.h"
#include "meta.h"
#include "encoding.h"
Include dependency graph for utility.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

class  garlic::lazy_string_splitter
 Lazy string splitter for getting tokens one by one. More...
 

Functions

template<GARLIC_VIEW L1, GARLIC_VIEW L2>
static std::enable_if_t<!is_comparable< L1, L2 >::value, bool > garlic::cmp_layers (const L1 &layer1, const L2 &layer2)
 Checks the equality of two layers. More...
 
template<GARLIC_VIEW LayerType, typename Callable >
static void garlic::resolve_layer_cb (const LayerType &value, std::string_view path, Callable &&cb)
 
template<typename OutputType , GARLIC_VIEW Layer>
static OutputType garlic::safe_resolve (const Layer &value, std::string_view key, OutputType default_value)
 
template<typename OutputType , GARLIC_VIEW Layer, typename Callable >
static void garlic::safe_resolve_cb (const Layer &value, std::string_view key, Callable &&cb)
 
template<typename OutputType , GARLIC_VIEW Layer>
static OutputType garlic::resolve (const Layer &value, std::string_view key, OutputType default_value)
 
template<typename OutputType , GARLIC_VIEW Layer, typename Callable >
static void garlic::resolve_cb (const Layer &value, std::string_view key, Callable &&cb)
 
template<GARLIC_VIEW Layer, typename Callable >
static void garlic::get_member (const Layer &value, const char *key, Callable &&cb) noexcept
 
template<GARLIC_VIEW Layer, typename Callable >
static void garlic::get_member (const Layer &value, std::string_view key, Callable &&cb) noexcept
 
template<GARLIC_VIEW Layer, std::integral IndexType, typename Callable >
static std::enable_if_t< std::__is_random_access_iter< ConstValueIteratorOf< Layer > >::value > garlic::get_item (Layer &&layer, IndexType index, Callable &&cb) noexcept
 
template<typename OutputType , GARLIC_VIEW Layer>
static OutputType garlic::get (Layer &&layer, const char *key)
 
template<typename OutputType , GARLIC_VIEW Layer, std::integral IndexType>
static std::enable_if_t< std::__is_random_access_iter< ConstValueIteratorOf< Layer > >::value, OutputType > garlic::get (Layer &&layer, IndexType index)
 
template<typename OutputType , GARLIC_VIEW Layer>
static OutputType garlic::get (Layer &&layer, const char *key, OutputType default_value)
 
template<typename OutputType , GARLIC_VIEW Layer, std::integral IndexType>
static OutputType garlic::get (Layer &&layer, IndexType index, OutputType default_value)
 
template<typename OutputType , typename Callable , GARLIC_VIEW Layer>
static void garlic::get_cb (Layer &&layer, const char *key, Callable &&cb)
 
template<typename OutputType , GARLIC_VIEW Layer, std::integral IndexType, typename Callable >
static void garlic::get_cb (Layer &&layer, IndexType index, Callable &&cb)
 
template<typename OutputType , GARLIC_VIEW Layer>
static OutputType garlic::safe_get (Layer &&layer, const char *key, OutputType default_value)
 
template<typename OutputType , GARLIC_VIEW Layer, std::integral IndexType>
static OutputType garlic::safe_get (Layer &&layer, IndexType index, OutputType default_value)
 
template<typename OutputType , GARLIC_VIEW Layer, typename Callable >
static void garlic::safe_get_cb (Layer &&layer, const char *key, Callable &&cb)
 
template<typename OutputType , GARLIC_VIEW Layer, std::integral IndexType, typename Callable >
static void garlic::safe_get_cb (Layer &&layer, IndexType index, Callable &&cb)
 
template<GARLIC_VIEW Layer, GARLIC_REF Output>
static void garlic::copy_layer (Layer &&layer, Output output)
 
template<GARLIC_VIEW Layer>
static size_t garlic::list_size (Layer &&layer)
 Get the size of a list from a layer. More...
 
template<GARLIC_VIEW Layer>
static size_t garlic::string_length (Layer &&layer)
 Get the length of a string from a layer. More...
 

Detailed Description

This file contains various functions and classes to work with layers like getting or resolving using a path.

Function Documentation

◆ cmp_layers()

template<GARLIC_VIEW L1, GARLIC_VIEW L2>
static std::enable_if_t<!is_comparable<L1, L2>::value, bool> garlic::cmp_layers ( const L1 &  layer1,
const L2 &  layer2 
)
inlinestatic

Checks the equality of two layers.

If the equality operator is defined or simply if layer1 == layer2 is valid, this function just returns the result of that comparison. This is done for performance reasons.

Note
Depending on the size of these two types, this method could be quite expensive as it performs a linear scan on both layers for members and lists.
Parameters
layer1The first layer, any type that conforms to the garlic::ViewLayer concept.
layer2The second layer, any type that conforms to the garlic::ViewLayer concept.

◆ copy_layer()

template<GARLIC_VIEW Layer, GARLIC_REF Output>
static void garlic::copy_layer ( Layer &&  layer,
Output  output 
)
inlinestatic

Deep copies the content of one layer to another.

Template Parameters
Layerany type that conforms to garlic::ViewLayer concept.
Outputany type that conforms to garlic::RefLayer concept.
Parameters
layerThe source view layer to copy values from.
outputThe destination ref layer to copy values to.

◆ get() [1/4]

template<typename OutputType , GARLIC_VIEW Layer>
static OutputType garlic::get ( Layer &&  layer,
const char *  key 
)
inlinestatic

Get the decoded value associated with the given key in the layer.

This method DOES NOT check if the found layer is possible to decode to the OutputType.

Attention
This method DOES NOT check if the layer is an object nor that it has the key.

◆ get() [2/4]

template<typename OutputType , GARLIC_VIEW Layer>
static OutputType garlic::get ( Layer &&  layer,
const char *  key,
OutputType  default_value 
)
inlinestatic

Get the decoded value associated with the given key in the layer, if found.

This method DOES NOT check if the found layer is possible to decode to the OutputType.

Attention
This method DOES NOT check if the layer is an object.

◆ get() [3/4]

template<typename OutputType , GARLIC_VIEW Layer, std::integral IndexType>
static std::enable_if_t<std::__is_random_access_iter<ConstValueIteratorOf<Layer> >::value, OutputType> garlic::get ( Layer &&  layer,
IndexType  index 
)
inlinestatic

Get the decoded i th element in the layer.

This method DOES NOT check if the found layer is possible to decode to the OutputType.

Attention
This method DOES NOT check if the layer is a list nor that it has i elements in it.
Note
If the view layer supports random access iterators, this is an O(1) function otherwise it's an O(n) function.

◆ get() [4/4]

template<typename OutputType , GARLIC_VIEW Layer, std::integral IndexType>
static OutputType garlic::get ( Layer &&  layer,
IndexType  index,
OutputType  default_value 
)
inlinestatic

Get the decoded i th element in the layer, if found.

This method DOES NOT check if the found layer is possible to decode to the OutputType.

Attention
This method DOES NOT check if the layer is a list.

◆ get_cb() [1/2]

template<typename OutputType , typename Callable , GARLIC_VIEW Layer>
static void garlic::get_cb ( Layer &&  layer,
const char *  key,
Callable &&  cb 
)
inlinestatic

Calls the callback function with the decoded value associated with the given key in the layer, if found.

This method DOES NOT check if the found layer is possible to decode to the OutputType.

Attention
This method DOES NOT check if the layer is an object.
Parameters
cbAny callable object/lambda with signature void(OutputType)

◆ get_cb() [2/2]

template<typename OutputType , GARLIC_VIEW Layer, std::integral IndexType, typename Callable >
static void garlic::get_cb ( Layer &&  layer,
IndexType  index,
Callable &&  cb 
)
inlinestatic

Calls the callback function with the decoded i th element in the layer, if found.

This method DOES NOT check if the found layer is possible to decode to the OutputType.

Attention
This method DOES NOT check if the layer is a list.
Parameters
cbAny callable object/lambda with signature void(OutputType)

◆ get_item()

template<GARLIC_VIEW Layer, std::integral IndexType, typename Callable >
static std::enable_if_t<std::__is_random_access_iter<ConstValueIteratorOf<Layer> >::value> garlic::get_item ( Layer &&  layer,
IndexType  index,
Callable &&  cb 
)
inlinestaticnoexcept

Calls the callback function with the i th element in the layer.

Note
If the view layer supports random access iterators, this is an O(1) function otherwise it's an O(n) function.
Parameters
cbAny callable object/lambda with signature void(LayerType)

◆ get_member() [1/2]

template<GARLIC_VIEW Layer, typename Callable >
static void garlic::get_member ( const Layer &  value,
const char *  key,
Callable &&  cb 
)
inlinestaticnoexcept

Calls the callback function with the value associated with the given key in the layer, if found.

Attention
This method DOES NOT check if the layer is an object.
Parameters
cbAny callable object/lambda with signature void(LayerType)

◆ get_member() [2/2]

template<GARLIC_VIEW Layer, typename Callable >
static void garlic::get_member ( const Layer &  value,
std::string_view  key,
Callable &&  cb 
)
inlinestaticnoexcept

Calls the callback function with the value associated with the given key in the layer, if found.

Attention
This method DOES NOT check if the layer is an object.
Parameters
cbAny callable object/lambda with signature void(LayerType)

◆ list_size()

template<GARLIC_VIEW Layer>
static size_t garlic::list_size ( Layer &&  layer)
inlinestatic

Get the size of a list from a layer.

Note
This method does NOT check if the layer is a list type.
Depending on the layer's capabilities, this method chooses the best way to get this count. If the layer has a list_size() method or it has random access iterators, this has time complexity of O(1), otherwise it'll be O(n)

◆ resolve()

template<typename OutputType , GARLIC_VIEW Layer>
static OutputType garlic::resolve ( const Layer &  value,
std::string_view  key,
OutputType  default_value 
)
inlinestatic

Navigates the layer using the given path and if found returns the decoded value.

This method DOES NOT check if the found layer is possible to decode to the OutputType.

Parameters
pathAn object path (e.g. users.2.first_name).
default_valueA default value in case the path does not exist.

◆ resolve_cb()

template<typename OutputType , GARLIC_VIEW Layer, typename Callable >
static void garlic::resolve_cb ( const Layer &  value,
std::string_view  key,
Callable &&  cb 
)
inlinestatic

Navigates the layer using the given path and if found, calls the callback function with the decoded value.

This method DOES NOT check if the found layer is possible to decode to the OutputType.

Parameters
pathAn object path (e.g. users.2.first_name).
cbAny callable object/lambda with signature void(OutputType)

◆ resolve_layer_cb()

template<GARLIC_VIEW LayerType, typename Callable >
static void garlic::resolve_layer_cb ( const LayerType &  value,
std::string_view  path,
Callable &&  cb 
)
inlinestatic

Navigates the layer using the given path and if found, calls the callback function with the found layer.

Parameters
pathAn object path (e.g. users.2.first_name).
cbAny callable object/lambda with signature void(LayerType)

◆ safe_get() [1/2]

template<typename OutputType , GARLIC_VIEW Layer>
static OutputType garlic::safe_get ( Layer &&  layer,
const char *  key,
OutputType  default_value 
)
inlinestatic

Get the decoded value associated with the given key in the layer, if found.

This method safely checks if the found layer is possible to decode to the OutputType.

Attention
This method DOES NOT check if the layer is an object.

◆ safe_get() [2/2]

template<typename OutputType , GARLIC_VIEW Layer, std::integral IndexType>
static OutputType garlic::safe_get ( Layer &&  layer,
IndexType  index,
OutputType  default_value 
)
inlinestatic

Get the decoded i th element in the layer, if found.

This method safely checks if the found layer is possible to decode to the OutputType.

Attention
This method DOES NOT check if the layer is a list.

◆ safe_get_cb() [1/2]

template<typename OutputType , GARLIC_VIEW Layer, typename Callable >
static void garlic::safe_get_cb ( Layer &&  layer,
const char *  key,
Callable &&  cb 
)
inlinestatic

Calls the callback function with the decoded value associated with the given key in the layer, if found.

This method safely checks if the found layer is possible to decode to the OutputType.

Attention
This method DOES NOT check if the layer is an object.
Parameters
cbAny callable object/lambda with signature void(OutputType)

◆ safe_get_cb() [2/2]

template<typename OutputType , GARLIC_VIEW Layer, std::integral IndexType, typename Callable >
static void garlic::safe_get_cb ( Layer &&  layer,
IndexType  index,
Callable &&  cb 
)
inlinestatic

Calls the callback function with the decoded i th element in the layer, if found.

This method safely checks if the found layer is possible to decode to the OutputType.

Attention
This method DOES NOT check if the layer is a list.
Parameters
cbAny callable object/lambda with signature void(OutputType)

◆ safe_resolve()

template<typename OutputType , GARLIC_VIEW Layer>
static OutputType garlic::safe_resolve ( const Layer &  value,
std::string_view  key,
OutputType  default_value 
)
inlinestatic

Navigates the layer using the given path and if found returns the decoded value.

This method safely checks if the found layer is possible to decode to the OutputType.

Parameters
pathAn object path (e.g. users.2.first_name).
default_valueA default value in case the path does not exist or it cannot be decoded to the OutputType.

◆ safe_resolve_cb()

template<typename OutputType , GARLIC_VIEW Layer, typename Callable >
static void garlic::safe_resolve_cb ( const Layer &  value,
std::string_view  key,
Callable &&  cb 
)
inlinestatic

Navigates the layer using the given path and if found calls the callback function with the decoded value.

This method safely checks if the found layer is possible to decode to the OutputType.

Parameters
pathAn object path (e.g. users.2.first_name).
cbAny callable object/lambda with signature void(OutputType)

◆ string_length()

template<GARLIC_VIEW Layer>
static size_t garlic::string_length ( Layer &&  layer)
inlinestatic

Get the length of a string from a layer.

Note
This method does NOT check if the layer is a string type.
This method relies on the layer's string_length method if provided. Otherwise, it's a simple strlen(layer.get_cstr()) call.