-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #858 from leapmotion/feature-config2
Add a configuration concept to Autowiring
- Loading branch information
Showing
30 changed files
with
1,102 additions
and
31 deletions.
There are no files selected for viewing
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved. | ||
#include "stdafx.h" | ||
#include "ConfigManager.h" | ||
#include <mutex> | ||
|
||
using namespace autowiring; | ||
|
||
ConfigManager::ConfigManager(void) : | ||
m_empty("") | ||
{} | ||
|
||
void ConfigManager::Register(void* pObj, const config_descriptor& desc) { | ||
std::lock_guard<autowiring::spin_lock> lk(m_lock); | ||
for (const auto& field : desc.fields) { | ||
Entry& entry = m_config[field.second.name]; | ||
entry.attached.push_back({ | ||
&field.second, | ||
static_cast<uint8_t*>(pObj) + field.second.offset | ||
}); | ||
|
||
if (entry.value) | ||
entry.attached.back().field->marshaller->unmarshal( | ||
entry.attached.back().pObj, | ||
entry.value->c_str() | ||
); | ||
} | ||
} | ||
|
||
const std::string& ConfigManager::Get(std::string name) const { | ||
auto q = m_config.find(name); | ||
if (q == m_config.end()) | ||
return m_empty; | ||
|
||
return *q->second.value; | ||
} | ||
|
||
void ConfigManager::Set(std::string&& name, std::string&& value) { | ||
std::lock_guard<autowiring::spin_lock> lk(m_lock); | ||
auto q = m_config.find(name); | ||
if (q == m_config.end()) | ||
q = m_config.insert(q, { std::move(name), {} }); | ||
|
||
Entry& entry = q->second; | ||
entry.value = std::move(value); | ||
|
||
for (Entry::Attachment& attachment : entry.attached) | ||
attachment.field->marshaller->unmarshal( | ||
attachment.pObj, | ||
entry.value->c_str() | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved. | ||
#pragma once | ||
#include "config_descriptor.h" | ||
#include "optional.h" | ||
#include "spin_lock.h" | ||
#include <string> | ||
#include <unordered_map> | ||
#include <vector> | ||
|
||
namespace autowiring { | ||
class ConfigManager { | ||
public: | ||
ConfigManager(void); | ||
|
||
private: | ||
spin_lock m_lock; | ||
|
||
// A single entry, which has a string representation part paired | ||
// with a pointer to the value part | ||
struct Entry { | ||
// The value held here | ||
optional<std::string> value; | ||
|
||
struct Attachment { | ||
const config_field* field; | ||
void* pObj; | ||
}; | ||
|
||
// Fields attached on this entry: | ||
std::vector<Attachment> attached; | ||
}; | ||
|
||
// Empty string sentry, used when a miss occurs | ||
const std::string m_empty; | ||
|
||
// All configuration values | ||
std::unordered_map<std::string, Entry> m_config; | ||
|
||
public: | ||
/// <summary> | ||
/// Registers a new configurable object | ||
/// </summary> | ||
/// <param name="pObj">The object that may be configured</param> | ||
/// <param name="desc">A descriptor for the object</param> | ||
void Register(void* pObj, const config_descriptor& desc); | ||
|
||
/// <summary> | ||
/// Gets the current configuration value from the map | ||
/// </summary> | ||
const std::string& Get(std::string name) const; | ||
|
||
/// <summary> | ||
/// Sets the named config value in the map | ||
/// </summary> | ||
void Set(std::string&& name, std::string&& value); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved. | ||
#include "stdafx.h" | ||
#include "ConfigRegistry.h" | ||
|
||
using namespace autowiring; | ||
|
||
std::atomic<config_registry_entry_base*> autowiring::g_pFirstEntry; | ||
|
||
config_registry_entry_base::config_registry_entry_base(void) { | ||
while (!g_pFirstEntry.compare_exchange_weak(pFlink, this)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved. | ||
#pragma once | ||
#include "config_descriptor.h" | ||
#include <atomic> | ||
|
||
namespace autowiring { | ||
struct config_registry_entry_base { | ||
protected: | ||
config_registry_entry_base(void); | ||
|
||
public: | ||
// Next entry in the registry: | ||
config_registry_entry_base* pFlink = nullptr; | ||
}; | ||
|
||
template<typename T, typename = void> | ||
struct config_registry_entry { | ||
static const config_descriptor* desc(void) { return nullptr; } | ||
}; | ||
|
||
template<typename T> | ||
struct config_registry_entry<T, typename std::enable_if<has_getconfigdescriptor<T>::value>::type> : | ||
config_registry_entry_base | ||
{ | ||
public: | ||
static const config_descriptor* desc(void) { | ||
static const config_descriptor desc = T::GetConfigDescriptor(); | ||
return &desc; | ||
} | ||
}; | ||
|
||
extern std::atomic<config_registry_entry_base*> g_pFirstEntry; | ||
|
||
/// <summary> | ||
/// Gets a string representation of the named configuration value | ||
/// </summary> | ||
template<typename T> | ||
std::string ConfigGet(const char* name, T& obj) { | ||
const config_descriptor& desc = *config_registry_entry<T>::desc(); | ||
auto q = desc.fields.find(name); | ||
if (q == desc.fields.end()) | ||
throw std::invalid_argument("Configuration name not found in the specified object's configuration descriptor"); | ||
|
||
const autowiring::config_field& f = q->second; | ||
return f.marshaller->marshal(reinterpret_cast<uint8_t*>(&obj) + f.offset); | ||
} | ||
|
||
/// <summary> | ||
/// Sets the named configuration value to the specified string value | ||
/// </summary> | ||
template<typename T> | ||
void ConfigSet(const char* name, T& obj, const char* value) { | ||
const config_descriptor& desc = *config_registry_entry<T>::desc(); | ||
auto q = desc.fields.find(name); | ||
if (q == desc.fields.end()) | ||
throw std::invalid_argument("Configuration name not found in the specified object's configuration descriptor"); | ||
|
||
const autowiring::config_field& f = q->second; | ||
f.marshaller->unmarshal( | ||
reinterpret_cast<uint8_t*>(&obj) + f.offset, | ||
value | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.