OdbDesignLib
OdbDesign ODB++ Parsing Library
Package.cpp
1 #include "Package.h"
2 #include <string>
3 #include "../ProtoBuf/package.pb.h"
4 #include "Pin.h"
5 #include <memory>
6 
7 namespace Odb::Lib::ProductModel
8 {
9  Package::Package(const std::string& name, unsigned int index)
10  : m_name(name)
11  , m_index(index)
12  {
13  }
14 
15  Package::~Package()
16  {
17  m_pins.clear();
18  m_pinsByName.clear();
19  }
20 
21  std::string Package::GetName() const
22  {
23  return m_name;
24  }
25 
26  const Pin::Vector& Package::GetPins() const
27  {
28  return m_pins;
29  }
30 
31  std::unique_ptr<Odb::Lib::Protobuf::ProductModel::Package> Package::to_protobuf() const
32  {
33  auto pPackageMsg = std::make_unique<Odb::Lib::Protobuf::ProductModel::Package>();
34  pPackageMsg->set_name(m_name);
35  pPackageMsg->set_index(m_index);
36  for (const auto& pPin : m_pins)
37  {
38  pPackageMsg->add_pins()->CopyFrom(*pPin->to_protobuf());
39  }
40  for (const auto& kvPin : m_pinsByName)
41  {
42  (*pPackageMsg->mutable_pinsbyname())[kvPin.first] = *kvPin.second->to_protobuf();
43  }
44  return pPackageMsg;
45  }
46 
47  void Package::from_protobuf(const Odb::Lib::Protobuf::ProductModel::Package& message)
48  {
49  m_name = message.name();
50  m_index = message.index();
51  for (const auto& pinMsg : message.pins())
52  {
53  auto pPin = std::make_shared<Pin>("", 0);
54  pPin->from_protobuf(pinMsg);
55  m_pins.push_back(pPin);
56  }
57  for (const auto& kvPinMsg : message.pinsbyname())
58  {
59  auto pPin = std::make_shared<Pin>("", 0);
60  pPin->from_protobuf(kvPinMsg.second);
61  m_pinsByName[kvPinMsg.first] = pPin;
62  }
63  }
64 
65  unsigned int Package::GetIndex() const
66  {
67  return m_index;
68  }
69 
70  void Package::AddPin(const std::string& name)
71  {
72  auto index = static_cast<unsigned int>(m_pins.size());
73  auto pPin = std::make_shared<Pin>(name, index);
74  m_pins.push_back(pPin);
75  m_pinsByName[pPin->GetName()] = pPin;
76  }
77 
78  std::shared_ptr<Pin> Package::GetPin(const std::string& name) const
79  {
80  auto findIt = m_pinsByName.find(name);
81  if (findIt != m_pinsByName.end())
82  {
83  return findIt->second;
84  }
85  return nullptr;
86  }
87 
88  std::shared_ptr<Pin> Package::GetPin(unsigned int index) const
89  {
90  if (index < m_pins.size())
91  {
92  return m_pins[index];
93  }
94  return nullptr;
95  }
96 
97  const Pin::StringMap& Package::GetPinsByName() const
98  {
99  return m_pinsByName;
100  }
101 
102 } // namespace Odb::Lib::ProductModel