]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/cppbind/from_nasal_detail.hxx
Add SGPath to the Nasal conversion helpers.
[simgear.git] / simgear / nasal / cppbind / from_nasal_detail.hxx
1 ///@file
2 /// Conversion helpers used by from_nasal<T>(naContext, naRef)
3 ///
4 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Library General Public
8 // License as published by the Free Software Foundation; either
9 // version 2 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Library General Public License for more details.
15 //
16 // You should have received a copy of the GNU Library General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
19
20 #ifndef SG_FROM_NASAL_DETAIL_HXX_
21 #define SG_FROM_NASAL_DETAIL_HXX_
22
23 #include "nasal_traits.hxx"
24
25 #include <simgear/nasal/nasal.h>
26
27 #include <boost/utility/enable_if.hpp>
28 #include <boost/type_traits.hpp>
29
30 #include <string>
31 #include <typeinfo> // std::bad_cast
32 #include <vector>
33
34 class SGPath;
35
36 namespace nasal
37 {
38   class Hash;
39
40   /**
41    * Thrown when converting a type from/to Nasal has failed
42    */
43   class bad_nasal_cast:
44     public std::bad_cast
45   {
46     public:
47       /**
48        * Construct with generic error message
49        */
50       bad_nasal_cast();
51
52       /**
53        * Construct from an error message
54        *
55        * @param msg Error message/description
56        */
57       explicit bad_nasal_cast(const std::string& msg);
58
59       virtual ~bad_nasal_cast() throw();
60
61       /**
62        * Get a description of the cause of the failed cast.
63        */
64       virtual const char* what() const throw();
65
66     protected:
67       std::string _msg;
68   };
69
70   /**
71    * Simple pass through for unified handling also of naRef.
72    */
73   inline naRef from_nasal_helper(naContext, naRef ref, naRef*) { return ref; }
74
75   /**
76    * Convert Nasal string to std::string
77    */
78   std::string from_nasal_helper(naContext c, naRef ref, std::string*);
79
80   /**
81    * Convert a Nasal string to an SGPath
82    */
83   SGPath from_nasal_helper(naContext c, naRef ref, SGPath*);
84
85   /**
86    * Convert a Nasal hash to a nasal::Hash
87    */
88   Hash from_nasal_helper(naContext c, naRef ref, Hash*);
89
90   /**
91    * Convert a Nasal number to a C++ numeric type
92    */
93   template<class T>
94   typename boost::enable_if< boost::is_arithmetic<T>,
95                              T
96                            >::type
97   from_nasal_helper(naContext c, naRef ref, T*)
98   {
99     naRef num = naNumValue(ref);
100     if( !naIsNum(num) )
101       throw bad_nasal_cast("Not a number");
102
103     return static_cast<T>(num.num);
104   }
105
106   /**
107    * Convert a Nasal vector to a std::vector
108    */
109   template<class T>
110   std::vector<T> from_nasal_helper(naContext c, naRef ref, std::vector<T>*)
111   {
112     if( !naIsVector(ref) )
113       throw bad_nasal_cast("Not a vector");
114
115     int size = naVec_size(ref);
116     std::vector<T> vec(size);
117
118     for(int i = 0; i < size; ++i)
119       vec[i] = from_nasal_helper(c, naVec_get(ref, i), static_cast<T*>(0));
120
121     return vec;
122   }
123
124   /**
125    * Convert a Nasal vector of 2 elements to a 2d vector
126    */
127   template<class Vec2>
128   typename boost::enable_if<is_vec2<Vec2>, Vec2>::type
129   from_nasal_helper(naContext c, naRef ref, Vec2*)
130   {
131     std::vector<double> vec =
132       from_nasal_helper(c, ref, static_cast<std::vector<double>*>(0));
133     if( vec.size() != 2 )
134       throw bad_nasal_cast("Expected vector with two elements");
135     return Vec2(vec[0], vec[1]);
136   }
137
138 } // namespace nasal
139
140 #endif /* SG_FROM_NASAL_DETAIL_HXX_ */