Chaste Commit::1fd4e48e3990e67db148bc1bc4cf6991a0049d0c
Identifiable.cpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19 * Neither the name of the University of Oxford nor the names of its
20 contributors may be used to endorse or promote products derived from this
21 software without specific prior written permission.
22
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36#include "Identifiable.hpp"
37
38#include <algorithm>
39#include <typeinfo>
40
41#include <boost/serialization/extended_type_info.hpp>
42#include <boost/serialization/extended_type_info_typeid.hpp>
43#include <boost/serialization/extended_type_info_no_rtti.hpp>
44#include <boost/serialization/type_info_implementation.hpp>
45#include "Warnings.hpp"
46
47std::string Identifiable::TidyTemplatedExportIdentifier(std::string identifier) const
48{
49 // First remove spaces, so identifier now takes the form "pack<void(NameOfDerivedType<DIM>)>::type"
50 std::string::iterator end_pos = std::remove(identifier.begin(), identifier.end(), ' ');
51 identifier.erase(end_pos, identifier.end());
52
53 // Then remove "pack<void(", so identifier now takes the form "NameOfDerivedType<DIM>)>::type"
54 const std::string s_pack = "pack<void(";
55 std::string::size_type i = identifier.find(s_pack);
56 if (i != identifier.npos)
57 {
58 identifier.erase(i, s_pack.length());
59 }
60
61 // Then replace "<" with "-", so identifier now takes the form "NameOfDerivedType-DIM>)>::type"
62 const std::string s_open = "<";
63 const std::string s_dash = "-";
64 i = identifier.find(s_open);
65 if (i != identifier.npos)
66 {
67 identifier.replace(i, s_open.length(), s_dash);
68 }
69
70 // Then replace "," with "-" to account for multiple template parameters
71 const std::string s_comma = ",";
72 i = identifier.find(s_comma);
73 while (i != identifier.npos)
74 {
75 identifier.replace(i, s_comma.length(), s_dash);
76 i = identifier.find(s_comma, i);
77 }
78
79 // Finally remove ">)>::type", so that identifier now takes the form "NameOfDerivedType-DIM"
80 const std::string s_end = ">)>::type";
81 i = identifier.find(s_end);
82 if (i != identifier.npos)
83 {
84 identifier.erase(i, s_end.length());
85 }
86
87 return identifier;
88}
89
93
94std::string Identifiable::GetIdentifier() const
95{
96 std::string id;
97#if BOOST_VERSION >= 103700
98 const boost::serialization::extended_type_info* p_type_info =
99 boost::serialization::type_info_implementation<Identifiable>::type::get_const_instance().get_derived_extended_type_info(*this);
100 if(p_type_info!=nullptr)
101 {
102 id = p_type_info->get_key();
103 }
104 else
105 {
106 WARN_ONCE_ONLY("Unable to determine the class type. If you are using C++ serialization may not be set up correctly. \n If you are using Python this behaviour is expected.");
107 id = "UnknownClass-ReflectionFailed";
108 }
109#else
110 id = boost::serialization::type_info_implementation<Identifiable>::type::get_derived_extended_type_info(*this)->get_key();
111#endif
113}
std::string TidyTemplatedExportIdentifier(std::string identifier) const
virtual ~Identifiable()
std::string GetIdentifier() const