Commit 6f868d66 authored by Guillaume Roguez's avatar Guillaume Roguez Committed by gerrit2

utils: c++11'ify RING_ARRAYSIZE and CONST_PJ_STR macros

Macros (C relica) are easly transformed into C++ using constexpr.
Using template provides type-safe.

RING_ARRAYSIZE becomes arraySize.
CONST_PJ_STR has been also moved into sip_utils.

Issue: #79703
Change-Id: Ic197f53bbfeba363f0609908fa680f18529f8075
parent db771a00
/*
* Copyright (C) 2004-2015 Savoir-Faire Linux Inc.
* Author: Tristan Matthews <tristan.matthews@savoirfairelinux.com>
* Copyright (C) 2015 Savoir-faire Linux Inc.
* Author: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -28,16 +28,15 @@
* as that of the covered work.
*/
#ifndef ARRAY_SIZE_H_
#define ARRAY_SIZE_H_
#pragma once
// Returns the number of elements in a, calculated at compile-time
#define RING_ARRAYSIZE(a) \
((sizeof(a) / sizeof(*(a))) / \
static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
#include <cstdlib>
/* Only use this macro with string literals or character arrays, will not work
* as expected with char pointers */
#define CONST_PJ_STR(X) {(char *) (X), RING_ARRAYSIZE(X) - 1}
namespace ring {
#endif // ARRAY_SIZE_H_
// Returns the number of elements in given-by-reference array of type T.
// Borrowed from Scott Meyers in "Effective Modern C++".
template<typename T, std::size_t N>
constexpr std::size_t arraySize(T (&)[N]) noexcept { return N; }
} // namespace ring
......@@ -268,7 +268,7 @@ OpenSLLayer::initAudioPlayback() const
SL_IID_PLAY};
const SLboolean req[] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
const unsigned nbInterface = RING_ARRAYSIZE(ids);
const unsigned nbInterface = arraySize(ids);
// create audio player
RING_DBG("Create audio player\n");
......
......@@ -40,6 +40,7 @@
#include "sip/sipvoiplink.h"
#include "sip/sipcall.h"
#include "sip/siptransport.h"
#include "sip/sip_utils.h"
#include "sips_transport_ice.h"
#include "ice_transport.h"
......@@ -79,6 +80,8 @@
namespace ring {
using sip_utils::CONST_PJ_STR;
static constexpr int ICE_COMPONENTS {1};
static constexpr int ICE_COMP_SIP_TRANSPORT {0};
static constexpr int ICE_INIT_TIMEOUT {5};
......
......@@ -44,7 +44,6 @@
#include <pjsip-simple/evsub.h>
#include <unistd.h>
#include "array_size.h"
#include "pres_sub_client.h"
#include "sipaccount.h"
#include "sippresence.h"
......@@ -59,6 +58,8 @@
namespace ring {
using sip_utils::CONST_PJ_STR;
int PresSubClient::modId_ = 0; // used to extract data structure from event_subscription
void
......
......@@ -39,9 +39,12 @@
#include "logger.h"
#include "pres_sub_server.h"
#include "client/ring_signal.h"
#include "sip_utils.h"
namespace ring {
using sip_utils::CONST_PJ_STR;
/* Callback called when *server* subscription state has changed. */
void
PresSubServer::pres_evsub_on_srv_state(pjsip_evsub *sub, pjsip_event *event)
......
......@@ -83,6 +83,14 @@ void addContactHeader(const pj_str_t *contactStr, pjsip_tx_data *tdata);
std::string sip_strerror(pj_status_t code);
// Helper function that return a constant pj_str_t from an array of any types
// that may be staticaly casted into char pointer.
// Per convention, the input array is supposed to be null terminated.
template<typename T, std::size_t N>
constexpr const pj_str_t CONST_PJ_STR(T (&a)[N]) noexcept {
return {const_cast<char*>(a), N-1};
}
}} // namespace ring::sip_utils
#endif // SIP_UTILS_H_
......@@ -89,6 +89,7 @@ namespace ring {
using yaml_utils::parseValue;
using yaml_utils::parseVectorMap;
using sip_utils::CONST_PJ_STR;
static const int MIN_REGISTRATION_TIME = 60;
static const int DEFAULT_REGISTRATION_TIME = 3600;
......@@ -356,7 +357,7 @@ SIPAccount::SIPStartCall(std::shared_ptr<SIPCall>& call)
return false;
}
pj_str_t subj_hdr_name = CONST_PJ_STR("Subject");
auto subj_hdr_name = CONST_PJ_STR("Subject");
pjsip_hdr* subj_hdr = (pjsip_hdr*) pjsip_parse_hdr(dialog->pool, &subj_hdr_name, (char *) "Phone call", 10, NULL);
pj_list_push_back(&dialog->inv_hdr, subj_hdr);
......@@ -1544,7 +1545,7 @@ SIPAccount::getSupportedTlsCiphers()
const std::vector<std::string>&
SIPAccount::getSupportedTlsProtocols()
{
static std::vector<std::string> availProtos {VALID_TLS_PROTOS, VALID_TLS_PROTOS+RING_ARRAYSIZE(VALID_TLS_PROTOS)};
static std::vector<std::string> availProtos {VALID_TLS_PROTOS, VALID_TLS_PROTOS + arraySize(VALID_TLS_PROTOS)};
return availProtos;
}
......
......@@ -41,9 +41,9 @@
#include "logger.h" // for _debug
#include "sdp.h"
#include "manager.h"
#include "array_size.h"
#include "string_utils.h"
#include "upnp/upnp_control.h"
#include "sip_utils.h"
#include "audio/audio_rtp_session.h"
......@@ -68,6 +68,8 @@
namespace ring {
using sip_utils::CONST_PJ_STR;
#ifdef RING_VIDEO
static DeviceParams
getVideoSettings()
......
......@@ -40,6 +40,7 @@
#include "pres_sub_client.h"
#include "sipvoiplink.h"
#include "client/ring_signal.h"
#include "sip_utils.h"
#include <thread>
#include <sstream>
......@@ -49,6 +50,8 @@
namespace ring {
using sip_utils::CONST_PJ_STR;
SIPPresence::SIPPresence(SIPAccount *acc)
: publish_sess_()
, status_data_()
......
......@@ -63,7 +63,7 @@ namespace ring {
constexpr const char* TRANSPORT_STATE_STR[] = {
"CONNECTED", "DISCONNECTED", "SHUTDOWN", "DESTROY", "UNKNOWN STATE"
};
constexpr const size_t TRANSPORT_STATE_SZ = RING_ARRAYSIZE(TRANSPORT_STATE_STR);
constexpr const size_t TRANSPORT_STATE_SZ = arraySize(TRANSPORT_STATE_STR);
std::string
SipTransportDescr::toString() const
......
......@@ -82,6 +82,8 @@
namespace ring {
using sip_utils::CONST_PJ_STR;
/**************** EXTERN VARIABLES AND FUNCTIONS (callbacks) **************************/
static pjsip_endpoint *endpt_;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment