/////////////////////////////////////////////////////////////////// // AllUsers.cpp // ... same as before namespace // anonymous namespace { const_val< long> MAX_USERS( get_props(), "max_users"); const_val< int> MAX_FAILS( get_props(), "max_fails"); }; // anonymous namespace // ... same as before /////////////////////////////////////////////////////////////////// // app_props.cpp #include "properties.h" #include <iostream> namespace { // anonymous namespace static properties s_props; }; void set_props_file( const std::string & strPropsFileName) { s_props.initialize( strPropsFileName); } properties_base & get_props() { return s_props; } /////////////////////////////////////////////////////////////////// // DbConnection.cpp //... namespace // anonymous namespace { const_val< std::string> DB_SERVER( get_props(), "db_server"); const_val< std::string> DB_NAME( get_props(), "db_name"); const_val< std::string> DB_USERNAME( get_props(), "db_username"); const_val< std::string> DB_PASSWORD( get_props(), "db_passw"); }; // anonymous namespace DbConnection::DbConnection() { std::string strConnect = DB_SERVER + "/" + DB_NAME + "[" +DB_USERNAME + ":" + DB_PASSWORD + "]"; std::cout << strConnect; // etc. }; /////////////////////////////////////////////////////////////////// // main.cpp // ... dependent_static< AllUsers> s_AllUsers; dependent_static< DbConnection> s_dbConnection; int main(int argc, char* argv[]) { std::cout << "Reading params from " << argv[ 1] << std::endl; set_props_file( argv[ 1]); all_constants::initialize_constants(); all_dependent_statics::initialize_statics(); // ... } #/////////////////////////////////////////////////////////////////// #// props.txt "max_fails" "5" "max_users" "1000" "db_server" "(local)" "db_name" "Test Database" "db_username" "sa" "db_passw" "sa" #/////////////////////////////////////////////////////////////////// #// props2.txt "max_fails" "10" "max_users" "20" "db_server" "(remote)" "db_name" "Test Database" "db_username" "mumu" "db_passw" "mumu" /////////////////////////////////////////////////////////////////// // properties.h // ... same as before class basic_properties { public: basic_properties() {} void initialize( const std::string & strFileName) { m_strFileName = strFileName; // ... what used to be in basic_properties' constructor this->set_is_initialized(); } }; /////////////////////////////////////////////////////////////////// // const_val.h #ifndef CONST_VAL__H #define CONST_VAL__H template< class type> class const_val_base { // ... protected: void set( const type & val) { if ( !m_bInitialized) m_val = val; // ... } const type & get() const { check_val(); return m_val; } // ... private: void check_val() const { if ( !m_bInitialized) throw std::runtime_error( "constant used too early! (before calling 'all_constants::initialize_constants()' )"); } protected: type m_val; bool m_bInitialized; }; // operators +,-,>,<, etc. for const_val_base and raw type values // ... // struct all_constants - allow management of const_val constants // ... (similar to all_dependent_statics class) template< class type> class const_val : public const_val_base< type>, private Private::const_val_allow_initialize { // disallow copying // ... public: template< class char_type> const_val( const basic_properties_base< char_type> & props, const char_type * strPropName) { // ... all_constants::add_constant( this); } // ... operator const type & () const { return this->get(); } private: void initialize() { assert( !this->m_bInitialized); m_pInitializer->do_set(); assert( this->m_bInitialized); } Private::const_val_initializer_base * m_pInitializer; }; //////////////////////////////////////////////////////////// // operators +,-,>,<, etc. for const_val values // ... #endif |