summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2023-09-03 20:09:44 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2023-09-03 20:12:53 +0100
commit476de9026a2b50b911181a5c0aab81a653c94464 (patch)
treeca8671fe37e75f1c36ac889fea8f807c2d637e00
parentReplace ifndef/define/endif with pragma once (diff)
downloadslicer-476de9026a2b50b911181a5c0aab81a653c94464.tar.bz2
slicer-476de9026a2b50b911181a5c0aab81a653c94464.tar.xz
slicer-476de9026a2b50b911181a5c0aab81a653c94464.zip
Simplify className and typeName setup with string_views
Replaces the pointer to string and initialisers function complication.
-rw-r--r--slicer/slicer/modelPartsTypes.cpp17
-rw-r--r--slicer/slicer/modelPartsTypes.h15
-rw-r--r--slicer/slicer/modelPartsTypes.impl.h18
-rw-r--r--slicer/tool/parser.cpp16
4 files changed, 25 insertions, 41 deletions
diff --git a/slicer/slicer/modelPartsTypes.cpp b/slicer/slicer/modelPartsTypes.cpp
index df281b2..dcfd338 100644
--- a/slicer/slicer/modelPartsTypes.cpp
+++ b/slicer/slicer/modelPartsTypes.cpp
@@ -28,11 +28,11 @@ namespace Ice {
namespace Slicer {
using ClassRefMap = std::map<std::string, ClassRef, std::less<>>;
- using ClassNamePair = std::pair<std::string, std::string>;
+ using ClassNamePair = std::pair<std::string_view, std::string>;
using ClassNameMap = boost::multi_index_container<ClassNamePair,
boost::multi_index::indexed_by<
boost::multi_index::ordered_unique<
- boost::multi_index::member<ClassNamePair, const std::string, &ClassNamePair::first>,
+ boost::multi_index::member<ClassNamePair, const std::string_view, &ClassNamePair::first>,
std::less<>>,
boost::multi_index::ordered_unique<
boost::multi_index::member<ClassNamePair, const std::string, &ClassNamePair::second>,
@@ -52,7 +52,7 @@ namespace Slicer {
}
}
- const std::string &
+ std::string_view
ModelPartForComplexBase::ToModelTypeName(const std::string & name)
{
const auto & right = names->get<1>();
@@ -196,7 +196,7 @@ namespace Slicer {
void
ModelPartForComplexBase::registerClass(
- const std::string & className, const std::string * typeName, const ClassRef & cr)
+ const std::string_view className, const std::optional<std::string_view> typeName, const ClassRef & cr)
{
refs->emplace(className, cr);
if (typeName) {
@@ -205,9 +205,12 @@ namespace Slicer {
}
void
- ModelPartForComplexBase::unregisterClass(const std::string & className, const std::string * typeName)
+ ModelPartForComplexBase::unregisterClass(
+ const std::string_view className, const std::optional<std::string_view> typeName)
{
- refs->erase(className);
+ if (const auto i = refs->find(className); i != refs->end()) {
+ refs->erase(i);
+ }
if (typeName) {
names->get<0>().erase(className);
}
@@ -223,7 +226,7 @@ namespace Slicer {
}
TypeId
- ModelPartForComplexBase::getTypeId(const std::string & id, const std::string & className)
+ ModelPartForComplexBase::getTypeId(const std::string & id, const std::string_view className)
{
return (id == className) ? TypeId() : ToExchangeTypeName(id);
}
diff --git a/slicer/slicer/modelPartsTypes.h b/slicer/slicer/modelPartsTypes.h
index b1e28c5..0403650 100644
--- a/slicer/slicer/modelPartsTypes.h
+++ b/slicer/slicer/modelPartsTypes.h
@@ -139,13 +139,14 @@ namespace Slicer {
protected:
void onSubclass(const std::string & name, void * m, const ModelPartHandler &);
- static void registerClass(const std::string & className, const std::string * typeName, const ClassRef &);
- static void unregisterClass(const std::string & className, const std::string * typeName);
- static TypeId getTypeId(const std::string & id, const std::string & className);
+ static void registerClass(
+ const std::string_view className, const std::optional<std::string_view> typeName, const ClassRef &);
+ static void unregisterClass(const std::string_view className, const std::optional<std::string_view> typeName);
+ static TypeId getTypeId(const std::string & id, const std::string_view className);
static std::string demangle(const char * mangled);
static const std::string & ToExchangeTypeName(const std::string &);
- static const std::string & ToModelTypeName(const std::string &);
+ static std::string_view ToModelTypeName(const std::string &);
};
template<typename T> class Hooks;
@@ -192,14 +193,12 @@ namespace Slicer {
[[nodiscard]] std::optional<std::string> GetTypeIdProperty() const override;
static const std::string typeIdProperty;
- static const std::string * className;
- static const std::string * typeName;
+ constinit static const std::string_view className;
+ constinit static const std::optional<const std::string_view> typeName;
static void CreateModelPart(void *, const ModelPartHandler &);
private:
- static void initClassName();
- static void deleteClassName();
static void registerClass() __attribute__((constructor(210)));
static void unregisterClass() __attribute__((destructor(210)));
};
diff --git a/slicer/slicer/modelPartsTypes.impl.h b/slicer/slicer/modelPartsTypes.impl.h
index 03ac8c8..718f654 100644
--- a/slicer/slicer/modelPartsTypes.impl.h
+++ b/slicer/slicer/modelPartsTypes.impl.h
@@ -502,27 +502,16 @@ namespace Slicer {
template<typename T>
void
- ModelPartForClass<T>::deleteClassName()
- {
- delete className;
- delete typeName;
- }
-
- template<typename T>
- void
ModelPartForClass<T>::registerClass()
{
- initClassName();
- ModelPartForComplexBase::registerClass(*className, typeName, &ModelPartForClass<T>::CreateModelPart);
+ ModelPartForComplexBase::registerClass(className, typeName, &ModelPartForClass<T>::CreateModelPart);
}
template<typename T>
void
ModelPartForClass<T>::unregisterClass()
{
- BOOST_ASSERT(className);
- ModelPartForComplexBase::unregisterClass(*className, typeName);
- deleteClassName();
+ ModelPartForComplexBase::unregisterClass(className, typeName);
}
template<typename T>
@@ -530,7 +519,6 @@ namespace Slicer {
ModelPartForClass<T>::GetTypeId() const
{
BOOST_ASSERT(this->Model);
- BOOST_ASSERT(className);
return ModelPartForComplexBase::getTypeId(
[this]() {
if constexpr (std::is_base_of_v<Ice::Object, T>) {
@@ -540,7 +528,7 @@ namespace Slicer {
return ModelPartForComplexBase::demangle(typeid(*this->Model->get()).name());
}
}(),
- *className);
+ className);
}
// ModelPartForStruct
diff --git a/slicer/tool/parser.cpp b/slicer/tool/parser.cpp
index 55a06fe..e40b2b7 100644
--- a/slicer/tool/parser.cpp
+++ b/slicer/tool/parser.cpp
@@ -418,20 +418,14 @@ namespace Slicer {
auto typeName = md.value("slicer:typename:");
fprintbf(cpp, "template<>\n");
- fprintbf(cpp, "const std::string * ModelPartForClass< %s >::className = nullptr;\n", decl->typeId());
+ fprintbf(cpp, "const std::string_view ModelPartForClass< %s >::className{\"%s\"};\n", decl->typeId(),
+ c->scoped());
fprintbf(cpp, "template<>\n");
- fprintbf(cpp, "const std::string * ModelPartForClass< %s >::typeName = nullptr;\n", decl->typeId());
- fprintbf(cpp,
- "template<>\nvoid ModelPartForClass< %s >::initClassName() {\n\tclassName = new "
- "std::string(\"%s\");\n\t",
- decl->typeId(), c->scoped());
+ fprintbf(cpp, "const std::optional<const std::string_view> ModelPartForClass< %s >::typeName{", decl->typeId());
if (typeName) {
- fprintbf(cpp, "typeName = new std::string(\"%s\");", *typeName);
- }
- else {
- fprintbf(cpp, "typeName = nullptr;");
+ fprintbf(cpp, "\"%s\"", *typeName);
}
- fprintbf(cpp, "\n}\n");
+ fprintbf(cpp, "};\n");
defineGetMetadata(md, c, "ModelPartForComplex");