OpFlex Framework  1.7.0
MO.h
Go to the documentation of this file.
1 /* -*- C++ -*-; c-basic-offset: 4; indent-tabs-mode: nil */
6 /*
7  * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
8  *
9  * This program and the accompanying materials are made available under the
10  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
11  * and is available at http://www.eclipse.org/legal/epl-v10.html
12  */
13 
14 #ifndef OPFLEX_CORE_MO_H
15 #define OPFLEX_CORE_MO_H
16 
17 #include <string>
18 
19 #include <boost/ref.hpp>
20 #include <boost/optional.hpp>
21 #include <boost/noncopyable.hpp>
22 
26 #include "opflex/ofcore/OFTypes.h"
27 
28 namespace opflex {
29 namespace modb {
30 namespace mointernal {
31 
48 class MO : private boost::noncopyable {
49 public:
53  virtual ~MO();
54 
63  class_id_t getClassId() const;
64 
71  const URI& getURI() const;
72 
73 protected:
81  MO(class_id_t class_id,
82  const URI& uri,
83  const OF_SHARED_PTR<const ObjectInstance>& oi);
84 
93  MO(ofcore::OFFramework& framework,
94  class_id_t class_id,
95  const URI& uri,
96  const OF_SHARED_PTR<const ObjectInstance>& oi);
97 
104 
110  const ObjectInstance& getObjectInstance() const;
111 
123  static OF_SHARED_PTR<const ObjectInstance>
124  resolveOI(ofcore::OFFramework& framework,
125  class_id_t class_id,
126  const URI& uri);
127 
131  static StoreClient& getStoreClient(ofcore::OFFramework& framework);
132 
139 
143  static void registerListener(ofcore::OFFramework& framework,
144  ObjectListener* listener,
145  class_id_t class_id);
146 
150  static void unregisterListener(ofcore::OFFramework& framework,
151  ObjectListener* listener,
152  class_id_t class_id);
153 
164  template <class T> static
165  boost::optional<OF_SHARED_PTR<T> >
167  class_id_t class_id,
168  const URI& uri) {
169  try {
170  return OF_MAKE_SHARED<T>(framework, uri,
171  resolveOI(framework, class_id, uri));
172  } catch (std::out_of_range e) {
173  return boost::none;
174  }
175  }
176 
181  template <class T> static
183  class_id_t parent_class,
184  const URI& parent_uri,
185  prop_id_t parent_prop,
186  class_id_t child_class,
187  /* out */ std::vector<OF_SHARED_PTR<T> >& out) {
188  std::vector<URI> childURIs;
189  MO::getStoreClient(framework)
190  .getChildren(parent_class, parent_uri, parent_prop,
191  child_class, childURIs);
192  std::vector<URI>::const_iterator it;
193  for (it = childURIs.begin(); it != childURIs.end(); ++it) {
194  boost::optional<OF_SHARED_PTR<T> > child =
195  resolve<T>(framework, child_class, *it);
196  if (child) out.push_back(child.get());
197  }
198  }
199 
204  template <class T>
205  OF_SHARED_PTR<T> addChild(class_id_t parent_class,
206  const URI& parent_uri,
207  prop_id_t parent_prop,
208  class_id_t child_class,
209  const URI& child_uri) {
210  return OF_MAKE_SHARED<T>(boost::ref(getFramework()),
211  child_uri,
212  getTLMutator().addChild(parent_class,
213  parent_uri,
214  parent_prop,
215  child_class,
216  child_uri));
217  }
218 
222  template <class T>
223  static OF_SHARED_PTR<T>
225  class_id_t class_id) {
226  return OF_MAKE_SHARED<T>(boost::ref(framework),
227  URI::ROOT,
228  framework.getTLMutator()
229  .modify(class_id, URI::ROOT));
230  }
231 
235  static void remove(ofcore::OFFramework& framework,
236  class_id_t class_id,
237  const modb::URI& uri) {
238  framework.getTLMutator().remove(class_id, uri);
239  }
240 
241 private:
242  class MOImpl;
243  friend class MOImpl;
244  MOImpl* pimpl;
245 
246  friend bool operator==(const MO& lhs, const MO& rhs);
247  friend bool operator!=(const MO& lhs, const MO& rhs);
248 };
249 
253 bool operator==(const MO& lhs, const MO& rhs);
257 bool operator!=(const MO& lhs, const MO& rhs);
258 
259 } /* namespace mointernal */
260 } /* namespace modb */
261 } /* namespace opflex */
262 
263 #endif /* OPFLEX_CORE_MO_H */
A client for accessing the object store scoped to an owner.
Definition: StoreClient.h:37
bool operator==(const MAC &lhs, const MAC &rhs)
Check for MAC equality.
This is the base class for all managed objects, which are the primary point of interface with data st...
Definition: MO.h:48
static StoreClient & getStoreClient(ofcore::OFFramework &framework)
Get a read-only store client for the framework instance.
static OF_SHARED_PTR< const ObjectInstance > resolveOI(ofcore::OFFramework &framework, class_id_t class_id, const URI &uri)
Resolve the specified URI to the underlying object instance, if it exists.
A mutator represents a set of changes to apply to the data store.
Definition: Mutator.h:61
class_id_t getClassId() const
Get the class ID associated with this managed object.
MO(class_id_t class_id, const URI &uri, const OF_SHARED_PTR< const ObjectInstance > &oi)
Construct an MO.
Interface definition file for MODB.
const URI & getURI() const
Get the URI associated with this managed object.
Interface for an object interested in updates to objects in the data store.
Definition: ObjectListener.h:41
Main interface to the OpFlex framework.
Definition: OFFramework.h:644
static const URI ROOT
Static root URI.
Definition: URI.h:84
friend bool operator==(const MO &lhs, const MO &rhs)
Check for MO equality.
const ObjectInstance & getObjectInstance() const
Get the raw object instance associated with this managed object.
OF_SHARED_PTR< mointernal::ObjectInstance > & addChild(class_id_t parent_class, const URI &parent_uri, prop_id_t parent_prop, class_id_t child_class, const URI &child_uri)
Create a new child object with the specified class and URI, and make it a child of the given parent...
void getChildren(class_id_t parent_class, const URI &parent_uri, prop_id_t parent_prop, class_id_t child_class, std::vector< URI > &output)
Get the children of the parent URI and property and put the result into the supplied vector...
static void registerListener(ofcore::OFFramework &framework, ObjectListener *listener, class_id_t class_id)
Register the listener for the specified class ID.
OF_SHARED_PTR< T > addChild(class_id_t parent_class, const URI &parent_uri, prop_id_t parent_prop, class_id_t child_class, const URI &child_uri)
Add a child of the specified type to the mutator and instantiate the correct wrapper class...
Definition: MO.h:205
OF_SHARED_PTR< mointernal::ObjectInstance > & modify(class_id_t class_id, const URI &uri)
Create a new mutable object with the given URI which is a copy of any existing object with the specif...
ofcore::OFFramework & getFramework() const
Get the framework instance associated with this managed object.
virtual ~MO()
Destroy an MO.
static void resolveChildren(ofcore::OFFramework &framework, class_id_t parent_class, const URI &parent_uri, prop_id_t parent_prop, class_id_t child_class, std::vector< OF_SHARED_PTR< T > > &out)
Resolve any children of the specified parent object to their managed object wrapper classes...
Definition: MO.h:182
friend bool operator!=(const MO &lhs, const MO &rhs)
Check for MO inequality.
An internal instance of an object in the managed object store.
Definition: ObjectInstance.h:103
void remove(class_id_t class_id, const URI &uri)
Delete the child object specified along with its link to its parents.
Mutator & getTLMutator()
Get the currently-active mutator.
uint64_t class_id_t
A unique class ID.
Definition: PropertyInfo.h:34
bool operator!=(const MAC &lhs, const MAC &rhs)
Check for MAC inequality.
A URI is used to identify managed objects in the MODB.
Definition: URI.h:43
uint64_t prop_id_t
A unique property ID.
Definition: PropertyInfo.h:39
Interface definition file for OFFramework.
static OF_SHARED_PTR< T > createRootElement(ofcore::OFFramework &framework, class_id_t class_id)
Add a root element of the given type to the framework.
Definition: MO.h:224
static void unregisterListener(ofcore::OFFramework &framework, ObjectListener *listener, class_id_t class_id)
Unregister the listener for the specified class ID.
static boost::optional< OF_SHARED_PTR< T > > resolve(ofcore::OFFramework &framework, class_id_t class_id, const URI &uri)
Resolve the specified URI to its managed object wrapper class, if it exists.
Definition: MO.h:166
Interface definition file for ObjectListeners.