Skip to content

Commit

Permalink
Merge pull request #208 from CppMicroServices/154-shrinkable-map-allo…
Browse files Browse the repository at this point in the history
…ws-modification

154 shrinkable map allows modification
  • Loading branch information
saschazelzer committed May 25, 2017
2 parents c8a89ec + 275e295 commit 05be900
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 25 deletions.
49 changes: 29 additions & 20 deletions framework/include/cppmicroservices/BundleContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -664,9 +664,10 @@ class US_Framework_EXPORT BundleContext
*
* \rststar
* .. deprecated:: 3.1.0
*
* This function exists only to maintain backwards compatibility
* and will be removed in the next major release.
* Use :func:`RemoveListener()` instead.
* and will be removed in the next major release.
* Use :any:`RemoveListener() <cppmicroservices::BundleContext::RemoveListener>` instead.
* \endrststar
*
* @param listener The callable object to remove.
Expand Down Expand Up @@ -701,9 +702,10 @@ class US_Framework_EXPORT BundleContext
*
* \rststar
* .. deprecated:: 3.1.0
*
* This function exists only to maintain backwards compatibility
* and will be removed in the next major release.
* Use :func:`RemoveListener()` instead.
* and will be removed in the next major release.
* Use :any:`RemoveListener() <cppmicroservices::BundleContext::RemoveListener>` instead.
* \endrststar
*
* @param listener The callable object to remove.
Expand Down Expand Up @@ -737,9 +739,10 @@ class US_Framework_EXPORT BundleContext
*
* \rststar
* .. deprecated:: 3.1.0
*
* This function exists only to maintain backwards compatibility
* and will be removed in the next major release.
* Use :func:`RemoveListener()` instead.
* and will be removed in the next major release.
* Use :any:`RemoveListener() <cppmicroservices::BundleContext::RemoveListener>` instead.
* \endrststar
*
* @param listener The callable object to remove.
Expand Down Expand Up @@ -813,10 +816,11 @@ class US_Framework_EXPORT BundleContext
*
* \rststar
* .. deprecated:: 3.1.0
*
* This function exists only to maintain backwards compatibility
* and will be removed in the next major release.
* and will be removed in the next major release.
* Use `std::bind` to bind the member function and then pass the result
* to the single parameter variant of :func:`AddServiceListener` instead.
* to :any:`AddServiceListener(const ServiceListener&) <cppmicroservices::BundleContext::AddServiceListener>` instead.
* \endrststar
*
* @tparam R The type of the receiver (containing the member function to be called)
Expand Down Expand Up @@ -850,9 +854,10 @@ class US_Framework_EXPORT BundleContext
*
* \rststar
* .. deprecated:: 3.1.0
*
* This function exists only to maintain backwards compatibility
* and will be removed in the next major release.
* Use :func:`RemoveListener()` instead.
* and will be removed in the next major release.
* Use :any:`RemoveListener() <cppmicroservices::BundleContext::RemoveListener>` instead.
* \endrststar
*
* @tparam R The type of the receiver (containing the member function to be removed)
Expand Down Expand Up @@ -881,10 +886,11 @@ class US_Framework_EXPORT BundleContext
* \rststar
* .. deprecated:: 3.1.0
*
* This function exists only to maintain backwards compatibility
* and will be removed in the next major release.
* Use `std::bind` to bind the member function and then pass the result to the
* single parameter variant of :func:`AddBundleListener` instead.
* and will be removed in the next major release.
* Use `std::bind` to bind the member function and then pass the result to
* :any:`AddBundleListener(const BundleListener&) <cppmicroservices::BundleContext::AddBundleListener>` instead.
* \endrststar
*
* @tparam R The type of the receiver (containing the member function to be called)
Expand Down Expand Up @@ -913,9 +919,10 @@ class US_Framework_EXPORT BundleContext
*
* \rststar
* .. deprecated:: 3.1.0
*
* This function exists only to maintain backwards compatibility
* and will be removed in the next major release.
* Use :func:`RemoveListener()` instead.
* and will be removed in the next major release.
* Use :any:`RemoveListener() <cppmicroservices::BundleContext::RemoveListener>` instead.
* \endrststar
*
* @tparam R The type of the receiver (containing the member function to be removed)
Expand Down Expand Up @@ -943,10 +950,11 @@ class US_Framework_EXPORT BundleContext
*
* \rststar
* .. deprecated:: 3.1.0
*
* This function exists only to maintain backwards compatibility
* and will be removed in the next major release.
* Use `std::bind` to bind the member function and then pass the result to the
* single parameter variant of :func:`AddFrameworkListener` instead.
* and will be removed in the next major release.
* Use `std::bind` to bind the member function and then pass the result to
* :any:`AddFrameworkListener(const FrameworkListener&) <cppmicroservices::BundleContext::AddFrameworkListener>` instead.
* \endrststar
*
* @tparam R The type of the receiver (containing the member function to be called)
Expand All @@ -973,9 +981,10 @@ class US_Framework_EXPORT BundleContext
*
* \rststar
* .. deprecated:: 3.1.0
*
* This function exists only to maintain backwards compatibility
* and will be removed in the next major release.
* Use :func:`RemoveListener()` instead.
* and will be removed in the next major release.
* Use :any:`RemoveListener() <cppmicroservices::BundleContext::RemoveListener>` instead.
* \endrststar
*
* @tparam R The type of the receiver (containing the member function to be removed)
Expand Down
25 changes: 22 additions & 3 deletions framework/include/cppmicroservices/ShrinkableMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ class ShrinkableMap

