]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/cppbind/to_nasal.hxx
Add nasal::Ghost class for exposing C++ classes to Nasal
[simgear.git] / simgear / nasal / cppbind / to_nasal.hxx
1 // Conversion functions to convert C++ types to Nasal types
2 //
3 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Library General Public License for more details.
14 //
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
18
19 #ifndef SG_TO_NASAL_HXX_
20 #define SG_TO_NASAL_HXX_
21
22 #include <simgear/nasal/nasal.h>
23
24 #include <boost/utility/enable_if.hpp>
25 #include <boost/type_traits.hpp>
26
27 #include <string>
28 #include <vector>
29
30 namespace nasal
31 {
32   class Hash;
33
34   /**
35    * Convert std::string to Nasal string
36    */
37   naRef to_nasal(naContext c, const std::string& str);
38
39   /**
40    * Convert function pointer to Nasal function
41    */
42   naRef to_nasal(naContext c, naCFunction func);
43
44   /**
45    * Convert a nasal::Hash to a Nasal hash
46    */
47   naRef to_nasal(naContext c, const Hash& hash);
48
49   /**
50    * Simple pass-through of naRef types to allow generic usage of to_nasal
51    */
52   naRef to_nasal(naContext c, naRef ref);
53
54   /**
55    * Convert a numeric type to Nasal number
56    */
57   template<class T>
58   typename boost::enable_if< boost::is_arithmetic<T>, naRef >::type
59   to_nasal(naContext c, T num)
60   {
61     return naNum(num);
62   }
63
64   /**
65    * Convert std::vector to Nasal vector
66    */
67   template< template<class T, class Alloc> class Vector,
68             class T,
69             class Alloc
70           >
71   typename boost::enable_if< boost::is_same< Vector<T,Alloc>,
72                                              std::vector<T,Alloc>
73                                            >,
74                              naRef
75                            >::type
76   to_nasal(naContext c, const Vector<T, Alloc>& vec)
77   {
78     naRef ret = naNewVector(c);
79     naVec_setsize(c, ret, vec.size());
80     for(size_t i = 0; i < vec.size(); ++i)
81       naVec_set(ret, i, to_nasal(c, vec[i]));
82     return ret;
83   }
84
85 } // namespace nasal
86
87 #endif /* SG_TO_NASAL_HXX_ */