Skip to content

Commit

Permalink
App: fixes #7102: Undo/Redo of PropertyEnumeration fails with MSVC
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Jun 29, 2022
1 parent e07cfa0 commit 9a70b16
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 53 deletions.
30 changes: 15 additions & 15 deletions src/App/Enumeration.cpp
Expand Up @@ -33,7 +33,7 @@
using namespace App;

namespace {
struct StringCopy {
struct StringCopy : public Enumeration::Object {
StringCopy(const char* str) : d(str) {
}
const char* data() const {
Expand All @@ -50,7 +50,7 @@ struct StringCopy {
std::string d;
};

struct StringView {
struct StringView : public Enumeration::Object {
StringView(const char* str) : d(str) {
}
const char* data() const {
Expand Down Expand Up @@ -82,15 +82,15 @@ Enumeration::Enumeration(const Enumeration &other)
Enumeration::Enumeration(const char *valStr)
: _index(0)
{
enumArray.push_back(StringCopy(valStr));
enumArray.push_back(std::make_shared<StringCopy>(valStr));
setValue(valStr);
}

Enumeration::Enumeration(const char **list, const char *valStr)
: _index(0)
{
while (list && *list) {
enumArray.push_back(StringView(*list));
enumArray.push_back(std::make_shared < StringView>(*list));
list++;
}
setValue(valStr);
Expand All @@ -113,7 +113,7 @@ void Enumeration::setEnums(const char **plEnums)

enumArray.clear();
while (plEnums && *plEnums) {
enumArray.push_back(StringView(*plEnums));
enumArray.push_back(std::make_shared<StringView>(*plEnums));
plEnums++;
}

Expand Down Expand Up @@ -142,7 +142,7 @@ void Enumeration::setEnums(const std::vector<std::string> &values)

enumArray.clear();
for (std::vector<std::string>::const_iterator it = values.begin(); it != values.end(); ++it) {
enumArray.push_back(StringCopy(it->c_str()));
enumArray.push_back(std::make_shared<StringCopy>(it->c_str()));
}

// set _index
Expand All @@ -157,7 +157,7 @@ void Enumeration::setValue(const char *value)
{
_index = 0;
for (std::size_t i = 0; i < enumArray.size(); i++) {
if (enumArray[i].isEqual(value)) {
if (enumArray[i]->isEqual(value)) {
_index = static_cast<int>(i);
break;
}
Expand All @@ -184,7 +184,7 @@ bool Enumeration::isValue(const char *value) const
if (i == -1) {
return false;
} else {
return enumArray[i].isEqual(value);
return enumArray[i]->isEqual(value);
}
}

Expand All @@ -195,7 +195,7 @@ bool Enumeration::contains(const char *value) const
}

for (const auto& it : enumArray) {
if (it.isEqual(value))
if (it->isEqual(value))
return true;
}

Expand All @@ -208,7 +208,7 @@ const char * Enumeration::getCStr() const
return nullptr;
}

return enumArray[_index].data();
return enumArray[_index]->data();
}

int Enumeration::getInt() const
Expand All @@ -224,7 +224,7 @@ std::vector<std::string> Enumeration::getEnumVector() const
{
std::vector<std::string> list;
for (const auto& it : enumArray)
list.emplace_back(it.data());
list.emplace_back(it->data());
return list;
}

Expand All @@ -249,7 +249,7 @@ int Enumeration::maxValue() const
bool Enumeration::isCustom() const
{
for (const auto& it : enumArray) {
if (it.isCustom())
if (it->isCustom())
return true;
}
return false;
Expand All @@ -272,11 +272,11 @@ bool Enumeration::operator==(const Enumeration &other) const
return false;
}
for (size_t i = 0; i < enumArray.size(); ++i) {
if (enumArray[i].data() == other.enumArray[i].data())
if (enumArray[i]->data() == other.enumArray[i]->data())
continue;
if (enumArray[i].data() == nullptr || other.enumArray[i].data() == nullptr)
if (enumArray[i]->data() == nullptr || other.enumArray[i]->data() == nullptr)
return false;
if (!enumArray[i].isEqual(other.enumArray[i].data()))
if (!enumArray[i]->isEqual(other.enumArray[i]->data()))
return false;
}
return true;
Expand Down
45 changes: 7 additions & 38 deletions src/App/Enumeration.h
Expand Up @@ -51,45 +51,13 @@ namespace App
*/
class AppExport Enumeration
{
protected:
public:
class Object {
public:
template <typename T>
Object(T&& obj): object(std::make_shared<Model<T>>(std::forward<T>(obj))){}
const char* data() const {
return object->data();
}
bool isEqual(const char* str) const {
return object->isEqual(str);
}
bool isCustom() const {
return object->isCustom();
}

struct Concept {
virtual ~Concept() {}
virtual const char* data() const = 0;
virtual bool isEqual(const char*) const = 0;
virtual bool isCustom() const = 0;
};

template< typename T >
struct Model : Concept {
Model(const T& t) : object(t) {}
const char* data() const override {
return object.data();
}
bool isEqual(const char* str) const override {
return object.isEqual(str);
}
bool isCustom() const override {
return object.isCustom();
}
private:
T object;
};

std::shared_ptr<Concept> object;
virtual ~Object() {}
virtual const char* data() const = 0;
virtual bool isEqual(const char*) const = 0;
virtual bool isCustom() const = 0;
};

public:
Expand Down Expand Up @@ -207,7 +175,8 @@ namespace App

private:
/// Handle to C Strings of possible enumeration values
std::vector<Object> enumArray;
using ObjectPtr = std::shared_ptr<Object>;
std::vector<ObjectPtr> enumArray;

/// Integer value of the enumeration
/*!
Expand Down

0 comments on commit 9a70b16

Please sign in to comment.