StateFS
 All Classes Files Functions Variables Typedefs Enumerations Groups Pages
property.hpp
1 #ifndef _STATEFS_PROPERTY_HPP_
2 #define _STATEFS_PROPERTY_HPP_
3 
4 #include <statefs/provider.hpp>
5 
6 #include <string>
7 #include <mutex>
8 
9 namespace statefs {
10 
11 template <typename T>
12 struct PropTraits
13 {
15  typedef std::shared_ptr<handle_type> handle_ptr;
16 
17  PropTraits(std::string const& aname, std::string const& adefval)
18  : name(name), defval(adefval) {}
19 
20  handle_ptr create() const
21  {
22  return std::make_shared<handle_type>(name, defval);
23  }
24 
25  std::string name;
26  std::string defval;
27 };
28 
29 template <typename T>
30 Namespace& operator << (Namespace &ns, PropTraits<T> const &t)
31 {
32  ns << t.create();
33  return ns;
34 }
35 
36 template <typename T, typename HandleT>
37 Namespace& operator <<
38 (Namespace &ns, std::shared_ptr<BasicPropertyOwner<T, HandleT> > const &p)
39 {
40  ns.insert(std::static_pointer_cast<ANode>(p));
41  return ns;
42 }
43 
44 typedef std::function<int (std::string const&)> setter_type;
45 
46 template <typename T>
47 int read_from(T &src, char *dst, statefs_size_t len, statefs_off_t off)
48 {
49  auto sz = src.size();
50  if (off > sz)
51  return 0;
52 
53  if (off + len > sz)
54  len = sz - off;
55  memcpy(dst, &src[off], len);
56  return len;
57 }
58 
59 class AnalogProperty;
60 setter_type property_setter(std::shared_ptr<AnalogProperty> const &);
61 
62 template <typename T>
63 setter_type setter(std::shared_ptr<BasicPropertyOwner<T, std::string> > const& h)
64 {
65  return property_setter(h->get_impl());
66 }
67 
69 {
70 public:
71  AnalogProperty(statefs::AProperty *, std::string const&);
72 
73  int getattr() const { return STATEFS_ATTR_READ; }
74  statefs_ssize_t size() const;
75 
76  bool connect(::statefs_slot *slot) { return false; }
77 
78  int read(std::string *h, char *dst, statefs_size_t len, statefs_off_t);
79 
80  int write(std::string *h, char const *src
81  , statefs_size_t len, statefs_off_t off)
82  {
83  return -1;
84  }
85 
86  void disconnect() { }
87  void release() {}
88 
89 protected:
90 
92  void operator =(AnalogProperty const&);
93 
94  friend setter_type property_setter(std::shared_ptr<AnalogProperty> const &);
95  virtual int update(std::string const&);
96 
97  statefs::AProperty *parent_;
98  std::mutex m_;
99  std::string v_;
100 };
101 
103 {
104 public:
105  DiscreteProperty(statefs::AProperty *parent, std::string const &defval);
106  int getattr() const;
107  bool connect(::statefs_slot *slot);
108  void disconnect();
109 
110 private:
111 
112  virtual int update(std::string const&);
113 
114  ::statefs_slot *slot_;
115 };
116 
119 
121 {
122 public:
123  BasicWriter(statefs::AProperty *parent, setter_type update);
124 
125  int getattr() const;
126  statefs_ssize_t size() const;
127 
128  bool connect(::statefs_slot *slot);
129 
130  int read(std::string *h, char *dst, statefs_size_t len
131  , statefs_off_t off);
132 
133  int write(std::string *h, char const *src
134  , statefs_size_t len, statefs_off_t off);
135 
136  void disconnect() { }
137  void release() {}
138 
139 protected:
140  statefs::AProperty *parent_;
141  setter_type update_;
142  size_t size_;
143 };
144 
145 
146 // -----------------------------------------------------------------------------
147 
148 inline int DiscreteProperty::getattr() const
149 {
151 }
152 
153 inline bool DiscreteProperty::connect(::statefs_slot *slot)
154 {
155  slot_ = slot;
156  return true;
157 }
158 
159 inline void DiscreteProperty::disconnect()
160 {
161  slot_ = nullptr;
162 }
163 
164 inline int BasicWriter::getattr() const
165 {
166  return STATEFS_ATTR_WRITE;
167 }
168 
169 inline statefs_ssize_t BasicWriter::size() const
170 {
171  return size_ ;
172 }
173 
174 inline bool BasicWriter::connect(::statefs_slot *slot)
175 {
176  return false;
177 }
178 
179 inline int BasicWriter::read
180 (std::string *h, char *dst, statefs_size_t len, statefs_off_t off)
181 {
182  return -1;
183 }
184 
185 }
186 
187 #endif // _STATEFS_PROPERTY_HPP_