1 | /*
|
---|
2 | * Copyright (c) 2018 Jaroslav Jindrak
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <cstring>
|
---|
30 | #include <typeinfo>
|
---|
31 |
|
---|
32 | namespace std
|
---|
33 | {
|
---|
34 |
|
---|
35 | type_info::~type_info()
|
---|
36 | { /* DUMMY BODY */ }
|
---|
37 |
|
---|
38 | bool type_info::operator==(const type_info& other) const noexcept
|
---|
39 | {
|
---|
40 | return (this == &other) || ::strcmp(name(), other.name()) == 0;
|
---|
41 | }
|
---|
42 |
|
---|
43 | bool type_info::operator!=(const type_info& other) const noexcept
|
---|
44 | {
|
---|
45 | return !(*this == other);
|
---|
46 | }
|
---|
47 |
|
---|
48 | bool type_info::before(const type_info& other) const noexcept
|
---|
49 | {
|
---|
50 | /**
|
---|
51 | * cppreference.com:
|
---|
52 | * Returns true if the type of this type_info precedes the type
|
---|
53 | * of rhs in the implementation's collation order. No guarantees
|
---|
54 | * are given; in particular, the collation order can change
|
---|
55 | * between the invocations of the same program.
|
---|
56 | *
|
---|
57 | * Seems completely arbitrary and the only guarantee
|
---|
58 | * we have to provide that two comparisons of two same
|
---|
59 | * type_info instances return the same result.
|
---|
60 | */
|
---|
61 | return name() < other.name();
|
---|
62 | }
|
---|
63 |
|
---|
64 | size_t type_info::hash_code() const noexcept
|
---|
65 | {
|
---|
66 | /**
|
---|
67 | * We only need for the hash codes of two type_info
|
---|
68 | * instances describing the same type to match, nothing
|
---|
69 | * else.
|
---|
70 | */
|
---|
71 | size_t res{};
|
---|
72 | const char* str = name();
|
---|
73 | while(*str)
|
---|
74 | res += static_cast<size_t>(*str++);
|
---|
75 |
|
---|
76 | return res;
|
---|
77 | }
|
---|
78 |
|
---|
79 | const char* type_info::name() const noexcept
|
---|
80 | {
|
---|
81 | return __name;
|
---|
82 | }
|
---|
83 | }
|
---|