OSVR-Core
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 
49  inline ClientContext::ClientContext(const char applicationIdentifier[],
50  const char host[],
51  uint32_t flags)
52  : m_context(osvrClientInitHost(applicationIdentifier, host, flags)) {}
53 
55  : m_context(context) {}
56 
58 
59  inline void ClientContext::update() {
60  OSVR_ReturnCode ret = osvrClientUpdate(m_context);
61  if (OSVR_RETURN_SUCCESS != ret) {
62  throw std::runtime_error("Error updating context.");
63  }
64  }
65 
66  inline Interface ClientContext::getInterface(const std::string &path) {
67  OSVR_ClientInterface iface = NULL;
68  OSVR_ReturnCode ret =
69  osvrClientGetInterface(m_context, path.c_str(), &iface);
70  if (OSVR_RETURN_SUCCESS != ret) {
71  throw std::runtime_error(
72  "Couldn't create interface because the path was invalid.");
73  }
74 
75  return Interface(*this, iface);
76  }
77 
78  inline std::string
79  ClientContext::getStringParameter(const std::string &path) {
80  size_t length = 0;
81  OSVR_ReturnCode ret = osvrClientGetStringParameterLength(
82  m_context, path.c_str(), &length);
83  if (OSVR_RETURN_SUCCESS != ret) {
84  throw std::runtime_error(
85  "Invalid context or null reference to length variable.");
86  }
87 
88  if (0 == length) {
89  return std::string();
90  }
91 
93 
94  ret = osvrClientGetStringParameter(m_context, path.c_str(),
95  buf.getBufferOfSize(length), length);
96  if (OSVR_RETURN_SUCCESS != ret) {
97  throw std::runtime_error("Invalid context, null reference to "
98  "buffer, or buffer is too small.");
99  }
100 
101  return buf.str();
102  }
103 
104  inline void ClientContext::free(Interface &iface) {
105  OSVR_ReturnCode ret = osvrClientFreeInterface(m_context, iface.get());
106  if (OSVR_RETURN_SUCCESS != ret) {
107  throw std::logic_error(
108  "Could not free interface: either null or already freed!");
109  }
110  // Null out the interface.
111  iface = Interface(*this, NULL);
112  }
113 
114  inline OSVR_ClientContext ClientContext::get() { return m_context; }
115 
116  inline bool ClientContext::checkStatus() const {
117  return osvrClientCheckStatus(m_context) == OSVR_RETURN_SUCCESS;
118  }
119 
120  inline void ClientContext::log(OSVR_LogLevel severity, const char* message) {
121  osvrClientLog(m_context, severity, message);
122  }
123 
124 } // end namespace clientkit
125 
126 } // end namespace osvr
127 
128 #endif // INCLUDED_Context_h_GUID_DD0155F5_61A4_4A76_8C2E_D9614C7A9EBD
OSVR_CLIENTKIT_EXPORT void osvrClientLog(OSVR_ClientContext ctx, OSVR_LogLevel severity, const char *message)
Log a message from the client.
Definition: ContextC.cpp:95
bool checkStatus() const
Checks to see if the client context is properly and fully started up.
Definition: Context.h:116
ClientContext(const char applicationIdentifier[], uint32_t flags=0u)
Initialize the library.
Definition: Context.h:45
Interface handle object.
Definition: Interface_decl.h:54
void log(OSVR_LogLevel severity, const char *message)
Log a message to the plugin-specific channel.
Definition: Context.h:120
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
void free(Interface &iface)
Frees an interface before it would normally be freed (at context close).
Definition: Context.h:104
size< coerce_list< Ts... >> length
Synonym for typepack::size.
Definition: Size.h:59
OSVR_CLIENTKIT_EXPORT OSVR_ReturnCode osvrClientGetStringParameter(OSVR_ClientContext ctx, const char path[], char *buf, size_t len)
Get a string parameter associated with the given path.
Definition: ParametersC.cpp:49
std::string str()
Converts the buffer into a string.
Definition: StringBufferBuilder.h:59
Interface getInterface(const std::string &path)
Get the interface associated with the given path.
Definition: Context.h:66
OSVR_CLIENTKIT_EXPORT OSVR_ReturnCode osvrClientUpdate(OSVR_ClientContext ctx)
Updates the state of the context - call regularly in your mainloop.
Definition: ContextC.cpp:80
OSVR_ClientInterface get()
Get the raw OSVR_ClientInterface from this wrapper.
Definition: Interface.h:55
OSVR_CLIENTKIT_EXPORT OSVR_ReturnCode osvrClientFreeInterface(OSVR_ClientContext ctx, OSVR_ClientInterface iface)
Free an interface object before context closure.
Definition: InterfaceC.cpp:53
OSVR_CLIENTKIT_EXPORT OSVR_ClientContext osvrClientInit(const char applicationIdentifier[], uint32_t flags OSVR_CPP_ONLY(=0))
Initialize the library.
#define OSVR_RETURN_SUCCESS
The "success" value for an OSVR_ReturnCode.
Definition: ReturnCodesC.h:45
OSVR_ClientContext get()
Gets the bare OSVR_ClientContext.
Definition: Context.h:114
OSVR_CLIENTKIT_EXPORT OSVR_ReturnCode osvrClientGetStringParameterLength(OSVR_ClientContext ctx, const char path[], size_t *len)
Get the length of a string parameter associated with the given path.
Definition: ParametersC.cpp:35
OSVR_LogLevel
Log message severity levels.
Definition: LogLevelC.h:44
A utility class to adapt APIs that first provide a length, then place a string in a user-allocated bu...
Definition: StringBufferBuilder.h:44
OSVR_CLIENTKIT_EXPORT OSVR_ReturnCode osvrClientCheckStatus(OSVR_ClientContext ctx)
Checks to see if the client context is fully started up and connected properly to a server...
Definition: ContextC.cpp:63
Definition: ClientContext.h:50
OSVR_CLIENTKIT_EXPORT OSVR_ReturnCode osvrClientShutdown(OSVR_ClientContext ctx)
Shutdown the library.
Definition: ContextC.cpp:86
void update()
Updates the state of the context - call regularly in your mainloop.
Definition: Context.h:59
Header containing the inline implementation of Interface.
Definition: ClientInterface.h:49
~ClientContext()
Destructor: Shutdown the library.
Definition: Context.h:57
Header.
char * getBufferOfSize(std::size_t n)
Allocates a buffer of size n, to include the null terminator, and returns a pointer into it...
Definition: StringBufferBuilder.h:48
OSVR_CLIENTKIT_EXPORT OSVR_ClientContext osvrClientInitHost(const char applicationIdentifier[], const char host[], uint32_t flags OSVR_CPP_ONLY(=0))
Initialize the library.
OSVR_CLIENTKIT_EXPORT OSVR_ReturnCode osvrClientGetInterface(OSVR_ClientContext ctx, const char path[], OSVR_ClientInterface *iface)
Get the interface associated with the given path.
Definition: InterfaceC.cpp:37
std::string getStringParameter(const std::string &path)
Get a string parameter value from the given path.
Definition: Context.h:79