CYCLUS
resource.h
Go to the documentation of this file.
1 #ifndef CYCLUS_SRC_RESOURCE_H_
2 #define CYCLUS_SRC_RESOURCE_H_
3 
4 #include <string>
5 #include <vector>
6 #include <boost/shared_ptr.hpp>
7 
8 class SimInitTest;
9 
10 namespace cyclus {
11 
12 class Context;
13 
15 
16 /// Resource defines an abstract interface implemented by types that are
17 /// offered, requested, and transferred between simulation agents. Resources
18 /// represent the lifeblood of a simulation.
19 class Resource {
20  friend class SimInit;
21  friend class ::SimInitTest;
22 
23  public:
24  typedef boost::shared_ptr<Resource> Ptr;
25 
26  Resource() : state_id_(nextstate_id_++), obj_id_(nextobj_id_++) {}
27 
28  virtual ~Resource() {}
29 
30  /// Returns the unique id corresponding to this resource object. Can be used
31  /// to track and/or associate other information with this resource object.
32  /// You should NOT track resources by pointer.
33  const int obj_id() const { return obj_id_; }
34 
35  /// Returns the unique id corresponding to this resource and its current
36  /// state. All resource id's are unique - even across different resource
37  /// types/implementations. Runtime tracking of resources should generally
38  /// use the obj_id rather than this.
39  const int state_id() const {
40  return state_id_;
41  }
42 
43  /// Assigns a new, unique internal id to this resource and its state. This should be
44  /// called by resource implementations whenever their state changes. A call to
45  /// BumpStateId is not necessarily accompanied by a change to the state id.
46  /// This should NEVER be called by agents.
47  void BumpStateId();
48 
49  /// Returns an id representing the specific resource implementation's internal
50  /// state that is not accessible via the Resource class public interface. Any
51  /// change to the qual_id should always be accompanied by a call to
52  /// BumpStateId.
53  virtual int qual_id() const = 0;
54 
55  /// A unique type/name for the concrete resource implementation.
56  virtual const ResourceType type() const = 0;
57 
58  /// Returns an untracked (not part of the simulation) copy of the resource.
59  /// A cloned resource should never record anything in the output database.
60  virtual Ptr Clone() const = 0;
61 
62  /// Records the resource's state to the output database. This method
63  /// should generally NOT record data accessible via the Resource class
64  /// public methods (e.g. qual_id, units, type, quantity).
65  /// @param ctx the simulation context used to record the data.
66  virtual void Record(Context* ctx) const = 0;
67 
68  /// Returns the units this resource is based in (e.g. "kg").
69  virtual std::string units() const = 0;
70 
71  /// Returns the quantity of this resource with dimensions as specified by
72  /// the return value of units().
73  virtual double quantity() const = 0;
74 
75  /// Splits the resource and returns the extracted portion as a new resource
76  /// object. Allows for things like ResBuf and Traders to split
77  /// offers/requests of arbitrary resource implementation type.
78  ///
79  /// @return a new resource object with same state id and quantity == quantity
80  virtual Ptr ExtractRes(double quantity) = 0;
81 
82  private:
83  static int nextstate_id_;
84  static int nextobj_id_;
85  int state_id_;
86  int obj_id_;
87 };
88 
89 /// Casts a vector of Resources into a vector of a specific resource type T.
90 template <class T>
91 std::vector<typename T::Ptr> ResCast(std::vector<Resource::Ptr> rs) {
92  std::vector<typename T::Ptr> casted;
93  for (int i = 0; i < rs.size(); ++i) {
94  casted.push_back(boost::dynamic_pointer_cast<T>(rs[i]));
95  }
96  return casted;
97 }
98 
99 /// Casts a Resource::Ptr into a pointer of a specific resource type T.
100 template <class T>
101 typename T::Ptr ResCast(Resource::Ptr r) {
102  return boost::dynamic_pointer_cast<T>(r);
103 }
104 
105 } // namespace cyclus
106 
107 #endif // CYCLUS_SRC_RESOURCE_H_
const int obj_id() const
Returns the unique id corresponding to this resource object.
Definition: resource.h:33
virtual const ResourceType type() const =0
A unique type/name for the concrete resource implementation.
const int state_id() const
Returns the unique id corresponding to this resource and its current state.
Definition: resource.h:39
std::string ResourceType
Definition: resource.h:12
std::vector< typename T::Ptr > ResCast(std::vector< Resource::Ptr > rs)
Casts a vector of Resources into a vector of a specific resource type T.
Definition: resource.h:91
virtual int qual_id() const =0
Returns an id representing the specific resource implementation&#39;s internal state that is not accessib...
virtual double quantity() const =0
Returns the quantity of this resource with dimensions as specified by the return value of units()...
virtual ~Resource()
Definition: resource.h:28
virtual std::string units() const =0
Returns the units this resource is based in (e.g. "kg").
Resource defines an abstract interface implemented by types that are offered, requested, and transferred between simulation agents.
Definition: resource.h:19
void BumpStateId()
Assigns a new, unique internal id to this resource and its state.
Definition: resource.cc:8
virtual Ptr Clone() const =0
Returns an untracked (not part of the simulation) copy of the resource.
A simulation context provides access to necessary simulation-global functions and state...
Definition: context.h:130
boost::shared_ptr< Resource > Ptr
Definition: resource.h:24
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
virtual Ptr ExtractRes(double quantity)=0
Splits the resource and returns the extracted portion as a new resource object.
Handles initialization of a simulation from the output database.
Definition: sim_init.h:24
virtual void Record(Context *ctx) const =0
Records the resource&#39;s state to the output database.