Пример #1
0
String String::operator + (const String& rhs) const
{
    String ret;
    ret.Resize(Length() + rhs.Length());
    CopyChars(ret.Buffer(), Buffer(), Length());
    CopyChars(ret.Buffer() + Length(), rhs.Buffer(), rhs.Length());
    
    return ret;
}
Пример #2
0
String String::operator + (const char* rhs) const
{
    size_t rhsLength = CStringLength(rhs);
    String ret;
    ret.Resize(Length() + rhsLength);
    CopyChars(ret.Buffer(), Buffer(), Length());
    CopyChars(ret.Buffer() + Length(), rhs, rhsLength);
    
    return ret;
}
Пример #3
0
    bool Ignition::Stop(const char* name, const bool cancel, IgniteError* err)
    {
        bool res = false;

        factoryLock.Enter();

        if (started)
        {
            JniErrorInfo jniErr;

            SharedPointer<JniContext> ctx(JniContext::Create(NULL, 0, JniHandlers(), &jniErr));

            IgniteError::SetError(jniErr.code, jniErr.errCls, jniErr.errMsg, err);

            if (err->GetCode() == IgniteError::IGNITE_SUCCESS)
            {
                char* name0 = CopyChars(name);

                bool res0 = ctx.Get()->IgnitionStop(name0, cancel, &jniErr);

                ReleaseChars(name0);

                IgniteError::SetError(jniErr.code, jniErr.errCls, jniErr.errMsg, err);

                if (err->GetCode() == IgniteError::IGNITE_SUCCESS)
                    res = res0;
            }
        }

        factoryLock.Leave();

        return res;
    }
