mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-24 00:47:57 -05:00
... you are too slow and QJson API is so ugly. Now using this wonderful json lib: https://github.com/nlohmann/json Results in release mode (QJson tests are not shown but QJson was even slower than string concat). PASS : TestQgsJsonUtils::testExportAttributesJson(Use json) RESULT : TestQgsJsonUtils::testExportAttributesJson():"Use json": 0.0022 msecs per iteration (total: 75, iterations: 32768) PASS : TestQgsJsonUtils::testExportAttributesJson(Use old string concat) RESULT : TestQgsJsonUtils::testExportAttributesJson():"Use old string concat": 0.0032 msecs per iteration (total: 54, iterations: 16384) PASS : TestQgsJsonUtils::testExportFeatureJson(Use json) RESULT : TestQgsJsonUtils::testExportFeatureJson():"Use json": 0.011 msecs per iteration (total: 96, iterations: 8192) PASS : TestQgsJsonUtils::testExportFeatureJson(Use old string concat) RESULT : TestQgsJsonUtils::testExportFeatureJson():"Use old string concat": 0.015 msecs per iteration (total: 64, iterations: 4096) PASS : TestQgsJsonUtils::testExportGeomToJson(Use json) RESULT : TestQgsJsonUtils::testExportGeomToJson():"Use json": 0.76 msecs per iteration (total: 98, iterations: 128) PASS : TestQgsJsonUtils::testExportGeomToJson(Use old string concat) RESULT : TestQgsJsonUtils::testExportGeomToJson():"Use old string concat": 0.85 msecs per iteration (total: 55, iterations: 64) PASS : TestQgsJsonUtils::cleanupTestCase()
70 lines
1.6 KiB
C++
70 lines
1.6 KiB
C++
#pragma once
|
|
|
|
#include <initializer_list>
|
|
#include <utility>
|
|
|
|
#include <nlohmann/detail/meta/type_traits.hpp>
|
|
|
|
namespace nlohmann
|
|
{
|
|
namespace detail
|
|
{
|
|
template<typename BasicJsonType>
|
|
class json_ref
|
|
{
|
|
public:
|
|
using value_type = BasicJsonType;
|
|
|
|
json_ref(value_type&& value)
|
|
: owned_value(std::move(value)), value_ref(&owned_value), is_rvalue(true)
|
|
{}
|
|
|
|
json_ref(const value_type& value)
|
|
: value_ref(const_cast<value_type*>(&value)), is_rvalue(false)
|
|
{}
|
|
|
|
json_ref(std::initializer_list<json_ref> init)
|
|
: owned_value(init), value_ref(&owned_value), is_rvalue(true)
|
|
{}
|
|
|
|
template <
|
|
class... Args,
|
|
enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
|
|
json_ref(Args && ... args)
|
|
: owned_value(std::forward<Args>(args)...), value_ref(&owned_value),
|
|
is_rvalue(true) {}
|
|
|
|
// class should be movable only
|
|
json_ref(json_ref&&) = default;
|
|
json_ref(const json_ref&) = delete;
|
|
json_ref& operator=(const json_ref&) = delete;
|
|
json_ref& operator=(json_ref&&) = delete;
|
|
~json_ref() = default;
|
|
|
|
value_type moved_or_copied() const
|
|
{
|
|
if (is_rvalue)
|
|
{
|
|
return std::move(*value_ref);
|
|
}
|
|
return *value_ref;
|
|
}
|
|
|
|
value_type const& operator*() const
|
|
{
|
|
return *static_cast<value_type const*>(value_ref);
|
|
}
|
|
|
|
value_type const* operator->() const
|
|
{
|
|
return static_cast<value_type const*>(value_ref);
|
|
}
|
|
|
|
private:
|
|
mutable value_type owned_value = nullptr;
|
|
value_type* value_ref = nullptr;
|
|
const bool is_rvalue;
|
|
};
|
|
} // namespace detail
|
|
} // namespace nlohmann
|