Skip to content

Commit 973ba30

Browse files
committed
fix(cache): avoid copying non-copyable Template objects
1 parent e1422d5 commit 973ba30

3 files changed

Lines changed: 22 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ This project follows a simple versioning strategy:
88
- fixes → patch
99

1010
---
11+
## [0.1.1] - fix(cache)
12+
- fix(cache): store compiled templates through shared immutable handles instead of copying non-copyable Template objects
1113

1214
## [0.1.0] - Initial release
1315

include/vix/template/Cache.hpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@
1717
#define VIX_TEMPLATE_CACHE_HPP
1818

1919
#include <memory>
20-
#include <optional>
2120
#include <string>
2221

2322
#include <vix/template/Template.hpp>
2423

2524
namespace vix::template_
2625
{
26+
/**
27+
* @brief Shared immutable template handle used by the cache.
28+
*/
29+
using TemplatePtr = std::shared_ptr<const Template>;
30+
2731
/**
2832
* @brief Template cache interface.
2933
*
@@ -46,20 +50,20 @@ namespace vix::template_
4650
* @brief Retrieve a cached template.
4751
*
4852
* @param name Template name.
49-
* @return Cached template if present.
53+
* @return Shared immutable template if present, nullptr otherwise.
5054
*/
51-
[[nodiscard]] virtual std::optional<Template> get(
55+
[[nodiscard]] virtual TemplatePtr get(
5256
const std::string &name) const = 0;
5357

5458
/**
5559
* @brief Store a compiled template.
5660
*
5761
* @param name Template name.
58-
* @param tpl Compiled template.
62+
* @param tpl Compiled template handle.
5963
*/
6064
virtual void put(
6165
const std::string &name,
62-
const Template &tpl) = 0;
66+
TemplatePtr tpl) = 0;
6367

6468
/**
6569
* @brief Remove a template from cache.

tests/cache_test.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include <cassert>
1717
#include <iostream>
1818
#include <map>
19-
#include <optional>
19+
#include <memory>
2020
#include <string>
2121

2222
#include <vix/template/Cache.hpp>
@@ -27,19 +27,19 @@ using namespace vix::template_;
2727
class MemoryCache : public Cache
2828
{
2929
public:
30-
std::optional<Template> get(const std::string &name) const override
30+
TemplatePtr get(const std::string &name) const override
3131
{
3232
const auto it = store_.find(name);
3333
if (it == store_.end())
3434
{
35-
return std::nullopt;
35+
return nullptr;
3636
}
3737
return it->second;
3838
}
3939

40-
void put(const std::string &name, const Template &tpl) override
40+
void put(const std::string &name, TemplatePtr tpl) override
4141
{
42-
store_[name] = tpl;
42+
store_[name] = std::move(tpl);
4343
}
4444

4545
bool erase(const std::string &name) override
@@ -58,12 +58,12 @@ class MemoryCache : public Cache
5858
}
5959

6060
private:
61-
std::map<std::string, Template> store_;
61+
std::map<std::string, TemplatePtr> store_;
6262
};
6363

64-
static Template make_template(const std::string &name)
64+
static TemplatePtr make_template(const std::string &name)
6565
{
66-
return Template(name, RootNode{});
66+
return std::make_shared<Template>(name, RootNode{});
6767
}
6868

6969
static void test_put_and_get()
@@ -75,7 +75,7 @@ static void test_put_and_get()
7575

7676
auto result = cache.get("home");
7777

78-
assert(result.has_value());
78+
assert(result != nullptr);
7979
assert(result->name() == "home");
8080
}
8181

@@ -96,7 +96,7 @@ static void test_erase()
9696
cache.put("home", make_template("home"));
9797
assert(cache.has("home"));
9898

99-
bool removed = cache.erase("home");
99+
const bool removed = cache.erase("home");
100100
assert(removed);
101101
assert(!cache.has("home"));
102102
}
@@ -119,7 +119,7 @@ static void test_get_missing()
119119
MemoryCache cache;
120120

121121
auto result = cache.get("missing");
122-
assert(!result.has_value());
122+
assert(result == nullptr);
123123
}
124124

125125
int main()

0 commit comments

Comments
 (0)