Skip to content

Commit

Permalink
Use local static const / constexpr for a strings (#1840)
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardnormier authored Feb 23, 2024
1 parent bd60710 commit bdd7563
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 120 deletions.
1 change: 1 addition & 0 deletions cpp/include/Ice/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <cstdio>
#include <cstring>
#include <string>
#include <string_view>
#include <vector>
#include <map>

Expand Down
4 changes: 2 additions & 2 deletions cpp/include/Ice/LocalException.h
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,8 @@ class ICE_CLASS(ICE_API) TwowayOnlyException : public LocalExceptionHelper<Twowa
* @param line The line number at which the exception was raised, typically __LINE__.
* @param operation The name of the operation that was invoked.
*/
TwowayOnlyException(const char* file, int line, const ::std::string& operation) : LocalExceptionHelper<TwowayOnlyException, LocalException>(file, line),
operation(operation)
TwowayOnlyException(const char* file, int line, ::std::string operation) : LocalExceptionHelper<TwowayOnlyException, LocalException>(file, line),
operation(std::move(operation))
{
}

Expand Down
2 changes: 1 addition & 1 deletion cpp/include/Ice/Proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ class ICE_API ObjectPrx : public Proxy<ObjectPrx>
const ::IceInternal::ReferencePtr& _getReference() const { return _reference; }
const ::IceInternal::RequestHandlerCachePtr& _getRequestHandlerCache() const { return _requestHandlerCache; }

void _checkTwowayOnly(const std::string&) const;
void _checkTwowayOnly(std::string_view) const;

int _hash() const;

Expand Down
4 changes: 4 additions & 0 deletions cpp/include/IceUtil/OutputUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <fstream>
#include <stack>
#include <vector>
#include <string_view>

namespace IceUtilInternal
{
Expand Down Expand Up @@ -99,6 +100,9 @@ class ICE_API Output : public OutputBase
void spar(char = '('); // Start a paramater list.
void epar(char = ')'); // End a paramater list.

void spar(std::string_view); // Start a paramater list.
void epar(std::string_view); // End a paramater list.

private:

std::string _blockStart;
Expand Down
4 changes: 2 additions & 2 deletions cpp/src/Ice/InputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1950,7 +1950,7 @@ Ice::InputStream::EncapsDecoder10::readInstance()
// For the 1.0 encoding, the type ID for the base Object class
// marks the last slice.
//
if(_typeId == Object::ice_staticId())
if(_typeId == Value::ice_staticId())
{
throw NoValueFactoryException(__FILE__, __LINE__, "", mostDerivedId);
}
Expand Down Expand Up @@ -2424,7 +2424,7 @@ Ice::InputStream::EncapsDecoder11::readInstance(int32_t index, PatchFunc patchFu
// We pass the "::Ice::Object" ID to indicate that this is the
// last chance to preserve the object.
//
v = newInstance(Object::ice_staticId());
v = newInstance(Value::ice_staticId());
if(!v)
{
v = make_shared<UnknownSlicedValue>(mostDerivedId);
Expand Down
43 changes: 18 additions & 25 deletions cpp/src/Ice/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,16 @@ namespace Ice
const Current emptyCurrent = Current();
}

namespace
{

const string object_ids[] =
{
"::Ice::Object"
};

const string object_all[] =
{
"ice_id",
"ice_ids",
"ice_isA",
"ice_ping"
};

}

Ice::Request::~Request()
{
// Out of line to avoid weak vtable
}

bool
Ice::Object::ice_isA(string s, const Current&) const
Ice::Object::ice_isA(string s, const Current& current) const
{
return s == object_ids[0];
vector<string> allTypeIds = ice_ids(current); // sorted type IDs
return ::std::binary_search(allTypeIds.begin(), allTypeIds.end(), s);
}

void
Expand All @@ -56,19 +39,21 @@ Ice::Object::ice_ping(const Current&) const
vector<string>
Ice::Object::ice_ids(const Current&) const
{
return vector<string>(&object_ids[0], &object_ids[1]);
static const vector<string> allTypeIds = { "::Ice::Object" };
return allTypeIds;
}

string
Ice::Object::ice_id(const Current&) const
{
return object_ids[0];
return ice_staticId();
}

const string&
Ice::Object::ice_staticId()
{
return object_ids[0];
static const ::std::string typeId = "::Ice::Object";
return typeId;
}

bool
Expand Down Expand Up @@ -152,14 +137,22 @@ Ice::Object::ice_dispatch(Request& request, std::function<bool()> r, std::functi
bool
Ice::Object::_iceDispatch(Incoming& in, const Current& current)
{
pair<const string*, const string*> r = equal_range(object_all, object_all + sizeof(object_all) / sizeof(string), current.operation);
static constexpr string_view allOperations[] =
{
"ice_id",
"ice_ids",
"ice_isA",
"ice_ping"
};

pair<const string_view*, const string_view*> r = equal_range(allOperations, allOperations + 4, current.operation);

if(r.first == r.second)
{
throw OperationNotExistException(__FILE__, __LINE__, current.id, current.facet, current.operation);
}

switch(r.first - object_all)
switch(r.first - allOperations)
{
case 0:
{
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/Ice/OutputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ Ice::OutputStream::EncapsEncoder10::endInstance()
//
// Write the Object slice.
//
startSlice(Object::ice_staticId(), -1, true);
startSlice(Value::ice_staticId(), -1, true);
_stream->writeSize(0); // For compatibility with the old AFM.
endSlice();
}
Expand Down
4 changes: 2 additions & 2 deletions cpp/src/Ice/Proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ Ice::ObjectPrx::ObjectPrx(ReferencePtr&& ref) :
}

void
Ice::ObjectPrx::_checkTwowayOnly(const string& name) const
Ice::ObjectPrx::_checkTwowayOnly(string_view name) const
{
if (!ice_isTwoway())
{
throw Ice::TwowayOnlyException(__FILE__, __LINE__, name);
throw Ice::TwowayOnlyException(__FILE__, __LINE__, string(name));
}
}

Expand Down
15 changes: 15 additions & 0 deletions cpp/src/IceUtil/OutputUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,21 @@ IceUtilInternal::Output::epar(char c)
_out << c;
}

void
IceUtilInternal::Output::spar(string_view s)
{
_emptyBlock = false;
_out << s;
_par = 0;
}

void
IceUtilInternal::Output::epar(string_view s)
{
_par = -1;
_out << s;
}

Output&
IceUtilInternal::operator<<(Output& out, ios_base& (*val)(ios_base&))
{
Expand Down
Loading

0 comments on commit bdd7563

Please sign in to comment.