void erase(iterator pos)
{
return container.erase(pos);
container.erase(pos);
}

void erase(iterator first, iterator last)
{
return container.erase(first, last);
container.erase(first, last);
}

size_type erase(const Key& key)
Expand Down Expand Up @@ -113,11 +113,30 @@ class ShrinkableMap
return container.max_size();
}

T& operator[](const Key& key)
/**
* \rststar
* .. deprecated:: 3.1.0
*
* This function exists only to maintain backwards compatibility
* and will be removed in the next major release.
* Use :any:`at(size_type pos) <cppmicroservices::ShrinkableMap::at>` instead.
* \endrststar
*/
US_DEPRECATED T& operator[](const Key& key)
{
return container[key];
}

T& at(const Key& key)
{
return container.at(key);
}

const T& at(const Key& key) const
{
return container.at(key);
}

size_type count(const Key& key) const
{
return container.count(key);
Expand Down
25 changes: 23 additions & 2 deletions framework/include/cppmicroservices/ShrinkableVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class ShrinkableVector
typedef typename container_type::size_type size_type;
typedef typename container_type::reference reference;
typedef typename container_type::const_reference const_reference;
typedef typename container_type::value_type value_type;

ShrinkableVector()
: container(emptyVector)
Expand Down Expand Up @@ -135,12 +136,32 @@ class ShrinkableVector
return container.at(pos);
}

const_reference operator[](size_type i) const
/**
* \rststar
* .. deprecated:: 3.1.0
*
* This function exists only to maintain backwards compatibility
* and will be removed in the next major release.
* Use :any:`at(size_type pos) <cppmicroservices::ShrinkableVector::at>`
* instead.
* \endrststar
*/
US_DEPRECATED const_reference operator[](size_type i) const
{
return container[i];
}

