]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/cppbind/from_nasal_detail.hxx
Fix file headers
[simgear.git] / simgear / nasal / cppbind / from_nasal_detail.hxx
1 // Conversion helpers used by from_nasal<T>(naContext, naRef)
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_FROM_NASAL_DETAIL_HXX_
20 #define SG_FROM_NASAL_DETAIL_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 <typeinfo> // std::bad_cast
29 #include <vector>
30
31 namespace nasal
32 {
33   class Hash;
34
35   /**
36    * Thrown when converting a type from/to Nasal has failed
37    */
38   class bad_nasal_cast:
39     public std::bad_cast
40   {
41     public:
42       bad_nasal_cast();
43       explicit bad_nasal_cast(const std::string& msg);
44
45       virtual ~bad_nasal_cast() throw();
46       virtual const char* what() const throw();
47
48     protected:
49       std::string _msg;
50   };
51
52   /**
53    * Convert Nasal string to std::string
54    */
55   std::string from_nasal(naContext c, naRef ref, std::string*);
56
57   /**
58    * Convert a Nasal hash to a nasal::Hash
59    */
60   Hash from_nasal(naContext c, naRef ref, Hash*);
61
62   /**
63    * Convert a Nasal number to a C++ numeric type
64    */
65   template<class T>
66   typename boost::enable_if< boost::is_arithmetic<T>,
67                              T
68                            >::type
69   from_nasal(naContext c, naRef ref, T*)
70   {
71     naRef num = naNumValue(ref);
72     if( !naIsNum(num) )
73       throw bad_nasal_cast("Not a number");
74
75     return static_cast<T>(num.num);
76   }
77
78   /**
79    * Convert a Nasal vector to a std::vector
80    */
81   template<class Vector, class T>
82   typename boost::enable_if< boost::is_same<Vector, std::vector<T> >,
83                              Vector
84                            >::type
85   from_nasal(naContext c, naRef ref, Vector*)
86   {
87     if( !naIsVector(ref) )
88       throw bad_nasal_cast("Not a vector");
89
90     int size = naVec_size(ref);
91     Vector vec(size);
92
93     for(int i = 0; i < size; ++i)
94       vec[i] = from_nasal<T>(c, naVec_get(ref, i));
95
96     return vec;
97   }
98
99 } // namespace nasal
100
101 #endif /* SG_FROM_NASAL_DETAIL_HXX_ */