Пример #4
0
void String::Resize(unsigned newLength)
{
    if (!capacity_)
    {
        // Calculate initial capacity
        capacity_ = newLength + 1;
        if (capacity_ < MIN_CAPACITY)
            capacity_ = MIN_CAPACITY;
        
        buffer_ = new char[capacity_];
    }
    else
    {
        if (newLength && capacity_ < newLength + 1)
        {
            // Increase the capacity with half each time it is exceeded
            while (capacity_ < newLength + 1)
                capacity_ += (capacity_ + 1) >> 1;
            
            char* newBuffer = new char[capacity_];
            // Move the existing data to the new buffer, then delete the old buffer
            if (length_)
                CopyChars(newBuffer, buffer_, length_);
            delete[] buffer_;
            
            buffer_ = newBuffer;
        }
    }
    
    buffer_[newLength] = 0;
    length_ = newLength;
}
Пример #5
0
String& String::operator += (const String& rhs)
{
    size_t oldLength = Length();
    Resize(Length() + rhs.Length());
    CopyChars(Buffer() + oldLength, rhs.Buffer(), rhs.Length());
    
    return *this;
}
Пример #6
0
String& String::operator = (char* rhs)
{
    size_t rhsLength = CStringLength(rhs);
    Resize(rhsLength);
    CopyChars(Buffer(), rhs, rhsLength);
    
    return *this;
}
Пример #7
0
String& String::operator += (char* rhs)
{
    size_t rhsLength = CStringLength(rhs);
    size_t oldLength = Length();
    Resize(Length() + rhsLength);
    CopyChars(Buffer() + oldLength, rhs, rhsLength);
    
    return *this;
}
Пример #8
0
String& String::Append(const char* str, unsigned length)
{
    if (str)
    {
        unsigned oldLength = length_;
        Resize(oldLength + length);
        CopyChars(&buffer_[oldLength], str, length);
    }
    return *this;
}
Пример #9
0
String& String::Append(const char* str, size_t numChars)
{
    if (str)
    {
        size_t oldLength = Length();
        Resize(oldLength + numChars);
        CopyChars(&Buffer()[oldLength], str, numChars);
    }
    return *this;
}
Пример #10
0
String String::Substring(size_t pos) const
{
    if (pos < Length())
    {
        String ret;
        ret.Resize(Length() - pos);
        CopyChars(ret.Buffer(), Buffer() + pos, ret.Length());
        
        return ret;
    }
    else
        return String();
}
Пример #11
0
String String::Substring(unsigned pos) const
{
    if (pos < length_)
    {
        String ret;
        ret.Resize(length_ - pos);
        CopyChars(ret.buffer_, buffer_ + pos, ret.length_);

        return ret;
    }
    else
        return String();
}
Пример #12
0
String String::substr(unsigned pos, unsigned length) const
{
    if (pos < length_)
    {
        String ret;
        if (pos + length > length_)
            length = length_ - pos;
        ret.resize(length);
        CopyChars(ret.buffer_, buffer_ + pos, ret.length_);

        return ret;
    }
    else
        return String();
}
Пример #13
0
String String::Substring(size_t pos, size_t numChars) const
{
    if (pos < Length())
    {
        String ret;
        if (pos + numChars > Length())
            numChars = Length() - pos;
        ret.Resize(numChars);
        CopyChars(ret.Buffer(), Buffer() + pos, ret.Length());
        
        return ret;
    }
    else
        return String();
}
Пример #14
0
void String::Reserve(unsigned newCapacity)
{
    if (newCapacity < length_ + 1)
        newCapacity = length_ + 1;
    if (newCapacity == capacity_)
        return;

    auto* newBuffer = new char[newCapacity];
    // Move the existing data to the new buffer, then delete the old buffer
    CopyChars(newBuffer, buffer_, length_ + 1);
    if (capacity_)
        delete[] buffer_;

    capacity_ = newCapacity;
    buffer_ = newBuffer;
}
Пример #15
0
void String::Reserve(size_t newCapacity)
{
    size_t length = Length();
    if (newCapacity < length + 1)
        newCapacity = length + 1;
    if (newCapacity == Capacity())
        return;
    
    char* newBuffer = new char[newCapacity + 2 * sizeof(size_t)];
    // Move the existing data to the new buffer (including the end zero), then delete the old buffer
    if (length)
        CopyChars(newBuffer + 2 * sizeof(size_t), Buffer(), length + 1);
    delete[] buffer;

    buffer = newBuffer;
    SetLength(length);
    SetCapacity(newCapacity);
}
Пример #16
0
void String::Resize(size_t newLength)
{
    if (!buffer)
    {
        // If zero length requested, do not allocate buffer yet
        if (!newLength)
            return;
        
        // Calculate initial capacity
        size_t capacity = newLength + 1;
        if (capacity < MIN_CAPACITY)
            capacity = MIN_CAPACITY;
        
        buffer = new char[capacity + 2 * sizeof(size_t)];
        SetCapacity(capacity);
    }
    else
    {
        size_t capacity = Capacity();

        if (newLength && capacity < newLength + 1)
        {
            // Increase the capacity with half each time it is exceeded
            while (capacity < newLength + 1)
                capacity += (capacity + 1) >> 1;
            
            char* newBuffer = new char[capacity + 2 * sizeof(size_t)];
            // Move the existing data to the new buffer, then delete the old buffer
            if (Length())
                CopyChars(newBuffer + 2 * sizeof(size_t), Buffer(), Length());
            delete[] buffer;
            
            buffer = newBuffer;
            SetCapacity(capacity);
        }
    }

    SetLength(newLength);
    Buffer()[newLength] = 0;
}
Пример #17
0
    Ignite Ignition::Get(const char* name, IgniteError* err)
    {
        Ignite res;

        factoryLock.Enter();

        if (started)
        {
            char* name0 = CopyChars(name);

            // 1. Create context for this operation.
            JniErrorInfo jniErr;

            SharedPointer<JniContext> ctx(JniContext::Create(NULL, 0, JniHandlers(), &jniErr));

            IgniteError::SetError(jniErr.code, jniErr.errCls, jniErr.errMsg, err);

            if (err->GetCode() == IgniteError::IGNITE_SUCCESS)
            {
                // 2. Get environment pointer.
                long long ptr = ctx.Get()->IgnitionEnvironmentPointer(name0, &jniErr);

                IgniteError::SetError(jniErr.code, jniErr.errCls, jniErr.errMsg, err);

                if (err->GetCode() == IgniteError::IGNITE_SUCCESS)
                {
                    if (ptr != 0)
                    {
                        // 3. Obtain real environment for this instance.
                        JniHandlers* hnds = reinterpret_cast<JniHandlers*>(ptr);

                        SharedPointer<IgniteEnvironment>* env =
                            static_cast<SharedPointer<IgniteEnvironment>*>(hnds->target);

                        // 4. Get fresh node reference.
                        jobject ref = ctx.Get()->IgnitionInstance(name0, &jniErr);

                        if (err->GetCode() == IgniteError::IGNITE_SUCCESS) {
                            if (ref)
                            {
                                IgniteImpl* impl = new IgniteImpl(*env, ref);

                                res = Ignite(impl);
                            }
                            else
                                // Error: concurrent node stop.
                                *err = IgniteError(IgniteError::IGNITE_ERR_GENERIC,
                                    "Failed to get Ignite instance because it was stopped concurrently.");

                        }
                    }
                    else
                        // Error: no node with the given name.
                        *err = IgniteError(IgniteError::IGNITE_ERR_GENERIC,
                            "Failed to get Ignite instance because it is either not started yet or already stopped.");
                }
            }

            ReleaseChars(name0);
        }
        else
            // Error: no node with the given name.
            *err = IgniteError(IgniteError::IGNITE_ERR_GENERIC,
                "Failed to get Ignite instance because it is either not started yet or already stopped.");

        factoryLock.Leave();

        return res;
    }
