]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/cppbind/to_nasal.hxx
Add SGPath to the Nasal conversion helpers.
[simgear.git] / simgear / nasal / cppbind / to_nasal.hxx
1 ///@file
2 /// Conversion functions to convert C++ types to Nasal types
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_TO_NASAL_HXX_
21 #define SG_TO_NASAL_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 <vector>
32
33 class SGPath;
34
35 namespace nasal
36 {
37   class Hash;
38
39   /**
40    * Convert std::string to Nasal string
41    */
42   naRef to_nasal(naContext c, const std::string& str);
43
44   /**
45    * Convert C-string to Nasal string
46    */
47   // We need this to prevent the array overload of to_nasal being called
48   naRef to_nasal(naContext c, const char* str);
49
50   /**
51    * Convert function pointer to Nasal function
52    */
53   naRef to_nasal(naContext c, naCFunction func);
54
55   /**
56    * Convert a nasal::Hash to a Nasal hash
57    */
58   naRef to_nasal(naContext c, const Hash& hash);
59
60   /**
61    * Simple pass-through of naRef types to allow generic usage of to_nasal
62    */
63   naRef to_nasal(naContext c, naRef ref);
64
65   naRef to_nasal(naContext c, const SGPath& path);
66     
67   /**
68    * Convert a numeric type to Nasal number
69    */
70   template<class T>
71   typename boost::enable_if< boost::is_arithmetic<T>, naRef >::type
72   to_nasal(naContext c, T num)
73   {
74     return naNum(num);
75   }
76
77   /**
78    * Convert a fixed size array to a Nasal vector
79    */
80   template<class T, size_t N>
81   naRef to_nasal(naContext c, const T(&array)[N])
82   {
83     naRef ret = naNewVector(c);
84     naVec_setsize(c, ret, N);
85     for(size_t i = 0; i < N; ++i)
86       naVec_set(ret, i, to_nasal(c, array[i]));
87     return ret;
88   }
89
90   /**
91    * Convert std::vector to Nasal vector
92    */
93   template< template<class T, class Alloc> class Vector,
94             class T,
95             class Alloc
96           >
97   typename boost::enable_if< boost::is_same< Vector<T,Alloc>,
98                                              std::vector<T,Alloc>
99                                            >,
100                              naRef
101                            >::type
102   to_nasal(naContext c, const Vector<T, Alloc>& vec)
103   {
104     naRef ret = naNewVector(c);
105     naVec_setsize(c, ret, vec.size());
106     for(size_t i = 0; i < vec.size(); ++i)
107       naVec_set(ret, i, to_nasal(c, vec[i]));
108     return ret;
109   }
110
111   /**
112    * Convert a 2d vector to Nasal vector with 2 elements
113    */
114   template<class Vec2>
115   typename boost::enable_if<is_vec2<Vec2>, naRef>::type
116   to_nasal(naContext c, const Vec2& vec)
117   {
118     // We take just double because in Nasal every number is represented as
119     // double
120     double nasal_vec[2] = {vec[0], vec[1]};
121     return to_nasal(c, nasal_vec);
122   }
123
124 } // namespace nasal
125
126 #endif /* SG_TO_NASAL_HXX_ */