Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / docs / tutorials / Quoter / RTCORBA / Stock_Database.h
blob1a22404b83123caba7c425616a317f9422b0c653
1 /**
2 * @file Stock_Database.h
3 * @author Shanshan Jiang <shanshan.jiang@vanderbilt.edu>
4 * @author William R. Otte <wotte@dre.vanderbilt.edu>
5 * @author Douglas C. Schmidt <schmidt@dre.vanderbilt.edu>
6 */
8 #ifndef STOCK_DATABASE_H_
9 #define STOCK_DATABASE_H_
11 // ACE headers
12 #include "ace/Task.h"
14 // TAO headrs
15 // #include "tao/orbconf.h"
17 // STL headers
18 #include <map>
19 #include <string>
21 /**
22 * @class Stock_Database
23 * @brief This class is used to install, update and publish the information of
24 * all the stocks. It uses the singleton design pattern.
25 * The parameter type may be any type that has a method called "updated_stocks" and
26 * accepts a std::vector of std::strings as an argument.
28 template <typename CALLBACK>
29 class Stock_Database : public ACE_Task_Base
31 public:
32 /// Default constructor
33 /// Initializes the stock database with MSFT, IBM, and INTEL.
34 /// @param the rate at which to perform updates
35 Stock_Database (u_int rate = 1);
37 /// Constructor.
38 /// @param file The name of a file to read initial stocks and values from.
39 Stock_Database (const char *file, u_int rate = 1);
41 typedef std::map <std::string,
42 unsigned int> Init_Map;
44 /// Constructor
45 /// @param stockmap A map containing stocks and initial values. An initial value
46 /// of 0 will be assigned a random start value.
47 Stock_Database (const Init_Map &stockmap, u_int rate = 0);
49 typedef std::string Cookie;
51 /**
52 * Register a callback object with the database. The callback object must have
53 * the () operator defined accepting a std::vector of strings as the argument.
54 * @returns A cookie to identify the registration
56 Cookie register_callback (CALLBACK &obj);
58 /**
59 * Removes a callback from the notification queue.
60 * @returns false if the provided cookie is not found.
62 bool remove_callback (const Cookie &);
64 /// Raised if an invalid stock name is requested.
65 class Invalid_Stock {};
67 struct StockInfo
69 StockInfo (void)
70 : name_(""), high_ (0), low_ (0), last_ (0)
71 {};
73 StockInfo (const std::string name)
74 : name_ (name), high_ (0), low_(0), last_(0)
75 {};
77 std::string name_;
78 int high_;
79 int low_;
80 int last_;
83 /**
84 * Create a StockInfo object stored in the database with the given name.
86 * @param name The name of the stock.
87 * @return A StockInfo object.
89 StockInfo get_stock_info (const char *name);
91 /**
92 * This function is used to calculate the new high, low and last values
93 * for each stock in the stock database randomly.
95 virtual int svc (void);
97 /// Change the rate at which database updates are made
98 void update_rate (u_int rate);
100 /// Launch the active object
101 void start (void);
103 /// Stop the active object
104 void stop (void);
106 typedef std::map <std::string, StockInfo> Stock_Map;
108 /// This method is not intended to be called by clients of this class,
109 /// it is public only by necessity.
110 virtual int handle_signal (int signum,
111 siginfo_t * = 0,
112 ucontext_t * = 0);
114 typedef std::map <Cookie, CALLBACK *> Callback_Map;
116 private:
117 /// The filname initialized from, if any.
118 const std::string filename_;
120 /// Keep track of the stock names and information about them.
121 Stock_Map stock_map_;
123 /// Lock to protect concurrent access to the <stock_map_>.
124 ACE_RW_Thread_Mutex lock_;
126 /// Rate at which updates are made.
127 u_int rate_;
129 Callback_Map callbacks_;
131 bool active_;
134 #include "Stock_Database.tpp"
136 //typedef ACE_Singleton<Stock_Database, TAO_SYNCH_MUTEX> Stock_Database_Singleton;
137 //#define STOCK_DATABASE Stock_Database_Singleton::instance()
139 #endif // !defined STOCK_DATABASE_H_