Permalink
Browse files

Made const char* conversion a typemap

Made const char* conversion a typemap instead of an implicit conversion
  • Loading branch information...
1 parent 8805bd6 commit 6ea30c79f0e20d3082052bbcfd4dabc54c8ccee7 @Leont committed Jun 15, 2011
Showing with 15 additions and 5 deletions.
  1. +0 −1 perl++/headers/scalar.h
  2. +8 −0 perl++/headers/typecasts.h
  3. +0 −3 perl++/source/scalar.C
  4. +7 −1 t/40-typecasts.C
@@ -583,7 +583,6 @@ namespace perl {
operator long double() const;
operator Raw_string() const;
operator const std::string() const;
- operator const char*() const;
operator bool() const;
bool as_bool() const;
@@ -18,6 +18,14 @@ namespace perl {
return interp.value_of(t);
}
};
+ template<> struct typemap<const char*> {
+ static const char* cast_to(const Scalar::Value& val) {
+ return static_cast<const char*>(val.operator Raw_string());
+ }
+ static const Scalar::Temp cast_from(Interpreter& interp, const char* str) {
+ return interp.value_of(str);
+ }
+ };
template<typename T, typename U> struct typemap< std::pair<T, U>, void> {
typedef typename boost::mpl::and_<typename is_pushable<T>::type, typename is_pushable<U>::type>::type from_type;
@@ -243,9 +243,6 @@ namespace perl {
Scalar::Value::operator Raw_string() const {
return string_value();
}
- Scalar::Value::operator const char*() const {
- return operator Raw_string();
- }
Scalar::Value::operator const std::string() const {
size_t len;
const char* const tmp = SvPVx(get_SV(true), len);
View
@@ -27,7 +27,7 @@ namespace perl {
}
int main() {
- plan(4);
+ plan(6);
Interpreter universe;
@@ -36,6 +36,12 @@ int main() {
int foo = typecast_to<int>(scalar);
is(foo, 1, "foo == 1");
+ const char* foo2 = typecast_to<const char*>(scalar);
+ is(foo2, std::string("1"), "foo2 == \"1\"");
+
+ std::string foo3 = typecast_to<const char*>(scalar);
+ is(foo3, std::string("1"), "foo3 == \"1\"");
+
my_type bar = typecast_to<my_type>(scalar);
is(bar.value, 1, "bar.value == 1");

0 comments on commit 6ea30c7

Please sign in to comment.