]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/cppbind/from_nasal_detail.hxx
One more fix for old gcc
[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    * Convert Nasal string to std::string
68    */
69   std::string from_nasal(naContext c, naRef ref, std::string*);
70
71   /**
72    * Convert a Nasal hash to a nasal::Hash
73    */
74   Hash from_nasal(naContext c, naRef ref, Hash*);
75
76   /**
77    * Convert a Nasal number to a C++ numeric type
78    */
79   template<class T>
80   typename boost::enable_if< boost::is_arithmetic<T>,
81                              T
82                            >::type
83   from_nasal(naContext c, naRef ref, T*)
84   {
85     naRef num = naNumValue(ref);
86     if( !naIsNum(num) )
87       throw bad_nasal_cast("Not a number");
88
89     return static_cast<T>(num.num);
90   }
91
92   /**
93    * Convert a Nasal vector to a std::vector
94    */
95   template<class Vector, class T>
96   typename boost::enable_if< boost::is_same<Vector, std::vector<T> >,
97                              Vector
98                            >::type
99   from_nasal(naContext c, naRef ref, Vector*)
100   {
101     if( !naIsVector(ref) )
102       throw bad_nasal_cast("Not a vector");
103
104     int size = naVec_size(ref);
105     Vector vec(size);
106
107     for(int i = 0; i < size; ++i)
108       vec[i] = from_nasal<T>(c, naVec_get(ref, i));
109
110     return vec;
111   }
112
113 } // namespace nasal
114
115 #endif /* SG_FROM_NASAL_DETAIL_HXX_ */