Skip to content

Commit

Permalink
Rework ImplicitContext (#1842)
Browse files Browse the repository at this point in the history
  • Loading branch information
pepone authored Feb 26, 2024
1 parent 23f7bd0 commit 8013a04
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 780 deletions.
19 changes: 18 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@
"typeinfo": "cpp",
"unordered_map": "cpp",
"variant": "cpp",
"algorithm": "cpp"
"algorithm": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"chrono": "cpp",
"compare": "cpp",
"concepts": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"random": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"numbers": "cpp",
"semaphore": "cpp",
"stop_token": "cpp",
"cinttypes": "cpp"
}
}
80 changes: 30 additions & 50 deletions cpp/include/Ice/ImplicitContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,11 @@
#ifndef __Ice_ImplicitContext_h__
#define __Ice_ImplicitContext_h__

#include <IceUtil/PushDisableWarnings.h>
#include <Ice/ProxyF.h>
#include <Ice/ObjectF.h>
#include <Ice/ValueF.h>
#include <Ice/Exception.h>
#include <Ice/StreamHelpers.h>
#include <Ice/Comparable.h>
#include <optional>
#include <Ice/ExceptionHelpers.h>
#include <Ice/LocalException.h>
#include <Ice/Current.h>
#include <IceUtil/UndefSysMacros.h>

#ifndef ICE_API
# if defined(ICE_STATIC_LIBS)
# define ICE_API /**/
# elif defined(ICE_API_EXPORTS)
# define ICE_API ICE_DECLSPEC_EXPORT
# else
# define ICE_API ICE_DECLSPEC_IMPORT
# endif
#endif

namespace Ice
{

class ImplicitContext;
#include "Config.h"
#include "OutputStream.h"
#include <Ice/Context.h>

}
#include <string>

namespace Ice
{
Expand All @@ -58,30 +34,28 @@ namespace Ice
* null replaced by the empty-string.
* \headerfile Ice/Ice.h
*/
class ICE_CLASS(ICE_API) ImplicitContext
class ICE_API ImplicitContext final
{
public:

ICE_MEMBER(ICE_API) virtual ~ImplicitContext();

/**
* Get a copy of the underlying context.
* @return A copy of the underlying context.
*/
virtual ::Ice::Context getContext() const = 0;
Context getContext() const;

/**
* Set the underlying context.
* @param newContext The new context.
*/
virtual void setContext(const Context& newContext) = 0;
void setContext(const Context& newContext);

/**
* Check if this key has an associated value in the underlying context.
* @param key The key.
* @return True if the key has an associated value, False otherwise.
*/
virtual bool containsKey(const ::std::string& key) const = 0;
bool containsKey(const std::string& key) const;

/**
* Get the value associated with the given key in the underlying context. Returns an empty string if no value is
Expand All @@ -90,41 +64,47 @@ class ICE_CLASS(ICE_API) ImplicitContext
* @param key The key.
* @return The value associated with the key.
*/
virtual ::std::string get(const ::std::string& key) const = 0;
std::string get(const std::string& key) const;

/**
* Create or update a key/value entry in the underlying context.
* @param key The key.
* @param value The value.
* @return The previous value associated with the key, if any.
*/
virtual ::std::string put(const ::std::string& key, const ::std::string& value) = 0;
std::string put(const std::string& key, const std::string& value);

/**
* Remove the entry for the given key in the underlying context.
* @param key The key.
* @return The value associated with the key, if any.
*/
virtual ::std::string remove(const ::std::string& key) = 0;
};
std::string remove(const std::string& key);

}
/**
* Marshals the underlying context plus the given context. Entries in the given context overwrite entries in the
* underlying context.
* @param context The context to write to the output stream.
* @param os The output stream.
*/
void write(const Context& context, Ice::OutputStream* os) const;

/// \cond STREAM
namespace Ice
{
/**
* Combines the underlying context plus the given context. Entries in the given context overwrite entries in the
* underlying context.
* @param context The context to combine with the underlying context.
* @param combined The combined context.
*/
void combine(const Context& context, Context& combined) const;

}
/// \endcond
private:

/// \cond INTERNAL
namespace Ice
{
mutable std::mutex _mutex;
Context _context;
};

