TestInterface.cpp

00001
00002 /***************************************************************************
00003  *  TestInterface.cpp - Fawkes BlackBoard Interface - TestInterface
00004  *
00005  *  Templated created:   Thu Oct 12 10:49:19 2006
00006  *  Copyright  2006-2007  Tim Niemueller
00007  *
00008  ****************************************************************************/
00009
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023
00024 #include <interfaces/TestInterface.h>
00025
00026 #include <core/exceptions/software.h>
00027
00028 #include <cstring>
00029 #include <cstdlib>
00030
00031 namespace fawkes {
00032 
00033 /** @class TestInterface <interfaces/TestInterface.h>
00034  * TestInterface Fawkes BlackBoard Interface.
00035  * Test interface. Use this to play around. Do NOT remove any fields, as this
00036       interface is used by BlackBoard QA.
00037  * @ingroup FawkesInterfaces
00038  */
00039
00040 
00041 /** TEST_CONSTANT constant */
00042 const int TestInterface::TEST_CONSTANT = 5;
00043 /** TEST_FLOAT_CONSTANT constant */
00044 const float TestInterface::TEST_FLOAT_CONSTANT = 1.2;
00045 
00046 /** Constructor */
00047 TestInterface::TestInterface() : Interface()
00048 {
00049   data_size = sizeof(TestInterface_data_t);
00050   data_ptr  = malloc(data_size);
00051   data      = (TestInterface_data_t *)data_ptr;
00052   memset(data_ptr, 0, data_size);
00053   add_fieldinfo(IFT_BOOL, "test_bool", 1, &data->test_bool);
00054   add_fieldinfo(IFT_INT, "test_int", 1, &data->test_int);
00055   add_fieldinfo(IFT_BYTE, "flags", 1, &data->flags);
00056   add_fieldinfo(IFT_STRING, "test_string", 30, data->test_string);
00057   add_fieldinfo(IFT_INT, "result", 1, &data->result);
00058   add_fieldinfo(IFT_UINT, "test_uint", 1, &data->test_uint);
00059   add_fieldinfo(IFT_LONGUINT, "test_ulint", 1, &data->test_ulint);
00060   add_fieldinfo(IFT_LONGINT, "test_lint", 1, &data->test_lint);
00061   add_messageinfo("SetTestIntMessage");
00062   add_messageinfo("SetTestStringMessage");
00063   add_messageinfo("CalculateMessage");
00064   unsigned char tmp_hash[] = {0x69, 0xac, 0x2a, 0x88, 0xec, 0xcf, 0x7, 0x18, 0x1, 0xf7, 0xfb, 0x54, 0xc, 0xb7, 0xa2, 0x1f};
00065   set_hash(tmp_hash);
00066 }
00067 
00068 /** Destructor */
00069 TestInterface::~TestInterface()
00070 {
00071   free(data_ptr);
00072 }
00073 /* Methods */
00074 /** Get test_bool value.
00075  * Test Bool
00076  * @return test_bool value
00077  */
00078 bool
00079 TestInterface::is_test_bool() const
00080 {
00081   return data->test_bool;
00082 }
00083 
00084 /** Get maximum length of test_bool value.
00085  * @return length of test_bool value, can be length of the array or number of 
00086  * maximum number of characters for a string
00087  */
00088 size_t
00089 TestInterface::maxlenof_test_bool() const
00090 {
00091   return 1;
00092 }
00093 
00094 /** Set test_bool value.
00095  * Test Bool
00096  * @param new_test_bool new test_bool value
00097  */
00098 void
00099 TestInterface::set_test_bool(const bool new_test_bool)
00100 {
00101   data->test_bool = new_test_bool;
00102 }
00103 
00104 /** Get test_int value.
00105  * Test integer
00106  * @return test_int value
00107  */
00108 int
00109 TestInterface::test_int() const
00110 {
00111   return data->test_int;
00112 }
00113 
00114 /** Get maximum length of test_int value.
00115  * @return length of test_int value, can be length of the array or number of 
00116  * maximum number of characters for a string
00117  */
00118 size_t
00119 TestInterface::maxlenof_test_int() const
00120 {
00121   return 1;
00122 }
00123 
00124 /** Set test_int value.
00125  * Test integer
00126  * @param new_test_int new test_int value
00127  */
00128 void
00129 TestInterface::set_test_int(const int new_test_int)
00130 {
00131   data->test_int = new_test_int;
00132 }
00133 
00134 /** Get flags value.
00135  * Flags spit down by the writer
00136  * @return flags value
00137  */
00138 unsigned char
00139 TestInterface::flags() const
00140 {
00141   return data->flags;
00142 }
00143 
00144 /** Get maximum length of flags value.
00145  * @return length of flags value, can be length of the array or number of 
00146  * maximum number of characters for a string
00147  */
00148 size_t
00149 TestInterface::maxlenof_flags() const
00150 {
00151   return 1;
00152 }
00153 
00154 /** Set flags value.
00155  * Flags spit down by the writer
00156  * @param new_flags new flags value
00157  */
00158 void
00159 TestInterface::set_flags(const unsigned char new_flags)
00160 {
00161   data->flags = new_flags;
00162 }
00163 
00164 /** Get test_string value.
00165  * A test sring
00166  * @return test_string value
00167  */
00168 char *
00169 TestInterface::test_string() const
00170 {
00171   return data->test_string;
00172 }
00173 
00174 /** Get maximum length of test_string value.
00175  * @return length of test_string value, can be length of the array or number of 
00176  * maximum number of characters for a string
00177  */
00178 size_t
00179 TestInterface::maxlenof_test_string() const
00180 {
00181   return 30;
00182 }
00183 
00184 /** Set test_string value.
00185  * A test sring
00186  * @param new_test_string new test_string value
00187  */
00188 void
00189 TestInterface::set_test_string(const char * new_test_string)
00190 {
00191   strncpy(data->test_string, new_test_string, sizeof(data->test_string));
00192 }
00193 
00194 /** Get result value.
00195  * Result of operation add operation from Calculate message.
00196  * @return result value
00197  */
00198 int
00199 TestInterface::result() const
00200 {
00201   return data->result;
00202 }
00203 
00204 /** Get maximum length of result value.
00205  * @return length of result value, can be length of the array or number of 
00206  * maximum number of characters for a string
00207  */
00208 size_t
00209 TestInterface::maxlenof_result() const
00210 {
00211   return 1;
00212 }
00213 
00214 /** Set result value.
00215  * Result of operation add operation from Calculate message.
00216  * @param new_result new result value
00217  */
00218 void
00219 TestInterface::set_result(const int new_result)
00220 {
00221   data->result = new_result;
00222 }
00223 
00224 /** Get test_uint value.
00225  * Test unsigned int
00226  * @return test_uint value
00227  */
00228 unsigned int
00229 TestInterface::test_uint() const
00230 {
00231   return data->test_uint;
00232 }
00233 
00234 /** Get maximum length of test_uint value.
00235  * @return length of test_uint value, can be length of the array or number of 
00236  * maximum number of characters for a string
00237  */
00238 size_t
00239 TestInterface::maxlenof_test_uint() const
00240 {
00241   return 1;
00242 }
00243 
00244 /** Set test_uint value.
00245  * Test unsigned int
00246  * @param new_test_uint new test_uint value
00247  */
00248 void
00249 TestInterface::set_test_uint(const unsigned int new_test_uint)
00250 {
00251   data->test_uint = new_test_uint;
00252 }
00253 
00254 /** Get test_ulint value.
00255  * Test unsigned long int
00256  * @return test_ulint value
00257  */
00258 unsigned long int
00259 TestInterface::test_ulint() const
00260 {
00261   return data->test_ulint;
00262 }
00263 
00264 /** Get maximum length of test_ulint value.
00265  * @return length of test_ulint value, can be length of the array or number of 
00266  * maximum number of characters for a string
00267  */
00268 size_t
00269 TestInterface::maxlenof_test_ulint() const
00270 {
00271   return 1;
00272 }
00273 
00274 /** Set test_ulint value.
00275  * Test unsigned long int
00276  * @param new_test_ulint new test_ulint value
00277  */
00278 void
00279 TestInterface::set_test_ulint(const unsigned long int new_test_ulint)
00280 {
00281   data->test_ulint = new_test_ulint;
00282 }
00283 
00284 /** Get test_lint value.
00285  * Test long int
00286  * @return test_lint value
00287  */
00288 long int
00289 TestInterface::test_lint() const
00290 {
00291   return data->test_lint;
00292 }
00293 
00294 /** Get maximum length of test_lint value.
00295  * @return length of test_lint value, can be length of the array or number of 
00296  * maximum number of characters for a string
00297  */
00298 size_t
00299 TestInterface::maxlenof_test_lint() const
00300 {
00301   return 1;
00302 }
00303 
00304 /** Set test_lint value.
00305  * Test long int
00306  * @param new_test_lint new test_lint value
00307  */
00308 void
00309 TestInterface::set_test_lint(const long int new_test_lint)
00310 {
00311   data->test_lint = new_test_lint;
00312 }
00313
00314 /* =========== message create =========== */
00315 Message *
00316 TestInterface::create_message(const char *type) const
00317 {
00318   if ( strncmp("SetTestIntMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00319     return new SetTestIntMessage();
00320   } else if ( strncmp("SetTestStringMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00321     return new SetTestStringMessage();
00322   } else if ( strncmp("CalculateMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00323     return new CalculateMessage();
00324   } else {
00325     throw UnknownTypeException("The given type '%s' does not match any known "
00326                                "message type for this interface type.", type);
00327   }
00328 }
00329
00330 
00331 /** Copy values from other interface.
00332  * @param other other interface to copy values from
00333  */
00334 void
00335 TestInterface::copy_values(const Interface *other)
00336 {
00337   const TestInterface *oi = dynamic_cast<const TestInterface *>(other);
00338   if (oi == NULL) {
00339     throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
00340                                 type(), other->type());
00341   }
00342   memcpy(data, oi->data, sizeof(TestInterface_data_t));
00343 }
00344
00345 /* =========== messages =========== */
00346 /** @class TestInterface::SetTestIntMessage <interfaces/TestInterface.h>
00347  * SetTestIntMessage Fawkes BlackBoard Interface Message.
00348  * 
00349     
00350  */
00351
00352 
00353 /** Constructor with initial values.
00354  * @param ini_test_int initial value for test_int
00355  */
00356 TestInterface::SetTestIntMessage::SetTestIntMessage(const int ini_test_int) : Message("SetTestIntMessage")
00357 {
00358   data_size = sizeof(SetTestIntMessage_data_t);
00359   data_ptr  = malloc(data_size);
00360   memset(data_ptr, 0, data_size);
00361   data      = (SetTestIntMessage_data_t *)data_ptr;
00362   data->test_int = ini_test_int;
00363   add_fieldinfo(IFT_INT, "test_int", 1, &data->test_int);
00364 }
00365 /** Constructor */
00366 TestInterface::SetTestIntMessage::SetTestIntMessage() : Message("SetTestIntMessage")
00367 {
00368   data_size = sizeof(SetTestIntMessage_data_t);
00369   data_ptr  = malloc(data_size);
00370   memset(data_ptr, 0, data_size);
00371   data      = (SetTestIntMessage_data_t *)data_ptr;
00372   add_fieldinfo(IFT_INT, "test_int", 1, &data->test_int);
00373 }
00374 
00375 /** Destructor */
00376 TestInterface::SetTestIntMessage::~SetTestIntMessage()
00377 {
00378   free(data_ptr);
00379 }
00380 
00381 /** Copy constructor.
00382  * @param m message to copy from
00383  */
00384 TestInterface::SetTestIntMessage::SetTestIntMessage(const SetTestIntMessage *m) : Message("SetTestIntMessage")
00385 {
00386   data_size = m->data_size;
00387   data_ptr  = malloc(data_size);
00388   memcpy(data_ptr, m->data_ptr, data_size);
00389   data      = (SetTestIntMessage_data_t *)data_ptr;
00390 }
00391
00392 /* Methods */
00393 /** Get test_int value.
00394  * Test integer
00395  * @return test_int value
00396  */
00397 int
00398 TestInterface::SetTestIntMessage::test_int() const
00399 {
00400   return data->test_int;
00401 }
00402 
00403 /** Get maximum length of test_int value.
00404  * @return length of test_int value, can be length of the array or number of 
00405  * maximum number of characters for a string
00406  */
00407 size_t
00408 TestInterface::SetTestIntMessage::maxlenof_test_int() const
00409 {
00410   return 1;
00411 }
00412 
00413 /** Set test_int value.
00414  * Test integer
00415  * @param new_test_int new test_int value
00416  */
00417 void
00418 TestInterface::SetTestIntMessage::set_test_int(const int new_test_int)
00419 {
00420   data->test_int = new_test_int;
00421 }
00422 
00423 /** Clone this message.
00424  * Produces a message of the same type as this message and copies the
00425  * data to the new message.
00426  * @return clone of this message
00427  */
00428 Message *
00429 TestInterface::SetTestIntMessage::clone() const
00430 {
00431   return new TestInterface::SetTestIntMessage(this);
00432 }
00433 /** @class TestInterface::SetTestStringMessage <interfaces/TestInterface.h>
00434  * SetTestStringMessage Fawkes BlackBoard Interface Message.
00435  * 
00436     
00437  */
00438
00439 
00440 /** Constructor with initial values.
00441  * @param ini_test_string initial value for test_string
00442  */
00443 TestInterface::SetTestStringMessage::SetTestStringMessage(const char * ini_test_string) : Message("SetTestStringMessage")
00444 {
00445   data_size = sizeof(SetTestStringMessage_data_t);
00446   data_ptr  = malloc(data_size);
00447   memset(data_ptr, 0, data_size);
00448   data      = (SetTestStringMessage_data_t *)data_ptr;
00449   strncpy(data->test_string, ini_test_string, 30);
00450   add_fieldinfo(IFT_STRING, "test_string", 30, data->test_string);
00451 }
00452 /** Constructor */
00453 TestInterface::SetTestStringMessage::SetTestStringMessage() : Message("SetTestStringMessage")
00454 {
00455   data_size = sizeof(SetTestStringMessage_data_t);
00456   data_ptr  = malloc(data_size);
00457   memset(data_ptr, 0, data_size);
00458   data      = (SetTestStringMessage_data_t *)data_ptr;
00459   add_fieldinfo(IFT_STRING, "test_string", 30, data->test_string);
00460 }
00461 
00462 /** Destructor */
00463 TestInterface::SetTestStringMessage::~SetTestStringMessage()
00464 {
00465   free(data_ptr);
00466 }
00467 
00468 /** Copy constructor.
00469  * @param m message to copy from
00470  */
00471 TestInterface::SetTestStringMessage::SetTestStringMessage(const SetTestStringMessage *m) : Message("SetTestStringMessage")
00472 {
00473   data_size = m->data_size;
00474   data_ptr  = malloc(data_size);
00475   memcpy(data_ptr, m->data_ptr, data_size);
00476   data      = (SetTestStringMessage_data_t *)data_ptr;
00477 }
00478
00479 /* Methods */
00480 /** Get test_string value.
00481  * A test sring
00482  * @return test_string value
00483  */
00484 char *
00485 TestInterface::SetTestStringMessage::test_string() const
00486 {
00487   return data->test_string;
00488 }
00489 
00490 /** Get maximum length of test_string value.
00491  * @return length of test_string value, can be length of the array or number of 
00492  * maximum number of characters for a string
00493  */
00494 size_t
00495 TestInterface::SetTestStringMessage::maxlenof_test_string() const
00496 {
00497   return 30;
00498 }
00499 
00500 /** Set test_string value.
00501  * A test sring
00502  * @param new_test_string new test_string value
00503  */
00504 void
00505 TestInterface::SetTestStringMessage::set_test_string(const char * new_test_string)
00506 {
00507   strncpy(data->test_string, new_test_string, sizeof(data->test_string));
00508 }
00509 
00510 /** Clone this message.
00511  * Produces a message of the same type as this message and copies the
00512  * data to the new message.
00513  * @return clone of this message
00514  */
00515 Message *
00516 TestInterface::SetTestStringMessage::clone() const
00517 {
00518   return new TestInterface::SetTestStringMessage(this);
00519 }
00520 /** @class TestInterface::CalculateMessage <interfaces/TestInterface.h>
00521  * CalculateMessage Fawkes BlackBoard Interface Message.
00522  * 
00523     
00524  */
00525
00526 
00527 /** Constructor with initial values.
00528  * @param ini_summand initial value for summand
00529  * @param ini_addend initial value for addend
00530  */
00531 TestInterface::CalculateMessage::CalculateMessage(const int ini_summand, const int ini_addend) : Message("CalculateMessage")
00532 {
00533   data_size = sizeof(CalculateMessage_data_t);
00534   data_ptr  = malloc(data_size);
00535   memset(data_ptr, 0, data_size);
00536   data      = (CalculateMessage_data_t *)data_ptr;
00537   data->summand = ini_summand;
00538   data->addend = ini_addend;
00539   add_fieldinfo(IFT_INT, "summand", 1, &data->summand);
00540   add_fieldinfo(IFT_INT, "addend", 1, &data->addend);
00541 }
00542 /** Constructor */
00543 TestInterface::CalculateMessage::CalculateMessage() : Message("CalculateMessage")
00544 {
00545   data_size = sizeof(CalculateMessage_data_t);
00546   data_ptr  = malloc(data_size);
00547   memset(data_ptr, 0, data_size);
00548   data      = (CalculateMessage_data_t *)data_ptr;
00549   add_fieldinfo(IFT_INT, "summand", 1, &data->summand);
00550   add_fieldinfo(IFT_INT, "addend", 1, &data->addend);
00551 }
00552 
00553 /** Destructor */
00554 TestInterface::CalculateMessage::~CalculateMessage()
00555 {
00556   free(data_ptr);
00557 }
00558 
00559 /** Copy constructor.
00560  * @param m message to copy from
00561  */
00562 TestInterface::CalculateMessage::CalculateMessage(const CalculateMessage *m) : Message("CalculateMessage")
00563 {
00564   data_size = m->data_size;
00565   data_ptr  = malloc(data_size);
00566   memcpy(data_ptr, m->data_ptr, data_size);
00567   data      = (CalculateMessage_data_t *)data_ptr;
00568 }
00569
00570 /* Methods */
00571 /** Get summand value.
00572  * Summand
00573  * @return summand value
00574  */
00575 int
00576 TestInterface::CalculateMessage::summand() const
00577 {
00578   return data->summand;
00579 }
00580 
00581 /** Get maximum length of summand value.
00582  * @return length of summand value, can be length of the array or number of 
00583  * maximum number of characters for a string
00584  */
00585 size_t
00586 TestInterface::CalculateMessage::maxlenof_summand() const
00587 {
00588   return 1;
00589 }
00590 
00591 /** Set summand value.
00592  * Summand
00593  * @param new_summand new summand value
00594  */
00595 void
00596 TestInterface::CalculateMessage::set_summand(const int new_summand)
00597 {
00598   data->summand = new_summand;
00599 }
00600 
00601 /** Get addend value.
00602  * Addend
00603  * @return addend value
00604  */
00605 int
00606 TestInterface::CalculateMessage::addend() const
00607 {
00608   return data->addend;
00609 }
00610 
00611 /** Get maximum length of addend value.
00612  * @return length of addend value, can be length of the array or number of 
00613  * maximum number of characters for a string
00614  */
00615 size_t
00616 TestInterface::CalculateMessage::maxlenof_addend() const
00617 {
00618   return 1;
00619 }
00620 
00621 /** Set addend value.
00622  * Addend
00623  * @param new_addend new addend value
00624  */
00625 void
00626 TestInterface::CalculateMessage::set_addend(const int new_addend)
00627 {
00628   data->addend = new_addend;
00629 }
00630 
00631 /** Clone this message.
00632  * Produces a message of the same type as this message and copies the
00633  * data to the new message.
00634  * @return clone of this message
00635  */
00636 Message *
00637 TestInterface::CalculateMessage::clone() const
00638 {
00639   return new TestInterface::CalculateMessage(this);
00640 }
00641 /** Check if message is valid and can be enqueued.
00642  * @param message Message to check
00643  */
00644 bool
00645 TestInterface::message_valid(const Message *message) const
00646 {
00647   const SetTestIntMessage *m0 = dynamic_cast<const SetTestIntMessage *>(message);
00648   if ( m0 != NULL ) {
00649     return true;
00650   }
00651   const SetTestStringMessage *m1 = dynamic_cast<const SetTestStringMessage *>(message);
00652   if ( m1 != NULL ) {
00653     return true;
00654   }
00655   const CalculateMessage *m2 = dynamic_cast<const CalculateMessage *>(message);
00656   if ( m2 != NULL ) {
00657     return true;
00658   }
00659   return false;
00660 }
00661 
00662 /// @cond INTERNALS
00663 EXPORT_INTERFACE(TestInterface)
00664 /// @endcond
00665 
00666
00667 } // end namespace fawkes