Пример #18
0
    Ignite Ignition::Start(const IgniteConfiguration& cfg, const char* name, IgniteError* err)
    {
        bool failed = false;

        SharedPointer<IgniteEnvironment> env;
        SharedPointer<IgniteEnvironment>* envTarget = NULL;

        jobject javaRef = NULL;

        factoryLock.Enter();

        // 1. Load JVM library if needed.
        if (!JVM_LIB_LOADED)
        {
            bool jvmLibFound;
            std::string jvmLib;

            if (cfg.jvmLibPath)
            {
                std::string jvmLibPath = std::string(cfg.jvmLibPath);

                jvmLib = FindJvmLibrary(&jvmLibPath, &jvmLibFound);
            }
            else
                jvmLib = FindJvmLibrary(NULL, &jvmLibFound);

            if (!jvmLibFound)
            {
                *err = IgniteError(IgniteError::IGNITE_ERR_JVM_LIB_NOT_FOUND,
                    "JVM library is not found (did you set JAVA_HOME environment variable?)");

                failed = true;
            }

            if (!failed) {
                if (!LoadJvmLibrary(jvmLib))
                {
                    *err = IgniteError(IgniteError::IGNITE_ERR_JVM_LIB_LOAD_FAILED, "Failed to load JVM library.");

                    failed = true;
                }
            }

            JVM_LIB_LOADED = true;
        }

        if (!failed)
        {
            // 2. Resolve IGNITE_HOME.
            bool homeFound;
            std::string home;

            if (cfg.igniteHome)
            {
                std::string homePath = std::string(cfg.igniteHome);

                home = ResolveIgniteHome(&homePath, &homeFound);
            }
            else
                home = ResolveIgniteHome(NULL, &homeFound);

            // 3. Create classpath.
            std::string cp;

            if (cfg.jvmClassPath)
            {
                std::string usrCp = cfg.jvmClassPath;

                cp = CreateIgniteClasspath(&usrCp, homeFound ? &home : NULL);
            }
            else
                cp = CreateIgniteClasspath(NULL, homeFound ? &home : NULL);

            if (!cp.empty())
            {
                // 4. Start JVM if needed.
                JniErrorInfo jniErr;

                env = SharedPointer<IgniteEnvironment>(new IgniteEnvironment());

                int optsLen;
                char** opts = CreateJvmOptions(cfg, homeFound ? &home : NULL, cp, &optsLen);

                envTarget = new SharedPointer<IgniteEnvironment>(env);
                
                SharedPointer<JniContext> ctx(
                    JniContext::Create(opts, optsLen, env.Get()->GetJniHandlers(envTarget), &jniErr));

                for (int i = 0; i < optsLen; i++)
                    ReleaseChars(*(opts + i));

                delete[] opts;

                if (!ctx.Get())
                {
                    IgniteError::SetError(jniErr.code, jniErr.errCls, jniErr.errMsg, err);
                    
                    failed = true;
                }

                // 5. Start Ignite.
                if (!failed)
                {
                    char* springCfgPath0 = CopyChars(cfg.springCfgPath);

                    if (!springCfgPath0)
                        springCfgPath0 = CopyChars(DFLT_CFG);

                    char* name0 = CopyChars(name);

                    interop::InteropUnpooledMemory mem(16);
                    interop::InteropOutputStream stream(&mem);
                    stream.WriteBool(false);
                    stream.Synchronize();

                    javaRef = ctx.Get()->IgnitionStart(springCfgPath0, name0, 2, mem.PointerLong(), &jniErr);

                    ReleaseChars(springCfgPath0);
                    ReleaseChars(name0);

                    if (!javaRef) {
                        IgniteError::SetError(jniErr.code, jniErr.errCls, jniErr.errMsg, err);
                        
                        failed = true;
                    }
                    else {
                        // 6. Ignite is started at this point.
                        env.Get()->Initialize(ctx);

                        started = true;
                    }
                }
            }
            else {
                *err = IgniteError(IgniteError::IGNITE_ERR_JVM_NO_CLASSPATH,
                    "Java classpath is empty (did you set IGNITE_HOME environment variable?)");

                failed = true;
            }
        }

        factoryLock.Leave();

        if (failed) 
        {
            if (envTarget)
                delete envTarget;

            return Ignite();
        }
        else 
        {
            IgniteImpl* impl = new IgniteImpl(env, javaRef);

            return Ignite(impl);
        }
    }