Cerata
A library to generate structural hardware designs
stream.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/stream.h"
16 
17 #include <utility>
18 
19 #include "cerata/type.h"
20 
21 namespace cerata {
22 
23 Stream &Stream::SetElementType(std::shared_ptr<Type> type) {
24  // Invalidate mappers that point to this type from the other side
25  for (auto &mapper : mappers_) {
26  mapper->b()->RemoveMappersTo(this);
27  }
28  // Invalidate all mappers from this type
29  mappers_ = {};
30  // Set the new element type
31  fields_.back()->SetType(std::move(type));
32 
33  return *this;
34 }
35 
36 Stream::Stream(const std::string &name,
37  const std::string &data_name,
38  const std::shared_ptr<Type> &data_type,
39  const std::vector<std::shared_ptr<Field>> &control)
40  : Record(name) {
41  for (const auto &f : control) {
42  AddField(f);
43  }
44  AddField(field(data_name, data_type));
45 }
46 
47 std::shared_ptr<Type> Stream::valid() {
48  static auto result = bit("valid");
49  return result;
50 }
51 
52 std::shared_ptr<Type> Stream::ready() {
53  static auto result = bit("ready");
54  return result;
55 }
56 
57 std::shared_ptr<Stream> stream(const std::string &name,
58  const std::string &element_name,
59  const std::shared_ptr<Type> &element_type,
60  const std::vector<std::shared_ptr<Field>> &control) {
61  return std::make_shared<Stream>(name, element_name, element_type, control);
62 }
63 
64 std::shared_ptr<Stream> stream(const std::string &element_name, const std::shared_ptr<Type> &element_type) {
65  return stream(element_name + "_stream", element_name, element_type);
66 }
67 
68 std::shared_ptr<Stream> stream(const std::shared_ptr<Type> &element_type) {
69  return stream(element_type->name(), element_type);
70 }
71 
72 } // namespace cerata
cerata::Record::fields_
std::vector< std::shared_ptr< Field > > fields_
The fields of this Record.
Definition: type.h:339
cerata::field
std::shared_ptr< Field > field(const std::string &name, const std::shared_ptr< Type > &type, bool reverse, bool sep)
Create a new RecordField, and return a shared pointer to it.
Definition: type.cc:273
cerata::Stream::ready
static std::shared_ptr< Type > ready()
Return a 'ready' bit type.
Definition: stream.cc:52
cerata::Stream::SetElementType
Stream & SetElementType(std::shared_ptr< Type > type)
Set the element type of this stream.
Definition: stream.cc:23
cerata::Record
A Record type containing zero or more fields.
Definition: type.h:301
cerata
Contains every Cerata class, function, etc...
Definition: api.h:41
cerata::Type::RemoveMappersTo
int RemoveMappersTo(Type *other)
Remove all mappers to a specific type.
Definition: type.cc:148
cerata::Type::mappers_
std::vector< std::shared_ptr< TypeMapper > > mappers_
A list of mappers that can map this type to another type.
Definition: type.h:167
cerata::Stream::valid
static std::shared_ptr< Type > valid()
Return a 'valid' bit type.
Definition: stream.cc:47
cerata::Stream
A Stream type.
Definition: stream.h:26
cerata::stream
std::shared_ptr< Stream > stream(const std::string &name, const std::string &element_name, const std::shared_ptr< Type > &element_type, const std::vector< std::shared_ptr< Field >> &control)
Construct a new Stream type and return a shared pointer to it.
Definition: stream.cc:57
cerata::Record::AddField
Record & AddField(const std::shared_ptr< Field > &field, std::optional< size_t > index=std::nullopt)
Add a field to this Record.
Definition: type.cc:286
cerata::Stream::Stream
Stream(const std::string &name, const std::string &element_name, const std::shared_ptr< Type > &element_type, const std::vector< std::shared_ptr< Field >> &control)
Stream constructor.
Definition: stream.cc:36
cerata::bit
std::shared_ptr< Type > bit(const std::string &name)
Return a static bit type.
Definition: type.cc:240