Changed LICENSE. Now referenced to web site and file on project root directory
[anna.git] / include / anna / core / functions.hpp
index f41d281..d43dcd0 100644 (file)
@@ -1,37 +1,9 @@
-// ANNA - Anna is Not 'N' Anymore
-//
-// (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
-//
-// https://bitbucket.org/testillano/anna
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions
-// are met:
-//
-//     * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-//     * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following disclaimer
-// in the documentation and/or other materials provided with the
-// distribution.
-//     * Neither the name of Google Inc. nor the names of its
-// contributors may be used to endorse or promote products derived from
-// this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-//
-// Authors: eduardo.ramos.testillano@gmail.com
-//          cisco.tierra@gmail.com
+// ANNA - Anna is Not Nothingness Anymore                                                         //
+//                                                                                                //
+// (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
+//                                                                                                //
+// See project site at http://redmine.teslayout.com/projects/anna-suite                           //
+// See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
 
 
 #ifndef anna_core_functions_hpp
@@ -117,7 +89,7 @@ struct functions {
   /*
    * Indica el número de bits de un entero largo.
    */
-  static const int int64BitSize = sizeof(Integer64) * 8;
+  static const int int64BitSize = sizeof(S64) * 8;
 
   /**
      \param number Numero a convertir.
@@ -141,13 +113,13 @@ struct functions {
      Devuelve un literal con tel numero convertido a cadena decimal
      @return Un literal con el numero signo convertido a cadena decimal.
   */
-  static std::string asString(const Integer64 number) throw();
+  static std::string asString(const S64 number) throw();
 
   /**
      Devuelve un literal con tel numero convertido a cadena decimal
      @return Un literal con el numero signo convertido a cadena decimal.
   */
-  static std::string asString(const Unsigned64 number) throw();
+  static std::string asString(const U64 number) throw();
 
   /**
      \param _bool Booleano a convertir.
@@ -195,7 +167,7 @@ struct functions {
      \param number Numero a convertir.
      @return Un literal con el numero convertido a cadena decimal.
   */
-  static std::string asText(const char* comment, const Integer64 number)
+  static std::string asText(const char* comment, const S64 number)
   throw() {
     std::string result(comment);
     return result += asString(number);
@@ -258,7 +230,7 @@ struct functions {
      \param number Numero a convertir.
      @return Un literal con el numero convertido a cadena hexadecimal.
   */
-  static std::string asHexString(const Integer64 number) throw();
+  static std::string asHexString(const S64 number) throw();
 
   /**
      \param number Numero a convertir.
@@ -270,7 +242,7 @@ struct functions {
      \param number Numero a convertir.
      @return Un literal con el numero convertido a cadena hexadecimal.
   */
-  static std::string asHexString(const Unsigned64 number) throw() { return asHexString((Integer64) number); }
+  static std::string asHexString(const U64 number) throw() { return asHexString((S64) number); }
 
   /**
      \param comment Comentario que precede al valor.
@@ -288,7 +260,7 @@ struct functions {
      \param number Numero a convertir.
      @return Un literal con el numero convertido a cadena decimal.
   */
-  static std::string asHexText(const char* comment, const Integer64 number)
+  static std::string asHexText(const char* comment, const S64 number)
   throw() {
     std::string result(comment);
     return result += asHexString(number);
@@ -336,13 +308,13 @@ struct functions {
      Calcula la funcion hash de la cadena recibida como parametro.
      \param str Cadena a la que aplicar la funcion hash.
   */
-  static Integer64 hash(const char* str) throw();
+  static S64 hash(const char* str) throw();
 
   /**
      Calcula la funcion hash de la cadena recibida como parametro.
      \param str Cadena a la que aplicar la funcion hash.
   */
-  static Integer64 hash(const std::string& str) throw() { return hash(str.c_str()); }
+  static S64 hash(const std::string& str) throw() { return hash(str.c_str()); }
 
   /**
      Calcula la funcion hash exclusive de la cadena recibida como parametro.
@@ -441,7 +413,14 @@ struct functions {
   */
   static Microsecond hardwareClock() throw() {
     timespec ts;
-    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
+    //clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); // DONT works (original)
+    //clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); // DONT works
+    //clock_gettime(CLOCK_MONOTONIC, &ts); // works
+      // Note that CLOCK_MONOTONIC is subject to discontinuities from system time
+      //  adjustment in Linux. CLOCK_MONOTONIC_RAW was defined to get around this
+      //  (gets hardware time not adjusted by NTP).
+    clock_gettime(CLOCK_MONOTONIC_RAW, &ts); // works
+
     Microsecond result((Microsecond::type_t)1000000 * ts.tv_sec + ts.tv_nsec / 1000);
     return result;
   }
@@ -468,7 +447,7 @@ struct functions {
      Interpreta la cadena recibida como parametro como un entero de 32 bits.
      \return
    */
-  static Integer64 asInteger64(const char* str) throw();
+  static S64 asInteger64(const char* str) throw();
 
   /**
      Devuelve el identificador de thread desde el que es invocado este metodo.
@@ -509,8 +488,8 @@ struct functions {
    * \param n Valor a estudiar.
    * \return el número de bits necesarios para representar el valor recibido como parámetro.
    */
-  static int bitsize(const Integer64 n) throw() {
-    register int aux = n >> intBitSize;
+  static int bitsize(const S64 n) throw() {
+    int aux = n >> intBitSize;
     return (aux != 0) ? (bitsize(aux) + intBitSize) : bitsize((int) n);
   }
 
@@ -524,7 +503,7 @@ struct functions {
    * \param bitShift Número de bits a desplazar.
    * \param n2 Número a combinar con el resultado de la operación (n1 << bitShift).
    */
-  static Integer64 merge(const char* whatis, const int n1, const int n2, const int bitShift) throw(RuntimeException);
+  static S64 merge(const char* whatis, const int n1, const int n2, const int bitShift) throw(RuntimeException);
 
   /**
    * Calcula el logaritmo en base 2 del número recibo como parámetro.
@@ -1033,6 +1012,90 @@ struct functions {
   */
   static std::string socketVectorAsString(const socket_v & socketVector) throw();
 
+
+  /**
+    Endianess of the system
+
+    @result Returns true when the system is little endian, false if big endian
+  */
+  static bool littleEndian() throw();
+
+  /**
+     Encodes an integer number with 32 bits over a buffer with at least 4 bytes of length.
+     @param result Buffer where the number is encoded.
+     @param n Number to encode.
+     \return Buffer with the encoded number.
+   */
+  static const char* codeInteger(char* result, const int n) throw();
+
+  /**
+     Encodes an integer number with 16 bits over a buffer with at least 2 bytes of length.
+     @param result Buffer where the number is encoded.
+     @param n Number to encode.
+     \return Buffer with the encoded number.
+  */
+  static const char* codeShort(char* result, const short int n) throw();
+
+  /**
+     Encodes an integer number with 64 bits over a buffer with at least 8 bytes of length.
+     @param result Buffer where the number is encoded.
+     @param n Number to encode.
+     \return Buffer with the encoded number.
+   */
+  static const char* codeInteger64(char* result, const S64 n) throw();
+
+  /**
+     Encodes a floating number with 32 bits (according to the standard IEEE-754) over a buffer with at least 4 bytes of length.
+     @param result Buffer where the number is encoded.
+     @param n Number to encode.
+     \return Buffer with the encoded number.
+   */
+  static const char* codeFloat(char* result, const float n) throw();
+
+  /**
+     Encodes a floating number with 64 bits (according to the standard IEEE-754) over a buffer with at least 8 bytes of length.
+     @param result Buffer where the number is encoded.
+     @param n Number to encode.
+     \return Buffer with the encoded number.
+   */
+  static const char* codeDouble(char* result, const double n) throw();
+
+  /**
+     Decodes an 32 bits integer number contained in a 4-bytes buffer.
+     @param data Buffer with the encoded number.
+     @return Value for the number contained in the buffer.
+  */
+  static int decodeInteger(const char* data)  throw();
+
+  /**
+     Decodes an 16 bits integer number contained in a 2-bytes buffer.
+     @param data Buffer with the encoded number.
+     @return Value for the number contained in the buffer.
+  */
+  static short int decodeShort(const char* data)  throw();
+
+  /**
+     Decodes an 64 bits integer number contained in a 8-bytes buffer.
+     @param data Buffer with the encoded number.
+     @return Value for the number contained in the buffer.
+  */
+  static S64 decodeInteger64(const char* data)  throw();
+
+  /**
+     Decodes an 32 bits floating number (according to the standard IEEE-754) contained in a 4-bytes buffer.
+     @param data Buffer with the encoded number.
+     @return Value for the number contained in the buffer.
+  */
+  static float decodeFloat(const char* data)  throw();
+
+  /**
+     Decodes an 64 bits floating number (according to the standard IEEE-754) contained in a 8-bytes buffer.
+     @param data Buffer with the encoded number.
+     @return Value for the number contained in the buffer.
+  */
+  static double decodeDouble(const char* data)  throw();
+
+
   /**
   * Decodes an ISUP Number (called or calling party number).
   *
@@ -1062,7 +1125,6 @@ struct functions {
   */
   static void codeIsupNumber(const isup_number_t & isupNumber, bool calledOrCalling, std::string & target) throw(RuntimeException);
 
-
 private:
   static ExclusiveHash <std::string> st_stringExclusiveHash;
   static ExclusiveHash <std::string, int> st_string2intExclusiveHash;