Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert unique_ptr usage from Type #62

Merged
merged 8 commits into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 63 additions & 49 deletions src/metkit/mars/Type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,38 @@

#include "metkit/mars/Type.h"

#include "metkit/mars/MarsExpandContext.h"
#include "metkit/mars/MarsRequest.h"

#include <algorithm>
#include <cstddef>
#include <iostream>
#include <iterator>
#include <memory>
#include <ostream>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

#include "eckit/exception/Exceptions.h"
#include "eckit/value/Value.h"

#include "metkit/mars/MarsExpandContext.h"
#include "metkit/mars/MarsRequest.h"

namespace metkit::mars {

//----------------------------------------------------------------------------------------------------------------------

ContextRule::ContextRule(const std::string& k) : key_(k) {}

std::ostream& operator<<(std::ostream& s, const ContextRule& r) {
r.print(s);
return s;
}

void Context::add(std::unique_ptr<ContextRule> rule) {
rules_.emplace_back(std::move(rule));
//----------------------------------------------------------------------------------------------------------------------

void Context::add(ContextRule* rule) {
rules_.emplace_back(rule);
}

bool Context::matches(MarsRequest req) const {
Expand Down Expand Up @@ -58,14 +69,12 @@ void Context::print(std::ostream& out) const {
out << "]";
}

namespace {
//----------------------------------------------------------------------------------------------------------------------

class Include : public ContextRule {
public:
Include(const std::string& k, const std::set<std::string>& vv) : ContextRule(k), vals_(vv) {}

~Include() override = default;

bool matches(MarsRequest req) const override {
if (!req.has(key_)) {
return false;
Expand All @@ -77,20 +86,17 @@ class Include : public ContextRule {
}
return false;
}

private: // methods
void print(std::ostream& out) const override { out << "Include[key=" << key_ << ",vals=[" << vals_ << "]]"; }

void print(std::ostream& out) const override {
out << "Include[key=" << key_ << ",vals=[" << vals_ << "]]";
}
private:
std::set<std::string> vals_;
};

class Exclude : public ContextRule {
public:
Exclude(const std::string& k, const std::set<std::string>& vv) : ContextRule(k), vals_(vv) {}

~Exclude() override = default;

bool matches(MarsRequest req) const override {
if (!req.has(key_)) {
return false;
Expand All @@ -102,77 +108,82 @@ class Exclude : public ContextRule {
}
return true;
}

private: // methods
void print(std::ostream& out) const override { out << "Exclude[key=" << key_ << ",vals=[" << vals_ << "]]"; }

void print(std::ostream& out) const override {
out << "Exclude[key=" << key_ << ",vals=[" << vals_ << "]]";
}
private:
std::set<std::string> vals_;
};

class Undef : public ContextRule {
public:
Undef(const std::string& k) : ContextRule(k) {}

~Undef() override = default;

bool matches(MarsRequest req) const override { return req.has(key_); }

bool matches(MarsRequest req) const override {
return !req.has(key_);
}
private: // methods
void print(std::ostream& out) const override { out << "Undef[key=" << key_ << "]"; }
void print(std::ostream& out) const override {
out << "Undef[key=" << key_ << "]";
}
};

class Def : public ContextRule {
public:
Def(const std::string& k) : ContextRule(k) {}

bool matches(MarsRequest req) const override { return req.has(key_); }

bool matches(MarsRequest req) const override {
return req.has(key_);
}
private: // methods
void print(std::ostream& out) const override { out << "Def[key=" << key_ << "]"; }
void print(std::ostream& out) const override {
out << "Def[key=" << key_ << "]";
}
};

std::unique_ptr<ContextRule> parseRule(std::string key, eckit::Value r) {
//----------------------------------------------------------------------------------------------------------------------
// HELPERS

namespace {

ContextRule* parseRule(std::string key, eckit::Value r) {

std::set<std::string> vals;

if (r.isList()) {
for (size_t k = 0; k < r.size(); k++) {
vals.insert(r[k]);
}
return std::make_unique<Include>(key, vals);
return new Include(key, vals);
}

ASSERT(r.contains("op"));
std::string op = r["op"];
ASSERT(op.size() == 1);
switch (op[0]) {
case 'u':
return std::make_unique<Undef>(key);
case 'd':
return std::make_unique<Def>(key);
case 'u': return new Undef(key);
case 'd': return new Def(key);
case '!':
ASSERT(r.contains("vals"));
eckit::Value vv = r["vals"];
for (size_t k = 0; k < vv.size(); k++) {
vals.insert(vv[k]);
}
return std::make_unique<Exclude>(key, vals);
return new Exclude(key, vals);
}
return nullptr;
}

std::unique_ptr<Context> parseContext(const eckit::Value c) {
Context* parseContext(eckit::Value c) {

auto context = std::make_unique<Context>();
Context* context = new Context{};

auto keys = c.keys();
eckit::Value keys = c.keys();

for (size_t j = 0; j < keys.size(); ++j) {
const auto& key = keys[j];
context->add(parseRule(key, c[key]));
for (size_t j = 0; j < keys.size(); j++) {
std::string key = keys[j];
ContextRule* r = parseRule(key, c[key]);
context->add(r);
}

return context;
}

Expand Down Expand Up @@ -218,13 +229,13 @@ Type::Type(const std::string& name, const eckit::Value& settings) :
vals.push_back(vv);
}

std::unique_ptr<Context> context;
Context* context = nullptr;
if (d.contains("context")) {
context = parseContext(d["context"]);
} else {
context = std::make_unique<Context>();
context = new Context;
}
defaults_.emplace(std::move(context), vals);
defaults_.emplace(context, vals);
}
}
}
Expand All @@ -246,14 +257,13 @@ Type::Type(const std::string& name, const eckit::Value& settings) :
for (size_t i = 0; i < unsets.size(); i++) {
eckit::Value u = unsets[i];
ASSERT(u.contains("context"));
unsets_.emplace(parseContext(u["context"]));
Context* c = parseContext(u["context"]);
unsets_.emplace(c);
}
}
}
}

Type::~Type() {}

bool Type::flatten() const {
return flatten_;
}
Expand Down Expand Up @@ -292,16 +302,19 @@ class InSet {
bool operator()(const std::string& s) const { return set_.find(s) != set_.end(); }
};

bool Type::matches(const std::vector<std::string>& match, const std::vector<std::string>& values) const {
bool Type::matches(const std::vector<std::string>& match,
const std::vector<std::string>& values) const {
InSet in_set(match);
return std::find_if(values.begin(), values.end(), in_set) != values.end();
}


std::ostream& operator<<(std::ostream& s, const Type& x) {
x.print(s);
return s;
}


std::string Type::tidy(const MarsExpandContext& ctx, const std::string& value) const {
std::string result = value;
expand(ctx, result);
Expand Down Expand Up @@ -444,4 +457,5 @@ void Type::check(const MarsExpandContext& ctx, const std::vector<std::string>& v
}

//----------------------------------------------------------------------------------------------------------------------

} // namespace metkit::mars
20 changes: 6 additions & 14 deletions src/metkit/mars/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,9 @@ namespace metkit::mars {
class MarsRequest;
class MarsExpandContext;

//----------------------------------------------------------------------------------------------------------------------

class ContextRule {
public:
ContextRule(const ContextRule&) = default;
ContextRule& operator=(const ContextRule&) = default;
ContextRule(ContextRule&&) = delete;
ContextRule& operator=(ContextRule&&) = delete;

explicit ContextRule(const std::string& k);
ContextRule(const std::string& k);

virtual ~ContextRule() = default;

Expand All @@ -55,11 +48,11 @@ class ContextRule {
virtual void print(std::ostream& out) const = 0;
};

//----------------------------------------------------------------------------------------------------------------------

class Context {
public:
void add(std::unique_ptr<ContextRule> rule);
/// @note takes ownership of the rule
void add(ContextRule* rule);

bool matches(MarsRequest req) const;

friend std::ostream& operator<<(std::ostream& s, const Context& x);
Expand All @@ -76,6 +69,8 @@ class Type : public eckit::Counted {
public: // methods
Type(const std::string& name, const eckit::Value& settings);

~Type() override = default;

virtual void expand(const MarsExpandContext& ctx,
std::vector<std::string>& values) const;
virtual bool expand(const MarsExpandContext& ctx, std::string& value) const;
Expand Down Expand Up @@ -122,9 +117,6 @@ class Type : public eckit::Counted {
std::map<std::unique_ptr<Context>, std::string> sets_;
std::set<std::unique_ptr<Context>> unsets_;

protected: // methods
virtual ~Type() override;

private: // methods
virtual void print(std::ostream& out) const = 0;
};
Expand Down
Loading