Cerata
A library to generate structural hardware designs
pool.h
1 // Copyright 2018-2019 Delft University of Technology
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <string>
18 #include <vector>
19 #include <memory>
20 #include <utility>
21 #include <optional>
22 
23 #include "cerata/logging.h"
24 #include "cerata/node.h"
25 #include "cerata/literal.h"
26 
27 namespace cerata {
28 
29 // Forward decl.
30 class Type;
31 class Component;
32 
38 template<typename T>
39 class Pool {
40  public:
42  void Add(const std::shared_ptr<T> &object) {
43  for (const auto &existing_object : objects_) {
44  if (existing_object->name() == object->name()) {
45  CERATA_LOG(FATAL, "Object " + PoolTypeToString(*existing_object) + " already exists in pool.");
46  }
47  }
48  objects_.push_back(object);
49  }
50 
52  std::optional<T *> Get(const std::string &name) {
53  // Check for potential duplicate
54  for (const auto &existing_object : objects_) {
55  if (existing_object->name() == name) {
56  return existing_object.get();
57  }
58  }
59  return std::nullopt;
60  }
61 
63  void Clear() {
64  objects_.clear();
65  }
66 
67  protected:
69  std::vector<std::shared_ptr<T>> objects_;
70 
71  private:
77  std::string PoolTypeToString(const T &object) {
78  return object.ToString();
79  }
80 };
81 
83 class TypePool : public Pool<Type> {};
84 
86 class ComponentPool : public Pool<Component> {};
87 
90  static TypePool pool;
91  return &pool;
92 }
93 
96  static ComponentPool pool;
97  return &pool;
98 }
99 
105 class NodePool : public Pool<Node> {
106  public:
108  template<typename LitType>
109  std::shared_ptr<Literal> GetLiteral(LitType value) {
110  // Attempt to find and return an already existing literal
111  for (const auto &node : objects_) {
112  if (node->IsLiteral()) {
113  auto lit_node = std::dynamic_pointer_cast<Literal>(node);
114  if (lit_node->storage_type() == StorageTypeOf<LitType>()) {
115  auto raw_value = RawValueOf<LitType>(*lit_node);
116  if (raw_value == value) {
117  return lit_node;
118  }
119  }
120  }
121  }
122  // No literal found, make a new one.
123  std::shared_ptr<Literal> ret = Literal::Make(value);
124  Add(ret);
125  return ret;
126  }
127 };
128 
133  static NodePool pool;
134  return &pool;
135 }
136 
137 // Convenience functions for fast access to literals in the default node pool.
138 
140 inline Literal *rintl(int64_t i) {
141  return default_node_pool()->GetLiteral(i).get();
142 }
144 inline std::shared_ptr<Literal> intl(int64_t i) {
145  return default_node_pool()->GetLiteral(i);
146 }
147 
149 inline Literal *rstrl(std::string str) { return default_node_pool()->GetLiteral<std::string>(std::move(str)).get(); }
151 inline std::shared_ptr<Literal> strl(std::string str) {
152  return default_node_pool()->GetLiteral<std::string>(std::move(str));
153 }
154 
156 inline std::shared_ptr<Literal> booll(bool value) {
157  return default_node_pool()->GetLiteral<bool>(value);
158 }
159 
160 } // namespace cerata
cerata::booll
std::shared_ptr< Literal > booll(bool value)
Return a literal node representing a Boolean.
Definition: pool.h:156
cerata::default_type_pool
TypePool * default_type_pool()
Return a global default TypePool.
Definition: pool.h:89
cerata::Literal::Make
static std::shared_ptr< Literal > Make(bool value)
Create a boolean literal.
Definition: literal.h:80
cerata::NodePool
A pool of nodes.
Definition: pool.h:105
cerata::Literal
A Literal Node.
Definition: literal.h:36
cerata::rstrl
Literal * rstrl(std::string str)
Obtain a raw pointer to a string literal from the default node pool.
Definition: pool.h:149
cerata::Pool::Get
std::optional< T * > Get(const std::string &name)
Retrieve a component from the pool by name, if it exists. Returns empty option otherwise.
Definition: pool.h:52
cerata::ComponentPool
A pool of Components.
Definition: pool.h:86
cerata
Contains every Cerata class, function, etc...
Definition: api.h:41
cerata::NodePool::GetLiteral
std::shared_ptr< Literal > GetLiteral(LitType value)
Obtain a literal node of raw storage type LitType with some value.
Definition: pool.h:109
cerata::TypePool
A pool of Types.
Definition: pool.h:83
cerata::strl
std::shared_ptr< Literal > strl(std::string str)
Obtain a shared pointer to a string literal from the default node pool.
Definition: pool.h:151
cerata::intl
std::shared_ptr< Literal > intl(int64_t i)
Obtain a shared pointer to an integer literal from the default node pool.
Definition: pool.h:144
cerata::default_component_pool
ComponentPool * default_component_pool()
Return a global default component pool.
Definition: pool.h:95
cerata::Pool::objects_
std::vector< std::shared_ptr< T > > objects_
A list of objects that this pool owns.
Definition: pool.h:69
cerata::Pool
A pool to share ownership of objects.
Definition: pool.h:39
cerata::rintl
Literal * rintl(int64_t i)
Obtain a raw pointer to an integer literal from the default node pool.
Definition: pool.h:140
cerata::Pool::Add
void Add(const std::shared_ptr< T > &object)
Add an object to the pool, taking shared ownership. Object may not already exist in the pool.
Definition: pool.h:42
cerata::Pool::Clear
void Clear()
Release ownership of all components.
Definition: pool.h:63
cerata::default_node_pool
NodePool * default_node_pool()
Return a global default node pool that can store nodes without being owned by a graph.
Definition: pool.h:132