diff options
Diffstat (limited to 'src/components/utils/include/utils')
24 files changed, 1660 insertions, 197 deletions
diff --git a/src/components/utils/include/utils/appenders_loader.h b/src/components/utils/include/utils/appenders_loader.h index 03d3217994..65556e6862 100644 --- a/src/components/utils/include/utils/appenders_loader.h +++ b/src/components/utils/include/utils/appenders_loader.h @@ -40,6 +40,7 @@ class AppendersLoader { AppendersLoader(); ~AppendersLoader(); bool Loaded() const; + private: void* handle_; }; diff --git a/src/components/utils/include/utils/back_trace.h b/src/components/utils/include/utils/back_trace.h index f2410e36bc..180714e5a6 100644 --- a/src/components/utils/include/utils/back_trace.h +++ b/src/components/utils/include/utils/back_trace.h @@ -68,8 +68,8 @@ class Backtrace { std::vector<void*> backtrace_; }; -std::ostream& operator<< (std::ostream& os, const Backtrace& bt); +std::ostream& operator<<(std::ostream& os, const Backtrace& bt); -} // namespace utils +} // namespace utils -#endif // SRC_COMPONENTS_UTILS_INCLUDE_UTILS_BACK_TRACE_H_ +#endif // SRC_COMPONENTS_UTILS_INCLUDE_UTILS_BACK_TRACE_H_ diff --git a/src/components/utils/include/utils/bitstream.h b/src/components/utils/include/utils/bitstream.h index 9bf41d187f..13eacc6cb1 100644 --- a/src/components/utils/include/utils/bitstream.h +++ b/src/components/utils/include/utils/bitstream.h @@ -32,7 +32,7 @@ #ifndef SRC_COMPONENTS_UTILS_INCLUDE_UTILS_BITSTREAM_H_ #define SRC_COMPONENTS_UTILS_INCLUDE_UTILS_BITSTREAM_H_ -#include "stdint.h" +#include <stdint.h> #include <climits> #include <cstring> @@ -57,12 +57,21 @@ class BitStream { // Mark stream as badly-formed. // Should be called by Extract* family of procedures if they decide // that stream is invalid - void MarkBad() { bad_ = true; } + void MarkBad() { + bad_ = true; + } // Predicates to check whether there were errors while parsing // Stream is good when it is created - bool IsGood() { return !bad_; } - bool IsBad() { return bad_; } - operator bool() { return IsGood(); } + bool IsGood() { + return !bad_; + } + bool IsBad() { + return bad_; + } + operator bool() { + return IsGood(); + } + private: // These two functions are used for internal stream checks only // Stream parser procedures must not define their logic depending on @@ -78,13 +87,13 @@ class BitStream { // Extract single value, amount of bits read from stream depends on T size // If there is not enough data in the stream, stream is marked bad - template<typename T> + template <typename T> void Extract(T& val); // Read single value, amount of bits read from stream is signaled by |bits| // parameter. T must be wide enough to hold this amount of bits. // If there is not enough data in the stream, stream is marked bad - template<typename T> + template <typename T> void ExtractBits(T& val, size_t bits); // Extract |length| bytes from the stream. Stream read position @@ -94,10 +103,11 @@ class BitStream { private: const uint8_t* bytes_; - const size_t bytes_count_; + const size_t bytes_count_; size_t byte_offset_; size_t bit_offset_; bool bad_; + private: friend void Extract(BitStream*, uint8_t*); friend void Extract(BitStream*, uint8_t*, size_t); @@ -105,7 +115,6 @@ class BitStream { friend void Extract(BitStream*, uint32_t*, size_t); friend void Extract(BitStream*, std::string*, size_t); friend void Extract(BitStream*, std::vector<uint8_t>*, size_t); - }; // Extract single byte from stream @@ -133,16 +142,15 @@ void Extract(BitStream* bs, std::string* str, size_t length); // vector |data|. If stream is too short it is marked bad. void Extract(BitStream* bs, std::vector<uint8_t>* data, size_t length); - // Template member definitions -template<typename T> +template <typename T> void BitStream::Extract(T& val) { // Slow but simple implementation // It's a space for bit stream reading optimization ExtractBits(val, sizeof(val) * CHAR_BIT); } -template<typename T> +template <typename T> void BitStream::ExtractBits(T& val, size_t bits) { DCHECK(sizeof(val) * CHAR_BIT >= bits); if (IsGood()) { @@ -164,7 +172,6 @@ void BitStream::ExtractBits(T& val, size_t bits) { } } -} // namespace utils - +} // namespace utils -#endif // SRC_COMPONENTS_UTILS_INCLUDE_UTILS_BITSTREAM_H_ +#endif // SRC_COMPONENTS_UTILS_INCLUDE_UTILS_BITSTREAM_H_ diff --git a/src/components/utils/include/utils/convert_utils.h b/src/components/utils/include/utils/convert_utils.h new file mode 100644 index 0000000000..0609164a60 --- /dev/null +++ b/src/components/utils/include/utils/convert_utils.h @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2016, Ford Motor Company + * All rights reserved. + * + * 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 the Ford Motor Company 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 HOLDER 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. + */ + +#ifndef SRC_COMPONENTS_UTILS_INCLUDE_UTILS_CONVERT_UTILS_H_ +#define SRC_COMPONENTS_UTILS_INCLUDE_UTILS_CONVERT_UTILS_H_ + +#include <stdint.h> +#include <limits> + +namespace utils { + +/** + * Convert int64 value to long long int value + * Using when int64 value should be assign to JSON value + */ +long long int ConvertInt64ToLongLongInt(const int64_t value); + +/** + * Convert long long int value to int64 value + */ +int64_t ConvertLongLongIntToInt64(const long long int value); + +/** + * Convert uint64 value to unsigned long long int value + * Using when uint64 value should be assign to JSON value + */ +unsigned long long int ConvertUInt64ToLongLongUInt(const uint64_t value); + +/** + * Convert unsigned long long int value to uint64 value + */ +uint64_t ConvertLongLongUIntToUInt64(const unsigned long long int value); + +/** + * Convert one number value to another type value + */ +template <typename InputType, typename OutputType> +OutputType SafeStaticCast(const InputType value) { + DCHECK_OR_RETURN(value >= std::numeric_limits<OutputType>::min(), + std::numeric_limits<OutputType>::min()); + DCHECK_OR_RETURN(value <= std::numeric_limits<OutputType>::max(), + std::numeric_limits<OutputType>::max()); + return static_cast<OutputType>(value); +} + +} // namespace utils + +#endif // SRC_COMPONENTS_UTILS_INCLUDE_UTILS_CONVERT_UTILS_H_ diff --git a/src/components/utils/include/utils/file_system.h b/src/components/utils/include/utils/file_system.h index a132837ca0..5862241c9c 100644 --- a/src/components/utils/include/utils/file_system.h +++ b/src/components/utils/include/utils/file_system.h @@ -41,7 +41,6 @@ namespace file_system { - /** * @brief Get available disc space. * @@ -147,6 +146,13 @@ void Close(std::ofstream* file_stream); std::string CurrentWorkingDirectory(); /** + * @brief Allows to obtaine absolute path for certain path. + * @param path the file name for which absolute path have to be calculated. + * @return absolute path for certain path. + */ +std::string GetAbsolutePath(const std::string& path); + +/** * @brief Removes file * * @param name path to file @@ -158,7 +164,8 @@ bool DeleteFile(const std::string& name); * @brief Removes directory. * * @param name path to directory. - * @param is_recursively true if you need delete directory recursively, otherwise false. + * @param is_recursively true if you need delete directory recursively, + *otherwise false. * @return returns true if the directory is successfully deleted. */ bool RemoveDirectory(const std::string& directory_name, @@ -213,8 +220,7 @@ bool WriteBinaryFile(const std::string& name, * @param result read data * @return returns true if the operation is successfully. */ -bool ReadBinaryFile(const std::string& name, - std::vector<uint8_t>& result); +bool ReadBinaryFile(const std::string& name, std::vector<uint8_t>& result); bool ReadFile(const std::string& name, std::string& result); @@ -248,8 +254,7 @@ uint64_t GetFileModificationTime(const std::string& path); * @param dst Destination file path * @return if result success return true */ -bool CopyFile(const std::string& src, - const std::string& dst); +bool CopyFile(const std::string& src, const std::string& dst); /** * @brief Move file from source to destination @@ -258,8 +263,8 @@ bool CopyFile(const std::string& src, * @param dst Destination file path * @return if result success return true */ -bool MoveFile(const std::string& src, - const std::string& dst); +bool MoveFile(const std::string& src, const std::string& dst); + void remove_directory_content(const std::string& directory_name); } // namespace file_system diff --git a/src/components/utils/include/utils/gen_hash.h b/src/components/utils/include/utils/gen_hash.h index e4102d36db..acb2e28a1d 100644 --- a/src/components/utils/include/utils/gen_hash.h +++ b/src/components/utils/include/utils/gen_hash.h @@ -37,6 +37,10 @@ namespace utils { +namespace custom_string { +class CustomString; +} + /** * @brief generate random alphanumeric string of specified length * @param size length of random string @@ -45,6 +49,32 @@ namespace utils { const std::string gen_hash(size_t size); +/** + * @brief Allows to generate hash from the specified string. + * The djb2 algorithm uses for hash generation. + * @param str_to_hash - the string from which hash should be generated. + * @return integer hash for the specified string. + */ +int32_t Djb2HashFromString(const std::string& str_to_hash); + +/** + * @brief Generates hash. + * The faq6 algorithm uses for hash generation. + * @param str_to_hash - the CustomSting contains ASCII or UTF8 string from which + * hash should be generated. + * @return uint32_t hash for the specified string. + */ +uint32_t CaseInsensitiveFaq6HashFromString( + const custom_string::CustomString& str_to_hash); + +/** + * @brief Transforms input string to lower case and then generates hash. + * The faq6 algorithm uses for hash generation. + * @param str_to_hash - the string from which hash should be generated. + * @return uint32_t hash for the specified string. + */ +uint32_t CaseInsensitiveFaq6HashFromString(const char* cstr); + } // namespace utils #endif // SRC_COMPONENTS_UTILS_INCLUDE_UTILS_GEN_HASH_H_ diff --git a/src/components/utils/include/utils/helpers.h b/src/components/utils/include/utils/helpers.h index e616dd56c1..17d4af27a1 100644 --- a/src/components/utils/include/utils/helpers.h +++ b/src/components/utils/include/utils/helpers.h @@ -31,12 +31,13 @@ */ #ifndef SRC_COMPONENTS_UTILS_INCLUDE_UTILS_HELPERS_H #define SRC_COMPONENTS_UTILS_INCLUDE_UTILS_HELPERS_H - +#include <algorithm> /** * These helpers allows to simplify compare strategy between some objects. * Suppose user has some enum with value E with some numbers of possible values * enum E {V1, V2, V3, V5}; - * So we want to know if some user-input value is belong to one of the enum's subset. + * So we want to know if some user-input value is belong to one of the enum's + *subset. * Usually user has to do next routine * * E input_value = V3; @@ -56,64 +57,72 @@ */ namespace helpers { - template<typename T> - bool EQ (T what, T to) { - return what == to; - } +template <typename T> +bool EQ(T what, T to) { + return what == to; +} + +template <typename T> +bool NEQ(T what, T to) { + return !EQ<T>(what, to); +} - template<typename T> - bool NEQ (T what, T to) { - return !EQ<T>(what, to); - } +template <class U = bool> +bool ALL(U what, U to) { + return what && to; +} - template<class U = bool> - bool ALL (U what, U to) { - return what && to; - } +template <class U = bool> +bool ONE(U what, U to) { + return what || to; +} - template<class U = bool> - bool ONE (U what, U to) { - return what || to; - } +template <typename T, + bool (*CompareType)(T, T), + bool (*CmpStrategy)(bool, bool)> +bool Compare(T what, T to) { + return CompareType(what, to); +} - template <typename T, - bool (*CompareType)(T ,T), - bool (*CmpStrategy)(bool ,bool)> - bool Compare (T what, T to) { - return CompareType(what, to); - } +template <typename T, + bool (*CompareType)(T, T), + bool (*CmpStrategy)(bool, bool)> +bool Compare(T what, T to, T to1) { + return CmpStrategy(Compare<T, CompareType, CmpStrategy>(what, to), + Compare<T, CompareType, CmpStrategy>(what, to1)); +} - template <typename T, - bool (*CompareType)(T ,T), - bool (*CmpStrategy)(bool, bool)> - bool Compare(T what, T to, T to1) { - return CmpStrategy(Compare<T, CompareType, CmpStrategy>(what, to), - Compare<T, CompareType, CmpStrategy>(what, to1)); - } +template <typename T, + bool (*CompareType)(T, T), + bool (*CmpStrategy)(bool, bool)> +bool Compare(T what, T to, T to1, T to2) { + return CmpStrategy(Compare<T, CompareType, CmpStrategy>(what, to, to1), + Compare<T, CompareType, CmpStrategy>(what, to2)); +} - template <typename T, - bool (*CompareType)(T ,T), - bool (*CmpStrategy)(bool, bool)> - bool Compare(T what, T to, T to1, T to2) { - return CmpStrategy(Compare<T, CompareType, CmpStrategy>(what, to, to1), - Compare<T, CompareType, CmpStrategy>(what, to2)); - } +template <typename T, + bool (*CompareType)(T, T), + bool (*CmpStrategy)(bool, bool)> +bool Compare(T what, T to, T to1, T to2, T to3) { + return CmpStrategy(Compare<T, CompareType, CmpStrategy>(what, to, to1, to2), + Compare<T, CompareType, CmpStrategy>(what, to3)); +} - template <typename T, - bool (*CompareType)(T ,T), - bool (*CmpStrategy)(bool, bool)> - bool Compare(T what, T to, T to1, T to2, T to3) { - return CmpStrategy(Compare<T, CompareType, CmpStrategy>(what, to, to1, to2), - Compare<T, CompareType, CmpStrategy>(what, to3)); - } +template <typename T, + bool (*CompareType)(T, T), + bool (*CmpStrategy)(bool, bool)> +bool Compare(T what, T to, T to1, T to2, T to3, T to4) { + return CmpStrategy( + Compare<T, CompareType, CmpStrategy>(what, to, to1, to2, to3), + Compare<T, CompareType, CmpStrategy>(what, to4)); +} - template <typename T, - bool (*CompareType)(T ,T), - bool (*CmpStrategy)(bool, bool)> - bool Compare(T what, T to, T to1, T to2, T to3, T to4) { - return CmpStrategy(Compare<T, CompareType, CmpStrategy>(what, to, to1, to2, to3), - Compare<T, CompareType, CmpStrategy>(what, to4)); - } +template <typename Container> +bool in_range(const Container& container, + const typename Container::value_type& value) { + return std::find(container.begin(), container.end(), value) != + container.end(); +} } -#endif // SRC_COMPONENTS_UTILS_INCLUDE_UTILS_HELPERS_H +#endif // SRC_COMPONENTS_UTILS_INCLUDE_UTILS_HELPERS_H diff --git a/src/components/utils/include/utils/log_message_loop_thread.h b/src/components/utils/include/utils/log_message_loop_thread.h index 87b6c7e531..deea1a07c0 100644 --- a/src/components/utils/include/utils/log_message_loop_thread.h +++ b/src/components/utils/include/utils/log_message_loop_thread.h @@ -39,7 +39,6 @@ #include "utils/macro.h" #include "utils/threads/message_loop_thread.h" -#include "utils/singleton.h" namespace logger { @@ -54,26 +53,20 @@ typedef struct { typedef std::queue<LogMessage> LogMessageQueue; -typedef threads::MessageLoopThread<LogMessageQueue> LogMessageLoopThreadTemplate; +typedef threads::MessageLoopThread<LogMessageQueue> + LogMessageLoopThreadTemplate; class LogMessageHandler : public LogMessageLoopThreadTemplate::Handler { public: virtual void Handle(const LogMessage message) OVERRIDE; }; -class LogMessageLoopThread : - public LogMessageLoopThreadTemplate, - public utils::Singleton<LogMessageLoopThread> { - +class LogMessageLoopThread : public LogMessageLoopThreadTemplate { public: - ~LogMessageLoopThread(); - - private: LogMessageLoopThread(); + ~LogMessageLoopThread(); -DISALLOW_COPY_AND_ASSIGN(LogMessageLoopThread); -FRIEND_BASE_SINGLETON_CLASS(LogMessageLoopThread); - + DISALLOW_COPY_AND_ASSIGN(LogMessageLoopThread); }; } // namespace logger diff --git a/src/components/utils/include/utils/qdb_wrapper/sql_database.h b/src/components/utils/include/utils/qdb_wrapper/sql_database.h new file mode 100644 index 0000000000..ba5c8a722a --- /dev/null +++ b/src/components/utils/include/utils/qdb_wrapper/sql_database.h @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * 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 the Ford Motor Company 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 HOLDER 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. + */ + +#ifndef SRC_COMPONENTS_POLICY_QDB_WRAPPER_INCLUDE_QDB_WRAPPER_SQL_DATABASE_H_ +#define SRC_COMPONENTS_POLICY_QDB_WRAPPER_INCLUDE_QDB_WRAPPER_SQL_DATABASE_H_ + +#include <qdb/qdb.h> +#include <string> +#include "qdb_wrapper/sql_error.h" +#include "utils/lock.h" + +namespace utils { +namespace dbms { + +class SQLQuery; + +/** + * Represents a connection to a database. + */ +class SQLDatabase { + public: + explicit SQLDatabase(const std::string& db_name); + ~SQLDatabase(); + + /** + * Opens connection to the temporary in-memory database + * @return true if successfully + */ + bool Open(); + + /** + * Closes connection to the database + */ + void Close(); + + /** + * Begins a transaction on the database + * @return true if successfully + */ + bool BeginTransaction(); + + /** + * Commits a transaction to the database + * @return true if successfully + */ + bool CommitTransaction(); + + /** + * Rolls back a transaction on the database + * @return true if successfully + */ + bool RollbackTransaction(); + + /** + * Gets information about the last error that occurred on the database + * @return last error + */ + SQLError LastError() const; + + /** + * Call backup for opened DB + */ + bool Backup(); + + protected: + /** + * Gets connection to the SQLite database + * @return pointer to connection + */ + qdb_hdl_t* conn() const; + + private: + /** + * The connection to the SQLite database + */ + qdb_hdl_t* conn_; + + /** + * Lock for guarding connection to database + */ + sync_primitives::Lock conn_lock_; + + /** + * The database name + */ + std::string db_name_; + + /** + * The last error that occurred on the database + */ + Error error_; + + /** + * Execs query for internal using in this class + * @param query sql query without return results + * @return true if query was executed successfully + */ + inline bool Exec(const std::string& query); + + friend class SQLQuery; +}; + +} // namespace dbms +} // namespace utils + +#endif // SRC_COMPONENTS_POLICY_QDB_WRAPPER_INCLUDE_QDB_WRAPPER_SQL_DATABASE_H_ diff --git a/src/components/utils/include/utils/qdb_wrapper/sql_error.h b/src/components/utils/include/utils/qdb_wrapper/sql_error.h new file mode 100644 index 0000000000..25fd558308 --- /dev/null +++ b/src/components/utils/include/utils/qdb_wrapper/sql_error.h @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * 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 the Ford Motor Company 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 HOLDER 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. + */ + +#ifndef SRC_COMPONENTS_POLICY_QDB_WRAPPER_INCLUDE_QDB_WRAPPER_SQL_ERROR_H_ +#define SRC_COMPONENTS_POLICY_QDB_WRAPPER_INCLUDE_QDB_WRAPPER_SQL_ERROR_H_ + +#include <string> + +namespace utils { +namespace dbms { + +typedef enum Error { + OK = 0, /* Successful result */ + ERROR /* Error */ +} Error; + +/** + * Provides SQL database error information + */ +class SQLError { + public: + SQLError(Error number, const std::string& text = ""); + + /** + * Gets number of error + * @return error number + */ + Error number() const; + + /** + * Gets text description of the error + * @return text + */ + std::string text() const; + + private: + /** + * Number of the error + */ + Error number_; + + /** + * Description of the error + */ + mutable std::string text_; +}; + +} // namespace dbms +} // namespace utils + +#endif // SRC_COMPONENTS_POLICY_QDB_WRAPPER_INCLUDE_QDB_WRAPPER_SQL_ERROR_H_ diff --git a/src/components/utils/include/utils/qdb_wrapper/sql_query.h b/src/components/utils/include/utils/qdb_wrapper/sql_query.h new file mode 100644 index 0000000000..f1da6ccad9 --- /dev/null +++ b/src/components/utils/include/utils/qdb_wrapper/sql_query.h @@ -0,0 +1,265 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * 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 the Ford Motor Company 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 HOLDER 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. + */ + +#ifndef SRC_COMPONENTS_POLICY_QDB_WRAPPER_INCLUDE_QDB_WRAPPER_SQL_QUERY_H_ +#define SRC_COMPONENTS_POLICY_QDB_WRAPPER_INCLUDE_QDB_WRAPPER_SQL_QUERY_H_ + +#include <stdint.h> +#include <qdb/qdb.h> +#include <string> +#include <vector> +#include <utility> +#include "qdb_wrapper/sql_error.h" +#include "utils/lock.h" + +namespace utils { +namespace dbms { + +class SQLDatabase; + +/** + * Provides a means of executing and manipulating SQL statements + */ +class SQLQuery { + public: + explicit SQLQuery(SQLDatabase* db); + ~SQLQuery(); + + /** + * Prepares the SQL query for executing + * @param query the utf-8 string of SQL query + * @return true if successfully + */ + bool Prepare(const std::string& query); + + /** + * Resets the binds of query for re-executing + * @return true if successfully + */ + bool Reset(); + + /** + * Deletes prepared SQL query + */ + void Finalize(); + + /** + * Executes SQL query without make binds + * @param query the utf-8 string of SQL query + * @return true if successfull + */ + bool Exec(const std::string& query); + + /** + * Executes prepared SQL query and positions the query on the first record + * @return true if successfull + */ + bool Exec(); + + /** + * Retrieves the next record in the result, if available, + * and positions the query on the retrieved record + * @return true if record was retrieved successfully, false if a error was + * or the result is empty or was retrieves last record + */ + bool Next(); + + /** + * Binds null in the prepared query + * @param pos position of param in the query + */ + void Bind(int pos); + + /** + * Binds int value in the prepared query. + * @param pos position of param in the query + * @param value value of param + */ + void Bind(int pos, int value); + + /** + * Binds int64_t value in the prepared query. + * @param pos position of param in the query + * @param value value of param + */ + void Bind(int pos, int64_t value); + + /** + * Binds double value in the prepared query. + * @param pos position of param in the query + * @param value value of param + */ + void Bind(int pos, double value); + + /** + * Binds bool value in the prepared query. + * @param pos position of param in the query + * @param value value of param + */ + void Bind(int pos, bool value); + + /** + * Binds string in the prepared query. + * @param pos position of param in the query + * @param value utf-8 string + */ + void Bind(int pos, const std::string& value); + + /** + * Gets value in the result record + * @param pos position of value + * @return boolean value + */ + bool GetBoolean(int pos) const; + + /** + * Gets value in the result record + * @param pos position of value + * @return integer value + */ + int GetInteger(int pos) const; + + /** + * Gets value in the result record + * @param pos position of value + * @return unsigned integer value + */ + uint32_t GetUInteger(int pos) const; + + /** + * Gets value in the result record + * @param pos position of value + * @return int64_t value + */ + int64_t GetLongInt(int pos) const; + + /** + * Gets value in the result record + * @param pos position of value + * @return double value + */ + double GetDouble(int pos) const; + + /** + * Gets value in the result record + * @param pos position of value + * @return string value + */ + std::string GetString(int pos) const; + + /** + * Checks if value is null + * @param pos position of value + * @return true if value is null + */ + bool IsNull(int pos) const; + + /** + * Gets last id of insert row + * @return id of insert row + */ + int64_t LastInsertId() const; + + /** + * Gets string of the query + * @return string of the query + */ + const std::string& query() const; + + /** + * Gets information about the last error that occurred on the database + * @return last error + */ + SQLError LastError() const; + + private: + /** + * The instantiation of database + */ + SQLDatabase* db_; + + /** + * The string of query + */ + std::string query_; + + /** + * The id of SQL statement in QDB + */ + int statement_; + + /** + * Containers for keeping bind data + */ + std::vector<std::pair<int, int64_t> > int_binds_; + std::vector<std::pair<int, double> > double_binds_; + std::vector<std::pair<int, std::string> > string_binds_; + std::vector<int> null_binds_; + + /** + * The array for binging data to the prepare query + */ + qdb_binding_t* bindings_; + + /** + * Lock for guarding bindings + */ + sync_primitives::Lock bindings_lock_; + + /** + * The result of query + */ + qdb_result_t* result_; + + /** + * The current row in result for select + */ + int current_row_; + + /** + * The number of rows in a result + */ + int rows_; + + /** + * The last error that occurred with this query + */ + Error error_; + + uint8_t SetBinds(); + bool Result(); +}; + +} // namespace dbms +} // namespace utils + +#endif // SRC_COMPONENTS_POLICY_QDB_WRAPPER_INCLUDE_QDB_WRAPPER_SQL_QUERY_H_ diff --git a/src/components/utils/include/utils/resource_usage.h b/src/components/utils/include/utils/resource_usage.h index ff90b2c22f..4dd8a7eafd 100644 --- a/src/components/utils/include/utils/resource_usage.h +++ b/src/components/utils/include/utils/resource_usage.h @@ -44,8 +44,8 @@ #include "utils/logger.h" -#define MAX_COMM_LEN 128 -#define MAX_CMDLINE_LEN 128 +#define MAX_COMM_LEN 128 +#define MAX_CMDLINE_LEN 128 namespace utils { @@ -56,7 +56,7 @@ struct ResourseUsage { }; class Resources { - public: + public: typedef uint32_t MemInfo; #if defined(__QNXNTO__) typedef procfs_info PidStats; @@ -108,19 +108,18 @@ class Resources { unsigned long long delayacct_blkio_ticks; unsigned long guest_time; long int cguest_time; - }; + }; #else #endif - public: - /* - * @brief Returns current resource usage of process - * @return Raw pointer on ResourseUsage if success, otherwise return NULL - */ + public: + /* + * @brief Returns current resource usage of process + * @return Raw pointer on ResourseUsage if success, otherwise return NULL + */ static ResourseUsage* getCurrentResourseUsage(); -private: - + private: #ifdef BUILD_TESTS friend class ResourceUsagePrivateTest; FRIEND_TEST(ResourceUsagePrivateTest, ReadStatFileTest); @@ -134,7 +133,8 @@ private: /* * @brief reads /proc/PID/stat file on linux * do not work on QNX ( return false, output wan't be changed ) - * @param output - storage for result string ( there will be separated content of /proc/PID/stat ) + * @param output - storage for result string ( there will be separated content + * of /proc/PID/stat ) * @return true on succes false onb fail */ static bool ReadStatFile(std::string& output); @@ -171,9 +171,6 @@ private: */ static const char* proc; }; - } - - #endif /* SRC_COMPONENTS_UTILS_INCLUDE_UTILS_RESOURCE_USAGE_H_ */ diff --git a/src/components/utils/include/utils/signals.h b/src/components/utils/include/utils/signals.h index 6c91836309..48120d53cd 100644 --- a/src/components/utils/include/utils/signals.h +++ b/src/components/utils/include/utils/signals.h @@ -34,15 +34,15 @@ #define SRC_COMPONENTS_UTILS_INCLUDE_UTILS_SIGNALS_H_ #ifdef __QNXNTO__ -typedef void (*sighandler_t) (int); +typedef void (*sighandler_t)(int); #else #include <signal.h> #endif namespace utils { -bool SubscribeToTerminateSignal(sighandler_t func); -bool SubscribeToFaultSignal(sighandler_t func); +bool UnsibscribeFromTermination(); +bool WaitTerminationSignals(sighandler_t sig_handler); } // namespace utils diff --git a/src/components/utils/include/utils/singleton.h b/src/components/utils/include/utils/singleton.h index fff7294d1c..b73780ee1d 100644 --- a/src/components/utils/include/utils/singleton.h +++ b/src/components/utils/include/utils/singleton.h @@ -43,15 +43,13 @@ namespace deleters { class DummyDeleter { public: - void grab(void* pointer) { - } + void grab(void* pointer) {} }; -template<typename T> +template <typename T> class Deleter { public: - Deleter() : pointer_(0) { - } + Deleter() : pointer_(0) {} ~Deleter() { if (pointer_) { delete pointer_; @@ -60,69 +58,68 @@ class Deleter { void grab(T* pointer) { pointer_ = pointer; } + private: T* pointer_; }; } // namespace deleters -template<typename T, class Deleter = deleters::DummyDeleter> +template <typename T, class Deleter = deleters::DummyDeleter> class Singleton { -/** - * @brief Singleton template - * Singleton classes must derive from this template specialized with class itself: - * - * class MySingleton : public Singleton<MySingleton> {...}; - * - * All such classes must declare instance() method as friend - * by adding FRIEND_BASE_SINGLETON_CLASS macro from macro.h to class definition: - * - * FRIEND_BASE_SINGLETON_CLASS(MySingleton); - * - * Instance of this class (if created) can be deleted by Deleter destructor - * which is called after main() (or from exit()) - * This requires T destructor to be accessible for Deleter (e.g. public) - * Deleter template parameter can be specified with any class - * with public default constructor, destructor and method - * void grab(T*); - * However, default Deleter specification does nothing - * - * Also instance can be deleted explicitly by calling destroy() method - * - * Both instance() and destroy() methods are thread safe - * but not thread safety between simultaneous calls - * of instance() and destroy() is cared about - */ + /** + * @brief Singleton template + * Singleton classes must derive from this template specialized with class + *itself: + * + * class MySingleton : public Singleton<MySingleton> {...}; + * + * All such classes must declare instance() method as friend + * by adding FRIEND_BASE_SINGLETON_CLASS macro from macro.h to class + *definition: + * + * FRIEND_BASE_SINGLETON_CLASS(MySingleton); + * + * Instance of this class (if created) can be deleted by Deleter destructor + * which is called after main() (or from exit()) + * This requires T destructor to be accessible for Deleter (e.g. public) + * Deleter template parameter can be specified with any class + * with public default constructor, destructor and method + * void grab(T*); + * However, default Deleter specification does nothing + * + * Also instance can be deleted explicitly by calling destroy() method + * + * Both instance() and destroy() methods are thread safe + * but not thread safety between simultaneous calls + * of instance() and destroy() is cared about + */ public: -/** - * @brief Returns the singleton of class - */ + /** + * @brief Returns the singleton of class + */ static T* instance(); -/** - * @brief Destroys the singleton (if it had been created) - */ + /** + * @brief Destroys the singleton (if it had been created) + */ static void destroy(); -/** - * @brief Checks whether the singleton exists - */ + /** + * @brief Checks whether the singleton exists + */ static bool exists(); private: - static T** instance_pointer(); static Deleter* deleter(); static sync_primitives::Lock lock_; }; - -template<typename T, class Deleter> +template <typename T, class Deleter> sync_primitives::Lock Singleton<T, Deleter>::lock_; - -template<typename T, class Deleter> +template <typename T, class Deleter> T* Singleton<T, Deleter>::instance() { - T* local_instance; atomic_pointer_assign(local_instance, *instance_pointer()); memory_barrier(); @@ -142,9 +139,8 @@ T* Singleton<T, Deleter>::instance() { return local_instance; } -template<typename T, class Deleter> +template <typename T, class Deleter> void Singleton<T, Deleter>::destroy() { - T* local_instance; atomic_pointer_assign(local_instance, *instance_pointer()); memory_barrier(); @@ -162,18 +158,18 @@ void Singleton<T, Deleter>::destroy() { } } -template<typename T, class Deleter> +template <typename T, class Deleter> bool Singleton<T, Deleter>::exists() { return *instance_pointer() != 0; } -template<typename T, class Deleter> +template <typename T, class Deleter> T** Singleton<T, Deleter>::instance_pointer() { static T* instance = 0; return &instance; } -template<typename T, class Deleter> +template <typename T, class Deleter> Deleter* Singleton<T, Deleter>::deleter() { static Deleter deleter; return &deleter; diff --git a/src/components/utils/include/utils/sqlite_wrapper/sql_database.h b/src/components/utils/include/utils/sqlite_wrapper/sql_database.h new file mode 100644 index 0000000000..720628ef3c --- /dev/null +++ b/src/components/utils/include/utils/sqlite_wrapper/sql_database.h @@ -0,0 +1,167 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * 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 the Ford Motor Company 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 HOLDER 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. + */ + +#ifndef SRC_COMPONENTS_POLICY_SQLITE_WRAPPER_INCLUDE_SQLITE_WRAPPER_SQL_DATABASE_H_ +#define SRC_COMPONENTS_POLICY_SQLITE_WRAPPER_INCLUDE_SQLITE_WRAPPER_SQL_DATABASE_H_ + +#include <string> +#include "utils/sqlite_wrapper/sql_error.h" +#include "utils/lock.h" + +struct sqlite3; + +namespace utils { +namespace dbms { + +class SQLQuery; + +/** + * Represents a connection to a database. + */ +class SQLDatabase { + public: + SQLDatabase(); + explicit SQLDatabase(const std::string& filename); + ~SQLDatabase(); + + /** + * Opens connection to the temporary in-memory database + * @return true if successfully + */ + bool Open(); + + /** + * Closes connection to the database + */ + void Close(); + + /** + * Begins a transaction on the database + * @return true if successfully + */ + bool BeginTransaction(); + + /** + * Commits a transaction to the database + * @return true if successfully + */ + bool CommitTransaction(); + + /** + * Rolls back a transaction on the database + * @return true if successfully + */ + bool RollbackTransaction(); + + /** + * Gets information about the last error that occurred on the database + * @return last error + */ + SQLError LastError() const; + + /** + * Sets path to database + * If the database is already opened then need reopen it + */ + void set_path(const std::string& path); + + /** + * @brief get_path databse location path. + * + * @return the path to the database location + */ + std::string get_path() const; + + /** + * Checks if database is read/write + * @return true if database is read/write + */ + bool IsReadWrite(); + + /** + * Call backup for opened DB + */ + bool Backup(); + + protected: + /** + * Gets connection to the SQLite database + * @return pointer to connection + */ + sqlite3* conn() const; + + private: + /** + * The connection to the SQLite database + */ + sqlite3* conn_; + + /** + * Lock for guarding connection to database + */ + sync_primitives::Lock conn_lock_; + + /** + * The filename of database + */ + std::string databasename_; + + /** + * The last error that occurred on the database + */ + int error_; + + /** + * The temporary in-memory database + * @see SQLite manual + */ + static const std::string kInMemory; + + /** + * The extension of filename of database + */ + static const std::string kExtension; + + /** + * Execs query for internal using in this class + * @param query sql query without return results + * @return true if query was executed successfully + */ + inline bool Exec(const std::string& query); + + friend class SQLQuery; +}; + +} // namespace dbms +} // namespace utils + +#endif // SRC_COMPONENTS_POLICY_SQLITE_WRAPPER_INCLUDE_SQLITE_WRAPPER_SQL_DATABASE_H_ diff --git a/src/components/utils/include/utils/sqlite_wrapper/sql_error.h b/src/components/utils/include/utils/sqlite_wrapper/sql_error.h new file mode 100644 index 0000000000..8a53e12169 --- /dev/null +++ b/src/components/utils/include/utils/sqlite_wrapper/sql_error.h @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * 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 the Ford Motor Company 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 HOLDER 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. + */ + +#ifndef SRC_COMPONENTS_POLICY_SQLITE_WRAPPER_INCLUDE_SQLITE_WRAPPER_SQL_ERROR_H_ +#define SRC_COMPONENTS_POLICY_SQLITE_WRAPPER_INCLUDE_SQLITE_WRAPPER_SQL_ERROR_H_ + +#include <string> + +namespace utils { +namespace dbms { + +typedef enum Error { + OK = 0, /* Successful result */ + ERROR, /* SQL error or missing database */ + INTERNAL, /* Internal logic error in SQLite */ + PERM, /* Access permission denied */ + ABORT, /* Callback routine requested an abort */ + BUSY, /* The database file is locked */ + LOCKED, /* A table in the database is locked */ + NOMEM, /* A malloc() failed */ + READONLY, /* Attempt to write a readonly database */ + INTERRUPT, /* Operation terminated by sqlite3_interrupt()*/ + IOERR, /* Some kind of disk I/O error occurred */ + CORRUPT, /* The database disk image is malformed */ + NOTFOUND, /* Unknown opcode in sqlite3_file_control() */ + FULL, /* Insertion failed because database is full */ + CANTOPEN, /* Unable to open the database file */ + PROTOCOL, /* Database lock protocol error */ + EMPTY, /* Database is empty */ + SCHEMA, /* The database schema changed */ + TOOBIG, /* String or BLOB exceeds size limit */ + CONSTRAINT, /* Abort due to constraint violation */ + MISMATCH, /* Data type mismatch */ + MISUSE, /* Library used incorrectly */ + NOLFS, /* Uses OS features not supported on host */ + AUTH, /* Authorization denied */ + FORMAT, /* Auxiliary database format error */ + RANGE, /* 2nd parameter to sqlite3_bind out of range */ + NOTADB, /* File opened that is not a database file */ + NOTICE, /* Notifications from sqlite3_log() */ + WARNING, /* Warnings from sqlite3_log() */ + ROW = 100, /* sqlite3_step() has another row ready */ + DONE = 101 /* sqlite3_step() has finished executing */ +} Error; + +/** + * Provides SQL database error information + */ +class SQLError { + public: + SQLError(Error number, const std::string& text = ""); + + /** + * Gets number of error + * @return error number + */ + Error number() const; + + /** + * Gets text description of the error + * @return text + */ + std::string text() const; + + private: + /** + * Number of the error + */ + Error number_; + + /** + * Description of the error + */ + mutable std::string text_; +}; + +} // namespace dbms +} // namespace utils + +#endif // SRC_COMPONENTS_POLICY_SQLITE_WRAPPER_INCLUDE_SQLITE_WRAPPER_SQL_ERROR_H_ diff --git a/src/components/utils/include/utils/sqlite_wrapper/sql_query.h b/src/components/utils/include/utils/sqlite_wrapper/sql_query.h new file mode 100644 index 0000000000..de75e37c62 --- /dev/null +++ b/src/components/utils/include/utils/sqlite_wrapper/sql_query.h @@ -0,0 +1,233 @@ +/* + * Copyright (c) 2015, Ford Motor Company + * All rights reserved. + * + * 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 the Ford Motor Company 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 HOLDER 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. + */ + +#ifndef SRC_COMPONENTS_POLICY_SQLITE_WRAPPER_INCLUDE_SQLITE_WRAPPER_SQL_QUERY_H_ +#define SRC_COMPONENTS_POLICY_SQLITE_WRAPPER_INCLUDE_SQLITE_WRAPPER_SQL_QUERY_H_ + +#include <stdint.h> +#include <string> +#include "utils/sqlite_wrapper/sql_error.h" +#include "utils/lock.h" + +struct sqlite3_stmt; + +namespace utils { +namespace dbms { + +class SQLDatabase; + +/** + * Provides a means of executing and manipulating SQL statements + */ +class SQLQuery { + public: + explicit SQLQuery(SQLDatabase* db); + ~SQLQuery(); + + /** + * Prepares the SQL query for executing + * @param query the utf-8 string of SQL query + * @return true if successfully + */ + bool Prepare(const std::string& query); + + /** + * Resets the binds of query for re-executing + * @return true if successfully + */ + bool Reset(); + + /** + * Deletes prepared SQL query + */ + void Finalize(); + + /** + * Executes SQL query without make binds + * @param query the utf-8 string of SQL query + * @return true if successfull + */ + bool Exec(const std::string& query); + + /** + * Executes prepared SQL query and positions the query on the first record + * @return true if successfull + */ + bool Exec(); + + /** + * Retrieves the next record in the result, if available, + * and positions the query on the retrieved record + * @return true if record was retrieved successfully, false if a error was + * or the result is empty or was retrieves last record + */ + bool Next(); + + /** + * Binds null in the prepared query + * @param pos position of param in the query + */ + void Bind(int pos); + + /** + * Binds int value in the prepared query. + * @param pos position of param in the query + * @param value value of param + */ + void Bind(int pos, int value); + + /** + * Binds int64_t value in the prepared query. + * @param pos position of param in the query + * @param value value of param + */ + void Bind(int pos, int64_t value); + + /** + * Binds double value in the prepared query. + * @param pos position of param in the query + * @param value value of param + */ + void Bind(int pos, double value); + + /** + * Binds bool value in the prepared query. + * @param pos position of param in the query + * @param value value of param + */ + void Bind(int pos, bool value); + + /** + * Binds string in the prepared query. + * @param pos position of param in the query + * @param value utf-8 string + */ + void Bind(int pos, const std::string& value); + + /** + * Gets value in the result record + * @param pos position of value + * @return boolean value + */ + bool GetBoolean(int pos) const; + + /** + * Gets value in the result record + * @param pos position of value + * @return integer value + */ + int GetInteger(int pos) const; + + /** + * Gets value in the result record + * @param pos position of value + * @return unsigned integer value + */ + uint32_t GetUInteger(int pos) const; + + /** + * Gets value in the result record + * @param pos position of value + * @return int64_t value + */ + int64_t GetLongInt(int pos) const; + + /** + * Gets value in the result record + * @param pos position of value + * @return double value + */ + double GetDouble(int pos) const; + + /** + * Gets value in the result record + * @param pos position of value + * @return string value + */ + std::string GetString(int pos) const; + + /** + * Checks if value is null + * @param pos position of value + * @return true if value is null + */ + bool IsNull(int pos) const; + + /** + * Gets last id of insert row + * @return id of insert row + */ + int64_t LastInsertId() const; + + /** + * Gets string of the query + * @return string of the query + */ + const std::string& query() const; + + /** + * Gets information about the last error that occurred on the database + * @return last error + */ + SQLError LastError() const; + + private: + /** + * The instantiation of database + */ + SQLDatabase& db_; + + /** + * The string of query + */ + std::string query_; + + /** + * The SQL statement in SQLite + */ + sqlite3_stmt* statement_; + + /** + * Lock for guarding statement + */ + sync_primitives::Lock statement_lock_; + + /** + * The last error that occurred with this query + */ + int error_; +}; + +} // namespace dbms +} // namespace utils + +#endif // SRC_COMPONENTS_POLICY_SQLITE_WRAPPER_INCLUDE_SQLITE_WRAPPER_SQL_QUERY_H_ diff --git a/src/components/utils/include/utils/stl_utils.h b/src/components/utils/include/utils/stl_utils.h index 70fbadbd5e..4a4e3d2fe8 100644 --- a/src/components/utils/include/utils/stl_utils.h +++ b/src/components/utils/include/utils/stl_utils.h @@ -40,45 +40,48 @@ namespace utils { * Utility class that automatically deletes STL collection of * freestore objects */ -template<class T> +template <class T> class StlCollectionDeleter { public: typedef T Collection; - StlCollectionDeleter(T* collection) - : collection_(collection) { + StlCollectionDeleter(T* collection) : collection_(collection) { DCHECK(collection_); } ~StlCollectionDeleter() { - for (typename Collection::iterator i = collection_->begin(), end = - collection_->end(); i != end; ++i) { + for (typename Collection::iterator i = collection_->begin(), + end = collection_->end(); + i != end; + ++i) { delete *i; *i = NULL; } } + private: Collection* collection_; }; -template<class T> +template <class T> class StlMapDeleter { public: typedef T Collection; - StlMapDeleter(T* collection) - : collection_(collection) { + StlMapDeleter(T* collection) : collection_(collection) { DCHECK(collection_); } ~StlMapDeleter() { - for (typename Collection::iterator i = collection_->begin(), end = - collection_->end(); i != end; ++i) { + for (typename Collection::iterator i = collection_->begin(), + end = collection_->end(); + i != end; + ++i) { delete i->second; i->second = NULL; } - } + private: Collection* collection_; }; } // namespace utils -#endif // SRC_COMPONENTS_UTILS_INCLUDE_UTILS_STL_UTILS_H_ +#endif // SRC_COMPONENTS_UTILS_INCLUDE_UTILS_STL_UTILS_H_ diff --git a/src/components/utils/include/utils/threads/pulse_thread_delegate.h b/src/components/utils/include/utils/threads/pulse_thread_delegate.h index 207b64caaf..623e12b261 100644 --- a/src/components/utils/include/utils/threads/pulse_thread_delegate.h +++ b/src/components/utils/include/utils/threads/pulse_thread_delegate.h @@ -42,9 +42,11 @@ namespace threads { /** * @brief This ThreadDelegate derivative is designed * to implement threads waiting for QNX Pulse messages - * When constucted, an instance of this class creates QNX channel and connects to it + * When constucted, an instance of this class creates QNX channel and connects + * to it * In exitThreadMain() channel is disconnected and destroyed - * In threadMain() endless loop event is armed via pure virtual method ArmEvent() + * In threadMain() endless loop event is armed via pure virtual method + * ArmEvent() * and thread blocks on MsgReceivePulse() waiting for Pulse * When Pulse comes, OnPulse() pure virtual method is invoked * Subclassed must implement ArmEvent() for events of interest @@ -52,39 +54,42 @@ namespace threads { */ class PulseThreadDelegate : public ThreadDelegate { public: -/** - * @brief default constructor - */ + /** + * @brief default constructor + */ PulseThreadDelegate(); virtual void threadMain(); virtual void exitThreadMain(); protected: -/** - * @brief This method is to be implemented to arm events of interest - * @param event pointer to structure sigevent - * @return If this method returns true, thread is blocked on MsgReceivePulse() waiting for Pulse - */ + /** + * @brief This method is to be implemented to arm events of interest + * @param event pointer to structure sigevent + * @return If this method returns true, thread is blocked on + * MsgReceivePulse() waiting for Pulse + */ virtual bool ArmEvent(struct sigevent* event) = 0; -/** - * @brief This method is invoked from threadMain() when Pulse comes - */ + /** + * @brief This method is invoked from threadMain() when Pulse comes + */ virtual void OnPulse() = 0; /** * This method is to be initialize child class * @return If this method returns false, thread will be stopped */ - virtual bool Init() { return true; } + virtual bool Init() { + return true; + } -/** - * Finalizes thread - * Can free resources - */ + /** + * Finalizes thread + * Can free resources + */ virtual void Finalize() {} private: - enum {PULSE_CODE = _PULSE_CODE_MINAVAIL + 1}; + enum { PULSE_CODE = _PULSE_CODE_MINAVAIL + 1 }; volatile bool run_; int chid_; diff --git a/src/components/utils/include/utils/threads/thread_manager.h b/src/components/utils/include/utils/threads/thread_manager.h index d72abb428f..3f53c902c4 100644 --- a/src/components/utils/include/utils/threads/thread_manager.h +++ b/src/components/utils/include/utils/threads/thread_manager.h @@ -51,7 +51,7 @@ #include "utils/threads/thread_delegate.h" namespace threads { - class Thread; +class Thread; /* * This class is here currently to remember names associated to threads. @@ -69,12 +69,13 @@ class ThreadManager : public utils::Singleton<ThreadManager> { }; ThreadManager() {} MessageQueue<ThreadDesc> threads_to_terminate; + private: DISALLOW_COPY_AND_ASSIGN(ThreadManager); FRIEND_BASE_SINGLETON_CLASS(ThreadManager); }; -} // namespace threads +} // namespace threads -#endif // SRC_COMPONENTS_UTILS_INCLUDE_UTILS_THREADS_THREAD_MANAGER_H_ +#endif // SRC_COMPONENTS_UTILS_INCLUDE_UTILS_THREADS_THREAD_MANAGER_H_ diff --git a/src/components/utils/include/utils/threads/thread_validator.h b/src/components/utils/include/utils/threads/thread_validator.h index dc2d138e2e..2e7dac02ab 100644 --- a/src/components/utils/include/utils/threads/thread_validator.h +++ b/src/components/utils/include/utils/threads/thread_validator.h @@ -68,11 +68,11 @@ class SingleThreadSimpleValidator { // of classes being checked for absence of concurrent access void AssertRunningOnCreationThread() const; PlatformThreadHandle creation_thread_id() const; + private: const PlatformThreadHandle creation_thread_id_; }; - /* * This is bit more sophisticated debug helper which allows * objects being checked to be transferred between threads. @@ -97,10 +97,11 @@ class SingleThreadValidator { // of classes being checked for absence of unintended concurrent // access void AssertRunningOnValidThread() const; + private: mutable PlatformThreadHandle owning_thread_id_; }; -} // namespace threads +} // namespace threads -#endif // SRC_COMPONENTS_UTILS_INCLUDE_THREADS_THREAD_VALIDATOR_H_ +#endif // SRC_COMPONENTS_UTILS_INCLUDE_THREADS_THREAD_VALIDATOR_H_ diff --git a/src/components/utils/include/utils/timer.h b/src/components/utils/include/utils/timer.h new file mode 100644 index 0000000000..690c9df5dc --- /dev/null +++ b/src/components/utils/include/utils/timer.h @@ -0,0 +1,217 @@ +/* + * Copyright (c) 2016, Ford Motor Company + * All rights reserved. + * + * 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 the Ford Motor Company 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 HOLDER 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. + */ +#ifndef SRC_COMPONENTS_UTILS_INCLUDE_UTILS_TIMER_H_ +#define SRC_COMPONENTS_UTILS_INCLUDE_UTILS_TIMER_H_ + +#include <string> +#include <stdint.h> +#include <memory> + +#include "utils/macro.h" +#include "utils/lock.h" +#include "utils/timer_task.h" +#include "utils/threads/thread.h" +#include "utils/threads/thread_delegate.h" + +namespace timer { + +typedef uint32_t Milliseconds; + +/** + * @brief Timer calls custom callback function after + * specified timeout has been elapsed. + * Thread-safe class + */ +class Timer { + public: + /** + * @brief Constructor + * Does not start timer + * @param name Timer name for identification + * @param task Task for tracking + */ + Timer(const std::string& name, TimerTask* task); + + /** + * @brief Destructor + * Stops timer if it's running + */ + ~Timer(); + + /** + * @brief Starts timer with specified timeout + * @param timeout Timer timeout + * @param single_shot Single shot flag for timer + */ + void Start(const Milliseconds timeout, const bool single_shot); + + /** + * @brief Stops timer if it's running + */ + void Stop(); + + /** + * @brief Gets current timer status + * @return True in case of timer is running, false otherwise + */ + bool is_running() const; + + /** + * @brief Gets current timer timeout + * @return Current timeout in milliseconds. + * Null if timer has not been started + */ + Milliseconds timeout() const; + + private: + /** + * @brief Delegate for timer thread + */ + class TimerDelegate : public threads::ThreadDelegate { + public: + /** + * @brief Constructor + * @param timer Timer instance pointer for callback calling + */ + TimerDelegate(const Timer* timer, sync_primitives::Lock& state_lock_ref); + + /** + * @brief Sets timer timeout + * @param timeout Timeout in milliseconds to be set + */ + void set_timeout(const Milliseconds timeout); + + /** + * @brief Gets timer timeout + * @return Timer timeout + */ + Milliseconds timeout() const; + + /** + * @brief Sets timer delegate stop flag + * @param stop_flag Bool flag to be set + */ + void set_stop_flag(const bool stop_flag); + + /** + * @brief Gets timer delegate stop flag + * @return Delegate stop flag + */ + bool stop_flag() const; + + /** + * @brief Sets timer delegate finalized flag + * @param finalized_flag Bool flag to be set + */ + void set_finalized_flag(const bool finalized_flag); + + /** + * @brief Gets timer delegate finalized flag + * @return Delegate finalized flag + */ + bool finalized_flag() const; + + void threadMain() OVERRIDE; + void exitThreadMain() OVERRIDE; + + private: + const Timer* timer_; + Milliseconds timeout_; + + /** + * @brief Stop flag shows if timer should be stopped + * after next iteration + */ + bool stop_flag_; + + /** + * @brief Finalized flag shows if timer is finalized + * and cannot be restarted until actual thread stopping + */ + bool finalized_flag_; + + sync_primitives::Lock& state_lock_ref_; + sync_primitives::ConditionalVariable state_condition_; + + DISALLOW_COPY_AND_ASSIGN(TimerDelegate); + }; + + /** + * @brief Sets up timer delegate to start state. + * Not thread-safe + * @param timeout Timer timeout + */ + void StartDelegate(const Milliseconds timeout) const; + + /** + * @brief Sets up timer delegate to stop state. + * Not thread-safe + */ + void StopDelegate() const; + + /** + * @brief Starts timer thread. + * Not thread-safe + */ + void StartThread(); + + /** + * @brief Stops timer thread. + * Not thread-safe + */ + void StopThread(); + + /** + * @brief Callback called on timeout. + * Not thread-safe + */ + void OnTimeout() const; + + const std::string name_; + TimerTask* task_; + + mutable sync_primitives::Lock state_lock_; + + mutable std::auto_ptr<TimerDelegate> delegate_; + threads::Thread* thread_; + + /** + * @brief Single shot flag shows if timer should be fired once + */ + bool single_shot_; + + DISALLOW_COPY_AND_ASSIGN(Timer); +}; + +} // namespace timer + +#endif // SRC_COMPONENTS_UTILS_INCLUDE_UTILS_TIMER_H_ diff --git a/src/components/utils/include/utils/timer_task.h b/src/components/utils/include/utils/timer_task.h new file mode 100644 index 0000000000..199fd804de --- /dev/null +++ b/src/components/utils/include/utils/timer_task.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2016, Ford Motor Company + * All rights reserved. + * + * 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 the Ford Motor Company 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 HOLDER 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. + */ + +#ifndef SRC_COMPONENTS_UTILS_INCLUDE_UTILS_TIMER_TASK_H_ +#define SRC_COMPONENTS_UTILS_INCLUDE_UTILS_TIMER_TASK_H_ + +namespace timer { + +/** + * @brief The TimerTask interface + */ +class TimerTask { + public: + /** + * @brief this method calls callback from callee + */ + virtual void run() const = 0; + + virtual ~TimerTask() {} +}; + +} // namespace timer + +#endif // SRC_COMPONENTS_UTILS_INCLUDE_UTILS_TIMER_TASK_H_ diff --git a/src/components/utils/include/utils/timer_task_impl.h b/src/components/utils/include/utils/timer_task_impl.h new file mode 100644 index 0000000000..28618551ec --- /dev/null +++ b/src/components/utils/include/utils/timer_task_impl.h @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2016, Ford Motor Company + * All rights reserved. + * + * 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 the Ford Motor Company 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 HOLDER 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. + */ + +#ifndef SRC_COMPONENTS_UTILS_INCLUDE_UTILS_TIMER_TASK_IMPL_H_ +#define SRC_COMPONENTS_UTILS_INCLUDE_UTILS_TIMER_TASK_IMPL_H_ + +#include "utils/timer_task.h" +#include "utils/macro.h" + +namespace timer { + +/** + * @brief The TaskImpl is proxy-template class for calling callback from Timer + */ +template <typename Trackable> +class TimerTaskImpl : public TimerTask { + public: + typedef void (Trackable::*CallbackFunction)(); + + TimerTaskImpl(Trackable* trackable, CallbackFunction callback); + ~TimerTaskImpl(); + /** + * @brief run method which call callback function from callee + */ + void run() const OVERRIDE; + + private: + Trackable* callee_; + CallbackFunction callback_; +}; + +// Implementation +template <typename Trackable> +TimerTaskImpl<Trackable>::TimerTaskImpl(Trackable* trackable, + CallbackFunction callback) + : callee_(trackable), callback_(callback) {} + +template <typename Trackable> +TimerTaskImpl<Trackable>::~TimerTaskImpl() { + callee_ = NULL; +} + +template <typename Trackable> +void TimerTaskImpl<Trackable>::run() const { + DCHECK_OR_RETURN_VOID(callee_ && callback_) + (callee_->*callback_)(); +} +} // namespace timer + +#endif // SRC_COMPONENTS_UTILS_INCLUDE_UTILS_TIMER_TASK_IMPL_H_ |