Fawkes API  Fawkes Development Version
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 int32_t 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   data_ts   = (interface_data_ts_t *)data_ptr;
00053   memset(data_ptr, 0, data_size);
00054   add_fieldinfo(IFT_BOOL, "test_bool", 1, &data->test_bool);
00055   add_fieldinfo(IFT_INT32, "test_int", 1, &data->test_int);
00056   add_fieldinfo(IFT_BYTE, "flags", 1, &data->flags);
00057   add_fieldinfo(IFT_STRING, "test_string", 30, data->test_string);
00058   add_fieldinfo(IFT_INT32, "result", 1, &data->result);
00059   add_fieldinfo(IFT_UINT32, "test_uint", 1, &data->test_uint);
00060   add_messageinfo("SetTestIntMessage");
00061   add_messageinfo("SetTestStringMessage");
00062   add_messageinfo("CalculateMessage");
00063   unsigned char tmp_hash[] = {0xe4, 0xe2, 0x1, 0xa9, 0xc8, 0x87, 0x8d, 0x3d, 0xa3, 0xab, 0xc9, 0xcd, 0xf3, 0xf, 0x5a, 0x33};
00064   set_hash(tmp_hash);
00065 }
00066 
00067 /** Destructor */
00068 TestInterface::~TestInterface()
00069 {
00070   free(data_ptr);
00071 }
00072 /** Convert TestEnum constant to string.
00073  * @param value value to convert to string
00074  * @return constant value as string.
00075  */
00076 const char *
00077 TestInterface::tostring_TestEnum(TestEnum value) const
00078 {
00079   switch (value) {
00080   case TEST_ENUM_1: return "TEST_ENUM_1";
00081   case TEST_ENUM_2: return "TEST_ENUM_2";
00082   default: return "UNKNOWN";
00083   }
00084 }
00085 /* Methods */
00086 /** Get test_bool value.
00087  * Test Bool
00088  * @return test_bool value
00089  */
00090 bool
00091 TestInterface::is_test_bool() const
00092 {
00093   return data->test_bool;
00094 }
00095 
00096 /** Get maximum length of test_bool value.
00097  * @return length of test_bool value, can be length of the array or number of 
00098  * maximum number of characters for a string
00099  */
00100 size_t
00101 TestInterface::maxlenof_test_bool() const
00102 {
00103   return 1;
00104 }
00105 
00106 /** Set test_bool value.
00107  * Test Bool
00108  * @param new_test_bool new test_bool value
00109  */
00110 void
00111 TestInterface::set_test_bool(const bool new_test_bool)
00112 {
00113   data->test_bool = new_test_bool;
00114   data_changed = true;
00115 }
00116 
00117 /** Get test_int value.
00118  * Test integer
00119  * @return test_int value
00120  */
00121 int32_t
00122 TestInterface::test_int() const
00123 {
00124   return data->test_int;
00125 }
00126 
00127 /** Get maximum length of test_int value.
00128  * @return length of test_int value, can be length of the array or number of 
00129  * maximum number of characters for a string
00130  */
00131 size_t
00132 TestInterface::maxlenof_test_int() const
00133 {
00134   return 1;
00135 }
00136 
00137 /** Set test_int value.
00138  * Test integer
00139  * @param new_test_int new test_int value
00140  */
00141 void
00142 TestInterface::set_test_int(const int32_t new_test_int)
00143 {
00144   data->test_int = new_test_int;
00145   data_changed = true;
00146 }
00147 
00148 /** Get flags value.
00149  * Flags spit down by the writer
00150  * @return flags value
00151  */
00152 uint8_t
00153 TestInterface::flags() const
00154 {
00155   return data->flags;
00156 }
00157 
00158 /** Get maximum length of flags value.
00159  * @return length of flags value, can be length of the array or number of 
00160  * maximum number of characters for a string
00161  */
00162 size_t
00163 TestInterface::maxlenof_flags() const
00164 {
00165   return 1;
00166 }
00167 
00168 /** Set flags value.
00169  * Flags spit down by the writer
00170  * @param new_flags new flags value
00171  */
00172 void
00173 TestInterface::set_flags(const uint8_t new_flags)
00174 {
00175   data->flags = new_flags;
00176   data_changed = true;
00177 }
00178 
00179 /** Get test_string value.
00180  * A test sring
00181  * @return test_string value
00182  */
00183 char *
00184 TestInterface::test_string() const
00185 {
00186   return data->test_string;
00187 }
00188 
00189 /** Get maximum length of test_string value.
00190  * @return length of test_string value, can be length of the array or number of 
00191  * maximum number of characters for a string
00192  */
00193 size_t
00194 TestInterface::maxlenof_test_string() const
00195 {
00196   return 30;
00197 }
00198 
00199 /** Set test_string value.
00200  * A test sring
00201  * @param new_test_string new test_string value
00202  */
00203 void
00204 TestInterface::set_test_string(const char * new_test_string)
00205 {
00206   strncpy(data->test_string, new_test_string, sizeof(data->test_string));
00207   data_changed = true;
00208 }
00209 
00210 /** Get result value.
00211  * Result of operation add operation from Calculate message.
00212  * @return result value
00213  */
00214 int32_t
00215 TestInterface::result() const
00216 {
00217   return data->result;
00218 }
00219 
00220 /** Get maximum length of result value.
00221  * @return length of result value, can be length of the array or number of 
00222  * maximum number of characters for a string
00223  */
00224 size_t
00225 TestInterface::maxlenof_result() const
00226 {
00227   return 1;
00228 }
00229 
00230 /** Set result value.
00231  * Result of operation add operation from Calculate message.
00232  * @param new_result new result value
00233  */
00234 void
00235 TestInterface::set_result(const int32_t new_result)
00236 {
00237   data->result = new_result;
00238   data_changed = true;
00239 }
00240 
00241 /** Get test_uint value.
00242  * Test uint32
00243  * @return test_uint value
00244  */
00245 uint32_t
00246 TestInterface::test_uint() const
00247 {
00248   return data->test_uint;
00249 }
00250 
00251 /** Get maximum length of test_uint value.
00252  * @return length of test_uint value, can be length of the array or number of 
00253  * maximum number of characters for a string
00254  */
00255 size_t
00256 TestInterface::maxlenof_test_uint() const
00257 {
00258   return 1;
00259 }
00260 
00261 /** Set test_uint value.
00262  * Test uint32
00263  * @param new_test_uint new test_uint value
00264  */
00265 void
00266 TestInterface::set_test_uint(const uint32_t new_test_uint)
00267 {
00268   data->test_uint = new_test_uint;
00269   data_changed = true;
00270 }
00271 
00272 /* =========== message create =========== */
00273 Message *
00274 TestInterface::create_message(const char *type) const
00275 {
00276   if ( strncmp("SetTestIntMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00277     return new SetTestIntMessage();
00278   } else if ( strncmp("SetTestStringMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00279     return new SetTestStringMessage();
00280   } else if ( strncmp("CalculateMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00281     return new CalculateMessage();
00282   } else {
00283     throw UnknownTypeException("The given type '%s' does not match any known "
00284                                "message type for this interface type.", type);
00285   }
00286 }
00287 
00288 
00289 /** Copy values from other interface.
00290  * @param other other interface to copy values from
00291  */
00292 void
00293 TestInterface::copy_values(const Interface *other)
00294 {
00295   const TestInterface *oi = dynamic_cast<const TestInterface *>(other);
00296   if (oi == NULL) {
00297     throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
00298                                 type(), other->type());
00299   }
00300   memcpy(data, oi->data, sizeof(TestInterface_data_t));
00301 }
00302 
00303 const char *
00304 TestInterface::enum_tostring(const char *enumtype, int val) const
00305 {
00306   if (strcmp(enumtype, "TestEnum") == 0) {
00307     return tostring_TestEnum((TestEnum)val);
00308   }
00309   throw UnknownTypeException("Unknown enum type %s", enumtype);
00310 }
00311 
00312 /* =========== messages =========== */
00313 /** @class TestInterface::SetTestIntMessage <interfaces/TestInterface.h>
00314  * SetTestIntMessage Fawkes BlackBoard Interface Message.
00315  * 
00316     
00317  */
00318 
00319 
00320 /** Constructor with initial values.
00321  * @param ini_test_int initial value for test_int
00322  */
00323 TestInterface::SetTestIntMessage::SetTestIntMessage(const int32_t ini_test_int) : Message("SetTestIntMessage")
00324 {
00325   data_size = sizeof(SetTestIntMessage_data_t);
00326   data_ptr  = malloc(data_size);
00327   memset(data_ptr, 0, data_size);
00328   data      = (SetTestIntMessage_data_t *)data_ptr;
00329   data_ts   = (message_data_ts_t *)data_ptr;
00330   data->test_int = ini_test_int;
00331   add_fieldinfo(IFT_INT32, "test_int", 1, &data->test_int);
00332 }
00333 /** Constructor */
00334 TestInterface::SetTestIntMessage::SetTestIntMessage() : Message("SetTestIntMessage")
00335 {
00336   data_size = sizeof(SetTestIntMessage_data_t);
00337   data_ptr  = malloc(data_size);
00338   memset(data_ptr, 0, data_size);
00339   data      = (SetTestIntMessage_data_t *)data_ptr;
00340   data_ts   = (message_data_ts_t *)data_ptr;
00341   add_fieldinfo(IFT_INT32, "test_int", 1, &data->test_int);
00342 }
00343 
00344 /** Destructor */
00345 TestInterface::SetTestIntMessage::~SetTestIntMessage()
00346 {
00347   free(data_ptr);
00348 }
00349 
00350 /** Copy constructor.
00351  * @param m message to copy from
00352  */
00353 TestInterface::SetTestIntMessage::SetTestIntMessage(const SetTestIntMessage *m) : Message("SetTestIntMessage")
00354 {
00355   data_size = m->data_size;
00356   data_ptr  = malloc(data_size);
00357   memcpy(data_ptr, m->data_ptr, data_size);
00358   data      = (SetTestIntMessage_data_t *)data_ptr;
00359   data_ts   = (message_data_ts_t *)data_ptr;
00360 }
00361 
00362 /* Methods */
00363 /** Get test_int value.
00364  * Test integer
00365  * @return test_int value
00366  */
00367 int32_t
00368 TestInterface::SetTestIntMessage::test_int() const
00369 {
00370   return data->test_int;
00371 }
00372 
00373 /** Get maximum length of test_int value.
00374  * @return length of test_int value, can be length of the array or number of 
00375  * maximum number of characters for a string
00376  */
00377 size_t
00378 TestInterface::SetTestIntMessage::maxlenof_test_int() const
00379 {
00380   return 1;
00381 }
00382 
00383 /** Set test_int value.
00384  * Test integer
00385  * @param new_test_int new test_int value
00386  */
00387 void
00388 TestInterface::SetTestIntMessage::set_test_int(const int32_t new_test_int)
00389 {
00390   data->test_int = new_test_int;
00391 }
00392 
00393 /** Clone this message.
00394  * Produces a message of the same type as this message and copies the
00395  * data to the new message.
00396  * @return clone of this message
00397  */
00398 Message *
00399 TestInterface::SetTestIntMessage::clone() const
00400 {
00401   return new TestInterface::SetTestIntMessage(this);
00402 }
00403 /** @class TestInterface::SetTestStringMessage <interfaces/TestInterface.h>
00404  * SetTestStringMessage Fawkes BlackBoard Interface Message.
00405  * 
00406     
00407  */
00408 
00409 
00410 /** Constructor with initial values.
00411  * @param ini_test_string initial value for test_string
00412  */
00413 TestInterface::SetTestStringMessage::SetTestStringMessage(const char * ini_test_string) : Message("SetTestStringMessage")
00414 {
00415   data_size = sizeof(SetTestStringMessage_data_t);
00416   data_ptr  = malloc(data_size);
00417   memset(data_ptr, 0, data_size);
00418   data      = (SetTestStringMessage_data_t *)data_ptr;
00419   data_ts   = (message_data_ts_t *)data_ptr;
00420   strncpy(data->test_string, ini_test_string, 30);
00421   add_fieldinfo(IFT_STRING, "test_string", 30, data->test_string);
00422 }
00423 /** Constructor */
00424 TestInterface::SetTestStringMessage::SetTestStringMessage() : Message("SetTestStringMessage")
00425 {
00426   data_size = sizeof(SetTestStringMessage_data_t);
00427   data_ptr  = malloc(data_size);
00428   memset(data_ptr, 0, data_size);
00429   data      = (SetTestStringMessage_data_t *)data_ptr;
00430   data_ts   = (message_data_ts_t *)data_ptr;
00431   add_fieldinfo(IFT_STRING, "test_string", 30, data->test_string);
00432 }
00433 
00434 /** Destructor */
00435 TestInterface::SetTestStringMessage::~SetTestStringMessage()
00436 {
00437   free(data_ptr);
00438 }
00439 
00440 /** Copy constructor.
00441  * @param m message to copy from
00442  */
00443 TestInterface::SetTestStringMessage::SetTestStringMessage(const SetTestStringMessage *m) : Message("SetTestStringMessage")
00444 {
00445   data_size = m->data_size;
00446   data_ptr  = malloc(data_size);
00447   memcpy(data_ptr, m->data_ptr, data_size);
00448   data      = (SetTestStringMessage_data_t *)data_ptr;
00449   data_ts   = (message_data_ts_t *)data_ptr;
00450 }
00451 
00452 /* Methods */
00453 /** Get test_string value.
00454  * A test sring
00455  * @return test_string value
00456  */
00457 char *
00458 TestInterface::SetTestStringMessage::test_string() const
00459 {
00460   return data->test_string;
00461 }
00462 
00463 /** Get maximum length of test_string value.
00464  * @return length of test_string value, can be length of the array or number of 
00465  * maximum number of characters for a string
00466  */
00467 size_t
00468 TestInterface::SetTestStringMessage::maxlenof_test_string() const
00469 {
00470   return 30;
00471 }
00472 
00473 /** Set test_string value.
00474  * A test sring
00475  * @param new_test_string new test_string value
00476  */
00477 void
00478 TestInterface::SetTestStringMessage::set_test_string(const char * new_test_string)
00479 {
00480   strncpy(data->test_string, new_test_string, sizeof(data->test_string));
00481 }
00482 
00483 /** Clone this message.
00484  * Produces a message of the same type as this message and copies the
00485  * data to the new message.
00486  * @return clone of this message
00487  */
00488 Message *
00489 TestInterface::SetTestStringMessage::clone() const
00490 {
00491   return new TestInterface::SetTestStringMessage(this);
00492 }
00493 /** @class TestInterface::CalculateMessage <interfaces/TestInterface.h>
00494  * CalculateMessage Fawkes BlackBoard Interface Message.
00495  * 
00496     
00497  */
00498 
00499 
00500 /** Constructor with initial values.
00501  * @param ini_summand initial value for summand
00502  * @param ini_addend initial value for addend
00503  */
00504 TestInterface::CalculateMessage::CalculateMessage(const int32_t ini_summand, const int32_t ini_addend) : Message("CalculateMessage")
00505 {
00506   data_size = sizeof(CalculateMessage_data_t);
00507   data_ptr  = malloc(data_size);
00508   memset(data_ptr, 0, data_size);
00509   data      = (CalculateMessage_data_t *)data_ptr;
00510   data_ts   = (message_data_ts_t *)data_ptr;
00511   data->summand = ini_summand;
00512   data->addend = ini_addend;
00513   add_fieldinfo(IFT_INT32, "summand", 1, &data->summand);
00514   add_fieldinfo(IFT_INT32, "addend", 1, &data->addend);
00515 }
00516 /** Constructor */
00517 TestInterface::CalculateMessage::CalculateMessage() : Message("CalculateMessage")
00518 {
00519   data_size = sizeof(CalculateMessage_data_t);
00520   data_ptr  = malloc(data_size);
00521   memset(data_ptr, 0, data_size);
00522   data      = (CalculateMessage_data_t *)data_ptr;
00523   data_ts   = (message_data_ts_t *)data_ptr;
00524   add_fieldinfo(IFT_INT32, "summand", 1, &data->summand);
00525   add_fieldinfo(IFT_INT32, "addend", 1, &data->addend);
00526 }
00527 
00528 /** Destructor */
00529 TestInterface::CalculateMessage::~CalculateMessage()
00530 {
00531   free(data_ptr);
00532 }
00533 
00534 /** Copy constructor.
00535  * @param m message to copy from
00536  */
00537 TestInterface::CalculateMessage::CalculateMessage(const CalculateMessage *m) : Message("CalculateMessage")
00538 {
00539   data_size = m->data_size;
00540   data_ptr  = malloc(data_size);
00541   memcpy(data_ptr, m->data_ptr, data_size);
00542   data      = (CalculateMessage_data_t *)data_ptr;
00543   data_ts   = (message_data_ts_t *)data_ptr;
00544 }
00545 
00546 /* Methods */
00547 /** Get summand value.
00548  * Summand
00549  * @return summand value
00550  */
00551 int32_t
00552 TestInterface::CalculateMessage::summand() const
00553 {
00554   return data->summand;
00555 }
00556 
00557 /** Get maximum length of summand value.
00558  * @return length of summand value, can be length of the array or number of 
00559  * maximum number of characters for a string
00560  */
00561 size_t
00562 TestInterface::CalculateMessage::maxlenof_summand() const
00563 {
00564   return 1;
00565 }
00566 
00567 /** Set summand value.
00568  * Summand
00569  * @param new_summand new summand value
00570  */
00571 void
00572 TestInterface::CalculateMessage::set_summand(const int32_t new_summand)
00573 {
00574   data->summand = new_summand;
00575 }
00576 
00577 /** Get addend value.
00578  * Addend
00579  * @return addend value
00580  */
00581 int32_t
00582 TestInterface::CalculateMessage::addend() const
00583 {
00584   return data->addend;
00585 }
00586 
00587 /** Get maximum length of addend value.
00588  * @return length of addend value, can be length of the array or number of 
00589  * maximum number of characters for a string
00590  */
00591 size_t
00592 TestInterface::CalculateMessage::maxlenof_addend() const
00593 {
00594   return 1;
00595 }
00596 
00597 /** Set addend value.
00598  * Addend
00599  * @param new_addend new addend value
00600  */
00601 void
00602 TestInterface::CalculateMessage::set_addend(const int32_t new_addend)
00603 {
00604   data->addend = new_addend;
00605 }
00606 
00607 /** Clone this message.
00608  * Produces a message of the same type as this message and copies the
00609  * data to the new message.
00610  * @return clone of this message
00611  */
00612 Message *
00613 TestInterface::CalculateMessage::clone() const
00614 {
00615   return new TestInterface::CalculateMessage(this);
00616 }
00617 /** Check if message is valid and can be enqueued.
00618  * @param message Message to check
00619  * @return true if the message is valid, false otherwise.
00620  */
00621 bool
00622 TestInterface::message_valid(const Message *message) const
00623 {
00624   const SetTestIntMessage *m0 = dynamic_cast<const SetTestIntMessage *>(message);
00625   if ( m0 != NULL ) {
00626     return true;
00627   }
00628   const SetTestStringMessage *m1 = dynamic_cast<const SetTestStringMessage *>(message);
00629   if ( m1 != NULL ) {
00630     return true;
00631   }
00632   const CalculateMessage *m2 = dynamic_cast<const CalculateMessage *>(message);
00633   if ( m2 != NULL ) {
00634     return true;
00635   }
00636   return false;
00637 }
00638 
00639 /// @cond INTERNALS
00640 EXPORT_INTERFACE(TestInterface)
00641 /// @endcond
00642 
00643 
00644 } // end namespace fawkes