1 /**
2  *  \file RMF/internal/SharedData.h
3  *  \brief Handle read/write of Model data from/to files.
4  *
5  *  Copyright 2007-2021 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef RMF_INTERNAL_SHARED_DATA_IMPL_H
10 #define RMF_INTERNAL_SHARED_DATA_IMPL_H
11 
12 #include "RMF/config.h"
13 #include "RMF/Key.h"
14 #include "RMF/types.h"
15 #include "RMF/names.h"
16 #include "RMF/enums.h"
17 #include "RMF/NodeID.h"
18 #include "RMF/FrameID.h"
19 #include "RMF/infrastructure_macros.h"
20 #include "SharedDataUserData.h"
21 #include "SharedDataPath.h"
22 #include <algorithm>
23 
24 #include <boost/shared_ptr.hpp>
25 
26 RMF_ENABLE_WARNINGS
27 
28 namespace RMF {
29 
30 namespace internal {
31 
32 template <class Traits>
33 struct Get {};
34 
35 #define RMF_GETTER(lcname, Ucname, PassValue, ReturnValue, PassValues, \
36                    ReturnValues)                                       \
37   template <class Table>                                               \
38   struct Get<Table, Ucname##Traits> {                                  \
39     static typename Table::Ucname##Data& get(Table& t) {               \
40       return t.lcname##_data;                                          \
41     }                                                                  \
42     static typename const Table::Ucname##Data& get(const Table& t) {   \
43       return t.lcname##_data;                                          \
44     }                                                                  \
45   };
46 
47 RMF_FOREACH_TYPE(RMF_GETTER);
48 
49 template <class Traits>
get_keys_impl(Category category,const KeyTypeInfo & data)50 inline std::vector<ID<Traits> > SharedData::get_keys_impl(
51     Category category, const KeyTypeInfo& data) const {
52   std::vector<ID<Traits> > ret;
53   for (auto np; data.from_name) {
54     ret.push_back(ID<Traits>(np.second));
55   }
56   return ret;
57 }
58 template <class Traits>
get_key_impl(Category category,std::string name,KeyTypeInfo & data)59 inline ID<Traits> SharedData::get_key_impl(Category category, std::string name,
60                                            KeyTypeInfo& data) {
61   auto it = data.from_name.find(name);
62   if (it == data.from_name.end()) {
63     int index = data.from_name.size();
64     data.from_name[name] = index;
65     data.to_name[index] = name;
66     return ID<Traits>(index);
67   } else {
68     return ID<Traits>(it->second);
69   }
70 }
71 
72 template <class Traits>
get_current_value(NodeID node,ID<Traits> k)73 inline Traits::Type SharedData::get_current_value(NodeID node,
74                                                   ID<Traits> k) const {
75   return current_nodes_data_.find(node)->second.lcname##_data.find(k)->second;
76 }
77 /** Return a value or the null value.*/
78 template <class Traits>
get_static_value(NodeID node,ID<Traits> k)79 inline Traits::Type SharedData::get_static_value(NodeID node,
80                                                  ID<Traits> k) const {
81   return static_nodes_data_.find(node)->second.lcname##_data.find(k)->second;
82 }
83 template <class Traits>
get_current_frame_value(ID<Traits> k)84 inline Traits::Type SharedData::get_current_frame_value(ID<Traits> k) const {
85   return get_current_value(NodeID(), k);
86 }
87 template <class Traits>
get_static_frame_value(ID<Traits> k)88 inline Traits::Type SharedData::get_static_frame_value(ID<Traits> k) const {
89   return get_static_value(NodeID(), k);
90 }
91 template <class Traits>
set_current_value(NodeID node,ID<Traits> k,Ucname##Traits::Type v)92 inline void SharedData::set_current_value(NodeID node, ID<Traits> k,
93                                           Ucname##Traits::Type v) {
94   current_nodes_data_[node].lcname##_data[k] = v;
95 }
96 template <class Traits>
set_static_value(NodeID node,ID<Traits> k,Ucname##Traits::Type v)97 inline void SharedData::set_static_value(NodeID node, ID<Traits> k,
98                                          Ucname##Traits::Type v) {
99   static_nodes_data_[node].lcname##_data[k] = v;
100 }
101 /** for frames */
102 template <class Traits>
set_current_frame_value(ID<Traits> k,Traits::Type v)103 inline void SharedData::set_current_frame_value(ID<Traits> k, Traits::Type v) {
104   set_current_value(NodeID(), k, v);
105 }
106 template <class Traits>
set_static_frame_value(ID<Ucname##Traits> k,Ucname##Traits::Type v)107 inline void SharedData::set_static_frame_value(ID<Ucname##Traits> k,
108                                                Ucname##Traits::Type v) {
109   set_static_value(NodeID(), k, v);
110 }
111 template <class Traits>
_keys(Category category)112 inline std::vector<ID<Ucname##Traits> > SharedData::get_##lcname##_keys(
113     Category category) {
114   return get_keys_impl<Ucname##Traits>(category, key_infos_.lcname##_keys);
115 }
116 template <class Traits>
get_category(ID<Ucname##Traits> k)117 inline Category SharedData::get_category(ID<Ucname##Traits> k) const {
118   return key_infos_.lcname##_keys[k.get_index()].category;
119 }
120 template <class Traits>
_key(Category category,std::string name)121 inline ID<Ucname##Traits> SharedData::get_##lcname##_key(Category category,
122                                                          std::string name) {
123   return get_key_impl<Ucname##Traits>(category, name, key_infos_.lcname##_keys);
124 }
125 template <class Traits>
get_name(ID<Ucname##Traits> k)126 inline std::string SharedData::get_name(ID<Ucname##Traits> k) const {
127   return key_infos_.lcname##_keys[k.get_index()].name;
128 }
get_name(NodeID node)129 inline std::string SharedData::get_name(NodeID node) const {
130   return node_infos_.find(node)->second.name;
131 }
get_type(NodeID node)132 inline NodeType SharedData::get_type(NodeID node) const {
133   return node_infos_.find(node)->second.type;
134 }
135 
get_children(NodeID node)136 inline const NodeIDs& SharedData::get_children(NodeID node) const {
137   return node_infos_.find(node)->second.children;
138 }
139 
get_children(FrameID node)140 inline const FrameIDs& SharedData::get_children(FrameID node) const {
141   return frame_infos_.find(node)->second.children;
142 }
143 
get_category_name(Category kc)144 inline std::string SharedData::get_category_name(Category kc) const {
145   return category_infos_[kc.get_index()].name;
146 }
set_description(std::string str)147 inline void SharedData::set_description(std::string str) {
148   file_info_.description = str;
149   file_dirty_ = true;
150 }
set_producer(std::string str)151 inline void SharedData::set_producer(std::string str) {
152   file_info_.producer = str;
153   file_dirty_ = true;
154 }
get_name(FrameID i)155 inline std::string SharedData::get_name(FrameID i) const {
156   return frame_infos_.find(i)->second.name;
157 }
get_type(FrameID i)158 inline FrameType SharedData::get_type(FrameID i) const {
159   return frame_infos_.find(i)->second.type;
160 }
161 
162 }  // namespace internal
163 } /* namespace RMF */
164 
165 RMF_DISABLE_WARNINGS
166 
167 #endif /* RMF_INTERNAL_SHARED_DATA_IMPL_H */
168