/////////////////////////////////////////////////////////////////// // AllUsers.cpp //... namespace // anonymous namespace { const_val< long> MAX_USERS( get_prop( "max_users") ); const_val< int> MAX_FAILS( get_prop( "max_fails") ); }; // anonymous namespace AllUsers::AllUsers() { std::cout << "there are max " << MAX_USERS << " users that can login." << std::endl; statistics_log() << "we created the AllUsers object " << std::endl; } /////////////////////////////////////////////////////////////////// // app_props.cpp // ... std::pair< properties_base*, std::string> get_prop( const char * strPropName) { return s_props( strPropName); } /////////////////////////////////////////////////////////////////// // Logs.cpp // ... namespace { // anonymous namespace const_val< std::string> STATISTICS_LOG_NAME( get_prop( "statistics_log"), with_default_val( "statistics.txt")); struct statistics_log : public std::ofstream //... }; dependent_static< statistics_log> s_statisticsLog; // FIXME set_dependent_static_name statsname( s_statisticsLog, "statistics"); }; std::ostream& statistics_log() { return s_statisticsLog; } /////////////////////////////////////////////////////////////////// // main.cpp //... // FIXME add_dependency_on_static dep( s_AllUsers, "statistics"); int main(int argc, char* argv[]) { // would throw: trying to use statistics_log(), before it's been // initialized! // statistics_log() << "this would be too early" << std::endl; // would throw - properties have not been initialized yet! // (in our case, we need to call set_props_file) // // constants_and_dependents::initialize(); //... std::cout << "Reading params from " << argv[ 1] << std::endl; set_props_file( argv[ 1]); constants_and_dependents::initialize(); statistics_log() << "this is a statistic," << std::endl; //... } /////////////////////////////////////////////////////////////////// // basic_properties.h #if !defined(BASIC_PROPERTIES_BASE___H) #define BASIC_PROPERTIES_BASE___H //... class basic_properties_base { //... // helper, to allow simpler usage when constructing a const_val std::pair< basic_properties_base< char_type>*, std::basic_string< char_type> > operator() ( const std::basic_string< char_type> & strPropName) { return std::make_pair( this, strPropName); } }; /////////////////////////////////////////////////////////////////// // const_val #ifndef CONST_VAL__H #define CONST_VAL__H //... template< class type> class const_val : public const_val_base< type>, private Private::const_val_allow_initialize { public: // creates a constant - from props & name template< class char_type> const_val( const std::pair< basic_properties_base< char_type>*, std::basic_string< char_type> > & props_and_name) { /* ... */ } // creates a constant which, if it can't be initialized, // it's initialized to a default value template< class char_type, class other_type> const_val( const std::pair< basic_properties_base< char_type>*, std::basic_string< char_type> > & props_and_name, const_with_default_val_t< other_type> val) { /* ... */ } }; #endif /////////////////////////////////////////////////////////////////// // constants_and_dependents.h #ifndef CONSTANTS_AND_DEPENDENDENTS__H #define CONSTANTS_AND_DEPENDENDENTS__H // ... struct constants_and_dependents { static void initialize() { Private::all_constants::initialize_constants(); Private::all_dependent_statics::initialize_statics(); } static void set_static_name( Private::dependent_static_base & val, const std::string &strName) { Private::all_dependent_statics::set_static_name( val, strName); } static void add_dependency( Private::dependent_static_base & val, const std::string &strDepName) { Private::all_dependent_statics::add_dependency( val, strDepName); } }; struct set_dependent_static_name { set_dependent_static_name( Private::dependent_static_base & val, const std::string &strName) { constants_and_dependents::set_static_name( val, strName); } }; struct add_dependency_on_static { add_dependency_on_static( Private::dependent_static_base & val, const std::string &strDepName) { constants_and_dependents::add_dependency( val, strDepName); } }; #endif |