stencet  v0.1.16
Build C++ web server modules that allow easy routing and deployment.
viewModel.h
1 /* Copyright (C) 2012-2013 Justin Berger
2  The full license is available in the LICENSE file at the root of this project and is also available at http://opensource.org/licenses/MIT. */
3 
4 #pragma once
5 #include <iostream>
6 #include <map>
7 #include <vector>
8 #include <mxcomp/reflection.h>
9 #include <stdexcept>
10 
11 namespace stencet {
12  struct UnsupportedCast : public std::runtime_error {
13  UnsupportedCast(const std::string& what) : std::runtime_error(what) {}
14  };
15 
16  struct ViewModel {
17  bool managed = true;
18  enum Type {
19  Null = -1,
20  Object = 0,
21  List = 1,
22  String = 2,
23  Double = 3,
24  Int = 4,
25  Bool = 5
26  };
27 
28  virtual bool isConvertible(Type t) const;
29 
30  virtual Type getType() const { return Object; }
31  virtual void asString(std::string& str) const { throw UnsupportedCast(""); };
32  virtual double asDouble() const { return asInt(); }
33  virtual bool asBool() const { return asInt() > 0; }
34  virtual int asInt() const { return asDouble(); };
35 
36  virtual size_t size() const;
37  virtual bool hasValue(const std::string& name) { return false; };
38 
39  virtual ~ViewModel();
40  virtual ViewModel* at(size_t);
41  virtual ViewModel* at(const std::string& name) { throw UnsupportedCast(""); } ;
42  };
43 
44  inline bool operator==(const ViewModel& lhs, const ViewModel& rhs);
45  inline bool operator!=(const ViewModel& lhs, const ViewModel& rhs);
46  inline bool operator< (const ViewModel& lhs, const ViewModel& rhs);
47  inline bool operator> (const ViewModel& lhs, const ViewModel& rhs);
48  inline bool operator<=(const ViewModel& lhs, const ViewModel& rhs);
49  inline bool operator>=(const ViewModel& lhs, const ViewModel& rhs);
50 
51  std::ostream& operator<<(std::ostream& stream, const ViewModel& variant);
52 
53  template <typename T> struct ViewModel_;
54 }
55 
56 #include "viewModel.tcc"