Samchon Framework for CPP  1.0.0
Wrapper.hpp
1 #pragma once
2 #include <samchon/examples/packer/ProductArray.hpp>
3 #include <samchon/examples/packer/Instance.hpp>
4 
5 namespace samchon
6 {
7 namespace examples
8 {
9 namespace packer
10 {
11  using namespace std;
12 
13  using namespace library;
14  using namespace protocol;
15 
27  class Wrapper
28  : public ProductArray,
29  public Instance
30  {
31  private:
32  typedef ProductArray super;
33 
34  public:
35  /* ---------------------------------------------------------
36  CONSTRUCTORS
37  --------------------------------------------------------- */
42  : super(),
43  Instance()
44  {
45  };
46 
55  Wrapper(const string &name, int price, int volume, int weight)
56  : super(),
57  Instance(name, price, volume, weight)
58  {
59  };
60 
66  Wrapper(const Wrapper &wrapper)
67  : super(),
68  Instance(wrapper)
69  {
70  };
71 
72  virtual ~Wrapper() = default;
73 
74  virtual void construct(shared_ptr<XML> xml) override
75  {
76  super::construct(xml);
77  Instance::construct(xml);
78  };
79 
80  public:
91  auto tryInsert(shared_ptr<Product> product) -> bool
92  {
93  int volume = 0;
94  int weight = 0;
95 
96  for (size_t i = 0; i < size(); i++)
97  {
98  volume += at(i)->getVolume();
99  weight += at(i)->getWeight();
100  }
101 
102  if (product->getVolume() + volume > this->volume ||
103  product->getWeight() + weight > this->weight)
104  {
105  return false;
106  }
107 
108  push_back(product);
109  return true;
110  };
111 
112  /* ---------------------------------------------------------
113  EXPORTERS
114  --------------------------------------------------------- */
115  virtual auto TAG() const -> string override
116  {
117  return "wrapper";
118  };
119 
120  virtual auto toXML() const -> shared_ptr<XML> override
121  {
122  shared_ptr<XML> &xml = super::toXML();
123  xml->insertAllProperties(Instance::toXML());
124 
125  return xml;
126  };
127 
134  virtual auto toString() const -> string override
135  {
136  string str = "\tWrapper " + Instance::toString() + "\n";
137  for (size_t i = 0; i < size(); i++)
138  str += "\t\t" + at(i)->toString()
139  + ((i == size() - 1) ? "" : "\n");
140 
141  return str;
142  };
143  };
144 };
145 };
146 };
virtual auto toString() const -> string override
Return a string represent the wrapper.
Definition: Wrapper.hpp:134
auto tryInsert(shared_ptr< Product > product) -> bool
Try to insert a product into the wrapper.
Definition: Wrapper.hpp:91
Wrapper()
Default Constructor.
Definition: Wrapper.hpp:41
Wrapper(const Wrapper &wrapper)
Copy Constructor.
Definition: Wrapper.hpp:66
An array of Product objects.
Wrapper(const string &name, int price, int volume, int weight)
Construct from argument of a wrapper.
Definition: Wrapper.hpp:55
virtual auto toString() const -> string
Return a string represents the Instance.
Definition: Instance.hpp:147
A wrapper can contain products.
Definition: Wrapper.hpp:27