1 /*
2  *  Open BEAGLE
3  *  Copyright (C) 2001-2007 by Christian Gagne and Marc Parizeau
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2.1 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  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  *  Contact:
20  *  Laboratoire de Vision et Systemes Numeriques
21  *  Departement de genie electrique et de genie informatique
22  *  Universite Laval, Quebec, Canada, G1K 7P4
23  *  http://vision.gel.ulaval.ca
24  *
25  */
26 
27 /*!
28  *  \file   beagle/GP/AddT.hpp
29  *  \brief  Definition of the templated class GP::AddT.
30  *  \author Christian Gagne
31  *  \author Marc Parizeau
32  *  $Revision: 1.9.2.1 $
33  *  $Date: 2007/05/09 01:51:03 $
34  */
35 
36 /*!
37  *  \defgroup MathPrimit Mathematical GP Primitives
38  *  \ingroup Primit
39  *  \brief Mathematical GP primitives library, part of the GP primitives module.
40  */
41 
42 #ifndef Beagle_GP_AddT_hpp
43 #define Beagle_GP_AddT_hpp
44 
45 #include <string>
46 
47 #include "beagle/config.hpp"
48 #include "beagle/macros.hpp"
49 #include "beagle/Object.hpp"
50 #include "beagle/AllocatorT.hpp"
51 #include "beagle/PointerT.hpp"
52 #include "beagle/ContainerT.hpp"
53 #include "beagle/WrapperT.hpp"
54 #include "beagle/GP/Primitive.hpp"
55 #include "beagle/GP/PrimitiveInline.hpp"
56 #include "beagle/GP/Context.hpp"
57 #include "beagle/GP/Datum.hpp"
58 
59 #ifdef BEAGLE_HAVE_RTTI
60 #include <typeinfo>
61 #endif // BEAGLE_HAVE_RTTI
62 
63 
64 namespace Beagle {
65 namespace GP {
66 
67 /*!
68  *  \class AddT beagle/GP/AddT.hpp "beagle/GP/AddT.hpp"
69  *  \brief GP generic 2 arguments addition primitive class.
70  *  \param T Type of the data added. operator+ must be properly defined for this type.
71  *  \ingroup GPF
72  *  \ingroup Primit
73  *  \ingroup MathPrimit
74  */
75 template <class T>
76 class AddT : public Primitive {
77 
78 public:
79 
80   //! GP::AddT allocator type.
81   typedef AllocatorT<AddT<T>,Primitive::Alloc>
82           Alloc;
83   //! GP::AddT handle type.
84   typedef PointerT<AddT<T>,Primitive::Handle>
85           Handle;
86   //! GP::AddT bag type.
87   typedef ContainerT<AddT<T>,Primitive::Bag>
88           Bag;
89 
90   explicit AddT(string inName="ADD");
~AddT()91   virtual ~AddT() { }
92 
93 #ifdef BEAGLE_HAVE_RTTI
94   virtual const std::type_info* getArgType(unsigned int inN, GP::Context& ioContext) const;
95   virtual const std::type_info* getReturnType(GP::Context& ioContext) const;
96 #endif // BEAGLE_HAVE_RTTI
97 
98   virtual void execute(GP::Datum& outResult, GP::Context& ioContext);
99 
100 };
101 
102 
103 /*!
104  *  \brief Add primitive for the double type.
105  */
106 typedef AddT<Double>
107         Add;
108 
109 }
110 }
111 
112 
113 /*!
114  *  \brief Construct a new addition primitive.
115  *  \param inName Name of the addition primitive.
116  */
117 template <class T>
AddT(string inName)118 Beagle::GP::AddT<T>::AddT(string inName) :
119   Beagle::GP::Primitive(2, inName)
120 { }
121 
122 
123 #ifdef BEAGLE_HAVE_RTTI
124 
125 /*!
126  *  \brief  Return the tag of the type of data needed as input for the primitive AddT.
127  *  \param  inN Index of the argument to get the type tag.
128  *  \param  ioContext Evolutionary context.
129  *  \return Type_info (RTTI) tagging the data type needed.
130  *  \throw  AssertException If the index inN given is greater than 1.
131  */
132 template <class T>
getArgType(unsigned int inN,Beagle::GP::Context & ioContext) const133 const std::type_info* Beagle::GP::AddT<T>::getArgType(unsigned int inN,
134                                                       Beagle::GP::Context& ioContext) const
135 {
136   Beagle_StackTraceBeginM();
137   Beagle_AssertM(inN<2);
138   return &typeid(T);
139   Beagle_StackTraceEndM("const std::type_info* GP::AddT<T>::getArgType(unsigned int inN, GP::Context& ioContext) const");
140 }
141 
142 
143 /*!
144  *  \brief  Return the tag of the type of data return by primitive AddT.
145  *  \param  ioContext Evolutionary context.
146  *  \return Type_info (RTTI) tagging the data type returned.
147  */
148 template <class T>
getReturnType(Beagle::GP::Context & ioContext) const149 const std::type_info* Beagle::GP::AddT<T>::getReturnType(Beagle::GP::Context& ioContext) const
150 {
151   Beagle_StackTraceBeginM();
152   return &typeid(T);
153   Beagle_StackTraceEndM("const std::type_info* GP::AddT<T>::getReturnType(GP::Context& ioContext) const");
154 }
155 
156 #endif // BEAGLE_HAVE_RTTI
157 
158 
159 /*!
160  *  \brief Execute the characteristic operation of the addition primitive.
161  *  \param outResult Result of the addition.
162  *  \param ioContext Evolutionary context.
163  *  \warning operator+= must be properly defined for the type T, the type of data added.
164  */
165 template <class T>
execute(Beagle::GP::Datum & outResult,Beagle::GP::Context & ioContext)166 void Beagle::GP::AddT<T>::execute(Beagle::GP::Datum& outResult, Beagle::GP::Context& ioContext)
167 {
168   Beagle_StackTraceBeginM();
169   T& lResult = castObjectT<T&>(outResult);
170   get1stArgument(lResult, ioContext);
171   T lArg2;
172   get2ndArgument(lArg2, ioContext);
173   lResult += lArg2;
174   Beagle_StackTraceEndM("void GP::AddT<T>::execute(GP::Datum& outResult, GP::Context& ioContext)");
175 }
176 
177 
178 #endif // Beagle_GP_AddT_hpp
179