using ImplicitContextPtr = ::std::shared_ptr<ImplicitContext>;
using ImplicitContextPtr = std::shared_ptr<ImplicitContext>;

}
/// \endcond

#include <IceUtil/PopDisableWarnings.h>
#endif
9 changes: 1 addition & 8 deletions cpp/include/Ice/ImplicitContextF.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@
#ifndef __Ice_ImplicitContextF_h__
#define __Ice_ImplicitContextF_h__

#include <Ice/ProxyF.h>
#include <Ice/ObjectF.h>
#include <Ice/ValueF.h>
#include <Ice/Exception.h>
#include <Ice/StreamHelpers.h>
#include <Ice/Comparable.h>
#include <optional>
#include <IceUtil/UndefSysMacros.h>
#include <memory>

namespace Ice
{
Expand Down
5 changes: 0 additions & 5 deletions cpp/src/Ice/DLLMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Copyright (c) ZeroC, Inc. All rights reserved.
//

#include <Ice/ImplicitContextI.h>
#include <Ice/Service.h>

extern "C" BOOL WINAPI _CRT_INIT(HINSTANCE, DWORD, LPVOID);
Expand All @@ -28,10 +27,6 @@ ice_DLL_Main(HINSTANCE hDLL, DWORD reason, LPVOID reserved)
{
Ice::Service::setModuleHandle(hDLL);
}
else if(reason == DLL_THREAD_DETACH)
{
Ice::ImplicitContextI::cleanupThread();
}

//
// During DETACH, we must call _CRT_INIT last.
Expand Down
116 changes: 101 additions & 15 deletions cpp/src/Ice/ImplicitContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,109 @@
// Copyright (c) ZeroC, Inc. All rights reserved.
//

#ifndef ICE_API_EXPORTS
# define ICE_API_EXPORTS
#endif
#include <Ice/ImplicitContext.h>
#include <IceUtil/PushDisableWarnings.h>
#include <Ice/InputStream.h>
#include <Ice/OutputStream.h>
#include <IceUtil/PopDisableWarnings.h>

#if defined(_MSC_VER)
# pragma warning(disable:4458) // declaration of ... hides class member
#elif defined(__clang__)
# pragma clang diagnostic ignored "-Wshadow"
#elif defined(__GNUC__)
# pragma GCC diagnostic ignored "-Wshadow"
#endif
using namespace std;
using namespace Ice;

Ice::ImplicitContext::~ImplicitContext()
Context
ImplicitContext::getContext() const
{
lock_guard lock(_mutex);
return _context;
}

void
ImplicitContext::setContext(const Context& newContext)
{
lock_guard lock(_mutex);
_context = newContext;
}

bool
ImplicitContext::containsKey(const string& key) const
{
lock_guard lock(_mutex);
return _context.find(key) != _context.end();
}

string
ImplicitContext::get(const string& key) const
{
lock_guard lock(_mutex);
Context::const_iterator p = _context.find(key);
if(p == _context.end())
{
return "";
}
return p->second;
}

string
ImplicitContext::put(const string& key, const string& value)
{
lock_guard lock(_mutex);
string& val = _context[key];

string oldVal = val;
val = value;
return oldVal;
}

string
ImplicitContext::remove(const string& key)
{
lock_guard lock(_mutex);
Context::iterator p = _context.find(key);
if(p == _context.end())
{
return "";
}
else
{
string oldVal = p->second;
_context.erase(p);
return oldVal;
}
}

void
ImplicitContext::write(const Context& contex, ::Ice::OutputStream* os) const
{
unique_lock lock(_mutex);
if(contex.size() == 0)
{
os->write(_context);
}
else if(_context.size() == 0)
{
lock.unlock();
os->write(contex);
}
else
{
Context combined = contex;
combined.insert(_context.begin(), _context.end());
lock.unlock();
os->write(combined);
}
}

void
ImplicitContext::combine(const Context& context, Context& combined) const
{
lock_guard lock(_mutex);
if(context.size() == 0)
{
combined = _context;
}
else if(_context.size() == 0)
{
combined = context;
}
else
{
combined = context;
combined.insert(_context.begin(), _context.end());
}
}
Loading

0 comments on commit 8013a04

Please sign in to comment.