Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickups2/src/cpptcl/details/conversions.h @ 2378

Last change on this file since 2378 was 1505, checked in by rgrieder, 16 years ago

f* svn: It doesn't even inform you if you attempt to set a non existing property. It is svn:eol-style and not eol-style when using the command by the way…

  • Property svn:eol-style set to native
File size: 2.1 KB
Line 
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
17template <typename T>
18struct tcl_cast;
19
20template <typename T>
21struct 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
49template <typename T>
50struct 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
61template <>
62struct tcl_cast<int>
63{
64     static int from(Tcl_Interp *, Tcl_Obj *);
65};
66
67template <>
68struct tcl_cast<long>
69{
70     static long from(Tcl_Interp *, Tcl_Obj *);
71};
72
73template <>
74struct tcl_cast<bool>
75{
76     static bool from(Tcl_Interp *, Tcl_Obj *);
77};
78
79template <>
80struct tcl_cast<double>
81{
82     static double from(Tcl_Interp *, Tcl_Obj *);
83};
84
85template <>
86struct tcl_cast<std::string>
87{
88     static std::string from(Tcl_Interp *, Tcl_Obj *);
89};
90
91template <>
92struct tcl_cast<char const *>
93{
94     static char const * from(Tcl_Interp *, Tcl_Obj *);
95};
96
97template <>
98struct tcl_cast<object>
99{
100     static object from(Tcl_Interp *, Tcl_Obj *);
101};
Note: See TracBrowser for help on using the repository browser.