codac 2.0.0
Loading...
Searching...
No Matches
codac2_Collection.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <type_traits>
13#include <cassert>
14#include <vector>
15#include <memory>
16
17namespace codac2
18{
19 template<typename T>
20 class Collection : public std::vector<std::shared_ptr<T>>
21 {
22 public:
23
24 Collection()
25 { }
26
27 template<typename... T_>
28 requires ((std::is_base_of_v<T,T_> // T_ should be some T class
29 && !std::is_same_v<Collection<T>,T_>) && ...)
30 Collection(const T_&... x)
31 {
32 (push_object_back(x), ...);
33 }
34
35 template<typename... T_>
36 requires (std::is_same_v<std::shared_ptr<T>,std::shared_ptr<T_>> && ...)
37 Collection(const std::shared_ptr<T_>&... x)
38 : std::vector<std::shared_ptr<T>>({x...})
39 { }
40
41 Collection(const Collection<T>& c)
42 : std::vector<std::shared_ptr<T>>(c.size())
43 {
44 for(size_t i = 0 ; i < c.size() ; i++)
45 (*this)[i] = std::dynamic_pointer_cast<T>(c[i]->copy());
46 }
47
48 template<typename T_>
49 requires std::is_base_of_v<T,T_>
50 void push_object_back(const T_& x)
51 {
52 this->push_back(std::make_shared<T_>(x));
53 }
54 };
55}