1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| #include <algorithm> #include <atomic> #include <cassert> #include <iostream> #include <map> #include <mutex> #include <vector>
class Container { public: Container() { std::lock_guard<std::mutex> lock(mut); auto it = uuid.load(); uuid.store(++it); serial_number_ = it; container_map.insert({ serial_number_, this }); }
virtual int get_fluid_capacity() = 0;
friend int totalFluidCapacity() { std::lock_guard<std::mutex> lock(mut); int result = 0; for (auto& it : container_map) { result += it.second->get_fluid_capacity(); } return result; }
friend Container* find_container(size_t serial_number) { std::lock_guard<std::mutex> lock(mut); if(container_map.find(serial_number) != container_map.end()) { return container_map[serial_number]; } else { return nullptr; } } inline static std::mutex mut; inline static std::atomic<size_t> uuid; inline static std::map<size_t, Container*> container_map;
protected: size_t serial_number_; };
int totalFluidCapacity(); Container* find_container(size_t serial_number);
class Buckets : public Container { static constexpr double pi = 3.1415926;
public: Buckets(int height, int radius) : height_(height), radius_(radius) {} virtual int get_fluid_capacity() override final { return static_cast<int>(radius_ * radius_ * height_ * pi); }
private: int height_; int radius_; };
class Boxes : public Container { public: enum class material_type : int { m = 0, c = 1, };
Boxes(const int length, const int width, const int height, material_type type) : length_(length), width_(width), height_(height), type_(type) {} virtual int get_fluid_capacity() override final { if (type_ == material_type::c) { return length_ * width_ * height_; } else { return 0; } }
private: int length_; int width_; int height_; material_type type_; };
int main() { Boxes box_c(1, 1, 1, Boxes::material_type::c); Boxes box_m(1, 1, 1, Boxes::material_type::m);
Buckets bucket(1, 1);
auto capa = totalFluidCapacity(); assert(capa == 4);
auto p_container = find_container(2); assert(p_container == & box_m); return 0; }
|