Skip to content

Commit fcffc87

Browse files
authored
Merge pull request #2 from Ultimaker/CURA-13049_process-the-end-variable-replacement-in-the-engine
CURA-13049 Add a Reusable ChainableEnvironment and optimize the EnvironmentMap
2 parents fd7cd2f + 0776300 commit fcffc87

7 files changed

Lines changed: 111 additions & 44 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ set(CURA_FORMULAE_ENGINE__SRC
6969
src/ast/binary_expr/or_expr.cpp
7070
src/ast/binary_expr/pow_expr.cpp
7171
src/ast/binary_expr/and_expr.cpp
72-
src/ast/primary_expr/string_expr.cpp
72+
src/ast/primary_expr/string_expr.cpp
7373
)
7474

7575
add_library(cura-formulae-engine SHARED ${CURA_FORMULAE_ENGINE__SRC})

conandata.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version: "1.0.0"
1+
version: "1.1.0"

include/cura-formulae-engine/ast/ast.h

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#pragma once
22

3-
#include "cura-formulae-engine/eval.h"
4-
5-
#include <zeus/expected.hpp>
6-
73
#include <functional>
84
#include <string>
5+
#include <unordered_map>
96
#include <unordered_set>
7+
#include <zeus/expected.hpp>
8+
9+
#include "cura-formulae-engine/eval.h"
1010

1111
namespace CuraFormulaeEngine::eval
1212
{
@@ -36,10 +36,13 @@ class EnvironmentMap : public Environment
3636
{
3737
private:
3838
std::unordered_map<std::string, eval::Value> environment_ = {};
39-
public:
4039

40+
public:
4141
EnvironmentMap() = default;
42-
explicit EnvironmentMap(const std::unordered_map<std::string, eval::Value>& map) : environment_(map) {}
42+
explicit EnvironmentMap(const std::unordered_map<std::string, eval::Value>& map)
43+
: environment_(map)
44+
{
45+
}
4346
~EnvironmentMap() override = default;
4447

4548
[[nodiscard]] std::optional<eval::Value> get(const std::string& key) const noexcept override;
@@ -52,31 +55,58 @@ class EnvironmentMap : public Environment
5255

5356
void set(const std::string& key, const eval::Value& value) noexcept;
5457

55-
[[nodiscard]] EnvironmentMap clone() const noexcept;
58+
void add(const std::unordered_map<std::string, eval::Value>& values);
5659

60+
[[nodiscard]] EnvironmentMap clone() const noexcept;
5761
};
5862

59-
class LocalEnvironment : public Environment
63+
class ChainableEnvironment : public Environment
6064
{
65+
private:
66+
const Environment* shadow_environment_{ nullptr };
67+
6168
public:
69+
explicit ChainableEnvironment(const Environment* shadow_environment = nullptr)
70+
: shadow_environment_(shadow_environment)
71+
{
72+
}
73+
~ChainableEnvironment() override = default;
6274

63-
LocalEnvironment() = default;
64-
explicit LocalEnvironment(const Environment* shadow_environment_)
65-
: shadow_environment_(shadow_environment_)
75+
[[nodiscard]] std::optional<eval::Value> get(const std::string& key) const noexcept override final;
76+
77+
[[nodiscard]] bool has(const std::string& key) const noexcept override final;
78+
79+
[[nodiscard]] std::unordered_map<std::string, eval::Value> getAll() const noexcept override final;
80+
81+
protected:
82+
[[nodiscard]] virtual std::optional<eval::Value> getImpl(const std::string& key) const noexcept = 0;
83+
84+
[[nodiscard]] virtual bool hasImpl(const std::string& key) const noexcept = 0;
85+
86+
[[nodiscard]] virtual std::unordered_map<std::string, eval::Value> getAllImpl() const noexcept = 0;
87+
};
88+
89+
class LocalEnvironment : public ChainableEnvironment
90+
{
91+
private:
92+
EnvironmentMap local_environment_;
93+
94+
public:
95+
explicit LocalEnvironment(const Environment* shadow_environment = nullptr)
96+
: ChainableEnvironment(shadow_environment)
6697
{
6798
}
6899
~LocalEnvironment() override = default;
69100

70-
EnvironmentMap local_environment_ = {};
71-
const Environment* shadow_environment_;
72-
73-
[[nodiscard]] std::optional<eval::Value> get(const std::string& key) const noexcept override;
101+
[[nodiscard]] std::optional<eval::Value> getImpl(const std::string& key) const noexcept override;
74102

75-
[[nodiscard]] bool has(const std::string& key) const noexcept override;
103+
[[nodiscard]] bool hasImpl(const std::string& key) const noexcept override;
76104

77-
[[nodiscard]] std::unordered_map<std::string, eval::Value> getAll() const noexcept override;
105+
[[nodiscard]] std::unordered_map<std::string, eval::Value> getAllImpl() const noexcept override;
78106

79107
void set(const std::string& key, const eval::Value& value);
108+
109+
void add(const std::unordered_map<std::string, eval::Value>& values);
80110
};
81111

82112
} // namespace CuraFormulaeEngine::env
@@ -123,7 +153,8 @@ struct Expr
123153
[[nodiscard]] virtual bool deepEq(const Expr& other) const = 0;
124154

