2016-10-15 06:14:18 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Charybdis Development Team
|
|
|
|
* Copyright (C) 2016 Jason Volk <jason@zemos.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice is present in all copies.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_JS_ID_H
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
namespace ircd {
|
|
|
|
namespace js {
|
|
|
|
namespace basic {
|
2016-10-15 06:14:18 +02:00
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<lifetime L>
|
2016-10-19 02:17:29 +02:00
|
|
|
struct id
|
2016-10-27 10:55:26 +02:00
|
|
|
:root<jsid, L>
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
2016-10-27 10:55:26 +02:00
|
|
|
using root<jsid, L>::root;
|
|
|
|
explicit id(const char *const &); // creates new id
|
|
|
|
explicit id(const std::string &); // creates new id
|
|
|
|
id(const typename string<L>::handle &);
|
|
|
|
id(const typename value<L>::handle &);
|
2016-10-19 02:17:29 +02:00
|
|
|
id(const JSProtoKey &);
|
|
|
|
id(const uint32_t &);
|
|
|
|
id(const jsid &);
|
|
|
|
id();
|
|
|
|
};
|
2016-10-15 06:14:18 +02:00
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<lifetime L> bool operator==(const id<L> &, const char *const &);
|
|
|
|
template<lifetime L> bool operator==(const id<L> &, const std::string &);
|
|
|
|
template<lifetime L> bool operator==(const char *const &, const id<L> &);
|
|
|
|
template<lifetime L> bool operator==(const std::string &, const id<L> &);
|
2016-10-15 06:14:18 +02:00
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
} // namespace basic
|
2016-10-15 06:14:18 +02:00
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
using id = basic::id<lifetime::stack>;
|
|
|
|
using heap_id = basic::id<lifetime::heap>;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Implementation
|
|
|
|
//
|
|
|
|
namespace basic {
|
2016-10-15 06:14:18 +02:00
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<lifetime L>
|
|
|
|
id<L>::id()
|
|
|
|
:id<L>::root::type{}
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
2016-10-15 06:14:18 +02:00
|
|
|
}
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<lifetime L>
|
|
|
|
id<L>::id(const jsid &i)
|
|
|
|
:id<L>::root::type{i}
|
2016-10-15 06:14:18 +02:00
|
|
|
{
|
2016-10-19 02:17:29 +02:00
|
|
|
}
|
2016-10-15 06:14:18 +02:00
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<lifetime L>
|
|
|
|
id<L>::id(const uint32_t &index)
|
|
|
|
:id<L>::root::type{}
|
2016-10-26 19:10:00 +02:00
|
|
|
{
|
|
|
|
if(!JS_IndexToId(*cx, index, &(*this)))
|
|
|
|
throw type_error("Failed to construct id from uint32_t index");
|
|
|
|
}
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<lifetime L>
|
|
|
|
id<L>::id(const JSProtoKey &key)
|
|
|
|
:id<L>::root::type{}
|
2016-10-26 19:10:00 +02:00
|
|
|
{
|
|
|
|
JS::ProtoKeyToId(*cx, key, &(*this));
|
|
|
|
}
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<lifetime L>
|
|
|
|
id<L>::id(const std::string &str)
|
|
|
|
:id(str.c_str())
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
2016-10-15 06:14:18 +02:00
|
|
|
}
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<lifetime L>
|
|
|
|
id<L>::id(const char *const &str)
|
|
|
|
:id<L>::root::type{jsid()}
|
2016-10-15 06:14:18 +02:00
|
|
|
{
|
2016-10-27 10:55:26 +02:00
|
|
|
if(!JS::PropertySpecNameToPermanentId(*cx, str, this->address()))
|
|
|
|
throw type_error("Failed to create id from native string");
|
2016-10-15 06:14:18 +02:00
|
|
|
}
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<lifetime L>
|
|
|
|
id<L>::id(const typename value<L>::handle &h)
|
|
|
|
:id<L>::root::type{}
|
2016-10-15 06:14:18 +02:00
|
|
|
{
|
2016-10-27 10:55:26 +02:00
|
|
|
if(!JS_ValueToId(*cx, h, &(*this)))
|
|
|
|
throw type_error("Failed to construct id from Value");
|
2016-10-19 02:17:29 +02:00
|
|
|
}
|
2016-10-15 06:14:18 +02:00
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<lifetime L>
|
|
|
|
id<L>::id(const typename string<L>::handle &h)
|
|
|
|
:id<L>::root::type{}
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
2016-10-27 10:55:26 +02:00
|
|
|
if(!JS_StringToId(*cx, h, &(*this)))
|
|
|
|
throw type_error("Failed to construct id from String");
|
2016-10-26 19:10:00 +02:00
|
|
|
}
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<lifetime L>
|
|
|
|
bool
|
|
|
|
operator==(const std::string &a, const id<L> &b)
|
2016-10-26 19:10:00 +02:00
|
|
|
{
|
|
|
|
return operator==(a.c_str(), b);
|
|
|
|
}
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<lifetime L>
|
|
|
|
bool
|
|
|
|
operator==(const char *const &a, const id<L> &b)
|
2016-10-26 19:10:00 +02:00
|
|
|
{
|
|
|
|
return JS::PropertySpecNameEqualsId(a, b);
|
|
|
|
}
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<lifetime L>
|
|
|
|
bool
|
|
|
|
operator==(const id<L> &a, const std::string &b)
|
2016-10-26 19:10:00 +02:00
|
|
|
{
|
|
|
|
return operator==(a, b.c_str());
|
|
|
|
}
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<lifetime L>
|
|
|
|
bool
|
|
|
|
operator==(const id<L> &a, const char *const &b)
|
2016-10-26 19:10:00 +02:00
|
|
|
{
|
|
|
|
return JS::PropertySpecNameEqualsId(b, a);
|
2016-10-15 06:14:18 +02:00
|
|
|
}
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
} // namespace basic
|
2016-10-15 06:14:18 +02:00
|
|
|
} // namespace js
|
|
|
|
} // namespace ircd
|