Attribute.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../Instancing/Instance.hpp"
4 #include "../Basics/volatile_set.hpp"
5 #include "../Basics/StringIO_Table.hpp"
6 #include "../Basics/valgrind.hpp"
7 
8 #include <typeindex>
9 #include <any>
10 
11 namespace iv
12 {
13 
14 class Attribute;
15 
16 //==================== AttributeListener ==========================
18 {
19 public:
23  virtual void Attribute_Activated( Attribute * ){}
24 
29  virtual void Attribute_Deactivated( Attribute * ){}
30 
34  virtual void Attribute_Changed( Attribute * ){}
35 };
36 
37 //==================== AttributeEventProcessor ========================
39 {
40 public:
41  enum class Event
42  {
43  Activated,
45  Changed,
48  };
49 
53  virtual void AttributeEvent( AttributeEventProcessor::Event event, Attribute * attr, AttributeListener * listener ) = 0;
54 };
55 
56 //====================== Attribute ================================
57 class Attribute
58 {
59 public:
60  enum class ValueMode
61  {
62  Disabled,
63  Value,
65  };
66 
72 
75  ClientMarker * owner() const;
76 
79  std::type_index Type();
80 
83  ValueMode Mode() const;
84 
87  std::any GetAny();
88 
92  void ModifyMode( ClientMarker * modifier, Attribute::ValueMode );
93 
98  void ModifyAny( ClientMarker * modifier, std::any );
99 
103 
107 
108 protected:
109  void SetAttributeMode( ValueMode );
110  void SourceValueChanged();
111 
114 
118  void Notify_Activated();
119 
123  void Notify_Deactivated();
124 
125  virtual std::type_index Type_Impl() = 0;
126  virtual std::any GetAny_Impl() = 0;
127  virtual void ModifyAny_Impl( ClientMarker * modifier, std::any ) = 0;
128 
129  bool allow_ModifyMode();
130 
131 private:
132  ClientMarker * cm;
133  ValueMode _mode;
134  AttributeEventProcessor * _processor;
135  bool _allow_ModifyMode;
136 };
137 
138 //===================== Attr ============================
141 template< class T >
142 class Attr : public Attribute
143 {
144 public:
146 
147  T Get() const;
148 
151  void Modify( ClientMarker * modifier, T const & );
152 
153 protected:
156  virtual void GetSourceValue( T & out ) const = 0;
157 
161  virtual void ModifySource( T const & ) = 0;
162 
163 private:
164  virtual std::type_index Type_Impl() override;
165  virtual std::any GetAny_Impl() override;
166  virtual void ModifyAny_Impl( ClientMarker * modifier, std::any ) override;
167 };
168 
169 
170 //=================== StringIO ========================================================================
171 template<>
172 struct StringIO< Attribute::ValueMode > : public StringIO_Table< Attribute::ValueMode >
173 {
174  static const ValuesType Values;
175 };
176 
177 template<>
178 struct StringIO< AttributeEventProcessor::Event > : public StringIO_Table< AttributeEventProcessor::Event >
179 {
180  static const ValuesType Values;
181 };
182 
183 template< class T >
184 struct StringIO< Attr< T > >
185 {
186  std::string Write( Attr< T > const & value, Context const * context ) const
187  {
188  std::stringstream ss;
189  ss << StringIO_Write( value.Mode(), context ) << "/" << StringIO_Write( value.Get(), context );
190  return ss.str();
191  }
192 };
193 
194 }
195 
196 #include "Attribute.inl"