1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#include "cmCTestResourceSpec.h"
#include <functional>
#include <map>
#include <string>
#include <utility>
#include <vector>
#include <cmext/string_view>
#include <cm3p/json/value.h>
#include "cmsys/RegularExpression.hxx"
#include "cmJSONHelpers.h"
namespace {
using JSONHelperBuilder = cmJSONHelperBuilder;
cmsys::RegularExpression const IdentifierRegex{ "^[a-z_][a-z0-9_]*$" };
cmsys::RegularExpression const IdRegex{ "^[a-z0-9_]+$" };
struct Version
{
int Major = 1;
int Minor = 0;
};
struct TopVersion
{
struct Version Version;
};
auto const VersionFieldHelper =
JSONHelperBuilder::Int(cmCTestResourceSpecErrors::INVALID_VERSION);
auto const VersionHelper = JSONHelperBuilder::Required<Version>(
cmCTestResourceSpecErrors::NO_VERSION,
JSONHelperBuilder::Object<Version>()
.Bind("major"_s, &Version::Major, VersionFieldHelper)
.Bind("minor"_s, &Version::Minor, VersionFieldHelper));
auto const RootVersionHelper = JSONHelperBuilder::Object<TopVersion>().Bind(
"version"_s, &TopVersion::Version, VersionHelper, false);
bool ResourceIdHelper(std::string& out, Json::Value const* value,
cmJSONState* state)
{
#if defined(__GNUC__) && __GNUC__ >= 15
# define CM_GCC_diagnostic_push_Wmaybe_uninitialized
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
if (!JSONHelperBuilder::String(cmCTestResourceSpecErrors::INVALID_RESOURCE)(
out, value, state)) {
return false;
}
#ifdef CM_GCC_diagnostic_push_Wmaybe_uninitialized
# pragma GCC diagnostic pop
# undef CM_GCC_diagnostic_push_Wmaybe_uninitialized
#endif
cmsys::RegularExpressionMatch match;
if (!IdRegex.find(out.c_str(), match)) {
cmCTestResourceSpecErrors::INVALID_RESOURCE(value, state);
return false;
}
return true;
}
auto const ResourceHelper =
JSONHelperBuilder::Object<cmCTestResourceSpec::Resource>()
.Bind("id"_s, &cmCTestResourceSpec::Resource::Id, ResourceIdHelper)
.Bind(
"slots"_s, &cmCTestResourceSpec::Resource::Capacity,
JSONHelperBuilder::UInt(cmCTestResourceSpecErrors::INVALID_RESOURCE, 1),
false);
auto const ResourceListHelper =
JSONHelperBuilder::Vector<cmCTestResourceSpec::Resource>(
cmCTestResourceSpecErrors::INVALID_RESOURCE_TYPE, ResourceHelper);
auto const ResourceMapHelper =
JSONHelperBuilder::MapFilter<std::vector<cmCTestResourceSpec::Resource>>(
cmCTestResourceSpecErrors::INVALID_SOCKET_SPEC, ResourceListHelper,
[](std::string const& key) -> bool {
cmsys::RegularExpressionMatch match;
return IdentifierRegex.find(key.c_str(), match);
});
auto const SocketSetHelper = JSONHelperBuilder::Vector<
std::map<std::string, std::vector<cmCTestResourceSpec::Resource>>>(
cmCTestResourceSpecErrors::INVALID_SOCKET_SPEC, ResourceMapHelper);
bool SocketHelper(cmCTestResourceSpec::Socket& out, Json::Value const* value,
cmJSONState* state)
{
std::vector<
std::map<std::string, std::vector<cmCTestResourceSpec::Resource>>>
sockets;
if (!SocketSetHelper(sockets, value, state)) {
return false;
}
if (sockets.size() > 1) {
cmCTestResourceSpecErrors::INVALID_SOCKET_SPEC(value, state);
return false;
}
if (sockets.empty()) {
out.Resources.clear();
} else {
out.Resources = std::move(sockets[0]);
}
return true;
}
auto const LocalRequiredHelper =
JSONHelperBuilder::Required<cmCTestResourceSpec::Socket>(
cmCTestResourceSpecErrors::INVALID_SOCKET_SPEC, SocketHelper);
auto const RootHelper = JSONHelperBuilder::Object<cmCTestResourceSpec>().Bind(
"local", &cmCTestResourceSpec::LocalSocket, LocalRequiredHelper, false);
}
bool cmCTestResourceSpec::ReadFromJSONFile(std::string const& filename)
{
Json::Value root;
this->parseState = cmJSONState(filename, &root);
if (!this->parseState.errors.empty()) {
return false;
}
TopVersion version;
bool result;
if ((result = RootVersionHelper(version, &root, &parseState)) != true) {
return result;
}
if (version.Version.Major != 1 || version.Version.Minor != 0) {
return false;
}
return RootHelper(*this, &root, &parseState);
}
bool cmCTestResourceSpec::operator==(cmCTestResourceSpec const& other) const
{
return this->LocalSocket == other.LocalSocket;
}
bool cmCTestResourceSpec::operator!=(cmCTestResourceSpec const& other) const
{
return !(*this == other);
}
bool cmCTestResourceSpec::Socket::operator==(
cmCTestResourceSpec::Socket const& other) const
{
return this->Resources == other.Resources;
}
bool cmCTestResourceSpec::Socket::operator!=(
cmCTestResourceSpec::Socket const& other) const
{
return !(*this == other);
}
bool cmCTestResourceSpec::Resource::operator==(
cmCTestResourceSpec::Resource const& other) const
{
return this->Id == other.Id && this->Capacity == other.Capacity;
}
bool cmCTestResourceSpec::Resource::operator!=(
cmCTestResourceSpec::Resource const& other) const
{
return !(*this == other);
}
|