]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/cppbind/to_nasal.hxx
First working version of DOM like Canvas event handling
[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 <simgear/nasal/nasal.h>
24
25 #include <boost/utility/enable_if.hpp>
26 #include <boost/type_traits.hpp>
27
28 #include <string>
29 #include <vector>
30
31 namespace nasal
32 {
33   class Hash;
34
35   /**
36    * Convert std::string to Nasal string
37    */
38   naRef to_nasal(naContext c, const std::string& str);
39
40   /**
41    * Convert function pointer to Nasal function
42    */
43   naRef to_nasal(naContext c, naCFunction func);
44
45   /**
46    * Convert a nasal::Hash to a Nasal hash
47    */
48   naRef to_nasal(naContext c, const Hash& hash);
49
50   /**
51    * Simple pass-through of naRef types to allow generic usage of to_nasal
52    */
53   naRef to_nasal(naContext c, naRef ref);
54
55   /**
56    * Convert a numeric type to Nasal number
57    */
58   template<class T>
59   typename boost::enable_if< boost::is_arithmetic<T>, naRef >::type
60   to_nasal(naContext c, T num)
61   {
62     return naNum(num);
63   }
64
65   /**
66    * Convert std::vector to Nasal vector
67    */
68   template< template<class T, class Alloc> class Vector,
69             class T,
70             class Alloc
71           >
72   typename boost::enable_if< boost::is_same< Vector<T,Alloc>,
73                                              std::vector<T,Alloc>
74                                            >,
75                              naRef
76                            >::type
77   to_nasal(naContext c, const Vector<T, Alloc>& vec)
78   {
79     naRef ret = naNewVector(c);
80     naVec_setsize(c, ret, vec.size());
81     for(size_t i = 0; i < vec.size(); ++i)
82       naVec_set(ret, i, to_nasal(c, vec[i]));
83     return ret;
84   }
85
86 } // namespace nasal
87
88 #endif /* SG_TO_NASAL_HXX_ */