stromx  0.8.0
Data.h
1 /*
2  * Copyright 2011 Matthias Fuchs
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef STROMX_RUNTIME_DATA_H
18 #define STROMX_RUNTIME_DATA_H
19 
20 #include <typeinfo>
21 #include "stromx/runtime/Config.h"
22 #include "stromx/runtime/DataInterface.h"
23 #include "stromx/runtime/Exception.h"
24 #include "stromx/runtime/VariantHandle.h"
25 
26 namespace stromx
27 {
28  namespace runtime
29  {
30  class Data;
31  class DataRef;
32  class InputProvider;
33  class OutputProvider;
34  class Version;
35 
37  template<class data_t>
38  class data_traits
39  {
40  public:
41  static const VariantHandle & variant();
42  };
43 
44  template<>
45  class STROMX_RUNTIME_API data_traits<Data>
46  {
47  public:
48  static const VariantHandle & variant();
49  };
53  class STROMX_RUNTIME_API Data : public DataInterface
54  {
55  public:
56  virtual ~Data() {}
57 
58  virtual void serialize(OutputProvider & out) const;
59  virtual void deserialize(InputProvider & in, const Version & version);
60  };
61 
67  template<typename data_t>
68  data_t & data_cast(Data & data)
69  {
70  if(data.isVariant(data_traits<data_t>::variant()))
71  return reinterpret_cast<data_t &>(data);
72  else
73  throw BadCast();
74  }
75 
81  template<typename data_t>
82  const data_t & data_cast(const Data & data)
83  {
84  if(data.isVariant(data_traits<data_t>::variant()))
85  return reinterpret_cast<const data_t &>(data);
86  else
87  throw BadCast();
88  }
89 
94  template<typename data_t>
95  data_t* data_cast(Data * data)
96  {
97  if(data && data->isVariant(data_traits<data_t>::variant()))
98  return reinterpret_cast<data_t*>(data);
99  else
100  return 0;
101  }
102 
107  template<typename data_t>
108  const data_t* data_cast(const Data * data)
109  {
110  if(data && data->isVariant(data_traits<data_t>::variant()))
111  return reinterpret_cast<const data_t*>(data);
112  else
113  return 0;
114  }
115  }
116 }
117 
118 #endif // STROMX_RUNTIME_DATA_H
A version of an operator or data type.
Definition: Version.h:46
Abstract data object.
Definition: Data.h:53
bool isVariant(const VariantInterface &v) const
Definition: DataInterface.h:72
Provides functions to serialize data to strings and files.
Definition: OutputProvider.h:27
Provides functions to deserialize data from strings and files.
Definition: InputProvider.h:27
Version version()
Returns the version of the stromx runtime library.
Definition: Runtime.cpp:54
The stromx class library.
Definition: AdjustRgbChannels.cpp:29
Common interface of data objects or references to data objects.
Definition: DataInterface.h:32
An impossible cast was attempted.
Definition: Exception.h:183
const data_t & data_cast(ConstDataRef &data)
Definition: ConstDataRef.h:97