diff options
author | Matthew Newhook <matthew@zeroc.com> | 2009-10-29 21:04:30 -0230 |
---|---|---|
committer | Matthew Newhook <matthew@zeroc.com> | 2009-10-29 21:04:30 -0230 |
commit | 41fb1a80471e9887adf5a9ddf8fc385ac2689a98 (patch) | |
tree | 9a90a9bb56322b07a4d2906b0b69d6c1b8a94b98 /cpp/src/Slice/PythonUtil.cpp | |
parent | http://bugzilla/bugzilla/show_bug.cgi?id=4342 - bug 4342 (diff) | |
download | ice-41fb1a80471e9887adf5a9ddf8fc385ac2689a98.tar.bz2 ice-41fb1a80471e9887adf5a9ddf8fc385ac2689a98.tar.xz ice-41fb1a80471e9887adf5a9ddf8fc385ac2689a98.zip |
http://bugzilla/bugzilla/show_bug.cgi?id=4343 - 4343
- replaced __cmp__ with rich operators.
- problem with __str__.
Diffstat (limited to 'cpp/src/Slice/PythonUtil.cpp')
-rw-r--r-- | cpp/src/Slice/PythonUtil.cpp | 58 |
1 files changed, 32 insertions, 26 deletions
diff --git a/cpp/src/Slice/PythonUtil.cpp b/cpp/src/Slice/PythonUtil.cpp index 905cd2769b4..b9485ad5c47 100644 --- a/cpp/src/Slice/PythonUtil.cpp +++ b/cpp/src/Slice/PythonUtil.cpp @@ -1386,40 +1386,46 @@ Slice::Python::CodeVisitor::visitEnum(const EnumPtr& p) } _out << nl << "self.value = val"; _out.dec(); + _out << sp << nl << "def __str__(self):"; _out.inc(); - _out << nl << "return _names(self.value)"; -#if 0 - for(q = enums.begin(), i = 0; q != enums.end(); ++q, ++i) - { - _out << nl; - if(q == enums.begin()) - { - _out << "if"; - } - else - { - _out << "elif"; - } - ostringstream idx; - idx << i; - _out << " self.value == " << idx.str() << ':'; - _out.inc(); - _out << nl << "return '" << (*q)->name() << "'"; - _out.dec(); - } - _out << nl << "return None"; -#endif + _out << nl << "return self._names[self.value]"; _out.dec(); _out << sp << nl << "__repr__ = __str__"; _out << sp << nl << "def __hash__(self):"; _out.inc(); _out << nl << "return self.value"; _out.dec(); - _out << sp << nl << "def __cmp__(self, other):"; - _out.inc(); - _out << nl << "return cmp(self.value, other.value)"; - _out.dec(); + + // + // Rich operators. __lt__, __le__, __eq__, __ne__, __gt__, __ge__ + // + static const char* richOps[] = { + "__lt__", "<", + "__le__", "<=", + "__eq__", "==", + "__ne__", "!=", + "__gt__", ">", + "__ge__", ">=" + }; + for(int opIndex = 0; opIndex != sizeof(richOps)/sizeof(richOps[0]); opIndex += 2) + { + const char* opName = richOps[opIndex]; + const char* opSymbol = richOps[opIndex+1]; + + _out << sp << nl << "def " << opName << "(self, other):"; + _out.inc(); + _out << nl << "if isinstance(other, _M_" << abs << "):"; + _out.inc(); + _out << nl << "return self.value " << opSymbol << " other.value;"; + _out.dec(); + _out << nl << "elif other == None:"; + _out.inc(); + _out << nl << "return False"; + _out.dec(); + _out << nl << "return NotImplemented"; + _out.dec(); + } _out << sp << nl << "_names = ("; for(q = enums.begin(), i = 0; q != enums.end(); ++q, ++i) |