125155
/**
126-
* @brief Traverses the expression tree and applies the visitor function to each node.
156+
* @brief Traverses the expression tree and applies the visitor function to
157+
* each node.
127158
*
128159
* @param visitor The visitor function to apply to each node.
129160
*/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#ifdef _WIN32
4+
# ifdef cura_formulae_engine_EXPORTS
5+
# define CURA_FORMULAE_ENGINE_API __declspec(dllexport)
6+
# else
7+
# define CURA_FORMULAE_ENGINE_API __declspec(dllimport)
8+
# endif
9+
#else
10+
# define CURA_FORMULAE_ENGINE_API
11+
#endif
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#pragma once
22

33
#include "cura-formulae-engine/ast/ast.h"
4+
#include "cura-formulae-engine/config.h"
45

56
namespace CuraFormulaeEngine::env
67
{
78

89
/**
910
* @brief The standard environment.
1011
*/
11-
extern const EnvironmentMap std_env;
12+
CURA_FORMULAE_ENGINE_API extern const EnvironmentMap std_env;
1213

1314
} // namespace CuraFormulaeEngine::env

src/ast/ast.cpp

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ namespace CuraFormulaeEngine::env
55

66
std::optional<eval::Value> EnvironmentMap::get(const std::string& key) const noexcept
77
{
8-
if (!has(key))
8+
auto iterator = environment_.find(key);
9+
if (iterator == environment_.end())
910
{
1011
return std::nullopt;
1112
}
12-
return environment_.at(key);
13+
return iterator->second;
1314
}
1415

1516
bool EnvironmentMap::has(const std::string& key) const noexcept
@@ -32,44 +33,67 @@ void EnvironmentMap::set(const std::string& key, const eval::Value& value) noexc
3233
environment_.insert_or_assign(key, value);
3334
}
3435

36+
void EnvironmentMap::add(const std::unordered_map<std::string, eval::Value>& values)
37+
{
38+
environment_.insert(values.begin(), values.end());
39+
}
40+
3541
EnvironmentMap EnvironmentMap::clone() const noexcept
3642
{
37-
return EnvironmentMap{environment_};
43+
return EnvironmentMap{ environment_ };
3844
}
3945

40-
std::optional<eval::Value> LocalEnvironment::get(const std::string& key) const noexcept
46+
std::optional<eval::Value> LocalEnvironment::getImpl(const std::string& key) const noexcept
4147
{
42-
if (local_environment_.has(key))
43-
{
44-
return local_environment_.get(key);
45-
}
46-
if (shadow_environment_ && shadow_environment_->has(key))
48+
return local_environment_.get(key);
49+
}
50+
51+
bool LocalEnvironment::hasImpl(const std::string& key) const noexcept
52+
{
53+
return local_environment_.has(key);
54+
}
55+
56+
std::unordered_map<std::string, eval::Value> LocalEnvironment::getAllImpl() const noexcept
57+
{
58+
return local_environment_.getAll();
59+
}
60+
61+
void LocalEnvironment::set(const std::string& key, const eval::Value& value)
62+
{
63+
local_environment_.set(key, value);
64+
}
65+
66+
void LocalEnvironment::add(const std::unordered_map<std::string, eval::Value>& values)
67+
{
68+
local_environment_.add(values);
69+
}
70+
71+
std::optional<eval::Value> ChainableEnvironment::get(const std::string& key) const noexcept
72+
{
73+
std::optional<eval::Value> value = getImpl(key);
74+
if (! value.has_value() && shadow_environment_)
4775
{
48-
return shadow_environment_->get(key);
76+
value = shadow_environment_->get(key);
4977
}
50-
return std::nullopt;
78+
return value;
5179
}
5280

53-
bool LocalEnvironment::has(const std::string& key) const noexcept
81+
bool ChainableEnvironment::has(const std::string& key) const noexcept
5482
{
55-
return local_environment_.has(key) || (shadow_environment_ && shadow_environment_->has(key));
83+
return hasImpl(key) || (shadow_environment_ && shadow_environment_->has(key));
5684
}
5785

58-
std::unordered_map<std::string, eval::Value> LocalEnvironment::getAll() const noexcept
86+
std::unordered_map<std::string, eval::Value> ChainableEnvironment::getAll() const noexcept
5987
{
6088
std::unordered_map<std::string, eval::Value> all;
61-
if (shadow_environment_) {
89+
if (shadow_environment_)
90+
{
6291
all = shadow_environment_->getAll();
6392
}
6493

65-
auto scoped = local_environment_.getAll();
94+
auto scoped = getAllImpl();
6695
all.insert(scoped.begin(), scoped.end());
6796
return all;
6897
}
6998

70-
void LocalEnvironment::set(const std::string& key, const eval::Value& value)
71-
{
72-
local_environment_.set(key, value);
73-
}
74-
75-
} // namespace CuraFormulaeEngine::env
99+
} // namespace CuraFormulaeEngine::env

src/env/env.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace CuraFormulaeEngine::env
3535
/**
3636
* @brief The standard environment.
3737
*/
38-
const EnvironmentMap std_env = []()
38+
CURA_FORMULAE_ENGINE_API const EnvironmentMap std_env = []()
3939
{
4040
EnvironmentMap env;
4141

0 commit comments

Comments
 (0)