OSVR-Core  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Context.h
Go to the documentation of this file.
1 
11 // Copyright 2014 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_Context_h_GUID_DD0155F5_61A4_4A76_8C2E_D9614C7A9EBD
26 #define INCLUDED_Context_h_GUID_DD0155F5_61A4_4A76_8C2E_D9614C7A9EBD
27 
28 // Internal Includes
34 
35 // Library/third-party includes
36 // - none
37 
38 // Standard includes
39 #include <string>
40 #include <stdexcept>
41 
42 namespace osvr {
43 
44 namespace clientkit {
45  inline ClientContext::ClientContext(const char applicationIdentifier[],
46  uint32_t flags)
47  : m_context(osvrClientInit(applicationIdentifier, flags)) {}
48 
50  : m_context(context) {}
51 
53 
54  inline void ClientContext::update() {
55  OSVR_ReturnCode ret = osvrClientUpdate(m_context);
56  if (OSVR_RETURN_SUCCESS != ret) {
57  throw std::runtime_error("Error updating context.");
58  }
59  }
60 
61  inline Interface ClientContext::getInterface(const std::string &path) {
62  OSVR_ClientInterface iface = NULL;
63  OSVR_ReturnCode ret =
64  osvrClientGetInterface(m_context, path.c_str(), &iface);
65  if (OSVR_RETURN_SUCCESS != ret) {
66  throw std::runtime_error(
67  "Couldn't create interface because the path was invalid.");
68  }
69 
70  return Interface(*this, iface);
71  }
72 
73  inline std::string
74  ClientContext::getStringParameter(const std::string &path) {
75  size_t length = 0;
76  OSVR_ReturnCode ret = osvrClientGetStringParameterLength(
77  m_context, path.c_str(), &length);
78  if (OSVR_RETURN_SUCCESS != ret) {
79  throw std::runtime_error(
80  "Invalid context or null reference to length variable.");
81  }
82 
83  if (0 == length) {
84  return std::string();
85  }
86 
88 
89  ret = osvrClientGetStringParameter(m_context, path.c_str(),
90  buf.getBufferOfSize(length), length);
91  if (OSVR_RETURN_SUCCESS != ret) {
92  throw std::runtime_error("Invalid context, null reference to "
93  "buffer, or buffer is too small.");
94  }
95 
96  return buf.str();
97  }
98 
99  inline void ClientContext::free(Interface &iface) {
100  OSVR_ReturnCode ret = osvrClientFreeInterface(m_context, iface.get());
101  if (OSVR_RETURN_SUCCESS != ret) {
102  throw std::logic_error(
103  "Could not free interface: either null or already freed!");
104  }
105  // Null out the interface.
106  iface = Interface(*this, NULL);
107  }
108 
109  inline OSVR_ClientContext ClientContext::get() { return m_context; }
110 
111  inline bool ClientContext::checkStatus() const {
112  return osvrClientCheckStatus(m_context) == OSVR_RETURN_SUCCESS;
113  }
114 
115  inline void ClientContext::log(OSVR_LogLevel severity, const char* message) {
116  osvrClientLog(m_context, severity, message);
117  }
118 
119 } // end namespace clientkit
120 
121 } // end namespace osvr
122 
123 #endif // INCLUDED_Context_h_GUID_DD0155F5_61A4_4A76_8C2E_D9614C7A9EBD
OSVR_ReturnCode osvrClientGetInterface(OSVR_ClientContext ctx, const char path[], OSVR_ClientInterface *iface)
Get the interface associated with the given path.
ClientContext(const char applicationIdentifier[], uint32_t flags=0u)
Initialize the library.
Definition: Context.h:45
OSVR_ReturnCode osvrClientFreeInterface(OSVR_ClientContext ctx, OSVR_ClientInterface iface)
Free an interface object before context closure.
Interface handle object. Typically acquired from a ClientContext.
void log(OSVR_LogLevel severity, const char *message)
Log a message to the plugin-specific channel.
Definition: Context.h:115
bool checkStatus() const
Checks to see if the client context is properly and fully started up.
Definition: Context.h:111
The main namespace for all C++ elements of the framework, internal and external.
Definition: ClientKit.h:31
OSVR_ClientContext osvrClientInit(const char applicationIdentifier[], uint32_t flags=0)
Initialize the library.
void free(Interface &iface)
Frees an interface before it would normally be freed (at context close).
Definition: Context.h:99
OSVR_ReturnCode osvrClientGetStringParameterLength(OSVR_ClientContext ctx, const char path[], size_t *len)
Get the length of a string parameter associated with the given path.
size< coerce_list< Ts...>> length
Synonym for typepack::size.
Definition: Size.h:59
std::string str()
Converts the buffer into a string.
Interface getInterface(const std::string &path)
Get the interface associated with the given path.
Definition: Context.h:61
OSVR_ClientInterface get()
Get the raw OSVR_ClientInterface from this wrapper.
Definition: Interface.h:55
OSVR_ReturnCode osvrClientCheckStatus(OSVR_ClientContext ctx)
Checks to see if the client context is fully started up and connected properly to a server...
OSVR_ReturnCode osvrClientShutdown(OSVR_ClientContext ctx)
Shutdown the library.
#define OSVR_RETURN_SUCCESS
The "success" value for an OSVR_ReturnCode.
Definition: ReturnCodesC.h:45
struct OSVR_ClientContextObject * OSVR_ClientContext
Opaque handle that should be retained by your application. You need only and exactly one...
OSVR_ReturnCode osvrClientUpdate(OSVR_ClientContext ctx)
Updates the state of the context - call regularly in your mainloop.
OSVR_ClientContext get()
Gets the bare OSVR_ClientContext.
Definition: Context.h:109
OSVR_LogLevel
Log message severity levels.
Definition: LogLevelC.h:44
void osvrClientLog(OSVR_ClientContext ctx, OSVR_LogLevel severity, const char *message)
Log a message from the client.
A utility class to adapt APIs that first provide a length, then place a string in a user-allocated bu...
void update()
Updates the state of the context - call regularly in your mainloop.
Definition: Context.h:54
Header containing the inline implementation of Interface.
struct OSVR_ClientInterfaceObject * OSVR_ClientInterface
Opaque handle to an interface used for registering callbacks and getting status.
OSVR_ReturnCode osvrClientGetStringParameter(OSVR_ClientContext ctx, const char path[], char *buf, size_t len)
Get a string parameter associated with the given path.
~ClientContext()
Destructor: Shutdown the library.
Definition: Context.h:52
Header.
char * getBufferOfSize(std::size_t n)
Allocates a buffer of size n, to include the null terminator, and returns a pointer into it...
std::string getStringParameter(const std::string &path)
Get a string parameter value from the given path.
Definition: Context.h:74