12 #include <boost/noncopyable.hpp>
13 #include <boost/any.hpp>
14 #include <boost/shared_ptr.hpp>
15 #include <boost/call_traits.hpp>
20 #define FLAME_API_VERSION 0
45 virtual void show(std::ostream&,
int level =0)
const {}
74 case 3: ret &= d[2]<dim[2];
75 case 2: ret &= d[1]<dim[1];
76 case 1: ret &= d[0]<dim[0];
81 void *raw(
size_t* d) {
82 char *ret = (
char*)ptr;
84 case 3: ret += d[2]*stride[2];
85 case 2: ret += d[1]*stride[1];
86 case 1: ret += d[0]*stride[0];
93 inline E*
get(
size_t *d) {
118 virtual bool getArray(
unsigned index, ArrayInfo& Info);
140 std::ostream& operator<<(std::ostream& strm,
const StateBase& s)
185 virtual const char*
type_name()
const =0;
208 virtual void show(std::ostream&,
int level)
const;
249 size_t max=-1)
const;
287 inline const std::string&
simtype()
const {
return p_simtype;}
290 inline std::ostream*
trace()
const {
return p_trace;}
301 typedef std::vector<ElementVoid*> p_elements_t;
306 LookupKey(
const std::string& n,
size_t i) :name(n), index(i) {}
307 bool operator<(
const LookupKey& o)
const {
308 int ord = name.compare(o.name);
309 if(ord<0)
return true;
310 else if(ord>0)
return false;
311 else return index<o.index;
315 typedef std::map<LookupKey, ElementVoid*> p_lookup_t;
319 inline size_t size()
const {
return p_elements.size(); }
337 iterator
begin() {
return p_elements.begin(); }
339 const_iterator
begin()
const {
return p_elements.begin(); }
342 iterator
end() {
return p_elements.end(); }
344 const_iterator
end()
const {
return p_elements.end(); }
350 p_lookup_t::const_iterator low (p_lookup.lower_bound(LookupKey(name, 0))),
351 high(p_lookup.upper_bound(LookupKey(name, (
size_t)-1)));
353 for(;low!=high;++low,++i) {
363 std::pair<lookup_iterator, lookup_iterator> all_range() {
370 std::pair<lookup_iterator, lookup_iterator>
equal_range(
const std::string& name) {
371 p_lookup_t::iterator low (p_lookup.lower_bound(LookupKey(name, 0))),
372 high(p_lookup.upper_bound(LookupKey(name, (
size_t)-1)));
380 p_lookup_t::iterator low (p_lookup_type.lower_bound(LookupKey(name, 0))),
381 high(p_lookup_type.upper_bound(LookupKey(name, (
size_t)-1)));
388 p_elements_t p_elements;
390 p_lookup_t p_lookup_type;
391 std::string p_simtype;
392 std::ostream* p_trace;
396 template<
typename State>
397 struct state_builder_impl {
399 {
return new State(c); }
401 struct element_builder_t {
402 virtual ~element_builder_t() {}
406 template<
typename Element>
407 struct element_builder_impl :
public element_builder_t {
408 virtual ~element_builder_impl() {}
410 {
return new Element(c); }
413 std::auto_ptr<ElementVoid> N(build(c));
414 Element *m =
dynamic_cast<Element*
>(o);
416 throw std::runtime_error(
"reconfigure() can't change element type");
423 state_builder_t builder;
424 typedef std::map<std::string, element_builder_t*> elements_t;
430 typedef std::map<std::string, state_info> p_state_infos_t;
431 static p_state_infos_t p_state_infos;
433 static void p_registerState(
const char *name, state_builder_t b);
435 static void p_registerElement(
const std::string& sname,
const char *ename, element_builder_t* b);
456 template<
typename State>
459 p_registerState(name, &state_builder_impl<State>::build);
473 template<
typename Element>
476 p_registerElement(sname, ename,
new element_builder_impl<Element>);
491 friend std::ostream& operator<<(std::ostream&,
const Machine& m);
496 unsigned short level;
497 std::ostringstream strm;
498 LogRecord(
const char *fname,
unsigned short lnum,
unsigned short lvl)
499 :fname(fname), lnum(lnum), level(lvl) {}
502 LogRecord& operator<<(T v)
503 { strm<<v;
return *
this; }
505 LogRecord(
const LogRecord&);
506 LogRecord& operator=(
const LogRecord&);
511 virtual void log(
const LogRecord& r)=0;
514 static int log_detail;
516 static inline bool detail(
int lvl) {
return log_detail<=lvl; }
518 static void set_logger(
const boost::shared_ptr<Logger>& p);
520 static boost::shared_ptr<Logger> p_logger;
523 #define FLAME_ERROR 40
524 #define FLAME_WARN 30
525 #define FLAME_INFO 20
526 #define FLAME_DEBUG 10
530 #define FLAME_LOG_ALWAYS(LVL) Machine::LogRecord(__FILE__, __LINE__, FLAME_##LVL)
532 #ifndef FLAME_DISABLE_LOG
533 #define FLAME_LOG_CHECK(LVL) UNLIKELY(Machine::detail(FLAME_##LVL))
535 #define FLAME_LOG_CHECK(LVL) (false)
539 #define FLAME_LOG(LVL) if(FLAME_LOG_CHECK(LVL)) FLAME_LOG_ALWAYS(LVL)
541 std::ostream& operator<<(std::ostream&,
const Machine& m);
544 void registerLinear();
546 void registerMoment();
548 #endif // FLAME_BASE_H
StateBase(const Config &c)
ElementVoid(const Config &conf)
Construct this element using the provided Config.
void propagate(StateBase *S, size_t start=0, size_t max=-1) const
Pass the given bunch State through this Machine.
p_elements_t::iterator iterator
Beamline element iterator.
virtual void show(std::ostream &, int level=0) const
Base class for all simulated elements.
double length
Longitudual length of this element (added to StateBase::pos)
virtual void view(const ElementVoid *elem, const StateBase *state)=0
Called from within Machine::propagate()
Machine(const Config &c)
Construct a new Machine.
bool inbounds(size_t *d) const
is the given index valid?
const std::string & simtype() const
Return the sim_type string found during construction.
static void registerState(const char *name)
Register a new State with the simulation framework.
static void registerElement(const char *sname, const char *ename)
Register a new Element type with the simulation framework.
The core simulate Machine engine.
virtual StateBase * clone() const =0
virtual const char * type_name() const =0
virtual void advance(StateBase &s)=0
Propogate the given State through this Element.
Type
The parameter type Double (double) or Sizet (size_t)
The abstract base class for all simulation state objects.
std::pair< lookup_iterator, lookup_iterator > equal_range_type(const std::string &name)
iterator begin()
Points to the first element.
virtual void assign(const ElementVoid *other)=0
ElementVoid * find(const std::string &name, size_t nth=0)
Associative configuration container.
Used with StateBase::getArray() to describe a single parameter.
virtual void assign(const StateBase &other)=0
static void registeryCleanup()
Discard all registered State and Element type information.
double pos
absolute longitudinal position at end of Element
const ElementVoid * at(size_t i) const
Access a beamline element.
const std::string name
Name of this element (unique in its Machine)
size_t dim[maxdims]
Array dimensions in elements.
const Config & conf() const
The Config used to construct this element.
Allow inspection of intermediate State.
const Config & conf() const
Fetch Config used to construct this Machine.
value_proxy_iterator< p_lookup_t::iterator > lookup_iterator
iterator for use with equal_range() and equal_range_type()
void set_trace(std::ostream *v)
Assign new tracing stream.
ElementVoid * operator[](size_t i)
Access a beamline element.
iterator end()
Points just after the last element.
ElementVoid * at(size_t i)
Access a beamline element.
void reconfigure(size_t idx, const Config &c)
Change the configuration of a single element.
size_t stride[maxdims]
Array strides in bytes.
const size_t index
Index of this element (unique in its Machine)
Observer * observer() const
The current observer, or NULL.
virtual bool getArray(unsigned index, ArrayInfo &Info)
Introspect named parameter of the derived class.
std::ostream * trace() const
The current tracing stream, or NULL.
std::pair< lookup_iterator, lookup_iterator > equal_range(const std::string &name)
const_iterator begin() const
Points to the first element.
const ElementVoid * operator[](size_t i) const
Access a beamline element.
const_iterator end() const
Points just after the last element.
void set_observer(Observer *o)
virtual void show(std::ostream &, int level) const
StateBase * allocState() const
const char * name
The parameter name.
p_elements_t::const_iterator const_iterator
Beamline element iterator (const version)