00001 /** 00002 * @file order.h 00003 * @author Reshen Amin <reshen@zensrc.com> 00004 * 00005 * @brief Contains the class definition for @ref Order 00006 * 00007 * @license Byzantine Generals Problem Coding Sample.<br> 00008 * Copyright (C) 2010 Reshen Amin.<br> 00009 * See @ref license_sec for details.<br> 00010 */ 00011 00012 #ifndef SRC_ORDER_H_ 00013 #define SRC_ORDER_H_ 00014 00015 #include <boost/shared_ptr.hpp> 00016 00017 #include "sharedmemtypes.h" 00018 00019 /** 00020 * @brief The types of Marching Orders that may be passed amongst 00021 * the Generals. We wrap in a namespace here to avoid polluting 00022 * the global namespace and to help avoid name collisions. 00023 */ 00024 namespace MarchingOrders { 00025 /** 00026 * @brief See @ref MarchingOrders 00027 */ 00028 enum type { 00029 kInvalid = 0,/*!< Invalid order */ 00030 kAttack, /*!< Attack city */ 00031 kRetreat, /*!< Retreat from battlefield */ 00032 }; 00033 }; 00034 00035 /** 00036 * @brief This represents the basic message that is passed around 00037 * amongst the generals during operation. Each receiving general 00038 * (regardless of loyalty) is obligated to append their Unique ID to 00039 * the enclosed @ref communication_path. A Loyal General will never 00040 * modify the enclosed @ref marching_orders, while Disloyal Generals 00041 * may to cause as much confusion as possible. 00042 */ 00043 class Order { 00044 public: 00045 00046 /** 00047 * @brief A list of Generals' Unique IDs whom have seen this Order. 00048 * The first entry was the General who created this Order, and 00049 * the last entry is the currently owning General. All Generals 00050 * who receive this Order are required to append their Unique ID 00051 * to the list, preferably using @ref AppendCommPathEntry. 00052 */ 00053 SharedMemoryUIntList communication_path; 00054 00055 /** 00056 * @brief The relayed Order. 00057 */ 00058 MarchingOrders::type marching_orders; 00059 00060 /** 00061 * @brief Constructor 00062 * 00063 * @param alloc The allocator to use in the underlying @ref 00064 * communication_path 00065 * @param march_orders The initial marching orders to carry 00066 */ 00067 Order(const SharedMemoryUIntAllocator &alloc, 00068 MarchingOrders::type march_orders = MarchingOrders::kInvalid); 00069 00070 /** 00071 * @brief Copy Constructor 00072 * 00073 * @param o Order to copy 00074 */ 00075 Order(const Order& o); 00076 00077 /** 00078 * @brief Destructor 00079 */ 00080 ~Order(); 00081 00082 /** 00083 * @brief Helper function to convert a marching order to a human 00084 * readable string. For example, MarchingOrders::kAttack will get 00085 * converted to "Attack". This same paradigm applies to both "Retreat" 00086 * and "Invalid". 00087 * 00088 * @param marching_order Marching order to convert 00089 * 00090 * @return Human friendly converted string 00091 */ 00092 static boost::interprocess::string 00093 MarchingOrderTypeToString(MarchingOrders::type marching_order); 00094 00095 /** 00096 * @brief Helper function to add a general's unique ID to the @ref 00097 * communication_path 00098 * 00099 * @param kUniqueID General's Unique ID 00100 */ 00101 void AppendCommPathEntry(const unsigned int kUniqueID); 00102 00103 /** 00104 * @brief Convert the enclosed communication path to a human 00105 * readable string where unique IDs are delimited by a space. 00106 * For example, if the communication path contains the General 00107 * IDs 0 and 1, the output of this function would be "0 1". 00108 * 00109 * @return Communication path as a string 00110 */ 00111 boost::interprocess::string CommunicationPathToString() const; 00112 }; 00113 00114 /** 00115 * @brief Type for a Shared pointer which wraps an @ref Order 00116 */ 00117 typedef boost::shared_ptr<Order> LocalOrder; 00118 00119 #endif // SRC_ORDER_H_
1.6.3