Cerata
A library to generate structural hardware designs
literal.cc
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 #include "cerata/literal.h"
16 #include "cerata/pool.h"
17 
18 namespace cerata {
19 
20 std::string Literal::ToString() const {
21  if (storage_type_ == StorageType::BOOL) {
22  return Bool_val_ ? "true" : "false";
23  } else if (storage_type_ == StorageType::STRING) {
24  return String_val_;
25  } else {
26  return std::to_string(Int_val_);
27  }
28 }
29 
30 #ifndef LITERAL_IMPL_FACTORY
31 #define LITERAL_IMPL_FACTORY(NAME, BASETYPE, IDNAME, TYPENAME) \
32  Literal::Literal(std::string name, const std::shared_ptr<Type> &type, TYPENAME value) \
33  : MultiOutputNode(std::move(name), Node::NodeID::LITERAL, type), \
34  storage_type_(StorageType::IDNAME), \
35  NAME##_val_(std::move(value)) {} \
36  \
37  std::shared_ptr<Literal> Literal::Make##NAME(TYPENAME value) { \
38  std::stringstream str; \
39  str << #NAME << "_" << value; \
40  auto ret = std::make_shared<Literal>(str.str(), BASETYPE(), value); \
41  return ret; \
42 }
43 #endif
44 
45 LITERAL_IMPL_FACTORY(String, string, STRING, std::string)
46 LITERAL_IMPL_FACTORY(Bool, boolean, BOOL, bool)
47 LITERAL_IMPL_FACTORY(Int, integer, INT, int64_t)
48 
49 std::shared_ptr<Object> Literal::Copy() const {
50  switch (storage_type_) {
51  default:return strl(String_val_);
52  case StorageType::BOOL:return booll(Bool_val_);
53  case StorageType::INT:return intl(Int_val_);
54  }
55 }
56 
57 void Literal::SetParent(Graph *graph) {
58  parent_ = std::nullopt;
59 }
60 
61 } // namespace cerata
cerata::booll
std::shared_ptr< Literal > booll(bool value)
Return a literal node representing a Boolean.
Definition: pool.h:156
cerata::Literal::SetParent
void SetParent(Graph *graph) override
Literal nodes are only owned by the literal pool, hence never have a parent graph.
Definition: literal.cc:57
cerata::Literal
A Literal Node.
Definition: literal.h:36
cerata::Graph
A graph representing a hardware structure.
Definition: graph.h:37
cerata
Contains every Cerata class, function, etc...
Definition: api.h:41
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::Literal::ToString
std::string ToString() const override
Convert the Literal value to a human-readable string.
Definition: literal.cc:20
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::Object
A Cerata Object on a graph.
Definition: object.h:34
cerata::integer
std::shared_ptr< Type > integer()
Return a static integer type.
Definition: type.cc:256
cerata::Literal::storage_type_
StorageType storage_type_
The raw storage type of the literal node.
Definition: literal.h:55
cerata::Object::parent_
std::optional< Graph * > parent_
An optional parent Graph to which this Object belongs. Initially no value.
Definition: object.h:73
cerata::String
String type.
Definition: type.h:212