reference operator[](size_type i)
/**
* \rststar
* .. deprecated:: 3.1.0
*
* This function exists only to maintain backwards compatibility
* and will be removed in the next major release.
* Use :any:`at(size_type pos) <cppmicroservices::ShrinkableVector::at>`
* instead.
* \endrststar
*/
US_DEPRECATED reference operator[](size_type i)
{
return container[i];
}
Expand Down
2 changes: 2 additions & 0 deletions framework/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ set(_tests
MemcheckTest
MultipleListenersTest
ResourceCompilerTest
ShrinkableMapTest
ShrinkableVectorTest
ServiceFactoryTest
ServiceHooksTest
ServiceRegistryPerformanceTest
Expand Down
70 changes: 70 additions & 0 deletions framework/test/ShrinkableMapTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*=============================================================================
Library: CppMicroServices
Copyright (c) The CppMicroServices developers. See the COPYRIGHT
file at the top-level directory of this distribution and at
https://github.com/CppMicroServices/CppMicroServices/COPYRIGHT .
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=============================================================================*/

#include "cppmicroservices/ShrinkableMap.h"

#include "TestingConfig.h"
#include "TestingMacros.h"
#include "TestUtils.h"

namespace cppmicroservices {

// Fake a ServiceHooks class so we can create
// ShrinkableMap instances
class ServiceHooks {

public:

template<class K, class V>
static ShrinkableMap<K,V> MakeMap(std::map<K,V>& m)
{
return ShrinkableMap<K,V>(m);
}
};

}

using namespace cppmicroservices;

int ShrinkableMapTest(int /*argc*/, char* /*argv*/[])
{
US_TEST_BEGIN("ShrinkableMapTest");

ShrinkableMap<int, std::string>::container_type m{
{ 1, "one" },
{ 2, "two" },
{ 3, "three" }
};

auto shrinkable = ServiceHooks::MakeMap(m);

US_TEST_CONDITION(m.size() == 3, "Original size")
US_TEST_CONDITION(m.size() == shrinkable.size(), "Equal size")
US_TEST_CONDITION(shrinkable.at(1) == "one", "At access")

shrinkable.erase(shrinkable.find(1));
US_TEST_CONDITION(m.size() == 2, "New size")
US_TEST_FOR_EXCEPTION(std::out_of_range, shrinkable.at(1))
US_TEST_CONDITION(shrinkable.at(2) == "two", "back() access")

US_TEST_END()
}
67 changes: 67 additions & 0 deletions framework/test/ShrinkableVectorTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*=============================================================================
Library: CppMicroServices
Copyright (c) The CppMicroServices developers. See the COPYRIGHT
file at the top-level directory of this distribution and at
https://github.com/CppMicroServices/CppMicroServices/COPYRIGHT .
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=============================================================================*/

#include "cppmicroservices/ShrinkableVector.h"

#include "TestingConfig.h"
#include "TestingMacros.h"
#include "TestUtils.h"

namespace cppmicroservices {

// Fake a BundleHooks class so we can create
// ShrinkableVector instances
class BundleHooks {

public:

template<class E>
static ShrinkableVector<E> MakeVector(std::vector<E>& c)
{
return ShrinkableVector<E>(c);
}
};

}

using namespace cppmicroservices;

int ShrinkableVectorTest(int /*argc*/, char* /*argv*/[])
{
US_TEST_BEGIN("ShrinkableVectorTest");

ShrinkableVector<int>::container_type vec{ 1, 2, 3 };

auto shrinkable = BundleHooks::MakeVector(vec);

US_TEST_CONDITION(vec.size() == 3, "Original size")
US_TEST_CONDITION(vec.size() == shrinkable.size(), "Equal size")
US_TEST_CONDITION(shrinkable.at(0) == 1, "At access")
US_TEST_CONDITION(shrinkable.back() == 3, "back() access")

shrinkable.pop_back();
US_TEST_CONDITION(shrinkable.back() == 2, "back() access")

US_TEST_FOR_EXCEPTION(std::out_of_range, shrinkable.at(3))

US_TEST_END()
}
2 changes: 2 additions & 0 deletions framework/test/TestingConfig.h.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef CPPMICROSERVICES_TESTINGCONFIG_H
#define CPPMICROSERVICES_TESTINGCONFIG_H

#include <string>

#ifdef CMAKE_INTDIR
#define US_LIBRARY_OUTPUT_DIRECTORY "@CMAKE_LIBRARY_OUTPUT_DIRECTORY_NATIVE@@DIR_SEP@" CMAKE_INTDIR
#define US_RUNTIME_OUTPUT_DIRECTORY "@CMAKE_RUNTIME_OUTPUT_DIRECTORY_NATIVE@@DIR_SEP@" CMAKE_INTDIR
Expand Down

0 comments on commit 05be900

Please sign in to comment.