Scopira 20080306

loop.h

00001 
00002 /*
00003  *  Copyright (c) 2004-2005    National Research Council
00004  *
00005  *  All rights reserved.
00006  *
00007  *  This material is confidential and proprietary information of
00008  *  National Research Council Canada ("Confidential Information").
00009  *  This Confidential Information may only be used and reproduced
00010  *  in accordance with the terms of the license agreement.
00011  *
00012  */
00013 
00014 #ifndef __INCLUDED_SCOPIRA_CORE_LOOP_H__
00015 #define __INCLUDED_SCOPIRA_CORE_LOOP_H__
00016 
00017 #include <string>
00018 #include <vector>
00019 #include <map>
00020 
00021 #include <scopira/tool/export.h>
00022 #include <scopira/tool/output.h>
00023 #include <scopira/tool/fileflow.h>
00024 #include <scopira/tool/netflow.h>
00025 #include <scopira/tool/objflowloader.h>
00026 
00027 namespace scopira
00028 {
00029   namespace tool
00030   {
00031     class dll; //fwd
00032   }
00037   namespace core
00038   {
00039     class basic_loop;
00040 
00041     class objecttype; //fwd
00042   }
00043 }
00044 
00064 class scopira::core::basic_loop : public virtual scopira::tool::object
00065 {
00066   public:
00068     static basic_loop * instance(void) { return dm_instance; }
00069 
00075     SCOPIRA_EXPORT basic_loop(int &argc, char **&argv);
00076 
00078     SCOPIRA_EXPORT ~basic_loop();
00079 
00086     SCOPIRA_EXPORT void list_config(std::vector<std::string> &out) const;
00087 
00093     SCOPIRA_EXPORT bool has_config(const std::string &key) const;
00094 
00101     SCOPIRA_EXPORT const std::string & get_config(const std::string &key) const;
00102 
00113     SCOPIRA_EXPORT bool get_config(const std::string &key, std::string &out) const;
00114 
00120     SCOPIRA_EXPORT void set_config(const std::string &key, const std::string &val);
00121 
00128     SCOPIRA_EXPORT void set_config_default(const std::string &key, const std::string &val);
00129 
00136     SCOPIRA_EXPORT void set_config_save(const std::string &key, const std::string &val);
00137 
00138 
00147     SCOPIRA_EXPORT void commit_config(void);
00148 
00156     SCOPIRA_EXPORT static std::string config_expand(const std::string &s);
00157 
00163     SCOPIRA_EXPORT objecttype * get_root_objecttype(void);
00164 
00171     const std::string &get_context_name(void) const { return dm_appctx; }
00172 
00178     const std::string &get_config_dir(void) const { return dm_configdir; }
00179 
00189     SCOPIRA_EXPORT bool save_object(const std::string &name, scopira::tool::object *o);
00190 
00199     SCOPIRA_EXPORT bool load_object(const std::string &name, scopira::tool::count_ptr<scopira::tool::object> &out);
00200 
00201   private:
00203     SCOPIRA_EXPORT static basic_loop *dm_instance;
00204 
00205 #ifndef NDEBUG
00206     // used to balance reference counts in debug mode only
00207     scopira::tool::objrefcounter dm_refcounter;
00208 #endif
00209     scopira::tool::fileflow dm_default_out;
00210     scopira::tool::output_stack dm_outputstack;
00211     scopira::tool::count_ptr<scopira::tool::objflowloader> dm_objloader;
00212 
00213     struct value_t
00214     {
00215       std::string pm_val;                 // the current value
00216       bool pm_disk;                       // is thie value marked for saving
00217       int pm_creatorid;                  // the stack level that this value was created at (used in config stacks)
00218 
00219       value_t(void) : pm_disk(false), pm_creatorid(0) { }
00220       value_t(const std::string &val, int creatorid) : pm_val(val), pm_disk(false),
00221         pm_creatorid(creatorid) { }
00222     };
00223 
00224     typedef std::map<std::string, value_t> configmap_t;
00225     configmap_t dm_configmap;
00226 
00227     // the current creator id to assign to new values
00228     // its incremented between each "phase" or level or context of parsing
00229     struct parse_context_t {
00230       int pm_creatorid;
00231       bool pm_disk;       // mark new nodes for writing (sets the value_t->pm_disk)
00232     };
00233     
00234     parse_context_t dm_parse_context;
00235 
00236     std::string dm_emptrystring;
00237     std::string dm_appctx;
00238     std::string dm_configdir;
00239 
00240     scopira::tool::count_ptr<objecttype> dm_root_objecttype;
00241 
00242     std::vector<scopira::tool::count_ptr< scopira::tool::dll > > dm_dynlibs;
00243     
00244 #ifdef PLATFORM_win32
00245     scopira::tool::net_loop dm_netlooper;
00246 #endif
00247 
00248   private:
00249     void parse_config(int argc, char **argv);
00250     void parse_config_file(const std::string &filename, bool cryonerrors);
00251     void parse_config_string(const std::string &s, bool newlinesonly);
00252     void parse_config_pair(const std::string &s);
00253     void process_config(void);
00254     void process_lib(const std::string &libname);
00255     void process_libdir(const std::string &dirname);
00256 };
00257 
00343 #endif
00344