2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
2019-02-24 20:50:49 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/util/meta/traits.hpp>
|
2019-02-24 20:50:49 +00:00
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <iostream>
|
2019-06-13 21:58:17 +00:00
|
|
|
#include <cassert>
|
2020-02-23 02:23:19 +00:00
|
|
|
#include <algorithm>
|
2020-05-01 19:51:15 +00:00
|
|
|
#include <string_view>
|
2019-02-24 20:50:49 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
/// simple guard class to restore stream flags.
|
|
|
|
struct FormatFlagsGuard
|
|
|
|
{
|
|
|
|
std::ios_base& m_base;
|
|
|
|
std::ios_base::fmtflags m_flags;
|
|
|
|
|
|
|
|
FormatFlagsGuard(std::ios_base& base) : m_base(base), m_flags(base.flags())
|
Config file improvements (#1397)
* Config file API/comment improvements
API improvements:
=================
Make the config API use position-independent tag parameters (Required,
Default{123}, MultiValue) rather than a sequence of bools with
overloads. For example, instead of:
conf.defineOption<int>("a", "b", false, true, 123, [] { ... });
you now write:
conf.defineOption<int>("a", "b", MultiValue, Default{123}, [] { ... });
The tags are:
- Required
- MultiValue
- Default{value}
plus new abilities (see below):
- Hidden
- RelayOnly
- ClientOnly
- Comment{"line1", "line2", "line3"}
Made option definition more powerful:
=====================================
- `Hidden` allows you to define an option that won't show up in the
generated config file if it isn't set.
- `RelayOnly`/`ClientOnly` sets up an option that is only accepted and
only shows up for relay or client configs. (If neither is specified
the option shows up in both modes).
- `Comment{...}` lets the option comments be specified as part of the
defineOption.
Comment improvements
====================
- Rewrote comments for various options to expand on details.
- Inlined all the comments with the option definitions.
- Several options that were missing comments got comments added.
- Made various options for deprecated and or internal options hidden by
default so that they don't show up in a default config file.
- show the section comment (but not option comments) *after* the
[section] tag instead of before it as it makes more sense that way
(particularly for the [bind] section which has a new long comment to
describe how it works).
Disable profiling by default
============================
We had this weird state where we use and store profiling by default but
never *load* it when starting up. This commit makes us just not use
profiling at all unless explicitly enabled.
Other misc changes:
===================
- change default worker threads to 0 (= num cpus) instead of 1, and fix
it to allow 0.
- Actually apply worker-threads option
- fixed default data-dir value erroneously having quotes around it
- reordered ifname/ifaddr/mapaddr (was previously mapaddr/ifaddr/ifname)
as mapaddr is a sort of specialization of ifaddr and so makes more
sense to come after it (particularly because it now references ifaddr
in its help message).
- removed peer-stats option (since we always require it for relays and
never use it for clients)
- removed router profiles filename option (this doesn't need to be
configurable)
- removed defunct `service-node-seed` option
- Change default logging output file to "" (which means stdout), and
also made "-" work for stdout.
* Router hive compilation fixes
* Comments for SNApp SRV settings in ini file
* Add extra blank line after section comments
* Better deprecated option handling
Allow {client,relay}-only options in {relay,client} configs to be
specified as implicitly deprecated options: they warn, and don't set
anything.
Add an explicit `Deprecated` tag and move deprecated option handling
into definition.cpp.
* Move backwards compat options into section definitions
Keep the "addBackwardsCompatibleConfigOptions" only for options in
sections that no longer exist.
* Fix INI parsing issues & C++17-ify
- don't allow inline comments because it seems they aren't allowed in
ini formats in general, and is going to cause problems if there is a
comment character in a value (e.g. an exit auth string). Additionally
it was breaking on a line such as:
# some comment; see?
because it was treating only `; see?` as the comment and then producing
an error message about the rest of the line being invalid.
- make section parsing stricter: the `[` and `]` have to be at the
beginning at end of the line now (after stripping whitespace).
- Move whitespace stripping to the top since everything in here does it.
- chop off string_view suffix/prefix rather than maintaining position
values
- fix potential infinite loop/segfault when given a line such as `]foo[`
* Make config parsing failure fatal
Load() LogError's and returns false on failure, so we weren't aborting
on config file errors.
* Formatting: allow `{}` for empty functions/structs
Instead of using two lines when empty:
{
}
* Make default dns bind 127.0.0.1 on non-Linux
* Don't show empty section; fix tests
We can conceivably have sections that only make sense for clients or
relays, and so want to completely omit that section if we have no
options for the type of config being generated.
Also fixes missing empty lines between tests.
Co-authored-by: Thomas Winget <tewinget@gmail.com>
2020-10-07 22:22:58 +00:00
|
|
|
{}
|
2019-02-24 20:50:49 +00:00
|
|
|
|
|
|
|
~FormatFlagsGuard()
|
|
|
|
{
|
|
|
|
m_base.flags(m_flags);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// A general-purpose, stateful printer class.
|
|
|
|
class Printer
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::ostream& m_stream;
|
|
|
|
const int m_level;
|
|
|
|
const int m_levelPlusOne;
|
|
|
|
const bool m_suppressIndent;
|
|
|
|
const int m_spaces;
|
|
|
|
|
|
|
|
public:
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
|
|
|
using PrintFunction = std::function<std::ostream&(std::ostream&, const Type&, int, int)>;
|
2019-02-24 20:50:49 +00:00
|
|
|
|
|
|
|
/// Create a printer.
|
|
|
|
/// - level: the indentation level to use. If negative, suppress indentation
|
|
|
|
/// on the first line.
|
|
|
|
/// - spaces: the number of spaces to indent. If negative, put all output on
|
|
|
|
/// a single line
|
|
|
|
Printer(std::ostream& stream, int level, int spaces);
|
|
|
|
|
|
|
|
~Printer();
|
|
|
|
|
|
|
|
/// Print the given `data` to the stream, using the following strategies:
|
|
|
|
/// - If `Type` is fundamental, print to stream
|
|
|
|
/// - If `Type` is a C-style array (and not a char array), print each
|
|
|
|
/// element to the stream
|
|
|
|
/// - If `Type` is a `void *`, `const void *` or function pointer, and not
|
|
|
|
/// null, print in hex format or print "null".
|
|
|
|
/// - If `Type` is a `char *`, a `const char *`, a C-style char array, a
|
2020-05-01 19:51:15 +00:00
|
|
|
/// `std::string` or `std::string_view` print the string wrapped in `"`.
|
2019-02-24 20:50:49 +00:00
|
|
|
/// - If `Type` is a pointer type, print the pointer, followed by the value
|
|
|
|
/// if not-null.
|
|
|
|
/// - If `Type` is a pair/tuple type, print the elements of the tuple.
|
|
|
|
/// - If `Type` has STL-style iterators, print all elements in the
|
|
|
|
/// container.
|
|
|
|
/// - If `Type` is any other type, call the `print` method on that type.
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
void
|
2020-05-01 19:51:15 +00:00
|
|
|
printAttribute(std::string_view name, const Type& value) const;
|
2019-02-24 20:50:49 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
void
|
2020-05-01 19:51:15 +00:00
|
|
|
printAttributeAsHex(std::string_view name, const Type& value) const;
|
2019-02-24 20:50:49 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename InputIt>
|
2019-02-24 20:50:49 +00:00
|
|
|
void
|
2020-05-01 19:51:15 +00:00
|
|
|
printAttribute(std::string_view name, const InputIt& begin, const InputIt& end) const;
|
2019-02-24 20:50:49 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
void
|
|
|
|
printValue(const Type& value) const;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename InputIt>
|
2019-02-24 20:50:49 +00:00
|
|
|
void
|
|
|
|
printValue(const InputIt& begin, const InputIt& end) const;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
printForeignAttribute(
|
2020-05-01 19:51:15 +00:00
|
|
|
std::string_view name, const Type& value, const PrintFunction<Type>& printFunction) const;
|
2019-02-24 20:50:49 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
printForeignValue(const Type& value, const PrintFunction<Type>& printFunction) const;
|
2019-02-24 20:50:49 +00:00
|
|
|
|
|
|
|
void
|
2020-05-01 19:51:15 +00:00
|
|
|
printHexAddr(std::string_view name, const void* address) const;
|
2019-02-24 20:50:49 +00:00
|
|
|
void
|
|
|
|
printHexAddr(const void* address) const;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <class Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
void
|
2020-05-01 19:51:15 +00:00
|
|
|
printOrNull(std::string_view name, const Type& address) const;
|
2020-04-07 18:38:56 +00:00
|
|
|
template <class Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
void
|
|
|
|
printOrNull(const Type& address) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void
|
|
|
|
printIndent() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// helper struct
|
|
|
|
struct PrintHelper
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
static void
|
|
|
|
print(std::ostream& stream, const Type& value, int level, int spaces);
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename InputIt>
|
2019-02-24 20:50:49 +00:00
|
|
|
static void
|
2020-04-07 18:38:56 +00:00
|
|
|
print(std::ostream& stream, const InputIt& begin, const InputIt& end, int level, int spaces);
|
2019-02-24 20:50:49 +00:00
|
|
|
|
|
|
|
// Specialisations
|
|
|
|
|
|
|
|
// Fundamental types
|
|
|
|
static void
|
2020-04-07 18:38:56 +00:00
|
|
|
printType(
|
|
|
|
std::ostream& stream,
|
|
|
|
char value,
|
|
|
|
int level,
|
|
|
|
int spaces,
|
|
|
|
traits::select::Case<std::is_fundamental>);
|
2019-02-24 20:50:49 +00:00
|
|
|
|
|
|
|
static void
|
2020-04-07 18:38:56 +00:00
|
|
|
printType(
|
|
|
|
std::ostream& stream,
|
|
|
|
bool value,
|
|
|
|
int level,
|
|
|
|
int spaces,
|
|
|
|
traits::select::Case<std::is_fundamental>);
|
|
|
|
|
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
static void
|
2020-04-07 18:38:56 +00:00
|
|
|
printType(
|
|
|
|
std::ostream& stream,
|
|
|
|
Type value,
|
|
|
|
int level,
|
|
|
|
int spaces,
|
|
|
|
traits::select::Case<std::is_fundamental>);
|
|
|
|
|
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
static void
|
2020-04-07 18:38:56 +00:00
|
|
|
printType(
|
|
|
|
std::ostream& stream,
|
|
|
|
Type value,
|
|
|
|
int level,
|
|
|
|
int spaces,
|
|
|
|
traits::select::Case<std::is_enum>);
|
2019-02-24 20:50:49 +00:00
|
|
|
|
|
|
|
// Function types
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
static void
|
2020-04-07 18:38:56 +00:00
|
|
|
printType(
|
|
|
|
std::ostream& stream,
|
|
|
|
Type value,
|
|
|
|
int level,
|
|
|
|
int spaces,
|
|
|
|
traits::select::Case<std::is_function>);
|
2019-02-24 20:50:49 +00:00
|
|
|
|
|
|
|
// Pointer types
|
|
|
|
static void
|
2020-04-07 18:38:56 +00:00
|
|
|
printType(
|
|
|
|
std::ostream& stream,
|
|
|
|
const char* value,
|
|
|
|
int level,
|
|
|
|
int spaces,
|
|
|
|
traits::select::Case<std::is_pointer>);
|
2019-02-24 20:50:49 +00:00
|
|
|
|
|
|
|
static void
|
2020-04-07 18:38:56 +00:00
|
|
|
printType(
|
|
|
|
std::ostream& stream,
|
|
|
|
const void* value,
|
|
|
|
int level,
|
|
|
|
int spaces,
|
|
|
|
traits::select::Case<std::is_pointer>);
|
|
|
|
|
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
static void
|
2020-04-07 18:38:56 +00:00
|
|
|
printType(
|
|
|
|
std::ostream& stream,
|
|
|
|
const Type* value,
|
|
|
|
int level,
|
|
|
|
int spaces,
|
|
|
|
traits::select::Case<std::is_pointer>);
|
|
|
|
|
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
static void
|
2020-04-07 18:38:56 +00:00
|
|
|
printType(
|
|
|
|
std::ostream& stream,
|
|
|
|
const Type* value,
|
|
|
|
int level,
|
|
|
|
int spaces,
|
|
|
|
traits::select::Case<std::is_array>);
|
2019-02-24 20:50:49 +00:00
|
|
|
|
|
|
|
// Container types
|
|
|
|
static void
|
2020-04-07 18:38:56 +00:00
|
|
|
printType(
|
|
|
|
std::ostream& stream,
|
|
|
|
const std::string& value,
|
|
|
|
int level,
|
|
|
|
int spaces,
|
|
|
|
traits::select::Case<traits::is_container>);
|
2019-02-24 20:50:49 +00:00
|
|
|
|
|
|
|
static void
|
2020-04-07 18:38:56 +00:00
|
|
|
printType(
|
|
|
|
std::ostream& stream,
|
2020-05-01 19:51:15 +00:00
|
|
|
const std::string_view& value,
|
2020-04-07 18:38:56 +00:00
|
|
|
int level,
|
|
|
|
int spaces,
|
|
|
|
traits::select::Case<traits::is_container>);
|
|
|
|
|
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
static void
|
2020-04-07 18:38:56 +00:00
|
|
|
printType(
|
|
|
|
std::ostream& stream,
|
|
|
|
const Type& value,
|
|
|
|
int level,
|
|
|
|
int spaces,
|
|
|
|
traits::select::Case<traits::is_container>);
|
2019-02-24 20:50:49 +00:00
|
|
|
|
|
|
|
// Utility types
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type1, typename Type2>
|
2019-02-24 20:50:49 +00:00
|
|
|
static void
|
2020-04-07 18:38:56 +00:00
|
|
|
printType(
|
|
|
|
std::ostream& stream,
|
|
|
|
const std::pair<Type1, Type2>& value,
|
|
|
|
int level,
|
|
|
|
int spaces,
|
|
|
|
traits::select::Case<>);
|
|
|
|
|
|
|
|
template <typename... Types>
|
2019-02-24 20:50:49 +00:00
|
|
|
static void
|
2020-04-07 18:38:56 +00:00
|
|
|
printType(
|
|
|
|
std::ostream& stream,
|
|
|
|
const std::tuple<Types...>& value,
|
|
|
|
int level,
|
|
|
|
int spaces,
|
|
|
|
traits::select::Case<>);
|
2019-02-24 20:50:49 +00:00
|
|
|
|
|
|
|
// Default type
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
static void
|
2020-04-07 18:38:56 +00:00
|
|
|
printType(
|
|
|
|
std::ostream& stream, const Type& value, int level, int spaces, traits::select::Case<>);
|
2019-02-24 20:50:49 +00:00
|
|
|
};
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
inline void
|
2020-05-01 19:51:15 +00:00
|
|
|
Printer::printAttribute(std::string_view name, const Type& value) const
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
|
|
|
assert(!name.empty());
|
|
|
|
printIndent();
|
|
|
|
|
|
|
|
m_stream << name << " = ";
|
|
|
|
|
|
|
|
PrintHelper::print(m_stream, value, -m_levelPlusOne, m_spaces);
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
inline void
|
2020-05-01 19:51:15 +00:00
|
|
|
Printer::printAttributeAsHex(std::string_view name, const Type& value) const
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
static_assert(std::is_integral<Type>::value, "type should be integral");
|
2019-02-24 20:50:49 +00:00
|
|
|
assert(!name.empty());
|
|
|
|
printIndent();
|
|
|
|
|
|
|
|
m_stream << name << " = ";
|
|
|
|
{
|
|
|
|
FormatFlagsGuard guard(m_stream);
|
|
|
|
m_stream << std::hex << value;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_spaces >= 0)
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
|
|
|
m_stream << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename InputIt>
|
2019-02-24 20:50:49 +00:00
|
|
|
inline void
|
2020-05-01 19:51:15 +00:00
|
|
|
Printer::printAttribute(std::string_view name, const InputIt& begin, const InputIt& end) const
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
|
|
|
assert(!name.empty());
|
|
|
|
printIndent();
|
|
|
|
|
|
|
|
m_stream << name << " = ";
|
|
|
|
|
|
|
|
PrintHelper::print(m_stream, begin, end, -m_levelPlusOne, m_spaces);
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
inline void
|
|
|
|
Printer::printValue(const Type& value) const
|
|
|
|
{
|
|
|
|
printIndent();
|
|
|
|
|
|
|
|
PrintHelper::print(m_stream, value, -m_levelPlusOne, m_spaces);
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename InputIt>
|
2019-02-24 20:50:49 +00:00
|
|
|
inline void
|
|
|
|
Printer::printValue(const InputIt& begin, const InputIt& end) const
|
|
|
|
{
|
|
|
|
printIndent();
|
|
|
|
|
|
|
|
PrintHelper::print(m_stream, begin, end, -m_levelPlusOne, m_spaces);
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
inline void
|
|
|
|
Printer::printForeignAttribute(
|
2020-05-01 19:51:15 +00:00
|
|
|
std::string_view name, const Type& value, const PrintFunction<Type>& printFunction) const
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
|
|
|
assert(!name.empty());
|
|
|
|
printIndent();
|
|
|
|
|
|
|
|
m_stream << name << " = ";
|
|
|
|
|
|
|
|
printFunction(m_stream, value, -m_levelPlusOne, m_spaces);
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
inline void
|
2020-04-07 18:38:56 +00:00
|
|
|
Printer::printForeignValue(const Type& value, const PrintFunction<Type>& printFunction) const
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
|
|
|
printIndent();
|
|
|
|
|
|
|
|
printFunction(m_stream, value, -m_levelPlusOne, m_spaces);
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
inline void
|
2020-05-01 19:51:15 +00:00
|
|
|
Printer::printOrNull(std::string_view name, const Type& address) const
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
|
|
|
assert(!name.empty());
|
|
|
|
printIndent();
|
|
|
|
|
|
|
|
m_stream << name << " = ";
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (address == nullptr)
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
|
|
|
m_stream << "null";
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_spaces >= 0)
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
|
|
|
m_stream << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PrintHelper::print(m_stream, *address, -m_levelPlusOne, m_spaces);
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
inline void
|
|
|
|
Printer::printOrNull(const Type& address) const
|
|
|
|
{
|
|
|
|
printIndent();
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (address == nullptr)
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
|
|
|
m_stream << "null";
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_spaces >= 0)
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
|
|
|
m_stream << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PrintHelper::print(m_stream, *address, -m_levelPlusOne, m_spaces);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline void
|
2020-05-01 19:51:15 +00:00
|
|
|
Printer::printOrNull<const void*>(std::string_view name, const void* const& address) const
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
|
|
|
assert(!name.empty());
|
|
|
|
printIndent();
|
|
|
|
|
|
|
|
m_stream << name << " = ";
|
|
|
|
const void* temp = address;
|
|
|
|
|
|
|
|
PrintHelper::print(m_stream, temp, -m_levelPlusOne, m_spaces);
|
|
|
|
}
|
|
|
|
template <>
|
|
|
|
inline void
|
2020-05-01 19:51:15 +00:00
|
|
|
Printer::printOrNull<void*>(std::string_view name, void* const& address) const
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
|
|
|
const void* const& temp = address;
|
|
|
|
|
|
|
|
printOrNull(name, temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline void
|
2020-04-07 18:38:56 +00:00
|
|
|
Printer::printOrNull<const void*>(const void* const& address) const
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
|
|
|
printIndent();
|
|
|
|
|
|
|
|
const void* temp = address;
|
|
|
|
|
|
|
|
PrintHelper::print(m_stream, temp, -m_levelPlusOne, m_spaces);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline void
|
2020-04-07 18:38:56 +00:00
|
|
|
Printer::printOrNull<void*>(void* const& address) const
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
|
|
|
const void* const& temp = address;
|
|
|
|
|
|
|
|
printOrNull(temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print Helper methods
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename InputIt>
|
2019-02-24 20:50:49 +00:00
|
|
|
inline void
|
2020-04-07 18:38:56 +00:00
|
|
|
PrintHelper::print(
|
|
|
|
std::ostream& stream, const InputIt& begin, const InputIt& end, int level, int spaces)
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
|
|
|
Printer printer(stream, level, spaces);
|
|
|
|
std::for_each(begin, end, [&](const auto& x) { printer.printValue(x); });
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
inline void
|
2020-04-07 18:38:56 +00:00
|
|
|
PrintHelper::printType(
|
|
|
|
std::ostream& stream, Type value, int, int spaces, traits::select::Case<std::is_fundamental>)
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
|
|
|
stream << value;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (spaces >= 0)
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
|
|
|
stream << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
inline void
|
2020-04-07 18:38:56 +00:00
|
|
|
PrintHelper::printType(
|
|
|
|
std::ostream& stream, Type value, int, int spaces, traits::select::Case<std::is_enum>)
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
printType(stream, value, 0, spaces, traits::select::Case<std::is_fundamental>());
|
2019-02-24 20:50:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
inline void
|
2020-04-07 18:38:56 +00:00
|
|
|
PrintHelper::printType(
|
|
|
|
std::ostream& stream,
|
|
|
|
Type value,
|
|
|
|
int level,
|
|
|
|
int spaces,
|
|
|
|
traits::select::Case<std::is_function>)
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
PrintHelper::print(stream, reinterpret_cast<const void*>(value), level, spaces);
|
2019-02-24 20:50:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
inline void
|
2020-04-07 18:38:56 +00:00
|
|
|
PrintHelper::printType(
|
|
|
|
std::ostream& stream,
|
|
|
|
const Type* value,
|
|
|
|
int level,
|
|
|
|
int spaces,
|
|
|
|
traits::select::Case<std::is_pointer>)
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
printType(
|
|
|
|
stream,
|
|
|
|
static_cast<const void*>(value),
|
|
|
|
level,
|
|
|
|
-1,
|
|
|
|
traits::select::Case<std::is_pointer>());
|
|
|
|
if (value == nullptr)
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (spaces >= 0)
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
|
|
|
stream << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stream << ' ';
|
|
|
|
PrintHelper::print(stream, *value, level, spaces);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
inline void
|
2020-04-07 18:38:56 +00:00
|
|
|
PrintHelper::printType(
|
|
|
|
std::ostream& stream,
|
|
|
|
const Type* value,
|
|
|
|
int level,
|
|
|
|
int spaces,
|
|
|
|
traits::select::Case<std::is_array>)
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
printType(stream, value, level, spaces, traits::select::Case<std::is_pointer>());
|
2019-02-24 20:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
2020-04-07 18:38:56 +00:00
|
|
|
PrintHelper::printType(
|
|
|
|
std::ostream& stream,
|
|
|
|
const std::string& value,
|
|
|
|
int level,
|
|
|
|
int spaces,
|
|
|
|
traits::select::Case<traits::is_container>)
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
printType(stream, value.c_str(), level, spaces, traits::select::Case<std::is_pointer>());
|
2019-02-24 20:50:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
inline void
|
2020-04-07 18:38:56 +00:00
|
|
|
PrintHelper::printType(
|
|
|
|
std::ostream& stream,
|
|
|
|
const Type& value,
|
|
|
|
int level,
|
|
|
|
int spaces,
|
|
|
|
traits::select::Case<traits::is_container>)
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
|
|
|
print(stream, value.begin(), value.end(), level, spaces);
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type1, typename Type2>
|
2019-02-24 20:50:49 +00:00
|
|
|
inline void
|
2020-04-07 18:38:56 +00:00
|
|
|
PrintHelper::printType(
|
|
|
|
std::ostream& stream,
|
|
|
|
const std::pair<Type1, Type2>& value,
|
|
|
|
int level,
|
|
|
|
int spaces,
|
|
|
|
traits::select::Case<>)
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
|
|
|
Printer print(stream, level, spaces);
|
|
|
|
print.printValue(value.first);
|
|
|
|
print.printValue(value.second);
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename... Types>
|
2019-02-24 20:50:49 +00:00
|
|
|
inline void
|
2020-04-07 18:38:56 +00:00
|
|
|
PrintHelper::printType(
|
|
|
|
std::ostream& stream,
|
|
|
|
const std::tuple<Types...>& value,
|
|
|
|
int level,
|
|
|
|
int spaces,
|
|
|
|
traits::select::Case<>)
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
|
|
|
Printer print(stream, level, spaces);
|
2020-04-07 18:38:56 +00:00
|
|
|
traits::for_each_in_tuple(value, [&](const auto& x) { print.printValue(x); });
|
2019-02-24 20:50:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
inline void
|
2020-04-07 18:38:56 +00:00
|
|
|
PrintHelper::printType(
|
|
|
|
std::ostream& stream, const Type& value, int level, int spaces, traits::select::Case<>)
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
|
|
|
value.print(stream, level, spaces);
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Type>
|
2019-02-24 20:50:49 +00:00
|
|
|
inline void
|
2020-04-07 18:38:56 +00:00
|
|
|
PrintHelper::print(std::ostream& stream, const Type& value, int level, int spaces)
|
2019-02-24 20:50:49 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
using Selection = traits::select::Select<
|
|
|
|
Type,
|
|
|
|
std::is_fundamental,
|
|
|
|
std::is_enum,
|
|
|
|
std::is_function,
|
|
|
|
std::is_pointer,
|
|
|
|
std::is_array,
|
|
|
|
traits::is_container>;
|
2019-02-24 20:50:49 +00:00
|
|
|
|
|
|
|
PrintHelper::printType(stream, value, level, spaces, Selection());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llarp
|