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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
#include <iostream>
#include <memory>
#include <map>
#include <typeinfo>
#include <typeindex>
#include <string>
struct Base
{
Base()
{
}
Base Clone() const
{
Base b;
b = *this;
return b;
}
Base &operator=(Base const&d)
{
this->~Base();
d.ConstructInPlace(this);
data = d.data;
return *this;
}
Base &operator=(Base &&d)
{
Base &&t = Clone();
this->~Base();
d.ConstructInPlace(this);
data = std::move(d.data);
d.~Base();
t.ConstructInPlace(&d);
d.data = std::move(t.data);
return *this;
}
virtual ~Base()
{
}
virtual std::ostream &Print(std::ostream &os)
{
return os << "I'm just a Base";
}
Base(Base const &) = default;
Base(Base &&) = default;
protected:
template<typename Class, typename T>
T *&Data()
{
return reinterpret_cast<T *&>(data[typeid(Class)]);
}
private:
std::map<std::type_index, void *> data;
virtual void ConstructInPlace(Base *p) const
{
new (p) Base;
}
};
struct Derived1 : Base
{
Derived1();
Derived1(std::string const&);
virtual ~Derived1();
virtual std::ostream &Print(std::ostream &);
Derived1(Derived1 const &) = default;
Derived1(Derived1 &&) = default;
private:
struct Impl;
virtual void ConstructInPlace(Base *p) const
{
new (p) Derived1;
}
};
struct Derived2 : Derived1
{
Derived2();
Derived2(std::string const&);
Derived2(std::string const&, double);
virtual ~Derived2();
virtual std::ostream &Print(std::ostream &);
Derived2(Derived2 const &) = default;
Derived2(Derived2 &&) = default;
private:
struct Impl;
virtual void ConstructInPlace(Base *p) const
{
new (p) Derived2;
}
};
int main()
{
Base ba, bb;
Derived1 d1 ("heart attack");
Derived2 d2 ("*facepalm*", 1.23456789);
auto display = [&]
{
ba.Print(std::cout << "ba: ") << std::endl;
bb.Print(std::cout << "bb: ") << std::endl;
d1.Print(std::cout << "d1: ") << std::endl;
d2.Print(std::cout << "d2: ") << std::endl;
};
display();
ba = d1;
bb = d2;
std::swap<Base>(d1, d2);
display();
{
Base b;
reinterpret_cast<Base &>(d1) = b;
reinterpret_cast<Base &>(d2) = b;
}
display();
}
struct Derived1::Impl
{
std::string s;
Impl(std::string const&s) : s(s) {}
};
Derived1::Derived1() : Base()
{
Data<Derived1, Impl>() = new Impl("default");
}
Derived1::Derived1(std::string const&s) : Base()
{
Data<Derived1, Impl>() = new Impl(s);
}
Derived1::~Derived1()
{
delete Data<Derived1, Impl>();
}
std::ostream &Derived1::Print(std::ostream &os)
{
os << "I'm a Derived1, s = \"" << Data<Derived1, Impl>()->s << "\", dad says ";
return Base::Print(os);
}
struct Derived2::Impl
{
double d;
Impl(double d) : d(d) {}
};
Derived2::Derived2() : Derived1("from Derived2")
{
Data<Derived2, Impl>() = new Impl(7.42);
}
Derived2::Derived2(std::string const&s) : Derived1(s)
{
Data<Derived2, Impl>() = new Impl(42.7);
}
Derived2::Derived2(std::string const&s, double d) : Derived1(s)
{
Data<Derived2, Impl>() = new Impl(d);
}
Derived2::~Derived2()
{
delete Data<Derived2, Impl>();
}
std::ostream &Derived2::Print(std::ostream &os)
{
os << "I'm a Derived2, d = " << Data<Derived2, Impl>()->d << ", mom says ";
return Derived1::Print(os);
}
|