]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/cppbind/to_nasal.hxx
cppbind: Allow getter without from_nasal defined and setter without to_nasal.
[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/call_traits.hpp>
29 #include <boost/type_traits.hpp>
30
31 #include <map>
32 #include <string>
33 #include <vector>
34
35 class SGPath;
36
37 namespace nasal
38 {
39   class Hash;
40
41   /**
42    * Convert std::string to Nasal string
43    */
44   naRef to_nasal(naContext c, const std::string& str);
45
46   /**
47    * Convert C-string to Nasal string
48    */
49   // We need this to prevent the array overload of to_nasal being called
50   naRef to_nasal(naContext c, const char* str);
51
52   /**
53    * Convert function pointer to Nasal function
54    */
55   naRef to_nasal(naContext c, naCFunction func);
56
57   /**
58    * Convert a nasal::Hash to a Nasal hash
59    */
60   naRef to_nasal(naContext c, const Hash& hash);
61
62   /**
63    * Simple pass-through of naRef types to allow generic usage of to_nasal
64    */
65   naRef to_nasal(naContext c, const naRef& ref);
66
67   naRef to_nasal(naContext c, const SGPath& path);
68     
69   /**
70    * Convert a numeric type to Nasal number
71    */
72   template<class T>
73   typename boost::enable_if< boost::is_arithmetic<T>, naRef >::type
74   to_nasal(naContext c, T num)
75   {
76     return naNum(num);
77   }
78
79   /**
80    * Convert a 2d vector to Nasal vector with 2 elements
81    */
82   template<class Vec2>
83   typename boost::enable_if<is_vec2<Vec2>, naRef>::type
84   to_nasal(naContext c, const Vec2& vec);
85
86   /**
87    * Convert a std::map to a Nasal Hash
88    */
89   template<class Value>
90   naRef to_nasal(naContext c, const std::map<std::string, Value>& map);
91
92   /**
93    * Convert a fixed size array to a Nasal vector
94    */
95   template<class T, size_t N>
96   naRef to_nasal(naContext c, const T(&array)[N]);
97
98   /**
99    * Convert std::vector to Nasal vector
100    */
101   template< template<class T, class Alloc> class Vector,
102             class T,
103             class Alloc
104           >
105   typename boost::enable_if< boost::is_same< Vector<T,Alloc>,
106                                              std::vector<T,Alloc>
107                                            >,
108                              naRef
109                            >::type
110   to_nasal(naContext c, const Vector<T, Alloc>& vec)
111   {
112     naRef ret = naNewVector(c);
113     naVec_setsize(c, ret, vec.size());
114     for(size_t i = 0; i < vec.size(); ++i)
115       naVec_set(ret, i, to_nasal(c, vec[i]));
116     return ret;
117   }
118
119   /**
120    * Wrapper to get pointer to specific version of to_nasal applicable to given
121    * type.
122    */
123   template<class Var>
124   struct to_nasal_ptr
125   {
126     typedef typename boost::call_traits<Var>::param_type param_type;
127     typedef naRef(*type)(naContext, param_type);
128
129     static type get()
130     {
131       return static_cast<type>(&to_nasal);
132     }
133   };
134
135   //----------------------------------------------------------------------------
136   template<class Vec2>
137   typename boost::enable_if<is_vec2<Vec2>, naRef>::type
138   to_nasal(naContext c, const Vec2& vec)
139   {
140     // We take just double because in Nasal every number is represented as
141     // double
142     double nasal_vec[2] = {vec[0], vec[1]};
143     return to_nasal(c, nasal_vec);
144   }
145
146   //----------------------------------------------------------------------------
147   template<class Value>
148   naRef to_nasal(naContext c, const std::map<std::string, Value>& map)
149   {
150     naRef hash = naNewHash(c);
151
152     typedef typename boost::call_traits<Value>::param_type param_type;
153     typedef typename std::map<std::string, Value>::const_iterator map_iterator;
154
155     for( map_iterator it = map.begin(); it != map.end(); ++it )
156       naHash_set
157       (
158         hash,
159         to_nasal(c, it->first),
160         to_nasal(c, static_cast<param_type>(it->second))
161       );
162
163     return hash;
164   }
165
166   //----------------------------------------------------------------------------
167   template<class T, size_t N>
168   naRef to_nasal(naContext c, const T(&array)[N])
169   {
170     naRef ret = naNewVector(c);
171     naVec_setsize(c, ret, N);
172     for(size_t i = 0; i < N; ++i)
173       naVec_set(ret, i, to_nasal(c, array[i]));
174     return ret;
175   }
176
177 } // namespace nasal
178
179 #endif /* SG_TO_NASAL_HXX_ */