]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/cppbind/from_nasal_detail.hxx
cppbind: Automatic conversion of derived ghosts and some cleanup/fixes
[simgear.git] / simgear / nasal / cppbind / from_nasal_detail.hxx
1 ///@file
2 /// Conversion helpers used by from_nasal<T>(naContext, naRef)
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_FROM_NASAL_DETAIL_HXX_
21 #define SG_FROM_NASAL_DETAIL_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 <typeinfo> // std::bad_cast
30 #include <vector>
31
32 namespace nasal
33 {
34   class Hash;
35
36   /**
37    * Thrown when converting a type from/to Nasal has failed
38    */
39   class bad_nasal_cast:
40     public std::bad_cast
41   {
42     public:
43       /**
44        * Construct with generic error message
45        */
46       bad_nasal_cast();
47
48       /**
49        * Construct from an error message
50        *
51        * @param msg Error message/description
52        */
53       explicit bad_nasal_cast(const std::string& msg);
54
55       virtual ~bad_nasal_cast() throw();
56
57       /**
58        * Get a description of the cause of the failed cast.
59        */
60       virtual const char* what() const throw();
61
62     protected:
63       std::string _msg;
64   };
65
66   /**
67    * Simple pass through for unified handling also of naRef.
68    */
69   inline naRef from_nasal_helper(naContext, naRef ref, naRef*) { return ref; }
70
71   /**
72    * Convert Nasal string to std::string
73    */
74   std::string from_nasal_helper(naContext c, naRef ref, std::string*);
75
76   /**
77    * Convert a Nasal hash to a nasal::Hash
78    */
79   Hash from_nasal_helper(naContext c, naRef ref, Hash*);
80
81   /**
82    * Convert a Nasal number to a C++ numeric type
83    */
84   template<class T>
85   typename boost::enable_if< boost::is_arithmetic<T>,
86                              T
87                            >::type
88   from_nasal_helper(naContext c, naRef ref, T*)
89   {
90     naRef num = naNumValue(ref);
91     if( !naIsNum(num) )
92       throw bad_nasal_cast("Not a number");
93
94     return static_cast<T>(num.num);
95   }
96
97   /**
98    * Convert a Nasal vector to a std::vector
99    */
100   template<class Vector>
101   typename boost::enable_if< boost::is_same
102                              < Vector,
103                                std::vector<typename Vector::value_type>
104                              >,
105                              Vector
106                            >::type
107   from_nasal_helper(naContext c, naRef ref, Vector*)
108   {
109     if( !naIsVector(ref) )
110       throw bad_nasal_cast("Not a vector");
111
112     int size = naVec_size(ref);
113     Vector vec(size);
114
115     for(int i = 0; i < size; ++i)
116       vec[i] = from_nasal_helper
117       (
118         c,
119         naVec_get(ref, i),
120         static_cast<typename Vector::value_type*>(0)
121       );
122
123     return vec;
124   }
125
126 } // namespace nasal
127
128 #endif /* SG_FROM_NASAL_DETAIL_HXX_ */