Skip to content

Commit 4a65796

Browse files
committed
Update block generation
1 parent 267e419 commit 4a65796

3 files changed

Lines changed: 10 additions & 10 deletions

File tree

src/model/BlockFactory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@
4242
/**
4343
* @brief General functional for the creation of different types of blocks
4444
*/
45-
using BlockFactoryFunc = std::function<Block*(int, Model*)>;
45+
using BlockFactoryFunc = std::function<std::shared_ptr<Block>(int, Model*)>;
4646

4747
#endif

src/model/Model.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232

3333
template <typename block_type>
3434
BlockFactoryFunc block_factory() {
35-
return [](int count, Model *model) -> Block * {
36-
return new block_type(count, model);
35+
return [](int count, Model *model) -> std::shared_ptr<Block>{
36+
return std::make_shared<block_type>(count, model);
3737
};
3838
}
3939

@@ -60,27 +60,27 @@ Model::Model() {
6060

6161
Model::~Model() {}
6262

63-
Block *Model::create_block(const std::string &block_type) {
63+
std::shared_ptr<Block> Model::create_block(const std::string &block_type) {
6464
// Get block from factory
6565
auto it = block_factory_map.find(block_type);
6666
if (it == block_factory_map.end()) {
6767
throw std::runtime_error("Invalid block type " + block_type);
6868
}
69-
Block *block = it->second(block_count, this);
69+
std::shared_ptr<Block> block = it->second(block_count, this);
7070
return block;
7171
}
7272

73-
int Model::add_block(Block *block, const std::string_view &name,
73+
int Model::add_block(std::shared_ptr<Block> block, const std::string_view &name,
7474
const std::vector<int> &block_param_ids, bool internal) {
7575
// Set global parameter IDs
7676
block->setup_params_(block_param_ids);
7777

7878
auto name_string = static_cast<std::string>(name);
7979

8080
if (internal) {
81-
hidden_blocks.push_back(std::shared_ptr<Block>(block));
81+
hidden_blocks.push_back(block);
8282
} else {
83-
blocks.push_back(std::shared_ptr<Block>(block));
83+
blocks.push_back(block);
8484
}
8585

8686
block_types.push_back(block->block_type);

src/model/Model.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class Model {
9999
* @param block_name The block name (defined in block_factory_map)
100100
* @return int Global ID of the block
101101
*/
102-
Block *create_block(const std::string &block_name);
102+
std::shared_ptr<Block> create_block(const std::string &block_name);
103103

104104
/**
105105
* @brief Add a block to the model (without parameters)
@@ -110,7 +110,7 @@ class Model {
110110
* @param internal Toggle whether block is internal
111111
* @return int Global ID of the block
112112
*/
113-
int add_block(Block *block, const std::string_view &name,
113+
int add_block(std::shared_ptr<Block> block, const std::string_view &name,
114114
const std::vector<int> &block_param_ids, bool internal = false);
115115

116116
/**

0 commit comments

Comments
 (0)