From fc444c5ac67182f8b3d26f01acf0ca5ba68da0b4 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Fri, 18 Mar 2011 17:50:04 +0100 Subject: [PATCH] Replaced uint by int. - documented that a integer is non-negative using assertions, not unsigned int (uint). --- Readme | 5 +++-- src/config/configuration.cpp | 4 ++-- src/config/configuration.h | 10 +++++----- src/config/configurationreader.cpp | 14 +++++++------- src/config/configurationreader.h | 4 ++-- src/config/host.cpp | 6 +++--- src/config/host.h | 6 +++--- src/dns/dnsresolver.cpp | 4 ++-- src/dns/hostaddress.cpp | 4 ++-- src/dns/hostaddress.h | 6 +++--- src/main.cpp | 2 +- src/ping/boostpinger.cpp | 10 +++++----- src/ping/boostpinger.h | 6 +++--- src/ping/pingscheduler.cpp | 4 ++-- src/ping/pingscheduler.h | 4 ++-- 15 files changed, 45 insertions(+), 44 deletions(-) diff --git a/Readme b/Readme index e4cfe76..909dd46 100644 --- a/Readme +++ b/Readme @@ -62,8 +62,9 @@ improve code reradability. variable has the given number of bits. This states clear the intent of the original programmer and avoids improper modifications. -- Use int or uint for regular integer numbers that do not require any specific - size. +- Use only int for regular integer numbers that do not require any specific + size. And document that a variable is non-negative using assertions. Do not + use unsigned types to say a number will never be negative. - Use std::size_t for integers that represent sizes of vectors, objects or buffers. Thus leaving the size difinition to the platform. diff --git a/src/config/configuration.cpp b/src/config/configuration.cpp index 78cf351..b20681f 100644 --- a/src/config/configuration.cpp +++ b/src/config/configuration.cpp @@ -33,12 +33,12 @@ void Configuration::set_config_file_name( const std::string &config_file_name ) this->ConfigFileName = config_file_name; } -uint Configuration::get_limit_to_notify() const +int Configuration::get_limit_to_notify() const { return LimitToNotify; } -void Configuration::set_limit_to_notify( const uint limit_to_notify ) +void Configuration::set_limit_to_notify( const int limit_to_notify ) { BOOST_ASSERT( ( MinLimitToNotify <= limit_to_notify ) && ( limit_to_notify <= MaxLimitToNotify) ); diff --git a/src/config/configuration.h b/src/config/configuration.h index d294b9c..d4f30ee 100644 --- a/src/config/configuration.h +++ b/src/config/configuration.h @@ -21,19 +21,19 @@ public: std::string get_config_file_name() const; void set_config_file_name( const std::string &config_file_name ); - uint get_limit_to_notify() const; - void set_limit_to_notify( const uint limit_to_notify ); + int get_limit_to_notify() const; + void set_limit_to_notify( const int limit_to_notify ); std::vector get_hosts() const; void set_hosts( const std::vector &hosts_list ); private: std::string ConfigFileName; - uint LimitToNotify; + int LimitToNotify; std::vector Hosts; - const uint MinLimitToNotify; - const uint MaxLimitToNotify; + const int MinLimitToNotify; + const int MaxLimitToNotify; }; diff --git a/src/config/configurationreader.cpp b/src/config/configurationreader.cpp index 5ee9a12..07e1e53 100644 --- a/src/config/configurationreader.cpp +++ b/src/config/configurationreader.cpp @@ -109,9 +109,9 @@ options_description ConfigurationReader::get_configuration_options() const { options_description options( "Configuration" ); options.add_options() - ( LimitToNotifyCmdStr.c_str(), value()->default_value( DefaultLimitToNotify ), LimitToNotifyCmdDesc.c_str() ) + ( LimitToNotifyCmdStr.c_str(), value()->default_value( DefaultLimitToNotify ), LimitToNotifyCmdDesc.c_str() ) ( HostNameCmdStr.c_str(), value< vector >(), HostNameCmdDesc.c_str() ) - ( HostIntervalCmdStr.c_str(), value< vector >(), HostIntervalCmdDesc.c_str() ) + ( HostIntervalCmdStr.c_str(), value< vector >(), HostIntervalCmdDesc.c_str() ) ; return options; @@ -129,13 +129,13 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm ) if ( vm.count( LimitToNotifyCmdStr ) ) { - uint limit_to_notify = vm[ LimitToNotifyCmdStr ].as (); + int limit_to_notify = vm[ LimitToNotifyCmdStr ].as (); Config.set_limit_to_notify( limit_to_notify ); cout << LimitToNotifyCmdStr << "=" << limit_to_notify << endl; } - uint hosts_names_total = 0; + int hosts_names_total = 0; if ( vm.count( HostNameCmdStr ) ) { vector hosts_list; @@ -154,14 +154,14 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm ) hosts_names_total = hosts_names.size(); } - uint hosts_interval_total = 0; + int hosts_interval_total = 0; if ( vm.count( HostIntervalCmdStr ) ) { vector hosts_list = Config.get_hosts(); vector::iterator hosts_it = hosts_list.begin(); - vector hosts_intervals = vm[ HostIntervalCmdStr ].as< vector > (); - BOOST_FOREACH( uint host_interval, hosts_intervals ) + vector hosts_intervals = vm[ HostIntervalCmdStr ].as< vector > (); + BOOST_FOREACH( int host_interval, hosts_intervals ) { HostItem host_item = *hosts_it; host_item->set_interval( host_interval ); diff --git a/src/config/configurationreader.h b/src/config/configurationreader.h index 378faf6..2c5bd3e 100644 --- a/src/config/configurationreader.h +++ b/src/config/configurationreader.h @@ -61,12 +61,12 @@ private: const std::string DefaultConfigFileName; const std::string ConfigFileCmdStr; const std::string ConfigFileCmdDesc; - const uint DefaultLimitToNotify; + const int DefaultLimitToNotify; const std::string LimitToNotifyCmdStr; const std::string LimitToNotifyCmdDesc; const std::string HostNameCmdStr; const std::string HostNameCmdDesc; - const uint DefaultHostInterval; + const int DefaultHostInterval; const std::string HostIntervalCmdStr; const std::string HostIntervalCmdDesc; diff --git a/src/config/host.cpp b/src/config/host.cpp index dbd0fa3..a92ffd0 100644 --- a/src/config/host.cpp +++ b/src/config/host.cpp @@ -44,14 +44,14 @@ void Host::set_port( const uint16_t port ) this->Port = port; } -uint Host::get_interval() const +int Host::get_interval() const { return Interval; } -void Host::set_interval( const uint interval ) +void Host::set_interval( const int interval ) { - BOOST_ASSERT( ( 0 < interval ) && ( interval < std::numeric_limits::max() ) ); + BOOST_ASSERT( ( 0 < interval ) && ( interval < std::numeric_limits::max() ) ); this->Interval = interval; } diff --git a/src/config/host.h b/src/config/host.h index 75f41bf..2485e04 100644 --- a/src/config/host.h +++ b/src/config/host.h @@ -24,8 +24,8 @@ public: uint16_t get_port() const; void set_port( const uint16_t port ); - uint get_interval() const; - void set_interval( const uint interval ); + int get_interval() const; + void set_interval( const int interval ); std::vector get_options() const; void set_options( const std::vector &options ); @@ -33,7 +33,7 @@ public: private: std::string Address; uint16_t Port; - uint Interval; + int Interval; std::vector Options; }; diff --git a/src/dns/dnsresolver.cpp b/src/dns/dnsresolver.cpp index eaf8370..7c7672e 100644 --- a/src/dns/dnsresolver.cpp +++ b/src/dns/dnsresolver.cpp @@ -85,14 +85,14 @@ int DnsResolver::get_resolved_ip_count() */ string DnsResolver::get_next_ip() { - uint list_size_before = ResolvedHostAddressList.size(); + int list_size_before = ResolvedHostAddressList.size(); HostAddress host_address = ResolvedHostAddressList.front(); ResolvedHostAddressList.pop_front(); string destination_ip = host_address.get_ip(); ResolvedHostAddressList.push_back( host_address ); - uint list_size_after = ResolvedHostAddressList.size(); + int list_size_after = ResolvedHostAddressList.size(); BOOST_ASSERT( list_size_before == list_size_after ); diff --git a/src/dns/hostaddress.cpp b/src/dns/hostaddress.cpp index 253b3c2..46a60ca 100644 --- a/src/dns/hostaddress.cpp +++ b/src/dns/hostaddress.cpp @@ -26,12 +26,12 @@ string HostAddress::get_ip() const return Ip; } -uint HostAddress::get_ttl() const +int HostAddress::get_ttl() const { return Ttl; } -void HostAddress::set_ttl( uint ttl ) +void HostAddress::set_ttl( int ttl ) { this->Ttl = ttl; } diff --git a/src/dns/hostaddress.h b/src/dns/hostaddress.h index 63df852..525761b 100644 --- a/src/dns/hostaddress.h +++ b/src/dns/hostaddress.h @@ -18,12 +18,12 @@ public: std::string get_ip() const; void set_ip( std::string ip ); - uint get_ttl() const; - void set_ttl( uint ttl ); + int get_ttl() const; + void set_ttl( int ttl ); private: std::string Ip; - uint Ttl; + int Ttl; }; diff --git a/src/main.cpp b/src/main.cpp index f4c83fc..ae4d889 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,7 +25,7 @@ int main( int argc, char* argv[] ) BOOST_FOREACH( HostItem host, hosts ) { string ping_address = (*host).get_address(); - uint ping_interval = (*host).get_interval(); + int ping_interval = (*host).get_interval(); PingSchedulerItem scheduler( new PingScheduler( io_service, ping_address, ping_interval diff --git a/src/ping/boostpinger.cpp b/src/ping/boostpinger.cpp index a61f1ea..b40df03 100644 --- a/src/ping/boostpinger.cpp +++ b/src/ping/boostpinger.cpp @@ -23,7 +23,7 @@ using namespace boost::posix_time; BoostPinger::BoostPinger( boost::asio::io_service &io_service, - const uint echo_reply_timeout_in_sec + const int echo_reply_timeout_in_sec ) : IoService( io_service ), Resolver( io_service ), @@ -157,7 +157,7 @@ void BoostPinger::handle_timeout_echo_reply() void BoostPinger::schedule_next_echo_request() { // Requests must be sent no less than one second apart. - const uint echo_request_interval_in_sec = 1; + const int echo_request_interval_in_sec = 1; Timer.expires_at( TimeSent + seconds( echo_request_interval_in_sec ) ); Timer.async_wait( boost::bind( &BoostPinger::start_send, this ) ); } @@ -216,10 +216,10 @@ void BoostPinger::print_echo_reply( size_t bytes_received = bytes_transferred - ipv4_hdr.get_header_length(); string source_address = ipv4_hdr.get_source_address().to_string(); - uint sequence_number = icmp_hdr.get_sequence_number(); - uint ttl = ipv4_hdr.get_time_to_live(); + int sequence_number = icmp_hdr.get_sequence_number(); + int ttl = ipv4_hdr.get_time_to_live(); ptime now = microsec_clock::universal_time(); - uint time = (now - TimeSent).total_milliseconds(); + int time = (now - TimeSent).total_milliseconds(); cout << bytes_received << " bytes " << "from " << source_address diff --git a/src/ping/boostpinger.h b/src/ping/boostpinger.h index f72fac5..b8b840b 100644 --- a/src/ping/boostpinger.h +++ b/src/ping/boostpinger.h @@ -16,7 +16,7 @@ class BoostPinger : public Pinger public: BoostPinger( boost::asio::io_service &io_service, - const uint echo_reply_timeout_in_sec + const int echo_reply_timeout_in_sec ); virtual ~BoostPinger(); @@ -60,8 +60,8 @@ private: uint16_t SequenceNumber; boost::posix_time::ptime TimeSent; boost::asio::streambuf ReplyBuffer; - uint RepliesCount; - uint EchoReplyTimeoutInSec; + int RepliesCount; + int EchoReplyTimeoutInSec; BoostPinger::PingStatus PingerStatus; }; diff --git a/src/ping/pingscheduler.cpp b/src/ping/pingscheduler.cpp index ee320e7..499751d 100644 --- a/src/ping/pingscheduler.cpp +++ b/src/ping/pingscheduler.cpp @@ -19,7 +19,7 @@ using namespace boost::posix_time; PingScheduler::PingScheduler( boost::asio::io_service &io_service, const string &ping_address, - const uint ping_interval_in_sec + const int ping_interval_in_sec ) : IoService( io_service ), Timer( io_service ), @@ -79,7 +79,7 @@ bool PingScheduler::ping( const string &destination ) BOOST_ASSERT( !destination.empty() ); io_service io_service; - uint echo_reply_timeout_in_sec = 5; // TODO configurable: this is the timeout to WAIT FOR the ping before considering a timeout + int echo_reply_timeout_in_sec = 5; // TODO configurable: this is the timeout to WAIT FOR the ping before considering a timeout BoostPinger pinger( io_service, echo_reply_timeout_in_sec ); return pinger.ping( destination ); diff --git a/src/ping/pingscheduler.h b/src/ping/pingscheduler.h index f4bc992..1060477 100644 --- a/src/ping/pingscheduler.h +++ b/src/ping/pingscheduler.h @@ -19,7 +19,7 @@ public: PingScheduler( boost::asio::io_service &io_service, const std::string &ping_address, - const uint ping_interval_in_sec + const int ping_interval_in_sec ); virtual ~PingScheduler(); @@ -39,7 +39,7 @@ private: boost::asio::deadline_timer Timer; boost::posix_time::ptime TimeSentLastPing; DnsResolver IpList; - const uint PingIntervalInSec; + const int PingIntervalInSec; PingAnalyzer Analyzer; }; -- 1.7.1