| 1 | // |
|---|
| 2 | // Copyright (C) 2004-2006, Maciej Sobczak |
|---|
| 3 | // |
|---|
| 4 | // Permission to copy, use, modify, sell and distribute this software |
|---|
| 5 | // is granted provided this copyright notice appears in all copies. |
|---|
| 6 | // This software is provided "as is" without express or implied |
|---|
| 7 | // warranty, and with no claim as to its suitability for any purpose. |
|---|
| 8 | // |
|---|
| 9 | |
|---|
| 10 | // Note: this file is not supposed to be a stand-alone header |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | // helper functor for converting Tcl objects to the given type |
|---|
| 14 | // (it is a struct instead of function, |
|---|
| 15 | // because I need to partially specialize it) |
|---|
| 16 | |
|---|
| 17 | template <typename T> |
|---|
| 18 | struct tcl_cast; |
|---|
| 19 | |
|---|
| 20 | template <typename T> |
|---|
| 21 | struct tcl_cast<T*> |
|---|
| 22 | { |
|---|
| 23 | static T * from(Tcl_Interp *, Tcl_Obj *obj) |
|---|
| 24 | { |
|---|
| 25 | std::string s(Tcl_GetString(obj)); |
|---|
| 26 | if (s.size() == 0) |
|---|
| 27 | { |
|---|
| 28 | throw tcl_error("Expected pointer value, got empty string."); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | if (s[0] != 'p') |
|---|
| 32 | { |
|---|
| 33 | throw tcl_error("Expected pointer value."); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | std::istringstream ss(s); |
|---|
| 37 | char dummy; |
|---|
| 38 | void *p; |
|---|
| 39 | ss >> dummy >> p; |
|---|
| 40 | |
|---|
| 41 | return static_cast<T*>(p); |
|---|
| 42 | } |
|---|
| 43 | }; |
|---|
| 44 | |
|---|
| 45 | // the following partial specialization is to strip reference |
|---|
| 46 | // (it returns a temporary object of the underlying type, which |
|---|
| 47 | // can be bound to the const-ref parameter of the actual function) |
|---|
| 48 | |
|---|
| 49 | template <typename T> |
|---|
| 50 | struct tcl_cast<T const &> |
|---|
| 51 | { |
|---|
| 52 | static T from(Tcl_Interp *interp, Tcl_Obj *obj) |
|---|
| 53 | { |
|---|
| 54 | return tcl_cast<T>::from(interp, obj); |
|---|
| 55 | } |
|---|
| 56 | }; |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | // the following specializations are implemented |
|---|
| 60 | |
|---|
| 61 | template <> |
|---|
| 62 | struct tcl_cast<int> |
|---|
| 63 | { |
|---|
| 64 | static int from(Tcl_Interp *, Tcl_Obj *); |
|---|
| 65 | }; |
|---|
| 66 | |
|---|
| 67 | template <> |
|---|
| 68 | struct tcl_cast<long> |
|---|
| 69 | { |
|---|
| 70 | static long from(Tcl_Interp *, Tcl_Obj *); |
|---|
| 71 | }; |
|---|
| 72 | |
|---|
| 73 | template <> |
|---|
| 74 | struct tcl_cast<bool> |
|---|
| 75 | { |
|---|
| 76 | static bool from(Tcl_Interp *, Tcl_Obj *); |
|---|
| 77 | }; |
|---|
| 78 | |
|---|
| 79 | template <> |
|---|
| 80 | struct tcl_cast<double> |
|---|
| 81 | { |
|---|
| 82 | static double from(Tcl_Interp *, Tcl_Obj *); |
|---|
| 83 | }; |
|---|
| 84 | |
|---|
| 85 | template <> |
|---|
| 86 | struct tcl_cast<std::string> |
|---|
| 87 | { |
|---|
| 88 | static std::string from(Tcl_Interp *, Tcl_Obj *); |
|---|
| 89 | }; |
|---|
| 90 | |
|---|
| 91 | template <> |
|---|
| 92 | struct tcl_cast<char const *> |
|---|
| 93 | { |
|---|
| 94 | static char const * from(Tcl_Interp *, Tcl_Obj *); |
|---|
| 95 | }; |
|---|
| 96 | |
|---|
| 97 | template <> |
|---|
| 98 | struct tcl_cast<object> |
|---|
| 99 | { |
|---|
| 100 | static object from(Tcl_Interp *, Tcl_Obj *); |
|---|
| 101 | }; |
|---|